list USB device with specified VID and PID without using Windows driver Kit

后端 未结 3 1754
半阙折子戏
半阙折子戏 2021-02-09 14:04

is there a way to find a USB device with specified VID and PID on windows without involve calling to WDK functions?

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-09 14:33

    Here's a simplified version of Guo Yanchao's code:

    unsigned index;
    HDEVINFO hDevInfo;
    SP_DEVINFO_DATA DeviceInfoData;
    TCHAR HardwareID[1024];
    
    // List all connected USB devices
    hDevInfo = SetupDiGetClassDevs(NULL, TEXT("USB"), NULL, DIGCF_PRESENT | DIGCF_ALLCLASSES);
    for (index = 0; ; index++) {
        DeviceInfoData.cbSize = sizeof(DeviceInfoData);
        if (!SetupDiEnumDeviceInfo(hDevInfo, index, &DeviceInfoData)) {
            return false;     // no match
        }
    
        SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_HARDWAREID, NULL, (BYTE*)HardwareID, sizeof(HardwareID), NULL);
    
        if (_tcsstr(HardwareID, _T("VID_1234&PID_5678"))) {
            return true;     // match
        }
    }
    

提交回复
热议问题