CreateFileMapping API question

Hi everyone:

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...
[4044 byte] By [dc_2000] at [2007-11-19 19:52:03]
# 1 Re: CreateFileMapping API question
I can't find documentation for this. If someone knows, please let me know.CreateFileMapping (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/createfilemapping.asp)
MapViewOfFile (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/mapviewoffile.asp)

When I do this I get really strange errors.What strange erros?

What am I doing wrong?Well, you cannot map a view outside of the 'file'. My suggestion is that you create a file mapping with, lets say 1024 bytes (or whatever), and then limit your message size.

- petter
wildfrog at 2007-11-9 13:20:21 >
# 2 Re: CreateFileMapping API question
What I can't find documentation for is whether it's possible to call MapViewOfFile with size larger than the one specified in CreateFileMapping? (I mean CreateFileMapping called after object creation to re-map it with new size.)

And if not, how to increase size of mapping object?

I cannot use 1024 bytes because I simply don't know that size as it could be well larger than that.
dc_2000 at 2007-11-9 13:21:30 >