3

I enumerate all the devices on a machine as follows:

HDEVINFO hDevList = SetupDiGetClassDevs( NULL, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT | DIGCF_ALLCLASSES /*| DIGCF_PROFILE*/ );
for ( int i = 0; true; ++i )
{
    SP_DEVINFO_DATA devinfo_data = {0};
    devinfo_data.cbSize = sizeof(SP_DEVINFO_DATA);
    if ( !SetupDiEnumDeviceInfo( hDevList, i, &devinfo_data ) )
        break;
    //TODO: get device interface GUID
    //...
}
SetupDiDestroyDeviceInfoList( hDevList );

How do I get device interface GUID for every item?

I tried

SP_DEVICE_INTERFACE_DATA interface_data = {0};
interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
SetupDiEnumDeviceInterfaces( hDevList, &devinfo_data, ???, 0, &interface_data );

but it seems to require actual GUID for the third parameter. Which is actually what I want to get! NULL is not an option there.

Roman Khvostikov
  • 305
  • 3
  • 14
  • I had the same question, but was only interested in usb devices. The is a guid fior all USB device that are not hubs or controllers. There is a define in a ddk header someplace, but you will also see it used a lot in examples for the 2 setupapi functions you are using. If I find the guid or the header I will try to update my answer. But I think you will find it first. I hope someone knows how to get the info on every device, I wanted to do this just for fum. But I really only needed USB fot some firmware debugging. – Dennis Dec 15 '18 at 21:24
  • I had the same question, but was only interested in usb devices. The is a guid fior all USB device that are not hubs or controllers. There is a define in a ddk header someplace, but you will also see it used a lot in examples for the 2 setupapi functions you are using. If I find the guid or the header I will try to update my answer. But I think you will find it first. I hope someone knows how to get the info on every device, I wanted to do this just for fum. But I really only needed USB fot some firmware debugging. Oh by the way that function does not return a DeviceInterfaceGUID or give you a – Dennis Dec 15 '18 at 21:28
  • I have same question. – Raymond Mar 29 '19 at 05:42
  • 2
    3rd parameter is a device interface *class* GUID, not a device interface GUID. You'll have to say what class of interfaces you're looking after. You cannot get all interfaces for all classes. There is no fixed list of classes, but Windows defines a lot already. For example GUID_DEVINTERFACE_USB_DEVICE will get you device interfaces for USB devices. https://learn.microsoft.com/en-us/windows-hardware/drivers/install/overview-of-device-interface-classes – Simon Mourier Mar 30 '19 at 08:26
  • 1
    No. At least we can get all `device interface class`. https://learn.microsoft.com/en-us/windows-hardware/drivers/install/hklm-system-currentcontrolset-control-registry-tree
    Yes, the words may misleading, and the API may misused. But the purpose is clear. **How to get all supported `device interface class` for a given device?** What's the API/methods? @SimonMourier
    – Raymond Mar 31 '19 at 08:04
  • @SimonMourier The 3rd parameter is **not** the _"Device Class GUID."_ It is also **not** correct to say _"device interface class GUID"_ as you had. There are two types of classes on Windows: _"Device Interface Classes"_ and _"Device Setup Classes"_. The 3rd parameter of `SetupDiEnumDeviceInterfaces (...)` must be the _"Device Interface GUID"_. It can be confusing sometimes, but when some says "class GUID" they are typically talkig about a _"Device Setup Class GUID"_. However, when someone explicitly says "interface GUID" they are speaking about _"Device Interface Class GUID."_ – Code Doggo Jan 14 '20 at 19:03
  • 2
    @CodeDoggo: It **is** correct to say "device interface class GUID"... as you did in your last sentence. – Ben Voigt Jan 14 '20 at 21:33
  • @BenVoigt Ah, you are right. I read that wrong. But, the 3rd parameter is still not the "class GUID," but rather the "interface class GUID." – Code Doggo Jan 14 '20 at 21:35
  • If people are still looking for the list of "device interface classess" I think `CM_Enumerate_Classes` with param 3 `CM_ENUMERATE_CLASSES_INTERFACE` is probably what you're looking for: https://learn.microsoft.com/en-us/windows/win32/api/cfgmgr32/nf-cfgmgr32-cm_enumerate_classes – John Stringer Sep 27 '22 at 12:35

1 Answers1

-2

You can get a lot of information using the Unified Device Property Model available in Vista and higher. It has the DEVPKEY_Device_Class and DEVPKEY_Device_ClassGuid properties:

HDEVINFO list = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT | DIGCF_ALLCLASSES /*| DIGCF_PROFILE*/);
for (int i = 0; true; ++i)
{
  SP_DEVINFO_DATA data = { 0 };
  data.cbSize = sizeof(SP_DEVINFO_DATA);
  if (!SetupDiEnumDeviceInfo(list, i, &data))
    break;

  // get name property
  DEVPROPTYPE type;
  DWORD size = 0;
  SetupDiGetDeviceProperty(list, &data, &DEVPKEY_NAME, &type, NULL, 0, &size, 0);
  if (size > 0)
  {
    LPWSTR name = (LPWSTR)malloc(size);
    SetupDiGetDeviceProperty(list, &data, &DEVPKEY_NAME, &type, (PBYTE)name, size, &size, 0);
    wprintf(L"name: %s\n", name);
    free(name);
  }

  // get class name
  SetupDiGetDeviceProperty(list, &data, &DEVPKEY_Device_Class, &type, NULL, 0, &size, 0);
  if (size > 0)
  {
    LPWSTR name = (LPWSTR)malloc(size);
    SetupDiGetDeviceProperty(list, &data, &DEVPKEY_Device_Class, &type, (PBYTE)name, size, &size, 0);
    wprintf(L" class: %s\n", name);
    free(name);
  }

  // get class guid
  SetupDiGetDeviceProperty(list, &data, &DEVPKEY_Device_ClassGuid, &type, NULL, 0, &size, 0);
  if (size > 0)
  {
    GUID* guid = (GUID*)malloc(size);
    SetupDiGetDeviceProperty(list, &data, &DEVPKEY_Device_ClassGuid, &type, (PBYTE)guid, size, &size, 0);
    wchar_t name[64];
    StringFromGUID2(*guid, (LPOLESTR)name, ARRAYSIZE(name));
    wprintf(L" class guid: %s\n", name);
    free(guid);
  }
}
SetupDiDestroyDeviceInfoList(list);

This will output something like this:

name: ACPI Fan                                      
 class: System                                      
 class guid: {4D36E97D-E325-11CE-BFC1-08002BE10318} 
name: ACPI Fan                                      
 class: System                                      
 class guid: {4D36E97D-E325-11CE-BFC1-08002BE10318} 
name: ACPI Fan                                      
 class: System                                      
 class guid: {4D36E97D-E325-11CE-BFC1-08002BE10318} 
name: ACPI Fan                                      
 class: System                                      
 class guid: {4D36E97D-E325-11CE-BFC1-08002BE10318} 
name: ACPI Fan                                      
 class: System                                      
 class guid: {4D36E97D-E325-11CE-BFC1-08002BE10318} 
name: Microsoft Hyper-V Virtual Machine Bus Provider
 class: System                                      
 class guid: {4D36E97D-E325-11CE-BFC1-08002BE10318} 
name: Plug and Play Software Device Enumerator      
 class: System                                      
 class guid: {4D36E97D-E325-11CE-BFC1-08002BE10318} 
etc...
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • 1
    The OP was asking for the _"Device Interface GUID"_, **not** the _"Device Class GUID"_. There are subtle differences between the two, but they are in fact different. There does exist `DEVPKEY_DeviceInterface_ClassGuid`, but I have been facing the same issue as OP. All the interface functions (e.g. `SetupDiGetDeviceInterfaceProperty`) require one to specify to _"Device Interface GUID"_ up front. – Code Doggo Jan 14 '20 at 18:25
  • 1
    @CodeDoggo - OP didn't deign to comment/downvote/upvote. His question's stil unclear to me and demonstrates lack of knowledge whatsoever. He was explicitly referring to the "InterfaceClassGuid" parameter of SetupDiEnumDeviceInterfaces method. I clearly said in the comment "There is no fixed list of classes, but Windows defines a lot already. For example GUID_DEVINTERFACE_USB_DEVICE will get you device interfaces for USB devices." I provided an answer that indeed doesn't allow to enumerate Device *Interface* Class Guids, but was expecting some sort of discussion/follow up... – Simon Mourier Jan 17 '20 at 16:06