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;
}
这个函数代码如上,我用windbg调试的时候是在ZwCreateFile这个函数调用的时候报错,我是初学者,完全看不懂错误,也不知道怎么解决,我不知道是不是我参数的问题,我的想法是往里面写数据,并且数据从当前文件的最后追加,求帮组!!
------解决方案--------------------
FILE_OPEN_IF 存在则打开,不存在则失败
另外你的 L"\\??\\C:\\KeyLog.log"路径也有问题吧,内核里还搞个C:?
------解决方案--------------------
L"\\??\\C:\\KeyLog.log"路径有问题 换一个试试吧
------解决方案--------------------
换成L"\\DosDevices\\C:\\KeyLog.log"看看