Export (0) Print
Expand All

Cancel routine

The Cancel routine cancels an I/O operation.

Syntax


DRIVER_CANCEL Cancel;

VOID Cancel(
  _Inout_  struct _DEVICE_OBJECT *DeviceObject,
  _Inout_  struct _IRP *Irp
)
{ ... }

Parameters

DeviceObject [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.

Irp [in, out]

Caller-supplied pointer to an IRP structure that describes the I/O operation to be canceled.

Return value

None

Remarks

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.

Examples

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:


DRIVER_CANCEL MyCancel;

Then, implement your callback routine as follows:


_Use_decl_annotations_
VOID
  MyCancel(
    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.

Requirements

Header

Wdm.h (include Wdm.h, Ntddk.h, or Ntifs.h)

IRQL

Called at DISPATCH_LEVEL (see Remarks section).

 

 

Send comments about this topic to Microsoft

Show:
© 2014 Microsoft