Link to home
Start Free TrialLog in
Avatar of jhjeon
jhjeon

asked on

Copying memory block between Windows..??

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..
Avatar of jhjeon
jhjeon

ASKER

each window is different application..
ASKER CERTIFIED SOLUTION
Avatar of faster
faster

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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);


Avatar of jhjeon

ASKER

Thanks, faster and aclynes !.

have a nice day !.