CreateFileMapping API question
I can't find documentation for this. If someone knows, please let me know.
I'm using file mapping to share data between two processes. The size of data in bytes is always changing. (In other words I send ID value to one process and receive description string from another.) Does file mapping object support changing the size after creation? Please review the following:
I create named mapping object when 1st process starts like this:
//Called from WM_CREATE
UINT nSize = sizeof(DWORD);
HANDLE hFileMapping = CreateFileMapping((HANDLE)INVALID_HANDLE_VALUE,
NULL, PAGE_READWRITE, 0, nSize, "UniqueMappingName123");
...
//This is called from WM_DESTROY
if(hFileMapping)
CloseHandle(hFileMapping);
}
Then when I need data from the 2nd process, I do the following in the 1st process:
UINT nSize = sizeof(DWORD);
HANDLE hFileMapping = CreateFileMapping((HANDLE)INVALID_HANDLE_VALUE,
NULL, PAGE_READWRITE, 0, nSize, "UniqueMappingName123");
if(hFileMapping && ::GetLastError() == ERROR_ALREADY_EXISTS)
{
DWORD* pSharedMem = (DWORD*)
MapViewOfFile(hFileMapping, FILE_MAP_ALL_ACCESS, 0, 0, nSize);
if(pSharedMem)
{
*pSharedMem = nMyIDValue;
UnmapViewOfFile(pSharedMem);
}
CloseHandle(hFileMapping);
}
Then in the 2nd process, I read data from mapping object:
UINT nSize = sizeof(DWORD);
HANDLE hFileMapping = CreateFileMapping((HANDLE)INVALID_HANDLE_VALUE,
NULL, PAGE_READWRITE, 0, nSize, "UniqueMappingName123");
DWORD nMyIDValue = NULL;
if(hFileMapping && ::GetLastError() == ERROR_ALREADY_EXISTS)
{
DWORD* pSharedMem = (DWORD*)
MapViewOfFile(hFileMapping, FILE_MAP_ALL_ACCESS, 0, 0, nSize);
if(pSharedMem)
{
nMyIDValue = *pSharedMem;
UnmapViewOfFile(pSharedMem);
}
CloseHandle(hFileMapping);
}
And then later on, in 2nd process after I get data according to 'nMyIDValue' received from the 1st process I need to return data back:
//pDescrStr = ASCIIZ text string
UINT nSize = sizeof(UINT) + (lstrlen(pDescrStr) + 1) * sizeof(TCHAR);
HANDLE hFileMapping = CreateFileMapping((HANDLE)INVALID_HANDLE_VALUE,
NULL, PAGE_READWRITE, 0, nSize, "UniqueMappingName123");
DWORD nMyIDValue = NULL;
if(hFileMapping && ::GetLastError() == ERROR_ALREADY_EXISTS)
{
DWORD* pSharedMem = (DWORD*)
MapViewOfFile(hFileMapping, FILE_MAP_ALL_ACCESS, 0, 0, nSize);
if(pSharedMem)
{
UINT* pcbSz = (UINT*)pSharedMem;
*pcbSz = nSize - sizeof(UINT);
memcpy(pSharedMem + 1, pDescrStr, nSize - sizeof(UINT));
UnmapViewOfFile(pSharedMem);
}
CloseHandle(hFileMapping);
}
After which, 1st process can read it like this:
UINT nSize = sizeof(UINT);
HANDLE hFileMapping = CreateFileMapping((HANDLE)INVALID_HANDLE_VALUE,
NULL, PAGE_READWRITE, 0, nSize, "UniqueMappingName123");
DWORD nMyIDValue = NULL;
if(hFileMapping && ::GetLastError() == ERROR_ALREADY_EXISTS)
{
DWORD* pSharedMem = (DWORD*)
MapViewOfFile(hFileMapping, FILE_MAP_ALL_ACCESS, 0, 0, nSize);
if(pSharedMem)
{
//Get size of data
UINT ncbSz = *pSharedMem;
UnmapViewOfFile(pSharedMem);
if(ncbSz)
{
DWORD* pSharedMem2 = (DWORD*)
MapViewOfFile(hFileMapping, FILE_MAP_ALL_ACCESS, 0, 0, ncbSz + sizeof(UINT));
if(pSharedMem2)
{
memcpy(pRetStr, pSharedMem2 + 1, ncbSz);
UnmapViewOfFile(pSharedMem2);
}
}
CloseHandle(hFileMapping);
}
When I do this I get really strange errors. What am I doing wrong?
Any help will be greatly appreciated...

