2 robot241 robot241 于 2014.01.29 18:18 提问

C++驱动开发的小问题,求大家指点,算是初学者的问题,我弄好久了。。
#include "Driver.h"

#pragma code_seg("INIT")
extern "C"
NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObj,PUNICODE_STRING pustrRegisterPath)
{
KdPrint(("Enter DriverEntry\n"));
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: IoCreateSymbolicLink\n"));
    IoDeleteSymbolicLink(&ustrSymbLinkName);
    IoDeleteDevice(pDeviceObj);
    return status;
}
PDEVICE_EXTENSION pDeviceExt = (PDEVICE_EXTENSION)pDeviceObj->DeviceExtension;
pDeviceExt->pDeviceObj = pDeviceObj;
pDeviceExt->ustrDeviceName = ustrDeviceName;
pDeviceExt->ustrSymbLinkName = ustrSymbLinkName;

KdPrint(("Leave DriverEntry\n"));
return status;

}

#pragma code_seg("PAGED")
VOID UnloadDR(PDRIVER_OBJECT pDriverObj)
{
KdPrint(("Enter UnloadDR\n"));
PDEVICE_EXTENSION pDeviceExt = (PDEVICE_EXTENSION)pDriverObj->DeviceObject->DeviceExtension;
IoDeleteSymbolicLink(&pDeviceExt->ustrSymbLinkName);
IoDeleteDevice(pDeviceExt->pDeviceObj);
KdPrint(("Leave UnloadDR\n"));
}

#pragma code_seg("PAGED")
NTSTATUS CreateDR(PDEVICE_OBJECT pDeviceObj,PIRP pIrp)
{
KdPrint(("Enter CreateDR\n"));
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 CreateDR\n"));
return status;

}

#pragma code_seg("PAGED")
NTSTATUS ReadDR(PDEVICE_OBJECT pDeviceObj,PIRP pIrp)
{
KdPrint(("Enter ReadDR\n"));
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 ReadDR\n"));
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 OnDpc\n"));
PDEVICE_OBJECT pDeviceObj = (PDEVICE_OBJECT)DeferredContext;
PDEVICE_EXTENSION pDeviceExt = (PDEVICE_EXTENSION)pDeviceObj;
PIRP pIrp = pDeviceExt->pPendingIrp;
KdPrint(("timeout\n"));
pIrp->IoStatus.Information = 0;
pIrp->IoStatus.Status = STATUS_CANCELLED;
IoCompleteRequest(pIrp,IO_NO_INCREMENT);

KdPrint(("Leave OnDpc\n"));

/* _asm
{
int 3;
}
*/
}

上面是我写的简单的代码,就是读这个驱动程序的时候,它会先挂起这个读IRP,当超时时结束这个IRP.
但是这儿我遇到个问题,一旦我在DPC里调用IOCompleteRequest之后就出错,但是不调用这个又不行,这样调用程序始终处于等待状态。
求指点,万分感谢。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!