Windows驱动派遣函数的学习

//派遣处理例程的介绍:

//IPR简介:
//IRP全称(I/O Request Package),即输入输出请求包。他是windows驱动的重要概念,用户模式下所有对驱动程序的I/O请求,全部由操作系统转化为一个叫做IRP的数据结构,不同的IRP数据会被“派遗”到不//同的派遣函数(DisPatch Function)中处理。

//作用:上层应用程序与底层驱动通信即EXE程序与SYS的通信

//五种常用的IRP类型:
#define IRP_MJ_CREATE                     0x00 //CreateFile
#define IRP_MJ_CLOSE                      0x02 //CloseHandle 
#define IRP_MJ_READ                       0x03//ReadFile
#define IRP_MJ_WRITE                      0x04//WriteFile
#define IRP_MJ_DEVICE_CONTROL		  0x0e//DeviceIoControl  


//过程:
//1、创建IRP处理函数
//2、在驱动入口函数里DriverEntry()注册IRP处理函数
//3、细化IRP函数处理例程

//相关内核API:

//获取指定IRP的堆栈
PIO_STACK_LOCATION IoGetCurrentIrpStackLocation( IN  PIRP Irp)

//完成IRP请求
VOID IoCompleteRequest(
			  IN  PIRP Irp,		    //指定需要完成的IPR
			  IN  CCHAR PriorityBoost)  //等级


//方法1:统一注册派遗函数

//注册派遗函数
pDriverObject->MajorFunction[IRP_MJ_CREATE]=ddk_DispatchRoutine;
//注册派遗函数
pDriverObject->MajorFunction[IRP_MJ_CLOSE]=ddk_DispatchRoutine;
//注册派遗函数
pDriverObject->MajorFunction[IRP_MJ_READ]=ddk_DispatchRoutine;
//注册派遗函数
pDriverObject->MajorFunction[IRP_MJ_WRITE]=ddk_DispatchRoutine;
//注册派遗函数
pDriverObject->MajorFunction[ IRP_MJ_DEVICE_CONTROL]=ddk_DispatchRoutine;

NTSTATUS ddk_DispatchRoutine(IN PDEVICE_OBJECT pDevobj, IN PIRP pIrp)
{
	//获取当前IRP的堆栈
   PIO_STACK_LOCATION irpsp=IoGetCurrentIrpStackLocation(pIrp);

   switch (irpsp->MajorFunction) 
   {
	   case IRP_MJ_CREATE:
			break;
	   case IRP_MJ_CLOSE:
			break;
	   case IRP_MJ_READ:
			break;
	   case IRP_MJ_WRITE:
			break;
	   case  IRP_MJ_DEVICE_CONTROL:
			break;
	   default:
	}

	//完成IRP请求
	// ......

	//成功返回
	return STATUS_SUCCESS;
}

//******************************************************************************************************

//方法二:分开注册派遗函数

//注册派遗函数
pDriverObject->MajorFunction[IRP_MJ_CREATE]=ddk_DispatchRoutine_CREATE;
//注册派遗函数
pDriverObject->MajorFunction[IRP_MJ_CLOSE]=ddk_DispatchRoutine_CLOSE;
//注册派遗函数
pDriverObject->MajorFunction[IRP_MJ_READ]=ddk_DispatchRoutine_READ;
//注册派遗函数
pDriverObject->MajorFunction[IRP_MJ_WRITE]=ddk_DispatchRoutine_WRITE;
//注册派遗函数
pDriverObject->MajorFunction[ IRP_MJ_DEVICE_CONTROL]=ddk_DispatchRoutine_CONTROL;

//派遣处理例程
NTSTATUS ddk_DispatchRoutine_CONTROL(IN PDEVICE_OBJECT pDevobj,IN PIRP pIrp	)
{
    //对相应的IPR进行处理

   pIrp->IoStatus.Information=0;		//设置操作的字节数为0
   pIrp->IoStatus.Status=STATUS_SUCCESS;	//返回成功
   IoCompleteRequest(pIrp,IO_NO_INCREMENT);	//指示完成此IRP
  
   return STATUS_SUCCESS; //返回成功
}


//参考资料:
//郁金香讲课资料整理

你可能感兴趣的:(Windows驱动派遣函数的学习)