MapViewOfFile

  LPVOID MapViewOfFile(hMapObject, fdwAccess, dwOffsetHigh, dwOffsetLow, cbMap)    
  HANDLE hMapObject; /* file mapping object to map into addr space */
  DWORD fdwAccess; /* desired access mode */
  DWORD dwOffsetHigh; /* high dword of file offset */
  DWORD dwOffsetLow; /* low dword of file offset */
  DWORD cbMap; /* number of bytes to map */

The MapViewOfFile function maps a view of a file into the address space of the calling process.

Parameters

hMapObject

Specifies an open handle to a file mapping object that is to be mapped into the caller's address space. The CreateFileMapping and OpenFileMapping functions return this handle.

fdwAccess

Specifies the access that is requested to the file mapping object. This determines the page protection of the pages mapped by the file. This parameter can be one of the following values:

Value Meaning

FILE_MAP_WRITE  
  Read and write access is desired. The mapping object must have been created with PAGE_READWRITE protection. hMapObject must have been created with FILE_MAP_WRITE access. A read and write view of the file will be mapped.
FILE_MAP_READ  
  Read-nly access is desired. The mapping object must have been created with PAGE_READWRITE or PAGE_READ protection. hMapObject must have been created with FILE_MAP_READ access. A read only view of the file will be mapped.

dwOffsetHigh

Specifies the high-order 32 bits of the file offset where mapping is to begin.

dwOffsetLow

Specifies the low-order 32 bits of the file offset where mapping is to begin. The combination of the high and low offsets must specify a 64K aligned offset within the file, or the function will fail.

cbMap

Specifies the number of bytes of the file to map. If cbMap is zero, the entire file is mapped.

Return Value

The return value is the starting address of the mapped view if the function was successful, or NULL if an error occurred. Use the GetLastError function to obtain extended error information.

Comments

Mapping a file makes the specified portion of the file visible in the address space of the calling process.

Multiple views of a file (or a file mapping object and its mapped file) are said to be “coherent” if they contain identical data at a given time. This occurs if the file views are derived from the same file mapping object. A process can duplicate a file mapping object handle into another process with DuplicateHandle, or another process can open a file mapping object by name by using the OpenFileMapping function.

A mapped view of a file is not guaranteed to be coherent with a file being accessed via ReadFile or WriteFile.

See Also

CreateFileMapping, DuplicateHandle, MapViewOfFileEx, OpenFileMapping, UnmapViewOfFile