CreateFileForMapping (Windows CE 5.0)

Send Feedback

This function creates or opens a file that can be used for memory mapping.

HANDLE CreateFileForMapping( LPCTSTRlpFileName,DWORD dwDesiredAccess,DWORD dwShareMode,LPSECURITY_ATTRIBUTESlpSecurityAttributes,DWORD dwCreationDisposition,DWORD dwFlagsAndAttributes,HANDLEhTemplateFile);

Parameters

  • lpFileName
    [in] 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.

    Each object type, such as memory maps, semaphores, events, message queues, mutexes, and watchdog timers, has its own separate namespace. Empty strings, "", are handled as named objects. On Windows desktop-based platforms, synchronization objects all share the same namespace.

  • dwDesiredAccess
    [in] Specifies the type of access. An application can obtain read-only access to files, or query device attributes. The following table shows flag constants to build a value for this parameter.

    Value Description
    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.
    GENERIC_WRITE Specifies write access to files in Windows CE 2.10 and later.

    The share mode for a file is determined by the dwDesiredAccess value. If dwDesiredAccess is set to GENERIC_READ, the FILE_SHARE_READ share mode is used. This allows other open operations to be performed on the file for read access. Otherwise, the share mode is set to 0 and file sharing is not enabled.

  • dwShareMode
    [in] Ignored.

  • lpSecurityAttributes
    [in] Not used; set to NULL.

  • dwCreationDisposition
    [in] The following table shows possible values for this parameter.

    Value Description
    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
    [in] Specifies attributes and flags for the file.

    Any combination of valid attributes is acceptable. However, all other file attributes override FILE_ATTRIBUTE_NORMAL. The following table shows possible attribute values for dwFlagsAndAttributes.

    Value Description
    FILE_ATTRIBUTE_ARCHIVE Indicates that the file is an archive file. Applications use this attribute to mark files for backup or removal.
    FILE_ATTRIBUTE_COMPRESSED Indicates that 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 Indicates that the file has no other attributes set. This attribute is valid only if used alone.
    FILE_ATTRIBUTE_HIDDEN Indicates that the file is hidden. It is not to be included in an ordinary directory listing.
    FILE_ATTRIBUTE_READONLY Indicates that the file is read only. Applications can read the file but cannot write to it or delete it.
    FILE_ATTRIBUTE_SYSTEM Indicates that the file is part of or is used exclusively by the operating system.

    Any combination of the following flags is acceptable. The following table shows the possible flag values for dwFlagsAndAttributes.

    Value Description
    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

An open handle to the specified file-mapping object indicates success. 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. INVALID_HANDLE_VALUE indicates failure. 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.

You can use CreateFileForMapping to open any file, including files that are created by CreateFile, for memory mapping. Like CreateFile, you can also create a file with CreateFileForMapping.

A memory-mapped file should not be accessed by using the ReadFile or WriteFile functions. This can result in incosistancies between the actual file and the memory-mapped file.

Two writeable handles to the same file cannot be open at the same time to prevent inconsistencies between the file and the mapped view of it. In order for two processes to share the data in a memory-mapped file, they must both memory-map the file.

The FILE_SHARE_WRITE flag will be ignored and should not be used.

The handle returned from CreateFileForMapping is used as the hFile parameter in a subsequent call to CreateFileMapping, as shown in the following example.

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

If your process exits without having called CreateFileMapping as well as CloseHandle, the handle returned by CreateFileForMapping is automatically closed.

The CreateFileForMapping function opens a file and returns a file handle that is passed to the CreateFileMapping function. The CreateFileMapping function then returns a handle to the file mapping. For Windows CE, it is only necessary to close the mapping handle as the file handle is automatically closed when you close the mapping handle.

Windows Mobile Remarks

This function is deprecated for Windows Mobile-based devices. To create or open a file that can be used for memory mapping, call CreateFile.

Requirements

OS Versions: Windows CE 1.01 and later.
Header: Winbase.h.
Link Library: Coredll.lib.

See Also

CreateFileMapping | MapViewOfFile | UnmapViewOfFile

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.