Creates or opens a file or I/O device. Returns a handle that can be used to access the file or device. The returned handle has the GENERIC_READ
access right if dwDesiredAccess specifies GENERIC_READ
or GENERIC_ALL
; otherwise, the handle has 0
access right.
HANDLE CreateFileW(
LPCWSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile
);
Parameter | Description |
---|---|
lpFileName |
The name of the file or device to be created or opened. For block devices, this is typically a disk extent name, such as \\.\PhysicalDrive0. For logical drives, this is typically a drive letter, such as D:. For the server message block (SMB) file system, the name format is \\Server\Share\File. Possible values for lpFileName include:
For a device, the name must be preceded by `\\.\`. |
dwDesiredAccess |
The requested access to the file or device. This parameter can be one or more of the following values:
For pipes, the access rights are `GENERIC_READ` and `GENERIC_WRITE`. |
dwShareMode |
A bitmask that specifies the sharing mode of an opening file or device. A complying application should not be affected by the share mode of other threads that are opening the same file or device. Possible values:
|
lpSecurityAttributes |
A pointer to a |
dwCreationDisposition |
An action to take if the file either exists or does not exist. This parameter can be one of the following values:
|
dwFlagsAndAttributes |
A file or device attribute and flags. This parameter can be a combination of the following values:
|
hTemplateFile |
A handle to a template file with the |
If the function succeeds, the return value is an open handle to the specified file, device, named pipe, mail slot, or communications resource. The handle has the GENERIC_READ
access right if dwDesiredAccess specifies GENERIC_READ
or GENERIC_ALL
; otherwise, the handle has 0
access right.
If the function fails, the return value is INVALID_HANDLE_VALUE
. To get extended error information, call GetLastError
.
To create or open a file, use the CreateFileW
function. To open a file for reading or writing, use the CreateFileA
function.
For more information on handling files and devices, see File Management.
The CreateFileW
function can return a handle to a non-file system device, such as a named pipe, mail slot, or communications resource.
When opening a file, the dwCreationDisposition
parameter determines what happens if the file exists or does not exist. The dwShareMode
parameter determines if other processes can access the file concurrently.
If you are opening a file for writing, you should typically specify GENERIC_WRITE
in dwDesiredAccess
. If you want to read and write, specify both GENERIC_READ
and GENERIC_WRITE
.
The dwFlagsAndAttributes
parameter allows you to specify various file attributes and flags, such as whether the file should be encrypted, compressed, or opened for sequential access.
The hTemplateFile
parameter can be used to pass attributes to the new file from a template file. This is often used to inherit security attributes or other properties.
Minimum supported client | Windows 7 |
Minimum supported server | Windows Server 2008 R2 |
Header | fileapi.h (include windows.h ) |
Library | Kernel32.lib |
DLL | Kernel32.dll |