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 260 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.
Value | Description |
---|---|
0 (zero) |
Specifies device query access to the object. An application can query device attributes without accessing the device. |
GENERIC_EXECUTE | The right to execute the file. |
GENERIC_READ |
Specifies read access to the object. Data can be read from the file and the file pointer can be moved. Combine with GENERIC_WRITE for read-write access. |
GENERIC_WRITE |
Specifies write access to the object. Data can be written to the file and the file pointer can be moved. Combine with GENERIC_READ for read-write access. |
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:
Value | Description |
---|---|
FILE_SHARE_DELETE |
Subsequent open operations on the object will succeed only if delete access is requested. |
FILE_SHARE_READ |
Subsequent open operations on the object will succeed only if read access is requested. |
FILE_SHARE_WRITE |
Subsequent open operations on the object will succeed only if write access is requested. |
lpSecurityAttributes (ignored by RTX64)
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:
Value | Description |
---|---|
CREATE_NEW |
Creates a new file. CreateFile fails if the specified file already exists. |
CREATE_ALWAYS |
Creates a new file. If the file exists, CreateFile overwrites the file and clears the existing attributes |
OPEN_EXISTING |
Opens the file. CreateFile fails if the file does not exist. See the Comments section for information on when to use the OPEN_EXISTING flag if using CreateFile for devices, including the console. |
OPEN_ALWAYS |
Opens the file, if it exists. If the file does not exist, CreateFile creates the file as if CreationDisposition 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. CreateFile fails if the file does not exist. |
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.
Value | Description |
---|---|
FILE_ATTRIBUTE_ARCHIVE |
The file should be archived. Applications use this attribute to mark files for backup or removal. |
FILE_ATTRIBUTE_HIDDEN |
The file is hidden. It is not to be included in an ordinary directory listing. |
FILE_ATTRIBUTE_NORMAL |
The file has no other attributes set. This attribute is valid only if used alone. |
FILE_ATTRIBUTE_OFFLINE |
The data of the file is not immediately available. Indicates that the file data has been physically moved to offline storage. |
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. |
FILE_ATTRIBUTE_TEMPORARY |
The file is being used for temporary storage. File systems attempt to keep all of the data in memory for quicker access rather than flushing the data back to mass storage. A temporary file should be deleted by the application as soon as it is no longer needed. |
Valid Flags
Any combination of the following flags is acceptable for the FlagsAndAttributes
parameter.
Value | Description |
---|---|
FILE_FLAG_WRITE_THROUGH |
Instructs the system to write through any intermediate cache and go directly to disk. Windows can still cache write operations, but cannot lazily flush them. |
FILE_FLAG_NO_BUFFERING |
Instructs the system to open the file with no intermediate buffering or caching. An application must meet certain requirements when working with files opened with FILE_FLAG_NO_BUFFERING:
|
FILE_FLAG_RANDOM_ACCESS |
Indicates that the file is accessed randomly. The system can use this as a hint to optimize file caching. |
FILE_FLAG_SEQUENTIAL_SCAN |
Indicates that the file is to be accessed sequentially from beginning to end. The system can use this as a hint to optimize file caching. If an application moves the file pointer for random access, optimum caching may not occur; however, correct operation is still guaranteed. 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. |
FILE_FLAG_DELETE_ON_CLOSE |
Indicates that the operating system is to delete the file immediately after all of its handles have been closed, not just the handle for which you specified FILE_FLAG_DELETE_ON_CLOSE. Subsequent open requests for the file will fail, unless FILE_SHARE_DELETE is used. |
hTemplateFile
Ignored.
Return Value
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.
Filesystem APIs only support absolute path names. If a relative or simple path name is passed into the function, it will return ERROR_PATH_NOT_FOUND.
Files
When creating a new file, CreateFile performs the following actions:
- Combines the file attributes and flags specified by FlagsAndAttributes with FILE_ATTRIBUTE_ARCHIVE.
- Sets the file length to zero.
- Copies the extended attributes supplied by the template file to the new file if the hTemplateFile parameter is specified.
When opening an existing file, CreateFile performs the following actions:
- Combines the file flags specified by FlagsAndAttributes with existing file attributes. CreateFile ignores the file attributes specified by FlagsAndAttributes.
- Sets the file length according to the value of CreationDisposition.
- Ignores the hTemplateFile parameter.
- Ignores the lpSecurityDescriptor member of the SECURITY_ATTRIBUTES structure if the lpSecurityAttributes parameter is not NULL. The other structure members are used. bInheritHandle is the only way to indicate whether the file handle can be inherited.
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.
Requirements
Minimum Supported Version | RTX64 2013 |
Header | windows.h |
Library | Rtx_Rtss.lib |
See Also: