Handling an IRP_MN_CANCEL_REMOVE_DEVICE Request
In response to an IRP_MN_CANCEL_REMOVE_DEVICE request, the drivers for a device must return the device to the state it was in prior to receiving the IRP_MN_QUERY_REMOVE_DEVICE request. Typically, drivers return the device to the started state.
In addition to sending an IRP_MN_CANCEL_REMOVE_DEVICE to a device, the PnP manager sends the IRP to the device's removal relations, if any. The PnP manager also sends a cancel-remove IRP to the device's children.
The PnP manager calls any EventCategoryTargetDeviceChange notification callbacks after the IRP_MN_CANCEL_REMOVE_DEVICE request completes. Such callbacks were registered on the device by calling IoRegisterPlugPlayNotification. The PnP manager also calls any user-mode components that registered for such notification by calling RegisterDeviceNotification.
An IRP_MN_CANCEL_REMOVE_DEVICE request must be handled first by the parent bus driver for a device and then by each higher driver in the device stack. A driver handles remove IRPs in its DispatchPnP routine.
A driver handles an IRP_MN_CANCEL_REMOVE_DEVICE request with a procedure such as the following in its DispatchPnP routine:
In a function or filter driver, postpone restarting the device until lower drivers have completed their restart operations.
A function or filter driver sets an IoCompletion routine, passes the IRP_MN_CANCEL_REMOVE_DEVICE down the device stack, and postpones its restart operations until all lower drivers have finished with the IRP. (See Postponing PnP IRP Processing Until Lower Drivers Finish.)
After lower drivers finish, return the device to its previous PnP state.
The drivers return the device to the state it was in prior to receiving the IRP_MN_QUERY_REMOVE_DEVICE request. Typically, drivers return the device to the started state. Exact operations depend on the device and the driver.
If the device was previously enabled for wake-up, the device power policy owner (typically the function driver) should send an IRP_MN_WAIT_WAKE request to reenable wake-up. See Power Management for details.
Set Irp->IoStatus.Status to STATUS_SUCCESS and complete the IRP with IoCompleteRequest.
As with any PnP IRP, a bus driver completes the IRP.
A function or filter driver also completes the IRP, in this case because the driver's IoCompletion routine interrupted completion processing by returning STATUS_MORE_PROCESSING_REQUIRED.
Drivers must succeed this IRP. If any driver fails this IRP, the device is left in an inconsistent state.
A driver might receive a spurious cancel-remove request when the device is started and active. This can occur, for example, if the driver (or a driver higher in the device stack) failed an IRP_MN_QUERY_REMOVE_DEVICE request. When a device is started and active, a driver simply succeeds a spurious cancel-remove request for the device.