CreateFileW
时间: 2023-08-09 10:09:12 浏览: 165
CreateFileW is a Windows API function that is used to create or open a file or device. It has several parameters that allow the user to specify the name of the file or device, the desired access mode, share mode, and other options.
The function returns a handle to the created or opened file, which can then be used in other file-related functions such as ReadFile, WriteFile, and CloseHandle.
Here is an example of how to use the CreateFileW function to open a text file:
```
#include <windows.h>
#include <stdio.h>
int main()
{
HANDLE hFile;
WCHAR fileName[] = L"C:\\example.txt";
DWORD dwDesiredAccess = GENERIC_READ;
DWORD dwShareMode = FILE_SHARE_READ;
LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL;
DWORD dwCreationDisposition = OPEN_EXISTING;
DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
HANDLE hTemplateFile = NULL;
hFile = CreateFileW(fileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("Failed to open file.\n");
return 1;
}
// File is open, do something with it...
CloseHandle(hFile);
return 0;
}
```
阅读全文