7.2 Using File Mapping

The Win32 file mapping functions provide a simple method for processes to share data. The code fragments in this section illustrate how two processes might access an existing file as named shared memory.

Note that if the two processes share memory simultaneously, the processes must use a semaphore to synchronize their access to the memory.

In the following code fragment, CreateFileMapping creates a file mapping object. When a corresponding file view is created, the process will have read and write access to the memory:

hMapFile = CreateFileMapping(hFile, /* existing file hndl */

(LPSECURITY_ATTRIBUTES) NULL, /* no security */

PAGE_READWRITE, /* read/write access */

0, /* max object size is */

0, /* size of hFile */

"MyFileMappingObject"); /* name of map. object */

if (hMapFile == NULL) {

ErrorHandler("Could not create file mapping object.");

}

This fragment uses the file mapping object handle returned by CreateFileMapping to create a view of the file in the process' address space:

MapAddress = MapViewOfFile(hMapFile, /* hndl to map. obj. */

FILE_MAP_ALL_ACCESS, /* read/write access */

0, /* max object size is */

0, /* size of hFile */

0); /* map entire file */

if (MapAddress == NULL) {

ErrorHandler("Could not map view of file.");

}

MapViewOfFile returns a pointer to the file view. The process can use the pointer to access the memory for whatever it needs to do.

If a second process needs to share the same memory, it can use OpenFileMapping with the name of the file mapping object to obtain an appropriate handle. As in the original process, MapViewOfFile uses this handle to return a pointer to the file view.

hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, /* r/w access */

FALSE, /* do not inherit */

"MyFileMappingObject"); /* name of mapping obj */

if (hMapFile == NULL) {

ErrorHandler("Could not open file mapping object.");

}

MapAddress = MapViewOfFile(hMapFile, /* hndl to map. obj. */

FILE_MAP_ALL_ACCESS, /* read/write access */

0, /* max object size is */

0, /* size of hFile */

0); /* map entire file */

if (MapAddress == NULL) {

ErrorHandler("Could not map view of file.");

}

Note that MapViewOfFile returns different addresses in different processes. To specify the location of the file view within a process' address space, use the MapViewOfFileEx function, rather than MapViewOfFile.

The following fragment uses UnmapViewOfFile to invalidate the pointer to the mapped memory. This destroys the file view in the process' address space. If any of the pages of the file view changed since the view was mapped, the UnmapViewOfFile function also copies the changed pages to the physical file:

if (!UnmapViewOfFile(MapAddress)) {

ErrorHandler("Could not unmap view of file.");

}

The FlushViewOfFile function copies the specified number of bytes of the file view to the physical file, but does not destroy the file view:

if (!FlushViewOfFile(MapAddress, dwBytesToFlush)) {

ErrorHandler("Could not flush memory to disk.");

}