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
        }
}
7
  • Can you post the full example including ManagementScope, ObjectQuery and ManagementObjectSearcher usage?
    – SwDevMan81
    Aug 15, 2013 at 12:27
  • ManagementObjectCollection collection; using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub")) collection = searcher.Get();
    – MelnikovI
    Aug 15, 2013 at 12:31
  • wbemtest.exe tool give same effect: Class cod, Subclass code, Protocol type fields are null
    – MelnikovI
    Aug 15, 2013 at 12:39
  • 1
    As described in the USB Specification you linked, your going to have to dig down into the device descriptor (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, 2013 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. Aug 15, 2013 at 20:14

1 Answer 1

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"));
   // ...
}
1
  • @PeterJ - Looks like the files were restructured, I've updated the link.
    – SwDevMan81
    Mar 5, 2015 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.