4

I'm working in a small tool to log activity in USB devices. My tool works as a windows service catching all the device events, basically I'm starting to monitor the USB device as soon I catch a DBT_DEVICEARRIVAL event. After this, I need to stop the monitor as soon I get the DBT_DEVICEQUERYREMOVE (otherwise my service will deny the device to be safe ejected). The problem is that the tool should be able to monitor several number of devices, so I need to be able to determine which device is the user trying to eject. I found out that the DBT_DEVICEQUERYREMOVE event carries a DEV_BROADCAST_HANDLE structure. I'm trying to extract some useful information from this structure that can allow me to identify which device is being ejected. I found out that there is a handle to the device, I tried to extract the drive letter using the system function GetFinalPathNameByHandle but is not working properly (returning empty value). Any idea how can I do this?

Thank you very much!

Ian R. O'Brien
  • 6,682
  • 9
  • 45
  • 73

1 Answers1

1

Since the answer seems to be a bit less obvious than I thought: Call RegisterDeviceNotification for each device you're interested in, identifying the device by its handle. Since YOU create the registration, you'll know which drive letter maps to which notification handle.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • I guess use that function via [pinvoke](http://pinvoke.net/default.aspx/user32/RegisterDeviceNotification.html)? – default Dec 14 '12 at 14:39
  • @Default: There's no other way, I think. The native API of Windows is still C-based. – MSalters Dec 14 '12 at 14:42