优化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)
{
// 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;