驱动层和r3程序通讯的列子参考爱写驱动的女装大佬

开发环境:VS2013+WDK8.1

驱动层:

#include 

#define	 DEVICE_NAME "\\Device\\MyReadDevice"
#define  SYM_LINK_NAME	"\\??\\MyRead"

NTSTATUS DriverUnload(PDRIVER_OBJECT driver)
{
	DbgPrint("unload me");
	return STATUS_SUCCESS;
}

NTSTATUS MyDriverRead(PDEVICE_OBJECT device,PIRP pirp)
{
	PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(pirp);
	ULONG	readLength = stack->Parameters.Read.Length;
	pirp->IoStatus.Status = STATUS_SUCCESS;
	pirp->IoStatus.Information = readLength;
	memset(pirp->AssociatedIrp.SystemBuffer, 0x90, readLength);
	IoCompleteRequest(pirp, IO_NO_INCREMENT);
	DbgPrint("ReadOver\n");
	return STATUS_SUCCESS;
}

NTSTATUS MyDriverCreate(PDEVICE_OBJECT device, PIRP pirp)
{

	pirp->IoStatus.Status = STATUS_SUCCESS;
	pirp->IoStatus.Information = 0;
	IoCompleteRequest(pirp, IO_NO_INCREMENT);
	DbgPrint("create device success\n");
	return STATUS_SUCCESS;
}
//NTSTATUS MyDriverWrite(PDEVICE_OBJECT device, PIRP pirp)
//{
//	PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(pirp);
//	ULONG	readLength = stack->Parameters.Read.Length;
//	pirp->IoStatus.Status = STATUS_SUCCESS;
//	pirp->IoStatus.Information = readLength;
//	memset(pirp->AssociatedIrp.SystemBuffer, 0x90, readLength);
//	IoCompleteRequest(pirp, IO_NO_INCREMENT);
//	DbgPrint("ReadOver\n");
//	return STATUS_SUCCESS;
//}

NTSTATUS DriverEntry(PDRIVER_OBJECT driver, PUNICODE_STRING reg_path)
{
	
	driver->MajorFunction[IRP_MJ_READ] = MyDriverRead;
	driver->MajorFunction[IRP_MJ_CREATE] = MyDriverCreate;
	driver->DriverUnload = DriverUnload;
	PDEVICE_OBJECT pDevice;
	UNICODE_STRING DeviceName;
	
	RtlInitUnicodeString(&DeviceName, L"\\Device\\MyReadDevice");
	NTSTATUS status = IoCreateDevice(driver, 0, &DeviceName,FILE_DEVICE_UNKNOWN,0,TRUE,&pDevice);
	if (!NT_SUCCESS(status)){
		DbgPrint("Create Device Failed\n");
		return STATUS_SUCCESS;
	}
	pDevice->Flags |= DO_BUFFERED_IO;
	DbgPrint("Crate Device Successfully!");
	UNICODE_STRING SymLinkName;
	RtlInitUnicodeString(&SymLinkName, L"\\??\\MyRead");
	status = IoCreateSymbolicLink(&SymLinkName, &DeviceName);
	if (!NT_SUCCESS(status)){
		DbgPrint("Create SymbolLink Failed\n");
		IoDeleteDevice(pDevice);
		return STATUS_SUCCESS;
	}
	
	
	return STATUS_SUCCESS;

}

r3层:

// R3Read.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include 
#include 

int _tmain(int argc, _TCHAR* argv[])
{

	HANDLE	hDevice = CreateFile(L"\\\\.\\MyRead", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hDevice == INVALID_HANDLE_VALUE){
		printf("Failed to obtain file handle to Device");
		getchar();
		return 1;
	}
	UCHAR buffer[10];
	ULONG ulRead;
	BOOL  bRet = ReadFile(hDevice, buffer, 10, &ulRead, NULL);
	if (bRet){
		printf("Read %d bytes;\n", ulRead);
		for (int i = 0; i < ulRead; i++){
			printf("%02x", buffer[i]);
		}
	}
	getchar();
	return 0;
}

查看结果:
驱动层和r3程序通讯的列子参考爱写驱动的女装大佬_第1张图片

你可能感兴趣的:(二进制)