Thread: Copy HWND into shared memory buffer

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    77

    Copy HWND into shared memory buffer

    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

  2. #2
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Quote Originally Posted by Opariti View Post
    // Write the handle into the shared buffer
    CopyMemory((PVOID) pBuf, parentHandle, 4);
    You forgot the &. You want the 4/8 bytes at the address of parentHandle, not the 4/8 bytes pointer to by parentHandle.
    CopyMemory((PVOID) pBuf, &parentHandle, sizeof(parentHandle));

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    77
    Thanks, that's it, I've already tried with & and went on.
    The explanation is clear!
    Just in case, though out of the forum: do you know how could I pick up the handle as a IntPtr in C# (from a 4 element byte-array)?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function call from another .c module
    By Ali.B in forum C Programming
    Replies: 14
    Last Post: 08-03-2009, 11:45 AM
  2. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  3. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM
  4. using shared memory in c
    By flawildcat in forum C Programming
    Replies: 1
    Last Post: 04-09-2002, 12:25 PM
  5. Shared memory in Linux: B-TREE of structures
    By zahid in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 11:15 PM