windows内核读写文件

///
///
/// Copyright (c) 2014 - <company name here>
///
/// Original filename: FileOperation.cpp
/// Project          : FileOperation
/// Date of creation : 2014-06-19
/// Author(s)        : <author name(s)>
///
/// Purpose          : <description>
///
/// Revisions:
///  0000 [2014-06-19] Initial revision.
///
///

// $Id$

#ifdef __cplusplus
extern "C" {
#endif
#include <ntddk.h>
#include <wdm.h>
#include <string.h>
#ifdef __cplusplus
}; // extern "C"
#endif

#include "FileOperation.h"


HANDLE g_FileHandle = NULL;
const WCHAR* g_wzFileName = L"\\??\\C:\\fileOper.txt";


VOID DriverUnload(
    IN PDRIVER_OBJECT		DriverObject
    )
{
	KdPrint(("DriverUnload....."));
}


NTSTATUS MCreateFile()
{

	UNICODE_STRING usFileName;
	OBJECT_ATTRIBUTES obj;
	LARGE_INTEGER lFileAllocationSize;
	IO_STATUS_BLOCK ioSB;
	NTSTATUS status = STATUS_SUCCESS;

	RtlInitUnicodeString(&usFileName,g_wzFileName);


		InitializeObjectAttributes(
		&obj,
		&usFileName,
		OBJ_CASE_INSENSITIVE /*|  OBJ_KERNEL_HANDLE*/,
		NULL,
		NULL);


		lFileAllocationSize = RtlConvertLongToLargeInteger((ULONG)1024);

	 
		status = ZwCreateFile(
		&g_FileHandle,
		FILE_ALL_ACCESS,
		&obj,
		&ioSB,
		&lFileAllocationSize,
		FILE_ATTRIBUTE_NORMAL,
		0,
		FILE_OVERWRITE_IF,
		/*FILE_DIRECTORY_FILE*/FILE_SYNCHRONOUS_IO_NONALERT,????
		NULL,
		0);

		if (!NT_SUCCESS(status))
		{
			KdPrint(("zwCreateFile Error"));
		}else
		{
			KdPrint(("zwCreateFile ok"));
		}


	return STATUS_SUCCESS;
}

NTSTATUS MWriteFile()
{

	NTSTATUS status;
	IO_STATUS_BLOCK ioSB;
	LARGE_INTEGER kOffset = RtlConvertLongToLargeInteger((ULONG)0);
	UNICODE_STRING usWriteData;
	ANSI_STRING asWriteData;
	RtlInitUnicodeString(&usWriteData,L"RtlConvertLongToLargeInteger中文测试");
	RtlUnicodeStringToAnsiString(&asWriteData,&usWriteData,TRUE);



	status = ZwWriteFile(
		g_FileHandle,
		NULL,
		NULL,
		NULL,
		&ioSB,
		asWriteData.Buffer,
		asWriteData.Length,
		&kOffset,
		NULL);

	RtlFreeAnsiString(&asWriteData);

	return STATUS_SUCCESS;

}

NTSTATUS MReadFile()
{

	NTSTATUS status;
	IO_STATUS_BLOCK ioSB;
	FILE_STANDARD_INFORMATION fsi;

	UNICODE_STRING usWriteData;
	LARGE_INTEGER kOffset = RtlConvertLongToLargeInteger((ULONG)0);

	
	//ANSI_STRING asWriteData;
	
	//RtlInitUnicodeString(&usWriteData,L"RtlConvertLongToLargeInteger中文测试");
	//RtlUnicodeStringToAnsiString(&asWriteData,&usWriteData,TRUE);

	ZwQueryInformationFile(g_FileHandle,&ioSB,&fsi,sizeof(FILE_STANDARD_INFORMATION),FileStandardInformation);



	
	PUCHAR Buffer = (PUCHAR)ExAllocatePool(PagedPool,(LONG)fsi.EndOfFile.QuadPart);



	status = ZwReadFile(
		g_FileHandle,
		NULL,
		NULL,
		NULL,
		&ioSB,
		Buffer,
		(LONG)fsi.EndOfFile.QuadPart,
		&kOffset,
		NULL);

	//RtlFreeAnsiString(&asWriteData);
	KdPrint(("readData:%s",Buffer));
	ExFreePool(Buffer);


	return STATUS_SUCCESS;
}

void MCloseFile()
{

	if (g_FileHandle)
	{
		ZwClose(g_FileHandle);
		g_FileHandle = NULL;
	}
	KdPrint(("Close File Handle..."));
}

NTSTATUS QueryFileAttributes()
{
	return STATUS_SUCCESS;

}

NTSTATUS SetFileAttributes()
{

	return STATUS_SUCCESS;
}
#ifdef __cplusplus
extern "C" {
#endif
NTSTATUS DriverEntry(
    IN OUT PDRIVER_OBJECT   DriverObject,
    IN PUNICODE_STRING      RegistryPath
    )
{
  
	KdPrint(("FileOperation:DriverEntry....."));

	MCreateFile();
	MWriteFile();
	MReadFile();
	MCloseFile();

    DriverObject->DriverUnload = DriverUnload;

    return STATUS_SUCCESS;
}
#ifdef __cplusplus
}; // extern "C"
#endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值