This function can map files of a full 64-bit size. The MapViewOfFile function can create views if the current process has enough virtual address space to contain them. Resource constraints prevent direct-ROM and non-pageable mapfiles that are larger than 4 GB.
A memory-mapped file should not be accessed by using the ReadFile or the WriteFile function. This can result in inconsistencies between the file and the memory-mapped file.
Two writable handles to the same file cannot be open at the same time to prevent inconsistencies between the file and the mapped view of it. For two processes to share the data in a memory-mapped file, they must both memory map the file.
If you call this function with a handle from CreateFileForMapping and it fails for any reason, the handle is closed. For example, any attempt to use the CloseHandle function on a file handle that was returned from CreateFileForMapping and then used in a failed call to this function automatically return FALSE. Instead, the kernel automatically closes the handle specified in hFile.
This function does not work on Windows Embedded CE devices that do not support demand paging.
After a file-mapping object has been created, the size of the file must not exceed the size of the file-mapping object. If it does, not all file contents are available for sharing.
If an application specifies a size for the file-mapping object that is larger than the size of the named file on disk, the file on disk is grown to match the specified size of the file-mapping object. If the file cannot be grown, the file-mapping object is not created. GetLastError returns ERROR_DISK_FULL.
The handle that this function returns has full access to the new file-mapping object. It can be used with any function that requires a handle to a file-mapping object. File-mapping objects can be shared through process creation.
File handles that have been used to create file-mapping objects must not be used in subsequent calls to file I/O functions, such as ReadFile and WriteFile. Generally, if a file handle has been used in a successful call to this function, do not use that handle unless you first close the corresponding file-mapping object.
Creating a file-mapping object creates the potential for mapping a view of the file but does not map the view. MapViewOfFile maps a view of a file into the address space of a process.
With one important exception, file views derived from a single file-mapping object are coherent, or identical, at a specified time. If multiple processes have handles of the same file-mapping object, they see a coherent view of the data when they map a view of the file.
The exception has to do with remote files. Although this function works with remote files, it does not keep them coherent. For example, if two computers both map a file as writable, and both change the same page, each computer sees only its own view as it writes to the page. When the data is updated on the disk, it is not merged.
To fully close a file-mapping object, an application must unmap all mapped views of the file-mapping object by calling the UnmapViewOfFile function, and close the file-mapping object handle by calling CloseHandle. The order in which these functions are called does not matter. The call to UnmapViewOfFile is necessary because mapped views of a file-mapping object maintain internal open handles to the object, and a file-mapping object does not close until all open handles to it are closed.
The CreateFileForMapping function opens a file and returns a file handle that is passed to this function. This function then returns a handle to the file mapping. For Windows Embedded CE, it is necessary only to close the mapping handle as the file handle is automatically closed when you close the mapping handle.
If the file being mapped is an uncompressed ROM file and it is mapped with PAGE_READONLY access, the file is accessed directly from ROM without using any RAM to cache the data.
Security 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. Information in a readable memory-mapped file can be read by other processes on the system. Do not store confidential information in a memory-mapped file. Information in a writable memory-mapped file can be written to by other processes on the system. You must validate all data that you read from a memory mapped file. |
To implement a mapping-object creation function that fails if the object already exists, an application can use the following code:
hMap = CreateFileMapping(...);
if (hMap != NULL && GetLastError() == ERROR_ALREADY_EXISTS)
{
CloseHandle(hMap);
hMap = NULL;
}
return hMap;