Hi experts!
I'm trying to use a memory shared (memory mapped files) mechanism to transmit a window handle (HWND type) from C to C#. I've successfully used it to transmit strings.
In the case of handle, I get a system run-time error (build is fine) at the shared memory buffer copy (CopyMemory) of the window's handle (I write below all associated code):
Code:
HWND parentHandle; // This is initialized elsewhere by creating a simple window
LPVOID pBuf;
....................
// Create the shared buffer (Memory Mapped File)
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
BUF_SIZE, // maximum object size (low-order DWORD)
szsharedBufferName); // name of mapping object
if (hMapFile == NULL)
{
DisplayLastError(L"Could not file mapping object");
return 1;
}
// Create a view into the created shared buffer so that can read/write access it
//pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
pBuf = MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
DisplayLastError(L"Could not map view of file");
CloseHandle(hMapFile);
return 1;
}
// Write the handle into the shared buffer
CopyMemory((PVOID) pBuf, parentHandle, 4);
The HWND's length is 4 bytes (tested also with sizeof(parentHandle)). The debugger (VS2010 Beta) brings me to the assembler code where a mov operation, inside CopyMemory, is corrupted.
For me, the HWND is a system number on 32 bits, associated to a system window object, so it should work.
Many thanks,
Opariti