我写程序,喜欢以最简单的方式写,因为我怕眼花,我的程序只能看到程序功能,没什么语法特点,都是最基本的.
#include <wdm.h>
#define NT_DEVICE_NAME L"\Device\Example"
#define DOS_DEVICE_NAME L"\DosDevices\Example"
VOID DriverUnload( IN PDRIVER_OBJECT pDriverObject );
NTSTATUS DispatchFunction( IN PDEVICE_OBJECT pDeviceObject, IN PIRP Irp );
NTSTATUS DispatchRead( IN PDEVICE_OBJECT pDeviceObject, IN PIRP Irp );
NTSTATUS DispatchWrite( IN PDEVICE_OBJECT pDeviceObject, IN PIRP Irp );
NTSTATUS DriverEntry( IN PDRIVER_OBJECT pDriverObject, IN PIRP Irp )
{
NTSTATUS status = STATUS_SUCCESS;
PDEVICE_OBJECT pDeviceObject = NULL;
UNICODE_STRING usNtDeviceName, usDosDeviceName;
int nIndex = 0;
RtlInitUnicodeString( &usNtDeviceName, NT_DEVICE_NAME );
status = IoCreateDevice( pDriverObject,
0,
&usNtDeviceName,
FILE_DEVICE_UNKNOWN,
FILE_DEVICE_SECURE_OPEN,
FALSE,
&pDeviceObject );
if ( NT_SUCCESS(status) )
{
DbgPrint( "DriverEntry" );
pDriverObject->DriverUnload = DriverUnload;
RtlInitUnicodeString( &usDosDeviceName, DOS_DEVICE_NAME );
IoCreateSymbolicLink( &usDosDeviceName, &usNtDeviceName );
for (nIndex; nIndex <= IRP_MJ_MAXIMUM_FUNCTION; nIndex ++)
pDriverObject->MajorFunction[ nIndex ] = DispatchFunction;
}
return status;
}
VOID DriverUnload( IN PDRIVER_OBJECT pDriverObject )
{
UNICODE_STRING usDosDeviceName;
RtlInitUnicodeString( &usDosDeviceName, DOS_DEVICE_NAME );
DbgPrint( "DriverUnload" );
IoDeleteSymbolicLink( &usDosDeviceName );
IoDeleteDevice( pDriverObject->DeviceObject );
}
NTSTATUS DispatchFunction( IN PDEVICE_OBJECT pDeviceObject, IN PIRP Irp )
{
NTSTATUS status = STATUS_SUCCESS;
PIO_STACK_LOCATION irpStack = NULL;
irpStack = IoGetCurrentIrpStackLocation( Irp );
switch ( irpStack->MajorFunction )
{
case IRP_MJ_CREATE:
break;
case IRP_MJ_READ:
DispatchRead( pDeviceObject, Irp );
break;
case IRP_MJ_WRITE:
DispatchWrite( pDeviceObject, Irp );
break;
default:
DbgPrint( "DispatchFunction" );
break;
}
IoCompleteRequest( Irp, IO_NO_INCREMENT );
return status;
}
NTSTATUS DispatchRead( IN PDEVICE_OBJECT pDeviceObject, IN PIRP Irp )
{
NTSTATUS status = STATUS_SUCCESS;
PIO_STACK_LOCATION irpStack = NULL;
char *pBuf = "88888888";
int nLen = sizeof ("88888888");
irpStack = IoGetCurrentIrpStackLocation( Irp );
if ( irpStack )
{
ProbeForWrite( Irp->UserBuffer, irpStack->Parameters.Read.Length, TYPE_ALIGNMENT(char) );
RtlCopyMemory( Irp->UserBuffer, pBuf, nLen );
}
return status;
}
NTSTATUS DispatchWrite( IN PDEVICE_OBJECT pDeviceObject, IN PIRP Irp )
{
NTSTATUS status = STATUS_SUCCESS;
PIO_STACK_LOCATION irpStack = NULL;
irpStack = IoGetCurrentIrpStackLocation( Irp );
if ( irpStack )
{
ProbeForRead( Irp->UserBuffer, irpStack->Parameters.Read.Length, TYPE_ALIGNMENT(char) );
DbgPrint( Irp->UserBuffer );
}
return status;
}