The IoCreateFile routine either causes a new file or directory to be created, or it opens an existing file, device, directory, or volume, giving the caller a handle for the file object.
NTSTATUS
IoCreateFile(
OUT PHANDLE FileHandle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes,
OUT PIO_STATUS_BLOCK IoStatusBlock,
IN PLARGE_INTEGER AllocationSize OPTIONAL,
IN ULONG FileAttributes,
IN ULONG ShareAccess,
IN ULONG Disposition,
IN ULONG CreateOptions,
IN PVOID EaBuffer OPTIONAL,
IN ULONG EaLength,
IN CREATE_FILE_TYPE CreateFileType,
IN PVOID ExtraCreateParameters OPTIONAL,
IN ULONG Options
) ;
FILE_CREATED
FILE_OPENED
FILE_OVERWRITTEN
FILE_SUPERSEDED
FILE_EXISTS
FILE_DOES_NOT_EXIST
| ShareAccess Flags | Meaning |
|---|---|
| FILE_SHARE_READ | The file can be opened for read access by other threads’ calls to IoCreateFile. |
| FILE_SHARE_WRITE | The file can be opened for write access by other threads’ calls to IoCreateFile. |
| FILE_SHARE_DELETE | The file can be opened for delete access by other threads’ calls to IoCreateFile. |
Device and intermediate drivers usually set ShareAccess to zero, which gives the caller exclusive access to the open file.
| Disposition Values | Meaning |
|---|---|
| FILE_SUPERSEDE | If the file already exists, replace it with the given file. If it does not, create the given file. |
| FILE_CREATE | If the file already exists, fail the request and do not create or open the given file. If it does not, create the given file. |
| FILE_OPEN | If the file already exists, open it instead of creating a new file. If it does not, fail the request and do not create a new file. |
| FILE_OPEN_IF | If the file already exists, open it. If it does not, create the given file. |
| FILE_OVERWRITE | If the file already exists, open it and overwrite it. If it does not, fail the request. |
| FILE_OVERWRITE_IF | If the file already exists, open it and overwrite it. If it does not, create the given file. |
| CreateOptions Flags | Meaning |
|---|---|
| FILE_DIRECTORY_FILE | The file being created or opened is a directory file. With this flag, the Disposition parameter must be set to one of FILE_CREATE, FILE_OPEN, or FILE_OPEN_IF. With this flag, other compatible CreateOptions flags include only the following: FILE_SYNCHRONOUS_IO_ALERT, FILE_SYNCHRONOUS_IO _NONALERT, FILE_WRITE_THROUGH, FILE_OPEN_FOR_BACKUP_INTENT, and FILE_OPEN_BY_FILE_ID. |
| FILE_NON_DIRECTORY_FILE | The file being opened must not be a directory file or this call will fail. The file object being opened can represent a data file, a logical, virtual, or physical device, or a volume. |
| FILE_WRITE_THROUGH | System services, FSDs, and drivers that write data to the file must actually transfer the data into the file before any requested write operation is considered complete. |
| FILE_SEQUENTIAL_ONLY | All accesses to the file will be sequential. |
| FILE_RANDOM_ACCESS | Accesses to the file can be random, so no sequential read-ahead operations should be performed on the file by FSDs or the system. |
| FILE_NO_INTERMEDIATE _BUFFERING |
The file cannot be cached or buffered in a driver’s internal buffers. This flag is incompatible with the DesiredAccess FILE_APPEND_DATA flag. |
| FILE_SYNCHRONOUS_IO_ALERT | All operations on the file are performed synchronously. Any wait on behalf of the caller is subject to premature termination from alerts. This flag also causes the I/O system to maintain the file position context. If this flag is set, the DesiredAccess SYNCHRONIZE flag also must be set. |
| FILE_SYNCHRONOUS_IO _NONALERT |
All operations on the file are performed synchronously. Waits in the system to synchronize I/O queuing and completion are not subject to alerts. This flag also causes the I/O system to maintain the file position context. If this flag is set, the DesiredAccess SYNCHRONIZE flag also must be set. |
| FILE_CREATE_TREE _CONNECTION |
Create a tree connection for this file in order to open it over the network. This flag is irrelevant to device and intermediate drivers. |
| FILE_COMPLETE_IF_OPLOCKED | Complete this operation immediately with an alternate success code if the target file is oplocked, rather than blocking the caller's thread. If the file is oplocked, another caller already has access to the file over the network. This flag is irrelevant to device and intermediate drivers. |
| FILE_NO_EA_KNOWLEDGE | If the extended attributes on an existing file being opened indicate that the caller must understand EAs to properly interpret the file, fail this request because the caller does not understand how to deal with EAs. Device and intermediate drivers can ignore this flag. |
| FILE_DELETE_ON_CLOSE | Delete the file when the last handle to it is passed to ZwClose. |
| FILE_OPEN_BY_FILE_ID | The file name specified in the ObjectAttributes parameter includes the 8-byte file reference number for the file. This number is assigned by the file system and is file-system-specific. If the file is a reparse point, the file name also includes the name of a device. Note: The FAT file system does not support FILE_OPEN_BY_FILE_ID. |
| FILE_OPEN_FOR_BACKUP _INTENT |
The file is being opened for backup intent, hence, the system should check for certain access rights and grant the caller the appropriate accesses to the file before checking the input DesiredAccess against the file's security descriptor. This flag is irrelevant to device and intermediate drivers. |
| Options Flags | Meaning |
|---|---|
| IO_NO_PARAMETER_CHECKING | Indicates that the parameters for this call should not be validated before attempting to issue the create request. Driver writers should use this flag with caution as certain invalid parameters can cause a system failure. |
| IO_FORCE_ACCESS_CHECK | Indicates that the I/O Manager must check the operation against the file's security descriptor. |
IoCreateFile either returns STATUS_SUCCESS or an appropriate error status. If it returns an error status, the caller can find additional information about the cause of the failure by checking the IoStatusBlock.
Declared in wdm.h and ntddk.h. Include wdm.h or ntddk.h.
The handle, obtained by IoCreateFile, can be used by subsequent calls to manipulate data within the file or the file object's state or attributes.
There are two alternate ways to specify the name of the file to be created or opened with IoCreateFile:
Certain DesiredAccess flags and combinations of flags have the following effects:
The ShareAccess parameter determines whether separate threads can access the same file, possibly simultaneously. Provided that both file openers have the privilege to access a file in the specified manner, the file can be successfully opened and shared. If the original caller of IoCreateFile does not specify FILE_SHARE_READ, FILE_SHARE_WRITE, or FILE_SHARE_DELETE, no other open operations can be performed on the file: that is, the original caller is given exclusive access to the file.
In order for a shared file to be successfully opened, the requested DesiredAccess to the file must be compatible with both the DesiredAccess and ShareAccess specifications of all preceding opens that have not yet been released with ZwClose. That is, the DesiredAccess specified to IoCreateFile for a given file must not conflict with the accesses that other openers of the file have disallowed.
The Disposition value FILE_SUPERSEDE requires that the caller have DELETE access to a existing file object. If so, a successful call to IoCreateFile with FILE_SUPERSEDE on an existing file effectively deletes that file, and then recreates it. This implies that, if the file has already been opened by another thread, it opened the file by specifying a ShareAccess parameter with the FILE_SHARE_DELETE flag set. Note that this type of disposition is consistent with the POSIX style of overwriting files.
The Disposition values FILE_OVERWRITE_IF and FILE_SUPERSEDE are similar. If IoCreateFile is called with a existing file and either of these Disposition values, the file will be replaced.
Overwriting a file is semantically equivalent to a supersede operation, except for the following:
The CreateOptions FILE_DIRECTORY_FILE value specifies that the file to be created or opened is a directory file. When a directory file is created, the file system creates an appropriate structure on the disk to represent an empty directory for that particular file system's on-disk structure. If this option was specified and the given file to be opened is not a directory file, or if the caller specified an inconsistent CreateOptions or Disposition value, the call to IoCreateFile will fail.
The CreateOptions FILE_NO_INTERMEDIATE_BUFFERING flag prevents the file system from performing any intermediate buffering on behalf of the caller. Specifying this value places certain restrictions on the caller's parameters to the Zw..File routines, including the following:
The CreateOptions FILE_SYNCHRONOUS_IO_ALERT and FILE_SYNCHRONOUS_IO_NONALERT, which are mutually exclusive as their names suggest, specify that all I/O operations on the file are to be synchronous as long as they occur through the file object referred to by the returned FileHandle. All I/O on such a file is serialized across all threads using the returned handle. With either of these CreateOptions, the DesiredAccess SYNCHRONIZE flag must be set so that the I/O Manager will use the file object as a synchronization object. With either of these CreateOptions set, the I/O Manager maintains the “file position context” for the file object, an internal, current file position offset. This offset can be used in calls to ZwReadFile and ZwWriteFile. Its position also can be queried or set with ZwQueryInformationFile and ZwSetInformationFile.
Driver routines that run in a process context other than that of the system process must set the OBJ_KERNEL_HANDLE attribute for the ObjectAttributes parameter of IoCreateFile. This restricts the use of the handle returned by IoCreateFile to processes running only in kernel mode. Otherwise, the handle can be accessed by the process in whose context the driver is running. Drivers can call InitializeObjectAttributes to set the OBJ_KERNEL_HANDLE attribute as follows.
InitializeObjectAttributes(&ObjectAttributes, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);
Callers of IoCreateFile must be running at IRQL = PASSIVE_LEVEL.