- WinAPI Programmer's References
Telephony API (TAPI)
- Websites for programmers
Unix Manual Pages
- Software for programmers
The CreateFile function creates or opens the following objects and returns a handle that can
be used to access the object:
- communications resources
- disk devices (Windows NT only)
- directories (open only)
Points to a null-terminated string that specifies the name of the object
(file, pipe, mailslot, communications resource, disk device, console, or directory)
to create or open.
If *lpFileName is a path, there is a default string size limit of MAX_PATH characters. This
limit is related to how the CreateFile function parses paths.
Windows NT: You can use paths longer than MAX_PATH characters by calling the wide (W)
version of CreateFile and prepending "\\?\" to the path. The "\\?\" tells the function to turn off
path parsing. This lets you use paths that are nearly 32,000 Unicode characters
long. You must use fully-qualified paths with this technique. This also works
with UNC names. The "\\?\" is ignored as part of the path. For example,
"\\?\C:\myworld\private" is seen as "C:\myworld\private", and
"\\?\UNC\tom_1\hotstuff\coolapps" is seen as "\\tom_1\hotstuff\coolapps".
Specifies 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.
| LPCTSTR lpFileName,
||// pointer to name of the file
| DWORD dwDesiredAccess,
||// access (read-write) mode
| DWORD dwShareMode,
||// share mode
| LPSECURITY_ATTRIBUTES lpSecurityAttributes,
||// pointer to security attributes
| DWORD dwCreationDistribution,
||// how to create
| DWORD dwFlagsAndAttributes,
||// file attributes
| HANDLE hTemplateFile
||// handle to file with attributes to copy
Set of bit flags that specifies how the object can be shared. If dwShareMode 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:
||Specifies device query access to the object. An application can query device
attributes without accessing the device.
||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.
||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.
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.
Windows NT: 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.
Windows 95: The lpSecurityDescriptor member of the structure is ignored.
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
Remarks section. This parameter must be one of the following values:
||Windows NT only: Subsequent open operations on the object will succeed only if delete access
||Subsequent open operations on the object will succeed only if read access is
||Subsequent open operations on the object will succeed only if write access is
Specifies the file attributes and flags for the file.
Any combination of the following attributes is acceptable, except all other
file attributes override FILE_ATTRIBUTE_NORMAL.
||Creates a new file. The function fails if the specified file already exists.
||Creates a new file. The function overwrites the file if it exists.
||Opens the file. The function fails if the file does not exist.
||See the Remarks section for a discussion of why you should use the
OPEN_EXISTING flag if you are using the CreateFile function for devices, including the console.
||Opens the file, if it exists. If the file does not exist, the function creates
the file as if dwCreationDistribution were CREATE_NEW.
||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.
Any combination of the following flags is acceptable.
||The file should be archived. Applications use this attribute to mark files for
backup or removal.
||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.
||The file is hidden. It is not to be included in an ordinary directory listing.
||The file has no other attributes set. This attribute is valid only if used
||The data of the file is not immediately available. Indicates that the file
data has been physically moved to offline storage.
||The file is read only. Applications can read the file but cannot write to it
or delete it.
||The file is part of or is used exclusively by the operating system.
||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.
||Instructs the operating system to write through any intermediate cache and go
directly to disk. The operating system can still cache write operations, but
cannot lazily flush them.
||Instructs the operating system to initialize the object, so ReadFile, WriteFile, ConnectNamedPipe, and TransactNamedPipe operations that take a significant amount of time to process return
ERROR_IO_PENDING. When the operation is finished, an event is set to the signaled state.
||When you specify FILE_FLAG_OVERLAPPED, the ReadFile and WriteFile functions must specify an OVERLAPPED structure. That is, when FILE_FLAG_OVERLAPPED is specified, an application must perform overlapped reading and writing.
||When FILE_FLAG_OVERLAPPED is specified, the operating system does not maintain
the file pointer. The file position must be passed as part of the lpOverlapped parameter (pointing to an OVERLAPPED structure) to the ReadFile and WriteFile functions.
||This flag also enables more than one operation to be performed simultaneously
with the handle (a simultaneous read and write operation, for example).
||Instructs the operating system to open the file with no intermediate buffering
or caching. This can provide performance gains in some situations.
An application must meet certain requirements when working with files opened
One way to align buffers on integer multiples of the volume sector size is to
use VirtualAlloc to allocate the buffers. It allocates memory that is aligned on addresses
that are integer multiples of the operating system's memory page size. Since both
memory page and volume sector sizes are powers of 2, this memory is also
aligned on addresses that are integer multiples of a volume's sector size.
- File access must begin at byte offsets within the file that are integer
multiples of the volume's sector size.
- File access must be for numbers of bytes that are integer multiples of the
volume's sector size. For example, if the sector size is 512 bytes, an application
can request reads and writes of 512, 1024, or 2048 bytes, but not of 335, 981,
or 7171 bytes.
- Buffer addresses for read and write operations must be aligned on addresses in
memory that are integer multiples of the volume's sector size.
An application can determine a volume's sector size by calling the GetDiskFreeSpace function.
||Indicates that the file is accessed randomly. Windows can use this as a hint
to optimize file caching.
||Indicates that the file is to be accessed sequentially from beginning to end.
Windows 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.
||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
Subsequent open requests for the file will fail, unless FILE_SHARE_DELETE is
||Windows NT only: Indicates that the file is being opened or created for a backup or restore
operation. The operating system ensures that the calling process overrides file
security checks, provided it has the necessary permission to do so. The relevant
permissions are SE_BACKUP_NAME and SE_RESTORE_NAME.
You can also set this flag to obtain a handle to a directory. A directory
handle can be passed to some Win32 functions in place of a file handle.
If the CreateFile function opens the client side of a named pipe, the dwFlagsAndAttributes parameter can also contain Security Quality of Service information. When the
calling application specifies the SECURITY_SQOS_PRESENT flag, the dwFlagsAndAttributes parameter can contain one or more of the following values:
||Indicates that the file is to be accessed according to POSIX rules. This
includes allowing multiple files with names, differing only in case, for file
systems that support such naming. Use care when using this option because files
created with this flag may not be accessible by applications written for MS-DOS,
Windows, or Windows NT.
For more information, see Security.
Specifies a handle with GENERIC_READ access to a template file. The template
file supplies file attributes and extended attributes for the file being
Windows 95: This value must be NULL. If you supply a handle under Windows 95, the call
fails and GetLastError returns ERROR_NOT_SUPPORTED.
If the function succeeds, the return value is an open handle to the specified
file. If the specified file 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 does not exist before the call, GetLastError returns zero.
If the function fails, the return value is INVALID_HANDLE_VALUE. To get
extended error information, call GetLastError.
Use the CloseHandle function to close an object handle returned by CreateFile.
As noted above, specifying zero for dwDesiredAccess 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.
When creating a new file, the CreateFile function performs the following actions:
||Specifies to impersonate the client at the Anonymous impersonation level.
||Specifies to impersonate the client at the Identification impersonation level.
||Specifies to impersonate the client at the Impersonation impersonation level.
||Specifies to impersonate the client at the Delegation impersonation level.
||Specifies that the security tracking mode is dynamic. If this flag is not
specified, Security Tracking Mode is static.
||Specifies that only the enabled aspects of the client's security context are
available to the server. If you do not specify this flag, all aspects of the
client's security context are available.
This flag allows the client to limit the groups and privileges that a server
can use while impersonating the client.
When opening an existing file, CreateFile performs the following actions:
- Combines the file attributes and flags specified by dwFlagsAndAttributes 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.
If you are attempting to create a file on a floppy drive that does not have a
floppy disk or a CD-ROM drive that does not have a CD, the system displays a
message box asking the user to insert a disk or a CD, respectively. To prevent
the system from displaying this message box, call the SetErrorMode function with SEM_FAILCRITICALERRORS.
If CreateFile opens the client end of a named pipe, the function uses any instance of the
named pipe that is in the listening state. The opening process can duplicate the
handle as many times as required but, once opened, the named pipe instance
cannot be opened by another client. The access specified when a pipe is opened
must be compatible with the access specified in the dwOpenMode parameter of the CreateNamedPipe function. For more information about pipes, see Pipes.
If CreateFile opens the client end of a mailslot, the function returns INVALID_HANDLE_VALUE
if the mailslot client attempts to open a local mailslot before the mailslot
server has created it with the CreateMailSlot function. For more information about mailslots, see Mailslots.
The CreateFile function can create a handle to a communications resource, such as the serial
port COM1. For communications resources, the dwCreationDistribution parameter must be OPEN_EXISTING, and the hTemplate parameter must be NULL. Read, write, or read-write access can be specified,
and the handle can be opened for overlapped I/O. For more information about
communications, see Communications.
Windows NT: You can use the CreateFile function to open a disk drive or a partition on a disk drive. The function
returns a handle to the disk device; that handle can be used with the DeviceIOControl function. The following requirements must be met in order for such a call to
- Combines the file flags specified by dwFlagsAndAttributes with existing file attributes. CreateFile ignores the file attributes specified by dwFlagsAndAttributes.
- Sets the file length according to the value of dwCreationDistribution.
- 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. The bInheritHandle member is the only way to indicate whether the file handle can be inherited.
- The caller must have administrative privileges for the operation to succeed on
a hard disk drive.
- The lpFileName string should be of the form \\.\PHYSICALDRIVEx to open the hard disk x. Hard disk numbers start at zero. For example:
||Obtains a handle to the third physical drive on the user's computer.
- The lpFileName string should be \\.\x: to open a floppy drive x or a partition x on a hard disk. For example:
Windows 95: This technique does not work for opening a logical drive. In Windows 95,
specifying a string in this form causes CreateFile to return an error.
||Obtains a handle to drive A on the user's computer.
||Obtains a handle to drive C on the user's computer.
The CreateFile function can create a handle to console input (CONIN$). If the process has an
open handle to it as a result of inheritance or duplication, it can also
create a handle to the active screen buffer (CONOUT$). The calling process must be
attached to an inherited console or one allocated by the AllocConsole function. For console handles, set the CreateFile parameters as follows:
- The dwCreationDistribution parameter must have the OPEN_EXISTING value.
- When opening a floppy disk or a partition on a hard disk, you must set the
FILE_SHARE_WRITE flag in the dwShareMode parameter.
The following list shows the effects of various settings of fwdAccess and lpFileName.
||Use the CONIN$ value to specify console input and the CONOUT$ value to specify
||CONIN$ gets a handle to the console's input buffer, even if the SetStdHandle function redirected the standard input handle. To get the standard input
handle, use the GetStdHandle function.
||CONOUT$ gets a handle to the active screen buffer, even if SetStdHandle redirected the standard output handle. To get the standard output handle, use GetStdHandle.
||GENERIC_READ | GENERIC_WRITE is preferred, but either one can limit access.
||If the calling process inherited the console or if a child process should be
able to access the console, this parameter must be FILE_SHARE_READ |
||If you want the console to be inherited, the bInheritHandle member of the SECURITY_ATTRIBUTES structure must be TRUE.
||You should specify OPEN_EXISTING when using CreateFile to open the console.
An application cannot create a directory with CreateFile; it must call CreateDirectory or CreateDirectoryEx to create a directory.
You can obtain a handle to a directory by setting the
FILE_FLAG_BACKUP_SEMANTICS flag. A directory handle can be passed to some Win32 functions in place of a
Some file systems, such as NTFS, support compression for individual files and
directories. On volumes formatted for such a file system, a new directory
inherits the compression attribute of its parent directory.
AllocConsole, CloseHandle, ConnectNamedPipe, CreateDirectory, CreateDirectoryEx, CreateNamedPipe, DeviceIOControl, GetDiskFreeSpace, GetOverlappedResult, GetStdHandle, OpenFile, OVERLAPPED, ReadFile, SECURITY_ATTRIBUTES, SetErrorMode, SetStdHandle TransactNamedPipe, VirtualAlloc, WriteFile
||Opens console for input.
||Opens console for output.
|Windows 95: Causes CreateFile to fail; GetLastError returns ERROR_PATH_NOT_FOUND.
Windows NT: Causes CreateFile to fail; GetLastError returns ERROR_FILE_NOT_FOUND.
Programming books recommended by Amazon.com
More programming books on Amazon.com