蓝屏错误:DRIVER_RETURNED_HOLDING_CANCEL_LOCK

时间:2013-05-13 06:26:40

标签: windows driver irp

我在Windows 7中编写了一个驱动程序。我使用pedding IRP从驱动程序向应用程序发送事件。关键代码如:

PIRP PeddingIRP;

...

VOID PtDriverCancelIRP(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp ) 
{ 
    UNREFERENCED_PARAMETER(DeviceObject);

    KdPrint(( "[WENZ] User Message Cancel Irp....\n" ));

    if ( Irp == PeddingIRP)
        PeddingIRP = NULL; 

    Irp->IoStatus.Status = STATUS_CANCELLED; 
    Irp->IoStatus.Information = 0; 
    IoCompleteRequest(Irp,IO_NO_INCREMENT); 
}

...

NTSTATUS
DeviceControl( PDEVICE_OBJECT DeviceObject, PIRP Irp )
{
    ...
    switch ( irpSp->Parameters.DeviceIoControl.IoControlCode )
    {
        ...
    case IOCTL_NOTIFY_STATE:
    Irp->IoStatus.Information = 0;
    Irp->IoStatus.Status = STATUS_PENDING;
    IoMarkIrpPending(Irp); 
    PeddingIRP = Irp;
        IoSetCancelRoutine(Irp, PtDriverCancelIRP);

    return STATUS_PENDING;
        ...
    }
    ...
}

当某些事件通知应用程序时,它可以正常工作。但是当我卸载驱动程序时,会出现蓝屏错误:驱动程序已从持有全局取消锁定的取消调用返回。错误代码ID为0x011B。我使用windbg跟踪它,并且跟踪发生在“PtDriverCancelIRP”函数中。

给一个锁定相关的错误?我不知道为什么?我是如何解决的?

P.S。此驱动程序在Windows XP中没有错误

1 个答案:

答案 0 :(得分:1)

取消例程负责释放取消螺旋锁。请使用IoReleaseCancelSpinLock

VOID PtDriverCancelIRP(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp ) 
{ 
    UNREFERENCED_PARAMETER(DeviceObject);

    KdPrint(( "[WENZ] User Message Cancel Irp....\n" ));

    if ( Irp == PeddingIRP)
        PeddingIRP = NULL; 

    IoReleaseCancelSpinLock(); // release the cancel spinlock

    Irp->IoStatus.Status = STATUS_CANCELLED; 
    Irp->IoStatus.Information = 0; 
    IoCompleteRequest(Irp,IO_NO_INCREMENT); 
}