【发布时间】:2020-10-12 07:24:25
【问题描述】:
我需要在 Windows 内核中获取文件大小。我将文件读入内核中的缓冲区,而代码如下。而且我挖掘了很多。
// read file
//LARGE_INTEGER byteOffset;
ntstatus = ZwCreateFile(&handle,
GENERIC_READ,
&objAttr, &ioStatusBlock,
NULL,
FILE_ATTRIBUTE_NORMAL,
0,
FILE_OPEN,
FILE_SYNCHRONOUS_IO_NONALERT,
NULL, 0);
if (NT_SUCCESS(ntstatus)) {
byteOffset.LowPart = byteOffset.HighPart = 0;
ntstatus = ZwReadFile(handle, NULL, NULL, NULL, &ioStatusBlock,
Buffer, (ULONG)BufferLength, &byteOffset, NULL);
if (NT_SUCCESS(ntstatus)) {
//Buffer[BufferLength - 1] = '\0';
//DbgPrint("%s\n", Buffer);
}
ZwClose(handle);
}
有人建议使用GetFileSize(),但是我的VS2019报如下错误:
Severity Code Description Project File Line Suppression State
Error (active) E0020 identifier "GetFileSize" is undefined TabletAudioSample D:\workspace\4\Windows-driver-samples\audio\sysvad\adapter.cpp 702
如果我添加头文件:
#include <fileapi.h>
然后又报错了:
Severity Code Description Project File Line Suppression State
Error (active) E1696 cannot open source file "fileapi.h" TabletAudioSample D:\workspace\4\Windows-driver-samples\audio\sysvad\adapter.cpp 24
谢谢!
【问题讨论】:
-
您需要
ZwQueryInformationFile或NtQueryInformationFile(取决于以前的模式)和FileStandardInformation