使用ZwCreateFile函数
/*打开文件 获取句柄的方法*/
#include "ntddk.h"
#include "wdm.h"
/*NTSTATUS DriverEntry(IN PDRIVER_OBJECT theDrverObject,IN PUNICODE_STRING theRegistryPath)
{
OBJECT_ATTRIBUTES objectAttributes;
IO_STATUS_BLOCK iostatus;
NTSTATUS ntStatus;
HANDLE hfile;
UNICODE_STRING logFileUnicodeString;
//初始化UNICODE_STRING字符串
RtlInitUnicodeString(&logFileUnicodeString,L"\\??\\C:\\1.log");
//或者写成"\\Device\\HarddiskVolume1\\1.LOG"
//初始化objectAttributes
InitializeObjectAttributes(&objectAttributes,&logFileUnicodeString,OBJ_CASE_INSENSITIVE,NULL,NULL);
//打开文件
ntStatus=ZwCreateFile(&hfile,GENERIC_READ,&objectAttributes,&iostatus,NULL,FILE_ATTRIBUTE_NORMAL,FILE_SHARE_READ,FILE_OPEN,FILE_SYNCHRONOUS_IO_NONALERT,NULL,0);
if(NT_SUCCESS(ntStatus))
{
KdPrint(("Open file successfully!\n"));
}else
{
KdPrint(("Open file unsuccessfully!\n"));
}
//文件操作
//关闭文件句柄
ZwClose(hfile);
}*/
NTSTATUS DriverEntry(IN PDRIVER_OBJECT theDrverObject,IN PUNICODE_STRING theRegistryPath)
{
OBJECT_ATTRIBUTES objectAttributes;
IO_STATUS_BLOCK iostatus;
HANDLE hfile;
NTSTATUS ntStatus;
UNICODE_STRING logFileUnicodeString;
//初始化UNICODE_STRING字符串
RtlInitUnicodeString(&logFileUnicodeString,L"\\??\\C:\\1.log");
//或者写成"\\Device\\HarddiskVolume1\\1.LOG"
//初始化objectAttributes
InitializeObjectAttributes(&objectAttributes,&logFileUnicodeString,OBJ_CASE_INSENSITIVE,NULL,NULL);
//打开文件
ntStatus=ZwOpenFile(&hfile,GENERIC_ALL,&objectAttributes,&iostatus,FILE_SHARE_READ|FILE_SHARE_WRITE,FILE_SYNCHRONOUS_IO_NONALERT);
if(NT_SUCCESS(ntStatus))
{
KdPrint(("Open file successfully!\n"));
}else
{
KdPrint(("Open file unsuccessfully!\n"));
}
//文件操作
//
//关闭文件
ZwClose(hfile);
}