kishoreanand kishoreanand - 1 year ago 175
C# Question

How to detect two USB devices connected to same USB hub in c#

I am working on a .NET 4.5 C# project and I need to find out if my USB devices are connected to the same USB hub.

I googled and tried to explore structure of many ManagementObjectCollection like Win32_USBHub and Win32_PnPEntity but I could not find any WMI structure has location or port chain information.

I tried USB Device tree tool where I can see the information :

Device: 1
Location IDs : PCIROOT(0)#PCI(1A00)#USBROOT(0)#USB(1)#USB(3)#USB(1)

Device: 2
Location IDs : PCIROOT(0)#PCI(1A00)#USBROOT(0)#USB(1)#USB(3)#USB(2)

Now from the above information I want to write the logic to store and compare the two ID's to identify if both device are on the same USB hub.

I tried exploring the USB device tree source code from:

But still not able to run the code because of some problem in NativeUsbLib.

Can anyone please help me share some sample code how can I get this information from WMI or from setupapi.dll ?

Answer Source

Finally thanks to my dear friend Lucas for pointing me to following link:

Find USB drive letter from VID/PID (Needed for XP and higher)

This solved my problem. Instead of port map now I am using ParentPnpDeviceId using CM_Get_Parent, to detect if both USB devices are connected to the same USB hub. Thank you Simon for the brilliant solution.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download