问题描述
- C++驱动开发的小问题,求大家指点,算是初学者的问题,我弄好久了。。
-
#include "Driver.h"
#pragma code_seg("INIT")
extern "C"
NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObj,PUNICODE_STRING pustrRegisterPath)
{
KdPrint(("Enter DriverEntryn"));
NTSTATUS status = STATUS_SUCCESS;pDriverObj->DriverUnload = UnloadDR; pDriverObj->MajorFunction[IRP_MJ_CREATE] = CreateDR; pDriverObj->MajorFunction[IRP_MJ_READ] = ReadDR; PDEVICE_OBJECT pDeviceObj; UNICODE_STRING ustrDeviceName; RtlInitUnicodeString(&ustrDeviceName,L"\Device\Timeout"); IoCreateDevice(pDriverObj,sizeof(DEVICE_EXTENSION),&ustrDeviceName,FILE_DEVICE_UNKNOWN, 0,FALSE,&pDeviceObj); UNICODE_STRING ustrSymbLinkName; RtlInitUnicodeString(&ustrSymbLinkName,L"\DosDevices\Timeout"); status = IoCreateSymbolicLink(&ustrSymbLinkName,&ustrDeviceName); if(!NT_SUCCESS(status)) { KdPrint(("Error: IoCreateSymbolicLinkn")); IoDeleteSymbolicLink(&ustrSymbLinkName); IoDeleteDevice(pDeviceObj); return status; } PDEVICE_EXTENSION pDeviceExt = (PDEVICE_EXTENSION)pDeviceObj->DeviceExtension; pDeviceExt->pDeviceObj = pDeviceObj; pDeviceExt->ustrDeviceName = ustrDeviceName; pDeviceExt->ustrSymbLinkName = ustrSymbLinkName; KdPrint(("Leave DriverEntryn")); return status;
}
#pragma code_seg("PAGED")
VOID UnloadDR(PDRIVER_OBJECT pDriverObj)
{
KdPrint(("Enter UnloadDRn"));
PDEVICE_EXTENSION pDeviceExt = (PDEVICE_EXTENSION)pDriverObj->DeviceObject->DeviceExtension;
IoDeleteSymbolicLink(&pDeviceExt->ustrSymbLinkName);
IoDeleteDevice(pDeviceExt->pDeviceObj);
KdPrint(("Leave UnloadDRn"));
}#pragma code_seg("PAGED")
NTSTATUS CreateDR(PDEVICE_OBJECT pDeviceObj,PIRP pIrp)
{
KdPrint(("Enter CreateDRn"));
NTSTATUS status = STATUS_SUCCESS;PDEVICE_EXTENSION pDeviceExt = (PDEVICE_EXTENSION)pDeviceObj->DeviceExtension; KeInitializeDpc(&pDeviceExt->kDpc,OnDpc,(PVOID)pDeviceObj); KeInitializeTimer(&pDeviceExt->kTimer); pIrp->IoStatus.Information = 0; pIrp->IoStatus.Status = STATUS_SUCCESS; IoCompleteRequest(pIrp,IO_NO_INCREMENT); KdPrint(("Leave CreateDRn")); return status;
}
#pragma code_seg("PAGED")
NTSTATUS ReadDR(PDEVICE_OBJECT pDeviceObj,PIRP pIrp)
{
KdPrint(("Enter ReadDRn"));
NTSTATUS status = STATUS_SUCCESS;PDEVICE_EXTENSION pDeviceExt = (PDEVICE_EXTENSION)pDeviceObj->DeviceExtension; IoMarkIrpPending(pIrp); pDeviceExt->pPendingIrp = pIrp; ULONG ulInterval = 3000000; pDeviceExt->liInterval = RtlConvertLongToLargeInteger(-10*ulInterval); KeSetTimer(&pDeviceExt->kTimer,pDeviceExt->liInterval,&pDeviceExt->kDpc); status = STATUS_PENDING; KdPrint(("Leave ReadDRn")); return status;
}
#pragma code_seg()
VOID OnDpc(
__in struct _KDPC *Dpc,
__in_opt PVOID DeferredContext,
__in_opt PVOID SystemArgument1,
__in_opt PVOID SystemArgument2
)
{
KdPrint(("Enter OnDpcn"));
PDEVICE_OBJECT pDeviceObj = (PDEVICE_OBJECT)DeferredContext;
PDEVICE_EXTENSION pDeviceExt = (PDEVICE_EXTENSION)pDeviceObj;
PIRP pIrp = pDeviceExt->pPendingIrp;
KdPrint(("timeoutn"));
pIrp->IoStatus.Information = 0;
pIrp->IoStatus.Status = STATUS_CANCELLED;
IoCompleteRequest(pIrp,IO_NO_INCREMENT);KdPrint(("Leave OnDpcn"));
/* _asm
{
int 3;
}
*/
}上面是我写的简单的代码,就是读这个驱动程序的时候,它会先挂起这个读IRP,当超时时结束这个IRP.
但是这儿我遇到个问题,一旦我在DPC里调用IOCompleteRequest之后就出错,但是不调用这个又不行,这样调用程序始终处于等待状态。
求指点,万分感谢。