核心驱动文件操作 How to access file in Kernel
2009-05-25 21:40
726 查看
NTSTATUS
ZwCreateFile
(
OUT PHANDLE
FileHandle
,
IN ACCESS_MASK
DesiredAccess
,
IN POBJECT_ATTRIBUTES
ObjectAttributes
,
OUT PIO_STATUS_BLOCK
IoStatusBlock
,
IN PLARGE_INTEGER
AllocationSize
OPTIONAL,
IN ULONG
FileAttributes
,
IN ULONG
ShareAccess
,
IN ULONG
CreateDisposition
,
IN ULONG
CreateOptions
,
IN PVOID
EaBuffer
OPTIONAL,
IN ULONG
EaLength
);
NTSTATUS
ZwQueryInformationFile
(
IN HANDLE
FileHandle
,
OUT PIO_STATUS_BLOCK
IoStatusBlock
,
OUT PVOID
FileInformation
,
IN ULONG
Length
,
IN FILE_INFORMATION_CLASS
FileInformationClass
);
NTSTATUS
ZwSetInformationFile
(
IN HANDLE
FileHandle
,
OUT PIO_STATUS_BLOCK
IoStatusBlock
,
IN PVOID
FileInformation
,
IN ULONG
Length
,
IN FILE_INFORMATION_CLASS
FileInformationClass
);
NTSTATUS
ZwWriteFile
(
IN HANDLE
FileHandle
,
IN HANDLE
Event
OPTIONAL,
IN PIO_APC_ROUTINE
ApcRoutine
OPTIONAL,
IN PVOID
ApcContext
OPTIONAL,
OUT PIO_STATUS_BLOCK
IoStatusBlock
,
IN PVOID
Buffer
,
IN ULONG
Length
,
IN PLARGE_INTEGER
ByteOffset
OPTIONAL,
IN PULONG
Key
OPTIONAL
);
NTSTATUS
ZwReadFile
(
IN HANDLE
FileHandle
,
IN HANDLE
Event
OPTIONAL,
IN PIO_APC_ROUTINE
ApcRoutine
OPTIONAL,
IN PVOID
ApcContext
OPTIONAL,
OUT PIO_STATUS_BLOCK
IoStatusBlock
,
OUT PVOID
Buffer
,
IN ULONG
Length
,
IN PLARGE_INTEGER
ByteOffset
OPTIONAL,
IN PULONG
Key
OPTIONAL
);
打开/创建/读/写文件 的操作基本上和 SDK API 的使用差不多,但是文件指针的移动,属性的修改比 SDK API
稍复杂一些,主要使用 ZwQueryInformationFile 和 ZwSetInformationFile
这两个函数完成,下面举个例子大概说明一下这两个函数的使用。
//Create the log file IO_STATUS_BLOCK file_status ; OBJECT_ATTRIBUTES obj_attrib ; CCHAR ntNameFile [ 64 ] = "//DosDevices//c://temp.txt"; STRING ntNameString ; UNICODE_STRING uFileName ; FILE_STANDARD_INFORMATION fsi ; FILE_POSITION_INFORMATION fpi ; RtlInitAnsiString ( & ntNameString , ntNameFile ); RtlAnsiStringToUnicodeString (& uFileName , & ntNameString , TRUE ); InitializeObjectAttributes (& obj_attrib , & uFileName , OBJ_CASE_INSENSITIVE , NULL , NULL ); Status = ZwCreateFile (& pKeyboardDeviceExtension -> hLogFile , GENERIC_WRITE ,& obj_attrib ,& file_status , NULL , FILE_ATTRIBUTE_NORMAL , FILE_SHARE_READ , FILE_OPEN_IF , FILE_SYNCHRONOUS_IO_NONALERT , NULL , 0 ); RtlFreeUnicodeString (& uFileName ); ZwQueryInformationFile ( pKeyboardDeviceExtension -> hLogFile , & file_status , & fsi , sizeof (FILE_STANDARD_INFORMATION), FileStandardInformation); fpi . CurrentByteOffset = fsi . EndOfFile ; ZwSetInformationFile ( pKeyboardDeviceExtension -> hLogFile , & file_status , & fpi , sizeof (FILE_POSITION_INFORMATION), FilePositionInformation); if ( Status != STATUS_SUCCESS ) { DbgPrint ("Failed to create log file.../n"); DbgPrint ("File Status = %x/n", file_status ); } else { DbgPrint ("Successfully created log file.../n"); DbgPrint ("File Handle = %x/n", pKeyboardDeviceExtension -> hLogFile ); DbgPrint ("Move to Eof %d/n", fsi . EndOfFile ); } |
在 SDK 编程中我们只需要
SetFilePointer
(hAppend,
0
,
NULL
,
FILE_END
);
就可以将文件指针移动到文件尾,但在 DDK 编程中我们需要使用 ZwQueryInformationFile 获得文件大小,然后使用 ZwSetInformationFile 将文件指针移动到需要的位置。
相关文章推荐
- 核心驱动文件操作 How to access file in Kernel Driver
- how to access file in kernel space
- How to create an XTR file from XML in Delphi XE4 using XML Mapper?(delphi中如何通过xmlmapper创建xtr文件)
- How to access a value defined in the application.properties file in Spring Boot ( Externalized Conf)
- How to get the size of file in C++ - 在C++中如何获取文件的大小
- 解决文件图片等上传DebugFilesKeyError: You tried to access the file "file" in the request
- How to know is the file exist in C++ - 在C++中如何判断文件是否存在
- How to Access and Use the Windows Hosts File in Windows 7 and Vista
- Shell: how to list all db links in oracle DB to generate a flat file (生成dblink列表文件)
- How to Enable or Disable Access to Windows Updates in Windows 7
- How to judge jack state in kernel
- Java IO 之 RandomAccessFile 操作文件内容
- How To Access Forms Directly In Oracle Applications R12 [ID 552301.1]
- HOW TO: Upload a File to a Web Server in ASP.NET by Using Visual Basic .NET
- How to read file in Java – BufferedInputStream Example
- How To Use The New Multivalue Field In Access 2007/2010 Sunday, June 19th, 2011
- How to determine who is logged on to a database by using Microsoft Jet UserRoster in Access 2000
- 如何缩小或者扩大数据文件 How to Resize a Datafile (Doc ID 1029252.6)
- How to determine if a driver should have been registered in kernel and lspci vs pci_device_id
- How to Parse or Read XML File in Java >> XML Tutorial Example