0

I'm trying to make an application that can disable/enable USB ports in some conditions. I did my research and found all functions that I need. In the enable/disable function I must send the ClassGuid variable as a parameter, but I can't get the ClassGuid variable when I was trying to get device information variables. I can't change that enable/disable function.

Here is my function that gives me device information variables:

static List<ManagementBaseObject> GetDevices()
        {
            List<ManagementBaseObject> devices = new List<ManagementBaseObject>();
            ManagementObjectCollection collection;
            using (var searcher = new ManagementObjectSearcher( "SELECT * FROM Win32_PnPEntity"))
                collection = searcher.Get();
            foreach (var device in collection)
            {
                devices.Add(device);
            }
            collection.Dispose();
            return devices;
        }

I did try to change the searching query, but I couldn't get the ClassGuid.

Here is what I get when I try to get device information variables:

\username\root\cimv2:Win32_PnPEntity.DeviceID="USB\VID_413C&PID_2111&MI_00\6&245229CD&0&0000

How can I fix this problem. If there is no problem how can I get the ClassGuid of a device?

  • The `Win32_PnPEntity` class also provides a `ClassGuid` property. Assuming that's what you're looking for (it may be not). – Jimi Feb 13 '21 at 19:17
  • No, it does not provide the `ClassGuid` property. Maybe I'm doing something wrong but I don't know which part is wrong. – Abdulkadir Can Kinsiz Feb 15 '21 at 08:38
  • [Win32_PnPEntity](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-pnpentity) class, third property. Or do you mean that `ClassGuid` is an empty string? Or it's not what you're looking for. Btw [here's an example](https://stackoverflow.com/a/51806262/7444103) of the *right way* to query a WMI class. – Jimi Feb 15 '21 at 08:43
  • I think I'm missing something. I tried everything but I'm still getting only DeviceID like this:`\username\root\cimv2:Win32_PnPEntity.DeviceID="USB\VID_413C&PID_2111&MI_00\6&245229CD&0&0000`. – Abdulkadir Can Kinsiz Feb 15 '21 at 13:12
  • `foreach (var moObj in searcher.Get()) { string classGuid = moObj.GetPropertyValue("ClassGuid")?.ToString(); string deviceId = moObj.GetPropertyValue("DeviceId")?.ToString(); }` etc. See al the properties available in the `Win32_PnPEntity` class. – Jimi Feb 15 '21 at 18:56
  • I fixed it. I just figured out when I try get `ClassGuid`, it can be null sometimes. I filtered and fixed it. You changed my my thoughts with that code. Thank you! – Abdulkadir Can Kinsiz Feb 15 '21 at 22:49

1 Answers1

0

I am working on a similar problem and I found that the ClassGuid is accessable under the Qualifiers with the name UUID. Here is the code that I used to expose all of the accessible items:

            DeviceIndex = 0;
            ManagementObjectSearcher device_searcher = 
                new ManagementObjectSearcher("SELECT * FROM Win32_USBHub");
            ManagementObjectCollection collection = device_searcher.Get();
            foreach(ManagementObject usb_device in collection)
            {
                var prop = usb_device.Properties;
                Debug.Print(Environment.NewLine);
                foreach (PropertyData data in prop)
                {
                    Debug.Print($"{data.Name} = {data.Value}");
                }

                var qual = usb_device.Qualifiers;
                Debug.Print(Environment.NewLine);
                foreach (QualifierData data in qual)
                {
                    Debug.Print($"{data.Name} = {data.Value}");
                }
            }
Aaron
  • 1