why use SetupDiGetDeviceRegistryProperty get SPDRP_CLASSGUID always return the same id

哲敏 陈 86 Reputation points
2024-12-04T11:56:10.7966667+00:00

I use the following demo to simultaneously obtain the instanceId and guid of the device, but on my computer, all devices obtain the same SPDRP_CLASSGUID value. Why is this?

int main()
{
    HDEVINFO hDeviceInfoSet = SetupDiGetClassDevs(&GUID_DEVCLASS_MEDIA, NULL, NULL, DIGCF_PRESENT);
    if (hDeviceInfoSet == INVALID_HANDLE_VALUE)
    {
        printf("Failed to get device information set.\n");
        return 1;
    }

    SP_DEVINFO_DATA deviceInfoData;
    deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
    for (DWORD i = 0; SetupDiEnumDeviceInfo(hDeviceInfoSet, i, &deviceInfoData); i++)
    {
        WCHAR deviceId[MAX_DEVICE_ID_LEN];
        if (CM_Get_Device_ID(deviceInfoData.DevInst, deviceId, MAX_DEVICE_ID_LEN, 0) != CR_SUCCESS)
        {
            printf("Failed to get device ID.\n");
        }
        else
        {
            printf("Device Instance ID: %ws\n", deviceId);
        }

        WCHAR deviceGuidString[MAX_GUID_STRING_LEN];
        DWORD bufferSize = sizeof(deviceGuidString);
        if (SetupDiGetDeviceRegistryProperty(hDeviceInfoSet, &deviceInfoData, SPDRP_CLASSGUID, NULL, (PBYTE)deviceGuidString, bufferSize, &bufferSize))
        {
            printf("Device GUID: %ws\n", deviceGuidString);
        }
        else
        {
            printf("Failed to get device GUID.\n");
        }
    }

    SetupDiDestroyDeviceInfoList(hDeviceInfoSet);
    return 0;
}

the code result like this: code result

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,700 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 86,396 Reputation points
    2024-12-04T13:33:38.1933333+00:00

    If you enumerate them from GUID_DEVCLASS_MEDIA, it is normal that they have the GUID_DEVCLASS_MEDIA Class Guid (their parent)...


  2. Jeanine Zhang-MSFT 10,046 Reputation points Microsoft Vendor
    2024-12-05T02:37:10.27+00:00

    Hi

    Welcome to Microsoft Q&A!

    why use SetupDiGetDeviceRegistryProperty get SPDRP_CLASSGUID always return the same id

    SPDRP_CLASSGUID:contains the GUID that represents the device setup class of a device.

    According to the Doc:Overview of device setup classes

    There is a GUID associated with each device setup class. System-defined setup class GUIDs are defined in Devguid.h and typically have symbolic names of the form GUID_DEVCLASS_Xxx.

    According to your code, you are using GUID_DEVCLASS_MEDIA.

    DEFINE_GUID( GUID_DEVCLASS_MEDIA,               0x4d36e96cL, 0xe325, 0x11ce, 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 );
    

    Refer to the link:

    https://github.com/tpn/winsdk-10/blob/master/Include/10.0.14393.0/shared/devguid.h#L47C1-L47C127

    So SPDRP_CLASSGUID always return the same id is correct.

    whether they are the same audio device

    A device instance ID is a system-supplied device identification string that uniquely identifies a device in the system.

    If you want to determine whether two audio devices are the same audio device. You could compare their device instance IDs. If the instance IDs are the same, the devices are the same.

    Jeanine

    Thank you


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.