This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
Creates or opens a named or unnamed file mapping object for a specified file.
To specify the NUMA node for the physical memory, seeCreateFileMappingNuma.
HANDLE CreateFileMappingA( [in] HANDLE hFile, [in, optional] LPSECURITY_ATTRIBUTES lpFileMappingAttributes, [in] DWORD flProtect, [in] DWORD dwMaximumSizeHigh, [in] DWORD dwMaximumSizeLow, [in, optional] LPCSTR lpName);[in] hFile
A handle to the file from which to create a file mapping object.
The file must be opened with access rights that are compatible with the protection flags that theflProtect parameter specifies. It is not required, but it is recommended that filesyou intend to map be opened for exclusive access. For more information, seeFile Security and Access Rights.
IfhFile isINVALID_HANDLE_VALUE, the calling processmust also specify a size for the file mapping object in thedwMaximumSizeHigh anddwMaximumSizeLow parameters. In this scenario,CreateFileMapping creates a file mapping objectof a specified size that is backed by the system paging file instead of by a file in the file system.
[in, optional] lpFileMappingAttributes
A pointer to aSECURITY_ATTRIBUTESstructure that determines whether a returned handle can be inherited by child processes. ThelpSecurityDescriptor member of theSECURITY_ATTRIBUTES structure specifies asecurity descriptor for a new file mapping object.
IflpFileMappingAttributes isNULL, the handle cannot be inheritedand the file mapping object gets a default security descriptor. The access control lists (ACL) in the defaultsecurity descriptor for a file mapping object come from the primary or impersonation token of the creator. Formore information, seeFile Mapping Security and Access Rights.
[in] flProtect
Specifies the page protection of the file mapping object. All mapped views of the object must be compatiblewith this protection.
This parameter can be one of the following values.
An application can specify one or more of the following attributes for the file mapping object by combiningthem with one of the preceding page protection values.
| Value | Meaning |
|---|---|
| If the file mapping object is backed by the operating system paging file (thehfile parameter isINVALID_HANDLE_VALUE), specifies that when a view of the file is mapped into a process address space, the entire range of pages is committed rather than reserved. The system must have enough committable pages to hold the entire mapping. Otherwise,CreateFileMapping fails. This attribute has no effect for file mapping objects that are backed by executable image files or datafiles (thehfile parameter is a handle to a file). SEC_COMMIT cannot be combined withSEC_RESERVE. If no attribute is specified,SEC_COMMIT is assumed. |
| Specifies that the file that thehFile parameter specifies is an executable image file. TheSEC_IMAGE attribute must be combined with a page protection value such asPAGE_READONLY. However, this page protection value has no effect on views of theexecutable image file. Page protection for views of an executable image file is determined by the executablefile itself. No other attributes are valid withSEC_IMAGE. |
| Specifies that the file that thehFile parameter specifies is an executable image file that will not be executed and the loaded image file will have no forced integrity checks run. Additionally, mapping a view of a file mapping object created with theSEC_IMAGE_NO_EXECUTE attribute will not invoke driver callbacks registered using thePsSetLoadImageNotifyRoutine kernel API. TheSEC_IMAGE_NO_EXECUTE attribute must be combined with thePAGE_READONLY page protection value. No other attributes are valid withSEC_IMAGE_NO_EXECUTE. Windows Server 2008 R2, Windows 7, Windows Server 2008, Windows Vista, Windows Server 2003 and Windows XP: This value is not supported before Windows Server 2012 and Windows 8. |
| Enables large pages to be used for file mapping objects that are backed by the operating system paging file (thehfile parameter isINVALID_HANDLE_VALUE). This attribute is not supported for file mapping objects that are backed by executable image files or data files (thehFile parameter is a handle to an executable image or data file). The maximum size of the file mapping object must be a multiple of the minimum size of a large page returnedby theGetLargePageMinimum function. If it isnot,CreateFileMapping fails. When mapping aview of a file mapping object created withSEC_LARGE_PAGES, the base address andview size must also be multiples of the minimum large page size. SEC_LARGE_PAGES requires theSeLockMemoryPrivilegeprivilege to be enabled in the caller's token. IfSEC_LARGE_PAGES is specified,SEC_COMMIT must alsobe specified. Windows Server 2003: This value is not supported until Windows Server 2003 with SP1. Windows XP: This value is not supported. |
| Sets all pages to be non-cacheable. Applications should not use this attribute except whenexplicitly required for a device. Using the interlocked functions with memory that is mapped withSEC_NOCACHE can result in anEXCEPTION_ILLEGAL_INSTRUCTION exception. SEC_NOCACHE requires either theSEC_RESERVE orSEC_COMMIT attribute to be set. |
| If the file mapping object is backed by the operating system paging file (thehfile parameter isINVALID_HANDLE_VALUE), specifies that when a view of the file is mapped into a process address space, the entire range of pages is reserved for later use by the process rather than committed. Reserved pages can be committed in subsequent calls to theVirtualAlloc function. After the pages arecommitted, they cannot be freed or decommitted with theVirtualFree function. This attribute has no effect for file mapping objects that are backed by executable image files or datafiles (thehfile parameter is a handle to a file). SEC_RESERVE cannot be combined withSEC_COMMIT. |
| Sets all pages to be write-combined. Applications should not use this attribute except whenexplicitly required for a device. Using the interlocked functions with memory that is mapped withSEC_WRITECOMBINE can result in anEXCEPTION_ILLEGAL_INSTRUCTION exception. SEC_WRITECOMBINE requires either theSEC_RESERVE orSEC_COMMIT attribute to be set. Windows Server 2003 and Windows XP: This flag is not supported until Windows Vista. |
[in] dwMaximumSizeHigh
The high-orderDWORD of the maximum size of the file mapping object.
[in] dwMaximumSizeLow
The low-orderDWORD of the maximum size of the file mapping object.
If this parameter anddwMaximumSizeHigh are 0 (zero), the maximum size of the filemapping object is equal to the current size of the file thathFile identifies.
An attempt to map a file with a length of 0 (zero) fails with an error code ofERROR_FILE_INVALID. Applications should test for files with a length of 0 (zero) andreject those files.
[in, optional] lpName
The name of the file mapping object.
If this parameter matches the name of an existing mapping object, the function requests access to theobject with the protection thatflProtect specifies.
If this parameter isNULL, the file mapping object is created without a name.
IflpName matches the name of an existing event, semaphore, mutex, waitable timer, orjob object, the function fails, and theGetLastErrorfunction returnsERROR_INVALID_HANDLE. This occurs because these objects share thesame namespace.
The name can have a "Global" or "Local" prefix to explicitly create theobject in the global or session namespace. The remainder of the name can contain any character except thebackslash character (\). Creating a file mapping object in the global namespace from a session other thansession zero requires theSeCreateGlobalPrivilegeprivilege. For more information, seeKernel Object Namespaces.
Fast user switching is implemented by using Terminal Services sessions. The first user to log on uses session0 (zero), the next user to log on uses session 1 (one), and so on. Kernel object names must follow theguidelines that are outlined for Terminal Services so that applications can support multiple users.
If the function succeeds, the return value is a handle to the newly created file mapping object.
If the object exists before the function call, the function returns a handle to the existing object (with itscurrent size, not the specified size), andGetLastError returnsERROR_ALREADY_EXISTS.
If the function fails, the return value isNULL. To get extended error information,callGetLastError.
After a file mapping object is created, the size of the file must not exceed the size of the file mappingobject; if it does, not all of the 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 actual namedfile on disk and if the page protection allows write access (that is, theflProtectparameter specifiesPAGE_READWRITE orPAGE_EXECUTE_READWRITE), then the file on disk is increased to match the specified sizeof the file mapping object. If the file is extended, the contents of the file between the old end of the file andthe new end of the file are not guaranteed to be zero; the behavior is defined by the file system. If the fileon disk cannot be increased,CreateFileMapping failsandGetLastError returnsERROR_DISK_FULL.
The initial contents of the pages in a file mapping object backed by the operating system paging file are 0(zero).
The handle thatCreateFileMapping returns hasfull access to a new file mapping object, and can be used with any function that requires a handle to a filemapping object.
Multiple processes can share a view of the same file by either using a single shared file mapping object orcreating separate file mapping objects backed by the same file. A single file mapping object can be shared bymultiple processes through inheriting the handle at process creation, duplicating the handle, or opening the filemapping object by name. For more information, see theCreateProcess,DuplicateHandle andOpenFileMapping functions.
Creating a file mapping object does not actually map the view into a process address space. TheMapViewOfFile andMapViewOfFileEx functions map a view of a file into aprocess address space.
With one important exception, file views derived from any file mapping object that is backed by the same fileare coherent or identical at a specific time. Coherency is guaranteed for views within a process and for viewsthat are mapped by different processes.
The exception is related to remote files. AlthoughCreateFileMapping works with remote files, it doesnot keep them coherent. For example, if two computers both map a file as writable, and both change the same page,each computer only sees its own writes to the page. When the data gets updated on the disk, it is not merged.
A mapped file and a file that is accessed by using the input and output (I/O) functions(ReadFile andWriteFile) are not necessarily coherent.
Mapped views of a file mapping object maintain internal references to the object, and a file mapping objectdoes not close until all references to it are released. Therefore, to fully close a file mapping object, anapplication must unmap all mapped views of the file mapping object by callingUnmapViewOfFile and close the file mapping objecthandle by callingCloseHandle. These functions can becalled in any order.
When modifying a file through a mapped view, the last modification timestamp may not be updated automatically.If required, the caller should useSetFileTime to set thetimestamp.
Creating a file mapping object in the global namespace from a session other than session zero requires theSeCreateGlobalPrivilege privilege.Note that this privilege check is limited to the creation of file mapping objects and does not apply to openingexisting ones. For example, if a service or the system creates a file mapping object in the global namespace, anyprocess running in any session can access that file mapping object provided that the caller has the requiredaccess rights.
Windows XP: The requirement described in the previous paragraph was introduced with Windows Server 2003and Windows XP with SP2
Use structured exception handling to protect any code that writes to or reads from a file view. For moreinformation, seeReading and Writing From a File View.
To have a mapping with executable permissions, an application must callCreateFileMapping with eitherPAGE_EXECUTE_READWRITE orPAGE_EXECUTE_READ, and thencallMapViewOfFile withFILE_MAP_EXECUTE | FILE_MAP_WRITE orFILE_MAP_EXECUTE | FILE_MAP_READ.
In Windows Server 2012, this function is supported by the following technologies.
| Technology | Supported |
|---|---|
| Server Message Block (SMB) 3.0 protocol | Yes |
| SMB 3.0 Transparent Failover (TFO) | Yes |
| SMB 3.0 with Scale-out File Shares (SO) | Yes |
| Cluster Shared Volume File System (CsvFS) | Yes |
| Resilient File System (ReFS) | Yes |
For an example, seeCreating Named Shared Memory orCreating a File Mapping Using Large Pages.
| Requirement | Value |
|---|---|
| Minimum supported client | Windows XP [desktop apps only] |
| Minimum supported server | Windows Server 2003 [desktop apps only] |
| Target Platform | Windows |
| Header | winbase.h (include Windows.h, Memoryapi.h) |
| Library | Kernel32.lib |
| DLL | Kernel32.dll |
Creating a File Mapping Object
File Mapping Functions
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?