This function maps a view of a file into the address space of the calling process.
At a Glance
Header file: | Winbase.h |
Windows CE versions: | 1.01 and later |
Syntax
LPVOID MapViewOfFile( HANDLE hFileMappingObject,
DWORD dwDesiredAccess, DWORD dwFileOffsetHigh,
DWORD dwFileOffsetLow, DWORD dwNumberOfBytesToMap);
Parameters
hFileMappingObject
Handle to an open handle of a file-mapping object. The CreateFileMapping function returns this handle.
dwDesiredAccess
Specifies the type of access to the file view and, therefore, the protection of the pages mapped by the file. This parameter can be one of the following values:
Value | Description |
FILE_MAP_WRITE | Read-write access. The hFileMappingObject parameter must have been created with PAGE_READWRITE protection. A read-write view of the file is mapped. |
FILE_MAP_READ | Read-only access. The hFileMappingObject parameter must have been created with PAGE_READWRITE or PAGE_READONLY protection. A read-only view of the file is mapped. |
FILE_MAP_ALL_ACCESS | Same as FILE_MAP_WRITE. |
FILE_MAP_COPY | Copy on write access. If you create the map with PAGE_WRITECOPY and the view with FILE_MAP_COPY, you will receive a view to file. If you write to it, the pages are automatically swappable and the modifications you make will not go to the original data file. |
Windows 95: You must pass PAGE_WRITECOPY to CreateFileMapping; otherwise, an error will be returned. | |
Windows NT: There is no restriction as to how the hFileMappingObject parameter must be created. Copy on write is valid for any type of view. |
dwFileOffsetHigh
Specifies the high-order 32 bits of the file offset where mapping is to begin.
dwFileOffsetLow
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 an offset within the file that matches the system’s memory allocation granularity, or the function fails. That is, the offset must be a multiple of the allocation granularity. Use the GetSystemInfo function, which fills in the members of a SYSTEM_INFO structure, to obtain the system’s memory allocation granularity.
dwNumberOfBytesToMap
Specifies the number of bytes of the file to map. If dwNumberOfBytesToMap is zero, the entire file is mapped.
Return Values
The starting address of the mapped view indicates success. NULL indicates failure. To get extended error information, call GetLastError.
Remarks
The 64 KB alignment is not required for the dwFileOffsetLow parameter.
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 specified 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 by using the DuplicateHandle function, 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 by the ReadFile or WriteFile function.
Windows 95: MapViewOfFile may require the swapfile to grow. If the swapfile cannot grow, the function fails.
Windows NT: If the file-mapping object is backed by the paging file (handle = 0xFFFFFFFF), the paging file must be large enough to hold the entire mapping. If it is not, MapViewOfFile fails.
Note
To guard against an access violation, use structured exception handling to protect any code that writes to or reads from a memory mapped view.
See Also
CreateFileMapping, GetSystemInfo, UnmapViewOfFile, SYSTEM_INFO