Creating a File View of a Memory Map (Windows Embedded CE 6.0)

1/6/2010

To map the data from a file to the virtual memory of a process, you must create a view of the file. The MapViewOfFile function uses the file mapping object handle returned by the CreateFileMapping function to create a view of the file or a portion of the file in the process's virtual address space. These functions fail if the access flags conflict with those specified when CreateFileMapping created the file mapping object.

MapViewOfFile returns a pointer to the file view. By dereferencing a pointer in the range of addresses specified in MapViewOfFile, an application can read data from the file and write data to the file. Writing to the file view results in changes to the file-mapping object. The actual writing to the file on disk is handled by the system. Data is not actually transferred at the time the file-mapping object is written to. Instead, much of the file I/O is cached to improve general system performance.

Applications can override this behavior by calling the FlushViewOfFile function to force the system to perform disk transactions immediately. The lpBaseAddress parameter of this function must be an integral multiple of the system memory allocation granularity, or the call fails. To obtain the system memory allocation granularity, use the GetSystemInfo function, which fills in the members of a SYSTEM_INFO structure.

An application can create multiple file views from the same file mapping object. A file view can be smaller than the file-mapping object from which it is derived. The offset specified by the dwOffsetHigh and dwOffsetLow parameters of MapViewOfFile must be a multiple of the allocation granularity of the system.

See Also

Reference

FlushViewOfFile
MapViewOfFile
UnmapViewOfFile

Concepts

Mapping a Memory Object to a File

Other Resources

Memory Mapping a File