title: How can I monitor USB device enumeration and removal in Windows?
body: <p>I would like to monitor the the USB bus of a computer running Windows for (un)plug events.  When a device is (un)plugged, I would like to know it's USB address, vendor id, the root hub id and the hub ports above the device.  For example
  usb_addr: 10, vendor_id: 1234, product_id: 5678, bus_id: 2, port_path: 5/4/3</p>
  
  <p>This would mean it is a device on bus id 2 port 5 and there is another hub plugged in and on port 4 of that hub there is another hub and on port 3 of that hub is the device being reported.</p>
  
  <p>I am trying to write a small OS-specific chunk of code to do USB device hotplug detection to integrate into a larger cross-platform (Linux, Windows, Mac) testing program.  The code that uses this hotplug code is written in Python 3, so I would prefer to write the hotplug code in Python as well, but I'm a little bit flexible on that, but I really want to avoid anything that requires me to pay for a license.</p>
  
  <p>It seems like there are two main options:</p>
  
  <p>I've seen some discussion of the Windows API function RegisterDeviceNotification, but it's not clear to me whether it is possible to register to receive events for all USB devices or whether you need to know a GUID for the specific device interface (eg. keyboards, mice, etc).  If that's the case, it seems that this solution won't work as I want to monitor all USB devices.</p>
  
  <p>The other option seems to involve something called "WMI" which seems to come up mostly in discussion of "managed" code which would to force me into writing this component in a .NET language such as C# or IronPython.  This question (<a href="https://stackoverflow.com/questions/3331043/get-list-of-connected-usb-devices">Get List of connected USB Devices</a>) asks about how to query for USB devices and provides an example, but it doesn't deal with hotplug events and user "Nedko" suggests that class <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa394505%28v=vs.85%29.aspx" rel="nofollow noreferrer">Win32_USBControllerDevice</a> should have been used rather than Win32_USBHub.  I don't really understand what the Win32_USBControllerDevice object actually models.  It seems like perhaps the Dependent member is a reference to a CIM_LogicalDevice object, but even that seems like it is missing the USB specific information that I am seeking.</p>
  
  <p>Which APIs should I be looking at to solve this problem?</p>