Copy HWND into shared memory buffer RRS feed

  • Question

  • Hi,
    I'd like to take advantage of the new ... .Net (4.2 Beta) capability in using memory mapped files, to do IPC between C++ native and C#. I've tested it for passing strings and integers; it works fine.
    Now, I create a window in C++, I get its handle and I want to copy it into the shared buffer, then, in .net, pick it up in a byte array, read into a IntPtr and use it as a handle (want to generate a child window in C# for my window created in C++).
    I'm getting a run time error at this instruction in C++:

    CopyMemory((PVOID) bufferMemory, parentHandle, sizeof(parentHandle));

    "bufferMemory" is an LPVOID returned by MapViewOfFile and "parentHandle" is the HWND of the parent window I've created.

    The debugger (I use VS 2010 Beta) brings me to an assembler instruction "mov", in the implementation of CopyMemory (memcpy in fact), saying that there is data corruption.
    What am I doing wrong?

    • Moved by Rong-Chun Zhang Monday, December 28, 2009 10:22 AM .NET 4 beta 2 (From:Visual C++ General)
    Saturday, December 26, 2009 2:39 PM


All replies

  • Change the second argument to &parentHandle.

    Saturday, December 26, 2009 5:04 PM
  • Thanks Viorel, that confirms my first move; it's true, this doesn't crash the app.

    Now, just in case you know, though is not a C++ issue, what should be the method to use so that to pick up the right handle value in the C# code (using Memory Mappped File in .Net 4.2 beta, but that's not the problem), as an IntPtr, from a 4 element byte array? Because what I've tried doesn't match the original parentHandle value sent from native code (using the &parentHandle argument).
    Many thanks,
    Saturday, December 26, 2009 6:35 PM