how to get vendor id and product id of a plugged usb device on windows

后端 未结 3 1194
执笔经年
执笔经年 2020-12-09 06:49

I am using Qt on windows platform.

i want to get and display vendor id and product id of a plugged usb device from my local system.

Below is my full source c

3条回答
  •  借酒劲吻你
    2020-12-09 07:15

    An alternative is to obtain the hardwareID which includes the VID and PID.

    Call SetupDiGetDeviceRegistryProperty with SPDRP_HARDWAREID like so:

    wchar_t *hardwareID;
    
    // First get requiredLength
    SetupDiGetDeviceRegistryProperty(deviceInfoList, &deviceInfoData, SPDRP_HARDWAREID, NULL, NULL, 0, &requiredLength);
    
    hardwareID = (wchar_t*)(new char[requiredLength]());
    
    // Second call to populate hardwareID
    SetupDiGetDeviceRegistryProperty(deviceInfoList, &deviceInfoData, SPDRP_HARDWAREID, NULL, (PBYTE)hardwareID, requiredLength, NULL);
    
    // Display the string
    qDebug() << "hardwareID =" << QString::fromWCharArray(hardwareID);
    

    This will give you a string like USB\ROOT_HUB20&VID1002&PID4396&REV0000 which you can parse.

    *Note: not all devices will have a VID and PID, such as non-USB devices.

提交回复
热议问题