Copy HWND into shared memory buffer
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):
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.
HWND parentHandle; // This is initialized elsewhere by creating a simple window
// 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");
// 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
if (pBuf == NULL)
DisplayLastError(L"Could not map view of file");
// Write the handle into the shared buffer
CopyMemory((PVOID) pBuf, parentHandle, 4);
For me, the HWND is a system number on 32 bits, associated to a system window object, so it should work.