Creating and Opening Files
The CreateFile() function can create a new file or open an existing file. You must specify the file name, creation instructions, and other attributes. When an application creates a new file, the operating system adds it to the specified directory. The operating system assigns a unique identifier, called a handle, to each file that is opened or created using CreateFile(). An application can use this handle with functions that read from, write to, and describe the file. It is valid until all references to that handle are closed. When an application starts, it inherits all open handles from the process that started it if the handles were created as inheritable. An application should check the value of the handle returned by CreateFile() before attempting to use the handle to access the file. If an error occurs, the handle value will be INVALID_HANDLE_VALUE and the application can use the GetLastError() function for extended error information.
When an application uses CreateFile(), it must use the dwDesiredAccess parameter to specify whether it intends to read from the file, write to the file, both read and write, or neither. This is known as requesting an access mode. The application must also use the dwCreationDisposition parameter to specify what action to take if the file already exists, known as the creation disposition. For example, an application can call CreateFile() with dwCreationDisposition set to CREATE_ALWAYS to always create a new file, even if a file of the same name already exists (thus overwriting the existing file). Whether this succeeds or not depends on factors such as the previous file's attributes and security settings (see the following sections for more information). An application also uses CreateFile() to specify whether it wants to share the file for reading, writing, both, or neither. This is known as the sharing mode. An open file that is not shared (dwShareMode set to zero) cannot be opened again, either by the application that opened it or by another application, until its handle has been closed. This is also referred to as exclusive access.
When a process uses CreateFile() to attempt to open a file that has already been opened in a sharing mode (dwShareMode set to a valid non-zero value), the system compares the requested access and sharing modes to those specified when the file was opened. If you specify an access or sharing mode that conflicts with the modes specified in the previous call, CreateFile() fails. The following table illustrates the valid combinations of two calls to CreateFile() using various access modes and sharing modes (dwDesiredAccess, dwShareMode respectively). It does not matter in which order the CreateFile() calls are made. However, any subsequent file I/O operations on each file handle will still be constrained by the current access and sharing modes associated with that particular file handle.
|
First call to CreateFile() |
Valid second calls to CreateFile() |
|
GENERIC_READ, FILE_SHARE_READ |
GENERIC_READ, FILE_SHARE_READ GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE |
|
GENERIC_READ, FILE_SHARE_WRITE |
GENERIC_WRITE, FILE_SHARE_READ GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE |
|
GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE |
GENERIC_READ, FILE_SHARE_READ GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE GENERIC_WRITE, FILE_SHARE_READ GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE |
|
GENERIC_WRITE, FILE_SHARE_READ |
GENERIC_READ, FILE_SHARE_WRITE GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE |
|
GENERIC_WRITE, FILE_SHARE_WRITE |
GENERIC_WRITE, FILE_SHARE_WRITE GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE |
|
GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE |
GENERIC_READ, FILE_SHARE_WRITE GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE GENERIC_WRITE, FILE_SHARE_WRITE GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE |
|
GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ |
GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE |
|
GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE |
GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE |
|
GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE |
GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE |
In addition to the standard file attributes, you can also specify security attributes by including a pointer to a SECURITY_ATTRIBUTES structure as the fourth parameter of CreateFile(). However, the underlying file system must support security for this to have any effect (for example, the NTFS file system supports it but the various FAT file systems do not). An application creating a new file can supply an optional handle to a template file, from which CreateFile() takes file attributes and extended attributes for creation of the new file.
CreateFile() Scenarios
There are several fundamental scenarios for initiating access to a file using the CreateFile() function. These are summarized as:
These scenarios are controlled by the proper use of the dwCreationDisposition parameter. Below is a breakdown of how these scenarios map to values for this parameter and what happens when they are used. When creating or opening a new file when a file with that name does not already exist (dwCreationDisposition set to either CREATE_NEW, CREATE_ALWAYS, or OPEN_ALWAYS), the CreateFile() function performs the following actions:
When creating a new file even if a file of the same name already exists (dwCreationDisposition set to CREATE_ALWAYS), the CreateFile() function performs the following actions:
When opening an existing file (dwCreationDisposition set to either OPEN_EXISTING, OPEN_ALWAYS, or TRUNCATE_EXISTING), the CreateFile() function performs the following actions: