9

I need to know what kind of USB devices currently used in system. There is a USB specification about class codes of USB devices. But I cant get device type, WMI request WQL: select * from Win32_UsbHub give null values on Class code, Subclass code, Protocol type fields. Any ideas how to detect USB device type currently in use?

My current code:

ManagementObjectCollection collection; 
using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub")) 
{
    collection = searcher.Get();
    foreach (var device in collection)
        {
            var deviceId = (string)GetPropertyValue("DeviceID");
            var pnpDeviceId = (string)GetPropertyValue("PNPDeviceID");
            var descr = (string)device.GetPropertyValue("Description");
            var classCode = device.GetPropertyValue("ClassCode"); //null here
        }
}
MelnikovI
  • 1,005
  • 1
  • 10
  • 24
  • Can you post the full example including `ManagementScope`, `ObjectQuery` and `ManagementObjectSearcher` usage? – SwDevMan81 Aug 15 '13 at 12:27
  • ManagementObjectCollection collection; using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub")) collection = searcher.Get(); – MelnikovI Aug 15 '13 at 12:31
  • wbemtest.exe tool give same effect: `Class cod, Subclass code, Protocol type` fields are null – MelnikovI Aug 15 '13 at 12:39
  • 1
    As described in the USB Specification you linked, your going to have to dig down into the [device descriptor](http://www.beyondlogic.org/usbnutshell/usb5.shtml) (Device Class) or interface descriptor (Interface Class) to retrieve that information. You might not be able to do that with WMI alone. – SwDevMan81 Aug 15 '13 at 13:34
  • What type of device are you hunting for? If you are looking for a particular device type (like Virtual Com Ports) you may be better off looking at the wmi queries which specifically go after those items. – Rich Dominelli Aug 15 '13 at 20:14
  • @SwDevMan81 How can i retrive device descriptor (Device Class) or interface descriptor (Interface Class) in C#? – MelnikovI Aug 16 '13 at 06:31
  • @MelnikovI - I've posted a solution to retrieve the information. Let me know if you have any questions or issues with it. – SwDevMan81 Aug 16 '13 at 13:22

1 Answers1

5

You can download USB View Source as a starting point. This loops through all USB devices on a PC (C#) and pulls information about each. To get the Class code, Subclass code, and Protocol type fields, you'll need to modify it slightly. Change the below and run it and you'll get the information on each USB device by clicking on the item in the tree view (information will appear in the right panel).

Modifications to USB.cs:

// Add the following properties to the USBDevice class
// Leave everything else as is
public byte DeviceClass
{
   get { return DeviceDescriptor.bDeviceClass; }
}

public byte DeviceSubClass
{
   get { return DeviceDescriptor.bDeviceSubClass; }
}

public byte DeviceProtocol
{
   get { return DeviceDescriptor.bDeviceProtocol; }
}

Modifications to fmMain.cs

// Add the following lines inside the ProcessHub function
// inside the "if (port.IsDeviceConnected)" statement
// Leave everything else as is
if (port.IsDeviceConnected)
{
   // ...
   sb.AppendLine("SerialNumber=" + device.SerialNumber);
   // Add these three lines
   sb.AppendLine("DeviceClass=0x" + device.DeviceClass.ToString("X"));
   sb.AppendLine("DeviceSubClass=0x" + device.DeviceSubClass.ToString("X"));
   sb.AppendLine("DeviceProtocol=0x" + device.DeviceProtocol.ToString("X"));
   // ...
}
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184