CreateFileForMapping

The CreateFileForMapping function creates a file that can be used for memory mapping.

Syntax

HANDLE CreateFileForMapping(LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile);

At a Glance

Header file: Winbase.h
Module: Nk
Platforms: H/PC
Windows CE versions: 1.01 and later

Parameters

lpFileName
Pointer to a null-terminated string that specifies the name of the file that is to be created and used as a file-mapping object. The maximum length is MAX_PATH characters.
dwDesiredAccess
Type of access. An application can obtain read-only access to files, or query device attributes. You can use the following flag constants to build a value for this parameter.
0
Allows an application to query device attributes without actually accessing the device.
GENERIC_READ
Specifies read access to the file. Data can be read from the file and the file pointer can be moved.
dwShareMode
How this file can be shared. This parameter must be some combination of the following values:
0
Prevents the file from being shared.
FILE_SHARE_READ
Other open operations can be performed on the file for read access. If CreateFile is opening the client end of a mailslot, this flag is specified.
FILE_SHARE_WRITE
Other open operations can be performed on the file for write access.
lpSecurityAttributes
Not used. Must be NULL.
dwCreationDisposition
This parameter must be one of the following values:
CREATE_NEW
Creates a new file. The function fails if the specified file already exists.
CREATE_ALWAYS
Creates a new file. The function overwrites the file if it exists.
OPEN_EXISTING
Opens the file. The function fails if the file does not exist.
OPEN_ALWAYS
Opens the file, if it exists. If the file does not exist, the function creates the file as if dwCreationDistribution were CREATE_NEW.
TRUNCATE_EXISTING
Opens the file. Once opened, the file is truncated so that its size is zero bytes. The calling process must open the file with at least GENERIC_WRITE access. The function fails if the file does not exist.
dwFlagsAndAttributes
File attributes and flags for the file.

Any combination of the following attributes is acceptable, except all other file attributes override FILE_ATTRIBUTE_NORMAL.

FILE_ATTRIBUTE_ARCHIVE
The file is an archive file. Applications use this attribute to mark files for backup or removal.
FILE_ATTRIBUTE_COMPRESSED
The file or directory is compressed. For a file, this means that all of the data in the file is compressed. For a directory, this means that compression is the default for newly created files and subdirectories.
FILE_ATTRIBUTE_NORMAL
The file has no other attributes set. This attribute is valid only if used alone.
FILE_ATTRIBUTE_HIDDEN
The file is hidden. It is not to be included in an ordinary directory listing.
FILE_ATTRIBUTE_READONLY
The file is read only. Applications can read the file but cannot write to it or delete it.
FILE_ATTRIBUTE_SYSTEM
The file is part of or is used exclusively by the operating system.

Any combination of the following flags is acceptable:

FILE_FLAG_WRITE_THROUGH
Instructs the operating system to write through any intermediate cache and go directly to the file. The operating system can still cache write operations, but cannot lazily flush them.
FILE_FLAG_RANDOM_ACCESS
Indicates that the file is accessed randomly. Windows uses this flag to optimize file caching. Specifying this flag can increase performance for applications that read large files using sequential access. Performance gains can be even more noticeable for applications that read large files mostly sequentially, but occasionally skip over small ranges of bytes.
hTemplateFile
Not used. Ignored.

Return Values

If the function succeeds, the return value is an open handle to the specified file-mapping object. If the specified object exists before the function call and dwCreationDistribution is CREATE_ALWAYS or OPEN_ALWAYS, a call to GetLastError returns ERROR_ALREADY_EXISTS (even though the function has succeeded). If the file-mapping object does not exist before the call, GetLastError returns zero. If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.

Remarks

The CreateFileForMapping function is a special version of CreateFile that is designed for file-mapping objects. It performs a callback into the kernel's address space so that the specified file is created by the kernel. This ensures that the file is available to processes other than the creating process.

The handle returned from CreateFileForMapping is used as the hFile parameter in a subsequent call to CreateFileMapping, as shown in the following psuedo-code:

// psuedocode fragment to show call sequence, use of the handle
HANDLE hFile;
hFile = CreateFileForMapping(...);
CreateFileMapping(hFile, ...);

See Also

CreateFileMapping, MapViewOfFile, UnmapViewOfFile