When should I disable my device interface & deregister with WMI?

Last week I talked about how a device interfaces and WMI instances are made unique by the kernel.  Both of these constructs rely on the device instance path being unique within the system.  So the question becomes, when is the device instance path no longer unique in the system?  The answer is, when two instances of the same device are plugged into the same port.

Huh?  How can that happen?  Note that I did not say two active instances of the same device, just two instances.  When you unplug a device from the system, the OS will send an IRP_MN_SURPRISE_REMOVAL (referred to as suprise removal from now on).  The device will recieve an IRP_MN_REMOVE_DEVICE only after all open handles against the device are closed.  This means that there is a window of time where the device stack is in a pseudo limbo state.  What happens in this limbo state if the user plugs in the device again into the same port?  Well, another device stack is built up and guess what…it uses the same device instance path as the first instance (which would only make sense, that is the only way the device can be identified and nothing has changed).

So getting back to device interfaces and WMI, you need to deregister them when handling surprise removal because otherwise, the new instance of the device will share the same paths.  The potential second instance of the same device stack can only be enumerated after the surprise removal PIRP has been completed back to the PnP manager, so it is safe to deregister during surprise removal.  To make life more complicated, if your device is gracefully removed (a query remove -> remove), you must detect this when handling the IRP_MN_REMOVE_DEVICE PIRP and deregister during this removal if you are not in the surprise removed state.  KMDF will do this for you automatically, a WDM driver needs to perform this manually.

In conclusion, while it is not necessary to understand the format that WMI and PnP uses to make insance strings, you need to understand how they are made unique. 

Comments (1)

  1. This is a side affect of not unregistering your device interface during surprise removal.  If you…