WDK。创建文件时候的错误,就帮组!!
NTSTATUS WriteFileTest(LPCWSTR pszDest)
{
OBJECT_ATTRIBUTES objectAttributes;
IO_STATUS_BLOCK iostatus;
HANDLE hfile;
UNICODE_STRING logFileUnicodeString;
//初始化UNICODE_STRING字符串
RtlInitUnicodeString(&logFileUnicodeString, L"\\??\\C:\\KeyLog.log");
//初始化objectAttributes
InitializeObjectAttributes(&objectAttributes,
&logFileUnicodeString,
OBJ_CASE_INSENSITIVE,//对大小写敏感
NULL,
NULL );
//创建文件
NTSTATUS ntStatus = ZwCreateFile(&hfile,
GENERIC_WRITE,
&objectAttributes,
&iostatus,
NULL,
FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_WRITE,
FILE_OPEN_IF,
FILE_NON_DIRECTORY_FILE |
FILE_RANDOM_ACCESS |
FILE_SYNCHRONOUS_IO_NONALERT,
NULL,
0);
if (!NT_SUCCESS(ntStatus))
{
KdPrint(("ZwCreateFile error\n"));
return ntStatus;
}
//构造要填充的数据
int const arraysize = BUFFER_SIZE;
WCHAR pBuffer[arraysize];
size_t cbDest = arraysize * sizeof(WCHAR);
RtlCopyMemory(pBuffer, pszDest, cbDest);
//获取文件信息
FILE_STANDARD_INFORMATION fsi;
ntStatus = ZwQueryInformationFile(hfile,
&iostatus,
&fsi,
sizeof(FILE_STANDARD_INFORMATION),
FileStandardInformation);
if (NT_SUCCESS(ntStatus))
{
KdPrint(("get the file pointer successfully.\n"));
}
//写文件
ZwWriteFile(hfile, NULL, NULL, NULL, &iostatus, pBuffer, cbDest, &fsi.EndOfFile, NULL);
KdPrint(("Write already!\n"));
//关闭文件句柄
ZwClose(hfile);
ExFreePool(pBuffer);
return ntStatus;
}