Solved

Copying memory block between Windows..??

Posted on 1998-09-14
4
451 Views
Last Modified: 2013-12-03
hello, experts..
I want to copy memory block between windows..

example is this..

window#1 (Sender Window, mem_block is BYTE *)
     FaceWin = FindWindow("MainWin",NULL);                        
     SendMessage(FaceWin,WM_SENDDATA,
                 (WPARAM)mem_block,(LPARAM)size);

window#2 (Receiver Window,  it's class name is "MainWin")

   case WM_SENDDATA :
        {BYTE *mem;

         mem = (BYTE *)wParam;
         memcpy(block,mem,(int)lParam); //block is BYTE *
        //occured Access Violation !!! at this  
        //point..(in memcpy,,)

why Access Violation is occured ?..
give me your solutions..
0
Comment
Question by:jhjeon
  • 2
4 Comments
 

Author Comment

by:jhjeon
Comment Utility
each window is different application..
0
 
LVL 7

Accepted Solution

by:
faster earned 100 total points
Comment Utility
If the memory block is not very huge, you can use WM_COPYDATA message.  It is quite simple so I think the standard sdk document will be enough for you.

Other alternatives include shared memory, pipe, socket, OLE etc, but WM_COPYDATA is the easiest.
0
 

Expert Comment

by:aclynes
Comment Utility
This example code copies data to a named shared memory block, then sends a message to notify the receiver window.

SENDER WINDOW
=============

//Set up some data to send.
int size = 256;
BYTE* mem = (BYTE*)malloc(size);

//Put some dummy data into the block;
_tcscpy((TCHAR*)mem, "Test data");

HANDLE hMapFile=NULL;
LPVOID lpMapAddress=NULL;

//Create a mapping object named "My mapping object" which we
//can share with the receiver window.
hMapFile = CreateFileMapping(
      (HANDLE)0xFFFFFFFF,    // Use the page file
      NULL, // Default security.
      PAGE_READWRITE, // Read/write permission.
      0, // Data size (high-order).
      size,       // Data (low-order).
      "My mapping object"); // Name of mapping object.
      
if (!hMapFile)
      return;

//Load the mapping object into our address space.
lpMapAddress = MapViewOfFile(
      hMapFile, // Handle to mapping object.
      FILE_MAP_ALL_ACCESS,      // Read/write permission.
      0, 0, 0); // Map entire file.

//Copy the data into the mapping object, and tell the receiver
//window about it.
if (lpMapAddress)
{
      //Copy the data.
      memmove((LPTSTR)lpMapAddress, mem, size);
      //Notify the receiver window with a registered message.
      ::SendMessage(FaceWin,
            RegisterWindowMessage("SEND_DATA_MESSAGE"),
            size, 0
      );

      //Cleanup
      UnmapViewOfFile(lpMapAddress);
}
/Cleanup
CloseHandle(hMapFile);
free(mem);


RECEIVER WINDOW (HANDLES THE REGISTERED MESSAGE)
================================================

HANDLE hMapFile;
LPVOID lpMapAddress;

//Open the mapping object created by the sender window
hMapFile = OpenFileMapping(
      FILE_MAP_ALL_ACCESS, // Read/write permission.
      FALSE,
      "My mapping object"); // of the mapping object.
 
if (!hMapFile)
      return 1;

//Load the mapping object named into our address space.
lpMapAddress = MapViewOfFile(
      hMapFile, // Handle to mapping object.
      FILE_MAP_ALL_ACCESS, // Read/write permission.
      0, 0, 0);
            
if (!lpMapAddress)
{
      CloseHandle(hMapFile);
      return 1;
}

//Do something with the data.
::MessageBox(NULL, (TCHAR*)lpMapAddress, "Received Data", MB_OK|MB_ICONINFORMATION);

//Cleanup
UnmapViewOfFile(lpMapAddress);
CloseHandle(hMapFile);


0
 

Author Comment

by:jhjeon
Comment Utility
Thanks, faster and aclynes !.

have a nice day !.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now