设置完成历程

来源:互联网 发布:mac 无线传文件 安卓 编辑:程序博客网 时间:2024/06/07 19:16
#include <windows.h>#include <stdio.h>int main(){HANDLE hDevice = CreateFile(L"\\\\.\\HelloDDKA",GENERIC_READ | GENERIC_WRITE,0,// share mode noneNULL,// no securityOPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL );// no templateif (hDevice == INVALID_HANDLE_VALUE){printf("Failed to obtain file handle to device ""with Win32 error code: %d\n", GetLastError() );return 1;}DWORD dRet;ReadFile(hDevice,NULL,0,&dRet,NULL);system("pause");CloseHandle(hDevice);return 0;}
/************************************************************************* 文件名称:Driver.h                                                 * 作    者:张帆* 完成日期:2007-11-1*************************************************************************/#pragma once#ifdef __cplusplusextern "C"{#endif#include <NTDDK.h>#ifdef __cplusplus}#endif #define PAGEDCODE code_seg("PAGE")#define LOCKEDCODE code_seg()#define INITCODE code_seg("INIT")#define PAGEDDATA data_seg("PAGE")#define LOCKEDDATA data_seg()#define INITDATA data_seg("INIT")#define arraysize(p) (sizeof(p)/sizeof((p)[0]))typedef struct _DEVICE_EXTENSION {PDEVICE_OBJECT pDevice;UNICODE_STRING ustrDeviceName;//设备名称UNICODE_STRING ustrSymLinkName;//符号链接名KDPC pollingDPC;// 存储DPC对象KTIMER pollingTimer;// 存储计时器对象PIRP currentPendingIRP;//记录当前挂起的IRP} DEVICE_EXTENSION, *PDEVICE_EXTENSION;// 函数声明NTSTATUS CreateDevice (IN PDRIVER_OBJECT pDriverObject);VOID HelloDDKUnload (IN PDRIVER_OBJECT pDriverObject);NTSTATUS HelloDDKDispatchRoutine(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp);NTSTATUS HelloDDKRead(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp);NTSTATUS HelloDDKCreate(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp);NTSTATUS HelloDDKClose(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp) ;
/************************************************************************* 文件名称:Driver.cpp                                                 * 作    者:张帆* 完成日期:2007-11-1*************************************************************************/#include "Driver.h"/************************************************************************* 函数名称:DriverEntry* 功能描述:初始化驱动程序,定位和申请硬件资源,创建内核对象* 参数列表:      pDriverObject:从I/O管理器中传进来的驱动对象      pRegistryPath:驱动程序在注册表的中的路径* 返回 值:返回初始化驱动状态*************************************************************************/#pragma INITCODEextern "C" NTSTATUS DriverEntry (IN PDRIVER_OBJECT pDriverObject,IN PUNICODE_STRING pRegistryPath) {NTSTATUS status;KdPrint(("DriverA:Enter A DriverEntry\n"));//注册其他驱动调用函数入口pDriverObject->DriverUnload = HelloDDKUnload;pDriverObject->MajorFunction[IRP_MJ_CREATE] = HelloDDKCreate;pDriverObject->MajorFunction[IRP_MJ_CLOSE] = HelloDDKClose;pDriverObject->MajorFunction[IRP_MJ_WRITE] = HelloDDKDispatchRoutine;pDriverObject->MajorFunction[IRP_MJ_READ] = HelloDDKRead;//创建驱动设备对象status = CreateDevice(pDriverObject);KdPrint(("DriverA:Leave A DriverEntry\n"));return status;}#pragma LOCKEDCODEVOID OnTimerDpc( IN PKDPC pDpc,IN PVOID pContext,IN PVOID SysArg1,IN PVOID SysArg2 ) {PDEVICE_OBJECT pDevObj = (PDEVICE_OBJECT)pContext;PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION)pDevObj->DeviceExtension;PIRP currentPendingIRP = pdx->currentPendingIRP;KdPrint(("DriverA:complete the Driver A IRP_MJ_READ irp!\n"));//设置完成状态为STATUS_SUCCESS currentPendingIRP->IoStatus.Status = STATUS_SUCCESS; currentPendingIRP->IoStatus.Information = 0;// bytes xfered IoCompleteRequest( currentPendingIRP, IO_NO_INCREMENT );}/************************************************************************* 函数名称:CreateDevice* 功能描述:初始化设备对象* 参数列表:      pDriverObject:从I/O管理器中传进来的驱动对象* 返回 值:返回初始化状态*************************************************************************/#pragma INITCODENTSTATUS CreateDevice (IN PDRIVER_OBJECTpDriverObject) {NTSTATUS status;PDEVICE_OBJECT pDevObj;PDEVICE_EXTENSION pDevExt;//创建设备名称UNICODE_STRING devName;RtlInitUnicodeString(&devName,L"\\Device\\MyDDKDeviceA");//创建设备status = IoCreateDevice( pDriverObject,sizeof(DEVICE_EXTENSION),&(UNICODE_STRING)devName,FILE_DEVICE_UNKNOWN,0, TRUE,&pDevObj );if (!NT_SUCCESS(status))return status;pDevObj->Flags |= DO_BUFFERED_IO;pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension;pDevExt->pDevice = pDevObj;pDevExt->ustrDeviceName = devName;KeInitializeTimer( &pDevExt->pollingTimer );KeInitializeDpc( &pDevExt->pollingDPC,OnTimerDpc,(PVOID) pDevObj );//创建符号链接UNICODE_STRING symLinkName;RtlInitUnicodeString(&symLinkName,L"\\??\\HelloDDKA");pDevExt->ustrSymLinkName = symLinkName;status = IoCreateSymbolicLink( &symLinkName,&devName );if (!NT_SUCCESS(status)) {IoDeleteDevice( pDevObj );return status;}return STATUS_SUCCESS;}/************************************************************************* 函数名称:HelloDDKUnload* 功能描述:负责驱动程序的卸载操作* 参数列表:      pDriverObject:驱动对象* 返回 值:返回状态*************************************************************************/#pragma PAGEDCODEVOID HelloDDKUnload (IN PDRIVER_OBJECT pDriverObject) {PDEVICE_OBJECTpNextObj;KdPrint(("DriverA:Enter A DriverUnload\n"));pNextObj = pDriverObject->DeviceObject;//while (pNextObj != NULL) //{//PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)//pNextObj->DeviceExtension;////删除符号链接//UNICODE_STRING pLinkName = pDevExt->ustrSymLinkName;//IoDeleteSymbolicLink(&pLinkName);//pNextObj = pNextObj->NextDevice;//IoDeleteDevice( pDevExt->pDevice );//}UNICODE_STRING symLinkName;RtlInitUnicodeString(&symLinkName, L"\\??\\HelloDDKA");pNextObj = pDriverObject->DeviceObject;//我的第一个设备                        IoDeleteSymbolicLink(&symLinkName);//删除符号连接                        IoDeleteDevice(pDriverObject->DeviceObject);//删除设备      KdPrint(("DriverB:Enter B DriverUnload\n"));KdPrint(("DriverA:Leave A DriverUnload\n"));}/************************************************************************* 函数名称:HelloDDKRead* 功能描述:对读IRP进行处理* 参数列表:      pDevObj:功能设备对象      pIrp:从IO请求包* 返回 值:返回状态*************************************************************************/#pragma PAGEDCODENTSTATUS HelloDDKRead(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp) {KdPrint(("DriverA:Enter A HelloDDKRead\n"));PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension;//将IRP设置为挂起IoMarkIrpPending(pIrp);//将挂起的IRP记录下来pDevExt->currentPendingIRP = pIrp;//定义3秒后将IRP_MJ_READ的IRP完成ULONG ulMicroSecond = 3000000;//将32位整数转化成64位整数LARGE_INTEGER timeout = RtlConvertLongToLargeInteger(-10*ulMicroSecond);KeSetTimer(&pDevExt->pollingTimer,timeout,&pDevExt->pollingDPC );KdPrint(("DriverA:Leave A HelloDDKRead\n"));//返回pending状态return STATUS_PENDING;}/************************************************************************* 函数名称:HelloDDKDispatchRoutine* 功能描述:对读IRP进行处理* 参数列表:      pDevObj:功能设备对象      pIrp:从IO请求包* 返回 值:返回状态*************************************************************************/#pragma PAGEDCODENTSTATUS HelloDDKDispatchRoutine(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp) {KdPrint(("DriverA:Enter A HelloDDKDispatchRoutine\n"));NTSTATUS status = STATUS_SUCCESS;// 完成IRPpIrp->IoStatus.Status = status;pIrp->IoStatus.Information = 0;// bytes xferedIoCompleteRequest( pIrp, IO_NO_INCREMENT );KdPrint(("DriverA:Leave A HelloDDKDispatchRoutine\n"));return status;}#pragma PAGEDCODENTSTATUS HelloDDKCreate(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp) {KdPrint(("DriverA:Enter A HelloDDKCreate\n"));NTSTATUS status = STATUS_SUCCESS;// 完成IRPpIrp->IoStatus.Status = status;pIrp->IoStatus.Information = 0;// bytes xferedIoCompleteRequest( pIrp, IO_NO_INCREMENT );KdPrint(("DriverA:Leave A HelloDDKCreate\n"));return status;}#pragma PAGEDCODENTSTATUS HelloDDKClose(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp) {KdPrint(("DriverA:Enter A HelloDDKClose\n"));NTSTATUS status = STATUS_SUCCESS;// 完成IRPpIrp->IoStatus.Status = status;pIrp->IoStatus.Information = 0;// bytes xferedIoCompleteRequest( pIrp, IO_NO_INCREMENT );KdPrint(("DriverA:Leave A HelloDDKClose\n"));return status;}
/************************************************************************* 文件名称:Driver.h                                                 * 作    者:张帆* 完成日期:2007-11-1*************************************************************************/#pragma once#ifdef __cplusplusextern "C"{#endif#include <NTDDK.h>#ifdef __cplusplus}#endif #define PAGEDCODE code_seg("PAGE")#define LOCKEDCODE code_seg()#define INITCODE code_seg("INIT")#define PAGEDDATA data_seg("PAGE")#define LOCKEDDATA data_seg()#define INITDATA data_seg("INIT")#define arraysize(p) (sizeof(p)/sizeof((p)[0]))typedef struct _DEVICE_EXTENSION {PDEVICE_OBJECT pDevice;UNICODE_STRING ustrDeviceName;//设备名称PDEVICE_OBJECT TargetDevice;} DEVICE_EXTENSION, *PDEVICE_EXTENSION;// 函数声明NTSTATUS CreateDevice (IN PDRIVER_OBJECT pDriverObject);VOID HelloDDKUnload (IN PDRIVER_OBJECT pDriverObject);NTSTATUS HelloDDKDispatchRoutine(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp);NTSTATUS HelloDDKCreate(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp);NTSTATUS HelloDDKClose(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp) ;NTSTATUS HelloDDKRead(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp) ;
#include "Driver.h"/************************************************************************* 函数名称:DriverEntry* 功能描述:初始化驱动程序,定位和申请硬件资源,创建内核对象* 参数列表:      pDriverObject:从I/O管理器中传进来的驱动对象      pRegistryPath:驱动程序在注册表的中的路径* 返回 值:返回初始化驱动状态*************************************************************************/#pragma INITCODEextern "C" NTSTATUS DriverEntry (IN PDRIVER_OBJECT pDriverObject,IN PUNICODE_STRING pRegistryPath) {NTSTATUS ntStatus;KdPrint(("DriverB:Enter B DriverEntry\n"));//注册其他驱动调用函数入口pDriverObject->DriverUnload = HelloDDKUnload;pDriverObject->MajorFunction[IRP_MJ_CREATE] = HelloDDKCreate;pDriverObject->MajorFunction[IRP_MJ_CLOSE] = HelloDDKClose;pDriverObject->MajorFunction[IRP_MJ_WRITE] = HelloDDKDispatchRoutine;pDriverObject->MajorFunction[IRP_MJ_READ] = HelloDDKRead;UNICODE_STRING DeviceName;RtlInitUnicodeString( &DeviceName, L"\\Device\\MyDDKDeviceA" );PDEVICE_OBJECT DeviceObject = NULL;PFILE_OBJECT FileObject = NULL;//寻找DriverA创建的设备对象ntStatus = IoGetDeviceObjectPointer(&DeviceName,FILE_ALL_ACCESS,&FileObject,&DeviceObject);if (!NT_SUCCESS(ntStatus)){KdPrint(("DriverB:IoGetDeviceObjectPointer() 0x%x\n", ntStatus ));return ntStatus;}//创建自己的驱动设备对象ntStatus = CreateDevice(pDriverObject);if ( !NT_SUCCESS( ntStatus ) ){ObDereferenceObject( FileObject );DbgPrint( "IoCreateDevice() 0x%x!\n", ntStatus );return ntStatus;}PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) pDriverObject->DeviceObject->DeviceExtension;PDEVICE_OBJECT FilterDeviceObject = pdx->pDevice;//将自己的设备对象挂载在DriverA的设备对象上PDEVICE_OBJECT TargetDevice = IoAttachDeviceToDeviceStack( FilterDeviceObject,DeviceObject );//将底层设备对象记录下来pdx->TargetDevice = TargetDevice;if ( !TargetDevice ){ObDereferenceObject( FileObject );IoDeleteDevice( FilterDeviceObject );DbgPrint( "IoAttachDeviceToDeviceStack() 0x%x!\n", ntStatus );return STATUS_INSUFFICIENT_RESOURCES;}FilterDeviceObject->DeviceType = TargetDevice->DeviceType;FilterDeviceObject->Characteristics = TargetDevice->Characteristics;FilterDeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;FilterDeviceObject->Flags |= ( TargetDevice->Flags & ( DO_DIRECT_IO | DO_BUFFERED_IO ) );ObDereferenceObject( FileObject );KdPrint(("DriverB:B attached A successfully!\n"));KdPrint(("DriverB:Leave B DriverEntry\n"));return ntStatus;}/************************************************************************* 函数名称:CreateDevice* 功能描述:初始化设备对象* 参数列表:      pDriverObject:从I/O管理器中传进来的驱动对象* 返回 值:返回初始化状态*************************************************************************/#pragma INITCODENTSTATUS CreateDevice (IN PDRIVER_OBJECTpDriverObject) {NTSTATUS ntStatus;PDEVICE_OBJECT pDevObj;PDEVICE_EXTENSION pDevExt;//创建设备名称UNICODE_STRING devName;RtlInitUnicodeString(&devName,L"\\Device\\MyDDKDeviceB");//创建设备ntStatus = IoCreateDevice( pDriverObject,sizeof(DEVICE_EXTENSION),&(UNICODE_STRING)devName,FILE_DEVICE_UNKNOWN,0, TRUE,&pDevObj );if (!NT_SUCCESS(ntStatus))return ntStatus;pDevObj->Flags |= DO_BUFFERED_IO;pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension;pDevExt->pDevice = pDevObj;pDevExt->ustrDeviceName = devName;return STATUS_SUCCESS;}/************************************************************************* 函数名称:HelloDDKUnload* 功能描述:负责驱动程序的卸载操作* 参数列表:      pDriverObject:驱动对象* 返回 值:返回状态*************************************************************************/#pragma PAGEDCODEVOID HelloDDKUnload (IN PDRIVER_OBJECT pDriverObject) {PDEVICE_OBJECTpNextObj;KdPrint(("DriverB:Enter B DriverUnload\n"));//pNextObj = pDriverObject->DeviceObject;//while (pNextObj != NULL) //{//PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)//pNextObj->DeviceExtension;//pNextObj = pNextObj->NextDevice;////从设备栈中弹出//IoDetachDevice( pDevExt->TargetDevice);////删除该设备对象//IoDeleteDevice( pDevExt->pDevice );//}//KdPrint(("DriverB:Enter B DriverUnload\n"));pNextObj = pDriverObject->DeviceObject;//我的第一个设备                                              IoDeleteDevice(pDriverObject->DeviceObject);//删除设备      KdPrint(("DriverB:Enter B DriverUnload\n"));}/************************************************************************* 函数名称:HelloDDKDispatchRoutine* 功能描述:对读IRP进行处理* 参数列表:      pDevObj:功能设备对象      pIrp:从IO请求包* 返回 值:返回状态*************************************************************************/#pragma PAGEDCODENTSTATUS HelloDDKDispatchRoutine(IN PDEVICE_OBJECT pDevObj,IN PIRP pIrp) {KdPrint(("DriverB:Enter B HelloDDKDispatchRoutine\n"));NTSTATUS ntStatus = STATUS_SUCCESS;// 完成IRPpIrp->IoStatus.Status = ntStatus;pIrp->IoStatus.Information = 0;// bytes xferedIoCompleteRequest( pIrp, IO_NO_INCREMENT );KdPrint(("DriverB:Leave B HelloDDKDispatchRoutine\n"));return ntStatus;}#pragma PAGEDCODENTSTATUS HelloDDKCreate(IN PDEVICE_OBJECT pDevObj,IN PIRP pIrp) {KdPrint(("DriverB:Enter B HelloDDKCreate\n"));NTSTATUS ntStatus = STATUS_SUCCESS;//// // 完成IRP// pIrp->IoStatus.Status = ntStatus;// pIrp->IoStatus.Information = 0;// bytes xfered// IoCompleteRequest( pIrp, IO_NO_INCREMENT );PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION)pDevObj->DeviceExtension;    IoSkipCurrentIrpStackLocation (pIrp);    ntStatus = IoCallDriver(pdx->TargetDevice, pIrp);KdPrint(("DriverB:Leave B HelloDDKCreate\n"));return ntStatus;}NTSTATUS MyIoCompletion(IN PDEVICE_OBJECT  DeviceObject,IN PIRP  Irp,IN PVOID  Context  ){//进入此函数标志底层驱动设备将IRP完成//KdPrint(("Enter MyIoCompletion\n"));KdPrint(("进入了我的完成历程"));    if (Irp->PendingReturned) {//传播pending位        IoMarkIrpPending( Irp );    }return STATUS_SUCCESS;//同STATUS_CONTINUE_COMPLETION}#pragma PAGEDCODENTSTATUS HelloDDKRead(IN PDEVICE_OBJECT pDevObj,IN PIRP pIrp) {KdPrint(("DriverB:Enter B HelloDDKRead\n"));NTSTATUS ntStatus = STATUS_SUCCESS;//将自己完成IRP,改成由底层驱动负责PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION)pDevObj->DeviceExtension;//将当前IRP堆栈拷贝底层堆栈IoCopyCurrentIrpStackLocationToNext(pIrp);//设置完成例程IoSetCompletionRoutine(pIrp,MyIoCompletion,NULL,TRUE,TRUE,TRUE);//调用底层驱动    ntStatus = IoCallDriver(pdx->TargetDevice, pIrp);//当IoCallDriver后,并且完成例程返回的是STATUS_SUCCESS//IRP就不在属于派遣函数了,就不能对IRP进行操作了if (ntStatus == STATUS_PENDING){KdPrint(("STATUS_PENDING\n"));}ntStatus = STATUS_PENDING; KdPrint(("DriverB:Leave B HelloDDKRead\n"));return ntStatus;}#pragma PAGEDCODENTSTATUS HelloDDKClose(IN PDEVICE_OBJECT pDevObj,IN PIRP pIrp) {KdPrint(("DriverB:Enter B HelloDDKClose\n"));NTSTATUS ntStatus = STATUS_SUCCESS;PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION)pDevObj->DeviceExtension;    IoSkipCurrentIrpStackLocation (pIrp);    ntStatus = IoCallDriver(pdx->TargetDevice, pIrp);KdPrint(("DriverB:Leave B HelloDDKClose\n"));return ntStatus;}




原创粉丝点击