Maps a view of a file mapping into the address space of a calling process.
To specify a suggested base address for the view, use the
MapViewOfFileEx function. However, this practice is not recommended.
Syntax
LPVOID WINAPI MapViewOfFile(
__in HANDLE hFileMappingObject,
__in DWORD dwDesiredAccess,
__in DWORD dwFileOffsetHigh,
__in DWORD dwFileOffsetLow,
__in SIZE_T dwNumberOfBytesToMap
);
Parameters
- hFileMappingObject [in]
-
A handle to a file mapping object. The
CreateFileMapping and
OpenFileMapping functions return this handle.
- dwDesiredAccess [in]
-
The type of access to a file mapping object, which determines the protection of the pages. This parameter can be one of the following values.
| Value | Meaning |
- FILE_MAP_ALL_ACCESS
| A read/write view of the file is mapped. The file mapping object must have been created with PAGE_READWRITE or PAGE_EXECUTE_READWRITE protection.
When used with the MapViewOfFile function, FILE_MAP_ALL_ACCESS is equivalent to FILE_MAP_WRITE.
|
- FILE_MAP_COPY
| A copy-on-write view of the file is mapped. The file mapping object must have been created with PAGE_READONLY, PAGE_READ_EXECUTE, PAGE_WRITECOPY, PAGE_EXECUTE_WRITECOPY, PAGE_READWRITE, or PAGE_EXECUTE_READWRITE protection.
When a process writes to a copy-on-write page, the system copies the original page to a new page that is private to the process. The new page is backed by the paging file. The protection of the new page changes from copy-on-write to read/write.
When copy-on-write access is specified, the system and process commit charge taken is for the entire view because the calling process can potentially write to every page in the view, making all pages private. The contents of the new page are never written back to the original file and are lost when the view is unmapped.
|
- FILE_MAP_READ
| A read-only view of the file is mapped. An attempt to write to the file view results in an access violation.
The file mapping object must have been created with PAGE_READONLY, PAGE_READWRITE, PAGE_EXECUTE_READ, or PAGE_EXECUTE_READWRITE protection.
|
- FILE_MAP_WRITE
| A read/write view of the file is mapped. The file mapping object must have been created with PAGE_READWRITE or PAGE_EXECUTE_READWRITE protection.
When used with MapViewOfFile, (FILE_MAP_WRITE | FILE_MAP_READ) and FILE_MAP_ALL_ACCESS are equivalent to FILE_MAP_WRITE.
|
Each of the preceding values can be combined with the following value.
| Value | Meaning |
- FILE_MAP_EXECUTE
| An executable view of the file is mapped (mapped memory can be run as code). The file mapping object must have been created with PAGE_EXECUTE_READ, PAGE_EXECUTE_WRITECOPY, or PAGE_EXECUTE_READWRITE protection.
Windows Server 2003 and Windows XP: This value is available starting with Windows XP with SP2 and Windows Server 2003 with SP1.
Windows 2000: This value is not supported.
|
For more information, see File Mapping Security and Access Rights.
- dwFileOffsetHigh [in]
-
A high-order DWORD of the file offset where the view begins.
- dwFileOffsetLow [in]
-
A low-order DWORD of the file offset where the view is to begin. The combination of the high and low offsets must specify an offset within the file mapping. They must also match the memory allocation granularity of the system. That is, the offset must be a multiple of the allocation granularity. To obtain the memory allocation granularity of the system, use the
GetSystemInfo function, which fills in the members of a
SYSTEM_INFO structure.
- dwNumberOfBytesToMap [in]
-
The number of bytes of a file mapping to map to the view. All bytes must be within the maximum size specified by CreateFileMapping. If this parameter is 0 (zero), the mapping extends from the specified offset to the end of the file mapping.
Return Value
If the function succeeds, the return value is the starting address of the mapped view.
If the function fails, the return value is NULL. To get extended error information, call
GetLastError.
Remarks
Mapping a file makes the specified portion of a file visible in the address space of the calling process.
For files that are larger than the address space, you can only map a small portion of the file data at one time. When the first view is complete, you can unmap it and map a new view.
To obtain the size of a view, use the VirtualQuery function.
Multiple views of a file (or a file mapping object and its mapped file) are 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 that is being accessed by the
ReadFile or
WriteFile function.
Do not store pointers in the memory mapped file; store offsets from the base of the file mapping so that the mapping can be used at any address.
To guard against EXCEPTION_IN_PAGE_ERROR exceptions, use structured exception handling to protect any code that writes to or reads from a memory mapped view of a file other than the page file. For more information, see
Reading and Writing From a File View.
If a file mapping object is backed by the paging file (CreateFileMapping is called with the hFile parameter set to INVALID_HANDLE_VALUE), the paging file must be large enough to hold the entire mapping. If it is not,
MapViewOfFile fails. The
initial contents of the pages in a file mapping object backed by the paging file are 0 (zero).
When a file mapping object that is backed by the paging file is created, the caller can specify whether MapViewOfFile should reserve and commit pages at the same time (SEC_COMMIT) or simply reserve pages (SEC_RESERVE). Mapping the file makes the entire mapped virtual address range unavailable to other allocations in the process. After a page from the reserved range is committed, it cannot be freed or decommitted by calling VirtualFree. Reserved and committed pages are released when the view is unmapped and the file mapping object is closed. For details, see the UnmapViewOfFile and CloseHandle functions.
To have a file with executable permissions, an application must call
CreateFileMapping with either PAGE_EXECUTE_READWRITE or PAGE_EXECUTE_READ,
and then call MapViewOfFile with FILE_MAP_EXECUTE | FILE_MAP_WRITE or FILE_MAP_EXECUTE | FILE_MAP_READ.
Examples
For an example, see
Creating Named Shared Memory.
Requirements
| Minimum supported client | Windows 2000 Professional |
| Minimum supported server | Windows 2000 Server |
| Header | Winbase.h (include Windows.h) |
| Library | Kernel32.lib |
| DLL | Kernel32.dll |
See Also
- CreateFileMapping
- Creating a File View
- DuplicateHandle
- File Mapping Functions
- GetSystemInfo
- MapViewOfFileEx
- OpenFileMapping
- SYSTEM_INFO
- UnmapViewOfFile
Send comments about this topic to Microsoft
Build date: 12/17/2009