一个比较简单驱动程序初学者可以看看
我寫程序,喜歡以最簡單的方式寫,因為我怕眼花,我的程序只能看到程序功能,沒什么語法特點,都是最基本的.
#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;
}
總結
以上是生活随笔為你收集整理的一个比较简单驱动程序初学者可以看看的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于DCF(判别相关滤波器)的闭式解详细
- 下一篇: Matconvnet安装:win7+VS