优化usbscan名字查找

This commit is contained in:
gb 2022-09-29 12:51:38 +08:00
parent 86d1ee0107
commit 76ebf7e91a
1 changed files with 29 additions and 20 deletions

View File

@ -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) 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_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(""); std::string path("SYSTEM\\CurrentControlSet\\Control\\Class\\{"), name("");
char strind[20] = { "0000" }, val[256] = { 0 }; char val[256] = { 0 };
int ind = 0, err = 0; int ind = 0, err = 0;
path += guid; path += guid;
path += "}\\"; path += "}";
err = RegOpenKeyA(HKEY_LOCAL_MACHINE, (path + strind).c_str(), &key); err = RegOpenKeyA(HKEY_LOCAL_MACHINE, path.c_str(), &root);
while (key) if (root)
{ {
DWORD len = _countof(val) - 1, DWORD index = 0;
type = REG_SZ;
if (RegQueryValueExA(key, "MatchingDeviceId", NULL, &type, (LPBYTE)val, &len) == ERROR_SUCCESS) while (RegEnumKeyA(root, index++, val, _countof(val) - 1) == ERROR_SUCCESS)
{ {
val[len] = 0; RegOpenKeyA(root, val, &key);
DEVID cid = usb_device::vid_pid_from_name(val); if (key)
if (cid == id)
{ {
len = _countof(val) - 1; DWORD len = _countof(val) - 1,
type = REG_SZ; type = REG_SZ;
if (RegQueryValueExA(key, "CreateFileName", NULL, &type, (LPBYTE)val, &len) == ERROR_SUCCESS) if (RegQueryValueExA(key, "MatchingDeviceId", NULL, &type, (LPBYTE)val, &len) == ERROR_SUCCESS)
{ {
val[len] = 0; 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); RegCloseKey(root);
if (!name.empty())
break;
sprintf_s(strind, _countof(strind) - 1, "%04d", ++ind);
RegOpenKeyA(HKEY_LOCAL_MACHINE, (path + strind).c_str(), &key);
} }
return name; return name;