DRIVER_CANCEL callback function (wdm.h)

The Cancel routine cancels an I/O operation.



void DriverCancel(
  _DEVICE_OBJECT *DeviceObject,
  _IRP *Irp



[in, out] Caller-supplied pointer to a DEVICE_OBJECT structure. This is the device object for the target device, previously created by the driver's AddDevice routine.


[in, out] Caller-supplied pointer to an IRP structure that describes the I/O operation to be canceled.

Return value



When a driver or other system component calls IoCancelIrp, the I/O manager calls the IRP's Cancel routine, if one has been registered for the IRP.

To register a Cancel routine for an IRP, a driver can use either of the following two methods:

  1. A driver that provides a StartIo routine and uses the I/O manager-supplied device queue can specify a Cancel routine when calling IoStartPacket.
  2. A driver that creates and manages supplemental device queues can register a Cancel routine by calling IoSetCancelRoutine.
Only one Cancel routine can be registered for an IRP at one time.

The I/O manager calls IoAcquireCancelSpinLock before calling a driver's Cancel routine, so the Cancel routine must call IoReleaseCancelSpinLock at some point. The routine should not hold the spin lock longer than necessary.

The Cancel routine executes in an arbitrary thread context at IRQL = DISPATCH_LEVEL until it calls IoReleaseCancelSpinLock, which changes the IRQL to a caller-supplied value. The driver should specify Irp->CancelIrql for this value.

The Cancel routine must set the I/O status block's Status member to STATUS_CANCELLED, and set its Information member to zero. The routine must then complete the specified IRP by calling IoCompleteRequest.

For detailed information about implementing a driver's Cancel routine, see Canceling IRPs.


To define a Cancel callback routine, you must first provide a function declaration that identifies the type of callback routine you're defining. Windows provides a set of callback function types for drivers. Declaring a function using the callback function types helps Code Analysis for Drivers, Static Driver Verifier (SDV), and other verification tools find errors, and it's a requirement for writing drivers for the Windows operating system.

For example, to define a Cancel callback routine that is named MyCancel, use the DRIVER_CANCEL type as shown in this code example:


Then, implement your callback routine as follows:

    struct _DEVICE_OBJECT  *DeviceObject,
    struct _IRP  *Irp
      // Function body

The DRIVER_CANCEL function type is defined in the Wdm.h header file. To more accurately identify errors when you run the code analysis tools, be sure to add the Use_decl_annotations annotation to your function definition. The Use_decl_annotations annotation ensures that the annotations that are applied to the DRIVER_CANCEL function type in the header file are used. For more information about the requirements for function declarations, see Declaring Functions by Using Function Role Types for WDM Drivers. For information about Use_decl_annotations, see Annotating Function Behavior.


Target Platform Desktop
Header wdm.h (include Wdm.h, Ntddk.h, Ntifs.h)
IRQL Called at DISPATCH_LEVEL (see Remarks section).