CreateFileW function
The CreateFileW function creates or opens a file or I/O device.
HANDLE CreateFileW(
LPCWSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile
);
Parameters
| Parameter | Description |
|---|---|
lpFileName |
The name of the file or device. For more information, see the Remarks section of this topic. |
dwDesiredAccess |
The requested access to the file or device.
For a list of valid values, see File Access Permissions.
This parameter can be zero, but cannot be both zero and This parameter can be one or more of the following flags:
|
dwShareMode |
Specifies how the file or device is to be shared in subsequent open operations. This parameter can be a combination of one of the following values:
|
lpSecurityAttributes |
A pointer to a
The |
dwCreationDisposition |
Specifies the action to take if the file specified by This parameter can be one of the following values:
|
dwFlagsAndAttributes |
Applies only when creating a new file. This parameter can include flags that specify attributes of the file or device, and modifiers that affect how the function handles the file or device. See File and Directory Attributes for a list of attribute flags.
Use the |
hTemplateFile |
A handle to a template file with the
This parameter can be
If this parameter is not |
Return Value
If the function succeeds, the return value is an open handle to the specified file, device, named pipe, or mail slot.
The handle has the specified access rights to the file or device.
If the function fails, the return value is INVALID_HANDLE_VALUE.
To get extended error information, call GetLastError.
Remarks
The CreateFileW function can return a handle to a different type of object than specified by the dwCreationDisposition parameter.
For example, if dwCreationDisposition is OPEN_EXISTING and the file object specified by lpFileName is a symbolic link, CreateFileW returns a handle to the symbolic link itself, not the target of the symbolic link.
You can use the GetFinalPathNameByHandle function to get the path to the final target of the symbolic link.
To specify a maximum period of time to wait for the CreateFileW function to retrieve a handle for a file, use the CreateFileTransacted function.
The lpFileName parameter can specify a relative path, an absolute path, or a device name.
The CreateFileW function is the primary function used to create or open files.
It provides control over access, sharing, creation, and attributes.
When opening a device, the dwCreationDisposition parameter is typically set to OPEN_EXISTING and the dwShareMode parameter should be set to 0.