0

I'm trying currently to test inter process communication by using filemaps. My first program, which i shall call the producer, doesn't error on the following code which createsa file map and writes to it, as follows:

 hEvent = CreateFileMapping(
             INVALID_HANDLE_VALUE,    // use paging file
             NULL,                    // default security
             PAGE_READWRITE,          // read/write access
             0,                       // maximum object size (high-order DWORD)
             256,                // maximum object size (low-order DWORD)
             TEXT("hEvent")); 
        
if (hEvent == NULL)
{
    MessageBox(NULL, TEXT("error: cannot create file map"), TEXT("gotit"), MB_OK);
  _tprintf(TEXT("Could not create file mapping object (%d).\n"),
         GetLastError());
  return 1;
}

 mapBuffer = (LPTSTR) MapViewOfFile(hEvent, FILE_MAP_ALL_ACCESS, NULL, NULL, 256);
            if (mapBuffer == NULL)
{
   MessageBox(NULL, TEXT("error: cannot view map"), TEXT("gotit"), MB_OK);
  _tprintf(TEXT("Could not map view of file (%d).\n"),
         GetLastError());

   CloseHandle(hEvent);

  return 1;
}
        
            CopyMemory((PVOID)mapBuffer, teststring, 256);
            _getch();
            
        

             UnmapViewOfFile(mapBuffer);
             CloseHandle(hEvent);

However, my second program, which is imitating the second process and i shall name the consumer, errors upon trying to re-open this filemap, using the following code:

 hEvent = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, TEXT("hEvent"));
             if (hEvent == NULL)
 {
  MessageBox(NULL, TEXT("error opening filemap"), TEXT("gotit"), MB_OK);
         GetLastError();
  return 1;
 }

Can anyone see anything obvious i'm missing?, as it's going straight over my head.

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
Kestami
  • 2,045
  • 3
  • 32
  • 47
  • Name your mapping object something unique, just in case any other software is also using named file mappings. – Ben Voigt Apr 29 '12 at 23:46
  • hah, what a silly mistake. Turns out i was ending the handle in the first process before the second process could get hold of it. CloseHandle(hEvent); I took this out of the first process and it stopped the second one erroring. – Kestami Apr 29 '12 at 23:47
  • Also, are both programs being run by the same user? At the same isolation level? – Ben Voigt Apr 29 '12 at 23:47
  • they're both on the same computer, in different visStud projects. i think i've solved THIS issue at hand, now just to get it to read the string correctly i'm passing between the two. – Kestami Apr 29 '12 at 23:49
  • I think it's safer to duplicate the handle than using a named object. There's no rule upon how to name a named object in windows. – BlueWanderer Apr 30 '12 at 04:39

1 Answers1

4

Like all kernel objects, file mappings are deleted when the last handle is closed. Since your first program closes the handle right away, there is nothing for the second program to find. You must keep the handle open for as long as you want the mapping to exist.

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
  • 1
    literally 3 minutes ago i figured out my own stupid mistake : P i'm going to select your answer though, as it will help anyone in the same boat as me that doesn't happen to get their error smack them straight in the face. – Kestami Apr 29 '12 at 23:51
  • +1 for my stupid code, the producer code ```CloseHandle()``` after ```MapViewOfFile```, it takes the comsumer cannot find the resources. – vrqq Feb 16 '22 at 05:52
  • except case `OBJ_PERMANENT` used on create or `ZwMakePermanentObject` called. but need have `SeCreatePermanentPrivilege` – RbMm Feb 17 '22 at 17:43