Reputation: 2073
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.
Upvotes: 0
Views: 2984
Reputation: 45172
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.
Upvotes: 4