优化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,15 +400,22 @@ 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 index = 0;
while (RegEnumKeyA(root, index++, val, _countof(val) - 1) == ERROR_SUCCESS)
{
RegOpenKeyA(root, val, &key);
if (key)
{
DWORD len = _countof(val) - 1,
type = REG_SZ;
@ -430,8 +437,10 @@ std::string usb_device::usb_scan_name(DEVID id, const char* guid)
RegCloseKey(key);
if (!name.empty())
break;
sprintf_s(strind, _countof(strind) - 1, "%04d", ++ind);
RegOpenKeyA(HKEY_LOCAL_MACHINE, (path + strind).c_str(), &key);
}
memset(val, 0, sizeof(val));
}
RegCloseKey(root);
}
return name;