In Microsoft Windows 2000, you can avoid the
Unsafe Removal dialog box. To do this, stop the device by selecting clicking
Unplug/Eject icon in the task bar notification area. Then, unplug the hardware. However, if you have a device that can be surprise-removed, this process is unnecessary. Instead, drivers for hot plug-in/unplug devices that do not maintain a persistent state should inform the operating systems that they can tolerate a surprise removal by setting the
SurpriseRemovalOK field in the
DeviceCapabilities structure to TRUE in response to the
IRP_MN_QUERY_CAPABILITIES Plug and Play IRP.
According to the Plug and Play rule, drivers should set the
SurpriseRemovalOK field in the
IRP_MN_QUERY_CAPABILITIES IRP before the drivers pass the IRP to the lower drivers in the device stack. Because the Windows 2000 USB hub driver incorrectly resets this field to FALSE, Windows 2000 drivers for USB devices must set the
SurpriseRemovalOK field on the way up. This behavior is demonstrated in the following code example. This code example is adapted for the toaster function driver sample in the DDK (Src\General\Toaster\Func\Toaster.c).
case IRP_MN_QUERY_CAPABILITIES: {
//
// Set the SurpiseremovalOK capability to true because this device
// can safely tolerate surprise remove, and we want to avoid the unsafe device
// removal UI
//
stack->Parameters.DeviceCapabilities.Capabilities->SurpriseRemovalOK = TRUE;
if (!IoIsWdmVersionAvailable(1, 0x20) /* & this is a for USB device */) {
//
// Reset the SurpriseRemovalOK capability back to TRUE when the IRP is on
// the way back up to work around a bug in Windows 2000 USB hub driver that
// incorrectly sets this capability to FALSE
//
// NOTE: IoIsWdmVersionAvailable(1, 0x20) returns TRUE on o/s after
// Windows 2000 & Windows Millennium Edition
//
//
// This function defined in toaster.c basically sends the Irp to the lower
// driver and waits for it complete.
//
status = ToasterSendIrpSynchronously(fdoData->NextLowerDriver, Irp);
if (NT_SUCCESS (status)) {
stack->Parameters.DeviceCapabilities.Capabilities->SurpriseRemovalOK=TRUE;
}
} else {
//
// WDM version is 2.0 or newer (or this is not a USB driver)
// so assume the bus driver will not erroneously reset the SurpiseRemovalOK
// capability and just pass the IRP down
//
IoSkipCurrentIrpStackLocation (Irp);
status = IoCallDriver (fdoData->NextLowerDriver, Irp);
ToasterIoDecrement(fdoData); //this function is defined in toaster.c
return status;
}
}
break;
Additionally, some devices that do not use persistent storage are not as safe to surprise remove. For example, you may surprise remove a PCMCIA card. Sometimes, hardware limitations may cause this removal to cause the PCI bus to fail. Therefore, the PCMCIA bus driver sets the
SurpiseRemovalOK field to FALSE for all PCMCIA cards.
NDIS miniport drivers cannot avoid the surprise removal dialog box by using the code example in this section because the following conditions are true:
- All the PnP IRPs are handled by the NDIS driver.
- Even if the bus driver shows that the device can tolerate surprise removal, NDIS always clears the SurpriseRemoval field.
Currently, there is no other documented interface to work around this problem.