伟大的狗王
伟大的狗王

Reputation: 1

why use SetupDiGetDeviceRegistryProperty get SPDRP_CLASSGUID always return the same id

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;
}

enter image description here

How can I obtain the correct instanceId and guid for the device simultaneously

Upvotes: 0

Views: 23

Answers (0)

Related Questions