CreateFile

CreateFile creates or opens a file. It then returns a handle that can be used to access the object.

Syntax

HANDLE CreateFile(
    LPCTSTR lpFileName,
    DWORD DesiredAccess,
    DWORD ShareMode,
    LPSECURITY_ATTRIBUTES lpSecurityAttributes,
    DWORD CreationDisposition,
    DWORD FlagsAndAttributes,
    HANDLE hTemplateFile
);

Parameters

lpFileName

A pointer to a null-terminated string that specifies the name of the object to create or open.

If *lpFileName is a path, the string size limit is RTX_MAX_PATH characters. This limit is related to how CreateFile parses paths.

The CreateFile API cannot be used to create or open files over a network under RTX.  The path should be to a file location on the local hard drive.

DesiredAccess

The type of access to the object. An application can obtain read access, write access, read-write access, or device query access. This parameter can be any combination of the following values.

ShareMode

Set of bit flags that specifies how the object can be shared. If ShareMode is 0, the object cannot be shared. Subsequent open operations on the object will fail, until the handle is closed.

To share the object, use a combination of one or more of the following values:

lpSecurityAttributes (ignored by RTX)

A pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpSecurityAttributes is NULL, the handle cannot be inherited.

The lpSecurityDescriptor member of the structure specifies a security descriptor for the object. If lpSecurityAttributes is NULL, the object gets a default security descriptor. The target file system must support security on files and directories for this parameter to have an effect on files.

CreationDisposition

Specifies which action to take on files that exist, and which action to take when files do not exist. For more information about this parameter, see the Comments section. This parameter must be one of the following values:

FlagsAndAttributes

The file attributes and flags for the file.

Valid Attributes
Any combination of the following attributes is acceptable for the FlagsAndAttributes parameter, except all other file attributes override FILE_ATTRIBUTE_NORMAL.

Valid Flags
Any combination of the following flags is acceptable for the FlagsAndAttributes parameter.

hTemplateFile

Ignored. A valid handle to a template file with the GENERIC_READ access right. The template file supplies file attributes and extended attributes for the file that is being created.

Return Values

An open handle to the specified file if the function succeeds, an INVALID_HANDLE_VALUE if the function fails

To get extended error information, call GetLastError

If the specified file exists before the function call and CreationDisposition is CREATE_ALWAYS or OPEN_ALWAYS, a call to GetLastError returns ERROR_ALREADY_EXISTS (even though the function has succeeded). If the file does not exist before the call, GetLastError returns ERROR_SUCCESS..

Remarks

Use CloseHandle to close an object handle returned by CreateFile.

As noted above, specifying zero for DesiredAccess allows an application to query device attributes without actually accessing the device. This type of querying is useful, for example, if an application wants to determine the size of a floppy disk drive and the formats it supports without having a floppy in the drive.

Files

When creating a new file, CreateFile performs the following actions:

When opening an existing file, CreateFile performs the following actions:

Some file systems, such as NTFS, support compression for individual files and directories. On volumes formatted for such a file system, a new file inherits the compression attribute of its directory.

Do not use CreateFile to set a file's compression state. Setting FILE_ATTRIBUTE_COMPRESSED in the FlagsAndAttributes parameter does nothing. Use DeviceIoControl and the FSCTL_SET_COMPRESSION operation to set a file's compression state.

Directories

An application cannot create a directory with CreateFile; it must call CreateDirectory to create a directory.

See Also:

CreateDirectory

DeviceIoControl

ReadFile

WriteFile

IntervalZero.com | Support | Give Feedback