From 76ebf7e91ad1606b7f756623a8897ce271c0b576 Mon Sep 17 00:00:00 2001 From: gb <741021719@qq.com> Date: Thu, 29 Sep 2022 12:51:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96usbscan=E5=90=8D=E5=AD=97?= =?UTF-8?q?=E6=9F=A5=E6=89=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- device/win_usb/win_usb.cpp | 49 ++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/device/win_usb/win_usb.cpp b/device/win_usb/win_usb.cpp index da17ab4..3b76690 100644 --- a/device/win_usb/win_usb.cpp +++ b/device/win_usb/win_usb.cpp @@ -400,38 +400,47 @@ int usb_device::get_device_address(const char* device_name, LPGUID lpguid) std::string usb_device::usb_scan_name(DEVID id, const char* guid) { // HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{6BDD1FC6-810F-11D0-BEC7-08002BE2092F} - HKEY key = NULL; + HKEY key = NULL, root = NULL; std::string path("SYSTEM\\CurrentControlSet\\Control\\Class\\{"), name(""); - char strind[20] = { "0000" }, val[256] = { 0 }; + char val[256] = { 0 }; int ind = 0, err = 0; path += guid; - path += "}\\"; - err = RegOpenKeyA(HKEY_LOCAL_MACHINE, (path + strind).c_str(), &key); - while (key) + path += "}"; + err = RegOpenKeyA(HKEY_LOCAL_MACHINE, path.c_str(), &root); + if (root) { - DWORD len = _countof(val) - 1, - type = REG_SZ; - if (RegQueryValueExA(key, "MatchingDeviceId", NULL, &type, (LPBYTE)val, &len) == ERROR_SUCCESS) + DWORD index = 0; + + while (RegEnumKeyA(root, index++, val, _countof(val) - 1) == ERROR_SUCCESS) { - val[len] = 0; - DEVID cid = usb_device::vid_pid_from_name(val); - if (cid == id) + RegOpenKeyA(root, val, &key); + if (key) { - len = _countof(val) - 1; - type = REG_SZ; - if (RegQueryValueExA(key, "CreateFileName", NULL, &type, (LPBYTE)val, &len) == ERROR_SUCCESS) + DWORD len = _countof(val) - 1, + type = REG_SZ; + if (RegQueryValueExA(key, "MatchingDeviceId", NULL, &type, (LPBYTE)val, &len) == ERROR_SUCCESS) { val[len] = 0; - name = val; + DEVID cid = usb_device::vid_pid_from_name(val); + if (cid == id) + { + len = _countof(val) - 1; + type = REG_SZ; + if (RegQueryValueExA(key, "CreateFileName", NULL, &type, (LPBYTE)val, &len) == ERROR_SUCCESS) + { + val[len] = 0; + name = val; + } + } } + RegCloseKey(key); + if (!name.empty()) + break; } + memset(val, 0, sizeof(val)); } - RegCloseKey(key); - if (!name.empty()) - break; - sprintf_s(strind, _countof(strind) - 1, "%04d", ++ind); - RegOpenKeyA(HKEY_LOCAL_MACHINE, (path + strind).c_str(), &key); + RegCloseKey(root); } return name;