#include "LibusbSupportDriverChecker.h" #ifdef WIN_COMPILER #include #include #include #include #include #endif #ifdef WIN_COMPILER std::wstring Incart::Usb::LibusbSupportDriverChecker::getNumberText(const uint16_t number, const uint16_t fieldWidth) { std::wstring numberText = QString("%1").arg(number, fieldWidth, 16, QChar('0')).toStdWString(); return std::wstring(fieldWidth - numberText.length(), '0') + numberText; } #endif bool Incart::Usb::LibusbSupportDriverChecker::isLibusbSupported(const uint16_t vid, const uint16_t pid) { #ifdef WIN_COMPILER std::wstring vidText = getNumberText(vid, 4); std::wstring pidText = getNumberText(pid, 4); std::wstring searchHardwareID = L"VID_" + vidText + L"&PID_" + pidText; unsigned index; HDEVINFO hDevInfo; SP_DEVINFO_DATA DeviceInfoData; TCHAR hardwareID[1024]; TCHAR serviceName[1024]; // Получаем список всех подключенных устройств hDevInfo = SetupDiGetClassDevs(NULL, TEXT("USB"), NULL, DIGCF_PRESENT | DIGCF_ALLCLASSES); if (INVALID_HANDLE_VALUE == hDevInfo) { return false; } DeviceInfoData.cbSize = sizeof(DeviceInfoData); bool isFound = false; BOOL result = FALSE; for (index = 0; SetupDiEnumDeviceInfo(hDevInfo, index, &DeviceInfoData); index++) { result = SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_HARDWAREID, NULL, (BYTE*)hardwareID, sizeof(hardwareID), NULL); if (result == FALSE) { continue; } // Пытаемся найти устройство с заданным VID и PID if (_tcsstr(hardwareID, searchHardwareID.c_str())) { // Проверяем свойство Service у найденного устройства // Service: libusb0 - у "старых" драйверов // Service: WinUSB - у "новых" драйверов result = SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_SERVICE, NULL, (BYTE*)serviceName, sizeof(serviceName), NULL); if (result == FALSE) { isFound = false; } else { isFound = _tcsstr(serviceName, L"WinUSB"); } break; } } // Очищаем список устройств SetupDiDestroyDeviceInfoList(hDevInfo); return isFound; #else return true; #endif }