新手不懂,如何在驱动中获取指定文件的大小。百度找了一些源码作参考
WIN32下实现
DLLAPI long APICALL getfilesize(LPTSTR filepath)
{
HANDLE handle = CreateFile(filepath, FILE_READ_EA,
FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
if (handle != INVALID_HANDLE_VALUE)
{
long size = GetFileSize(handle, NULL);
CloseHandle(handle);
return size;
}
else
{
return 0;
}
}
驱动中实现:
NTSTATUS MyCreateFile(OUT PHANDLE lpFileHandle,
IN PUNICODE_STRING usFileName,
IN ULONG dwDesiredAccess,
IN ULONG dwShareAccess,
IN ULONG dwCreateDisposition,
IN ULONG dwCreateOptions)
{
NTSTATUS ntStatus = STATUS_UNSUCCESSFUL;
OBJECT_ATTRIBUTES oaName;
IO_STATUS_BLOCK iosBlock;
if (lpFileHandle != NULL && usFileName != NULL && usFileName->Buffer != NULL)
{
if (PASSIVE_LEVEL != KeGetCurrentIrql())
{
DbgPrint("IRQL错误\n");
return ntStatus;
}
InitializeObjectAttributes(&oaName,
usFileName,
OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
NULL,
NULL);
ntStatus = ZwCreateFile(lpFileHandle,
dwDesiredAccess,
&oaName,
&iosBlock,
NULL,
FILE_ATTRIBUTE_NORMAL,
dwShareAccess,
dwCreateDisposition,
dwCreateOptions,
NULL,
0);
if (!NT_SUCCESS(ntStatus))
{
DbgPrint("[MyCreateFile]ZwCreateFile(%ws)failed with error:%08x\r\n", usFileName->Buffer, ntStatus);
return ntStatus;
}
}
return ntStatus;
}
NTSTATUS MyCloseFile(IN HANDLE hFile)
{
return ZwClose(hFile);
}
BOOL testCreateFile (IN PUNICODE_STRING filename)
{
HANDLE hFile=NULL;
NTSTATUS status;
IO_STATUS_BLOCK isb;
OBJECT_ATTRIBUTES oa;
InitializeObjectAttributes(&oa,filename,OBJ_CASE_INSENSITIVE|OBJ_KERNEL_HANDLE,NULL,NULL);
status=ZwCreateFile(&hFile,GENERIC_ALL,&oa,&isb,NULL,FILE_ATTRIBUTE_NORMAL,FILE_SHARE_READ,FILE_CREATE,FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT,NULL,0);
if(NT_SUCCESS(status))
{
status=STATUS_SUCCESS;
}
else
{
status=isb.Status;
}
DbgPrint("hFile=%08X",hFile);
if(hFile)
{
ZwClose(hFile);
}
return status;
}
自己测试:
void daimaceshi()
{
UNICODE_STRING str = RTL_CONSTANT_STRING(L"E:\\1.txt");
HANDLE FileHandle=NULL;
MyCreateFile(&FileHandle,&str,8,1,3,0);
}
在E盘有1.txt这个文件的前提下,打开错误,返回值0xC000003B
希望大大们能多提供一些源码,本人扣扣 3799 05426
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课