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_SEN DDATA,
(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)lPar am); //block is BYTE *
//occured Access Violation !!! at this
//point..(in memcpy,,)
why Access Violation is occured ?..
give me your solutions..
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_SEN
(WPARAM)mem_block,(LPARAM)
window#2 (Receiver Window, it's class name is "MainWin")
case WM_SENDDATA :
{BYTE *mem;
mem = (BYTE *)wParam;
memcpy(block,mem,(int)lPar
//occured Access Violation !!! at this
//point..(in memcpy,,)
why Access Violation is occured ?..
give me your solutions..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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)lpMapAddre ss, mem, size);
//Notify the receiver window with a registered message.
::SendMessage(FaceWin,
RegisterWindowMessage("SEN D_DATA_MES SAGE"),
size, 0
);
//Cleanup
UnmapViewOfFile(lpMapAddre ss);
}
/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(lpMapAddre ss);
CloseHandle(hMapFile);
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)lpMapAddre
//Notify the receiver window with a registered message.
::SendMessage(FaceWin,
RegisterWindowMessage("SEN
size, 0
);
//Cleanup
UnmapViewOfFile(lpMapAddre
}
/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(lpMapAddre
CloseHandle(hMapFile);
ASKER
Thanks, faster and aclynes !.
have a nice day !.
have a nice day !.
ASKER