INFO: CreateFileMapping() SEC_* Flags

This article was previously published under Q108231
This article has been archived. It is offered "as is" and will no longer be updated.
The definition of CreateFileMapping() is as follows:

   HANDLE CreateFileMapping(hFile, lpsa, fdwProtect, dwMaximumSizeHigh,				
dwMaximumSizeLow, lpszMapName)

   HANDLE hFile;   LPSECURITY_ATTRIBUTES lpsa;   DWORD fdwProtect;   DWORD dwMaximumSizeHigh;   DWORD dwMaximumSizeLow;   LPCTSTR lpszMapName;				

The following flags are four possible values for the parameter fdwProtect:

All pages of a section are to be set to the commit state.
The file specified for a section's file mapping is an executable image file.
All pages of a section are to be set as noncacheable.
All pages of a section are to be set to the reserved state.

If none of these flags are specified, SEC_COMMIT is the default. Thisbehaves the same way as MEM_COMMIT in VirtualAlloc().

Windows NT

The SEC_RESERVE flag is intended for file mappings that are backed by thepaging file, and therefore use SEC_RESERVE only when hFile is -1. The pagesare reserved just as they are when the MEM_RESERVE flag is used inVirtualAlloc(). The pages can be committed by subsequently using theVirtualAlloc() application programming interface (API), specifyingMEM_COMMIT. Once committed, these pages cannot be decommitted.

The SEC_NOCACHE flag is intended for architectures that require variouslocking structures to be located in memory that is not ever fetched intothe CPU cache. On x86 and MIPS machines, use of this flag just slows downthe performance because the hardware keeps the cache coherent. Certaindevice drivers may require noncached data so that programs can writethrough to the physical memory. SEC_NOCACHE requires that eitherSEC_RESERVE or SEC_COMMIT be used in addition to SEC_NOCACHE.

The SEC_IMAGE flag indicates that the file handle points to an executablefile, and it should be loaded as such. The mapping information and fileprotection are taken from the image file, and therefore no other optionsare allowed when SEC_IMAGE is used.

Windows 95

Under Windows NT, the Win32 loader simply sits on top of the memory mappedfile subsystem, and so when the loader needs to load a PE executable, itsimply calls down into the existing memory mapped file code. Therefore, itis extremely easy for to support SEC_IMAGE in CreateFileMapping() underWindows NT.

In Windows 95, the loader is more complex and the memory mapped files aresimple and only support the bare minimum of functionality to make theexisting MapViewOfFile() work. Therefore, Windows 95 does not supportSEC_IMAGE. There is support for SEC_NOCACHE, SEC_RESERVE and SEC_COMMIT.
3.10 3.50 4.00

Article ID: 108231 - Last Review: 02/24/2014 08:18:09 - Revision: 3.2

Microsoft Win32 Application Programming Interface

  • kbnosurvey kbarchive kbinfo KB108231