From 0e0c6dff90e6b3b4431ce4316ba246bd63b4d0b7 Mon Sep 17 00:00:00 2001 From: Nagib Gulam Date: Tue, 13 Jun 2023 02:39:14 -0500 Subject: [PATCH 1/4] Initial add --find-hwid option --- src/Devcon.cpp | 150 +++++++++++++++++++++++++++++++++++++++++++++ src/Devcon.h | 2 + src/NefConUtil.cpp | 20 +++++- 3 files changed, 171 insertions(+), 1 deletion(-) diff --git a/src/Devcon.cpp b/src/Devcon.cpp index 029553b..96173b5 100644 --- a/src/Devcon.cpp +++ b/src/Devcon.cpp @@ -17,6 +17,8 @@ // STL // #include +#include + // // Logging @@ -1135,6 +1137,154 @@ bool devcon::inf_default_uninstall(const std::wstring& fullInfPath, bool* reboot return errCode == ERROR_SUCCESS; } +bool devcon::find_hwid(const std::wstring& matchstring) +{ + HDEVINFO devs = INVALID_HANDLE_VALUE; +// IdEntry* templ = NULL; +// int failcode = EXIT_FAIL; + int retcode; + int argIndex; + DWORD devIndex; + SP_DEVINFO_DATA devInfo; + SP_DEVINFO_LIST_DETAIL_DATA devInfoListDetail; + BOOL doSearch = FALSE; +// BOOL match; +// BOOL all = FALSE; +// GUID cls; +// DWORD numClass = 0; +// int skip = 0; + + LPTSTR buffer; + DWORD size; + DWORD reqSize; + DWORD dataType; + size = 8192; // initial guess, nothing magic about this + buffer = new TCHAR[(size / sizeof(TCHAR)) + 2]; + if (!buffer) { + return FALSE; + } + + devs = SetupDiGetClassDevsW(NULL, + NULL, + NULL, + DIGCF_ALLCLASSES | DIGCF_PRESENT); + + if (devs == INVALID_HANDLE_VALUE) { + return FALSE; + } + +#if 0 + devInfoListDetail.cbSize = sizeof(devInfoListDetail); + if (!SetupDiGetDeviceInfoListDetail(devs, &devInfoListDetail)) { + goto final; + } +#endif + // + // now enumerate them + // + + devInfo.cbSize = sizeof(devInfo); + for (devIndex = 0; SetupDiEnumDeviceInfo(devs, devIndex, &devInfo); devIndex++) { + // std::cout << "devIndex = " << devIndex << std::endl; +#if 0 + TCHAR devID[MAX_DEVICE_ID_LEN]; + LPTSTR* hwIds = NULL; + LPTSTR* compatIds = NULL; + // + // determine instance ID + // + if (CM_Get_Device_ID_Ex(devInfo.DevInst, devID, MAX_DEVICE_ID_LEN, 0, devInfoListDetail.RemoteMachineHandle) != CR_SUCCESS) { + devID[0] = TEXT('\0'); + } +#endif + while (!SetupDiGetDeviceRegistryProperty(devs, &devInfo, SPDRP_HARDWAREID, &dataType, (LPBYTE)buffer, size, &reqSize)) { + if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { + goto loop; + } + if (dataType != REG_MULTI_SZ) { + goto loop; + } + size = reqSize; + delete[] buffer; + buffer = new TCHAR[(size / sizeof(TCHAR)) + 2]; + if (!buffer) { + goto failed; + } + } + loop: + // std::wcout << "Buffer from SetupDiGetDeviceRegistryProperty is " << buffer << std::endl; + //CStringArray strings; + std::vector arraybuf; + const TCHAR* p = buffer; + while (*p) // while not at the end of strings + { + arraybuf.push_back(p); + //strings.Add(p); // add string to array + p += _tcslen(p) + 1; // find next string + } + + //delete[] buffer; + // Print Strings stored in Vector + for (int i = 0; i < arraybuf.size(); i++) { + if (arraybuf[i].find(matchstring) != std::wstring::npos) { + // std::cout << "found!" << '\n'; + std::wcout << L"Hardware ID: " << arraybuf[i] << std::endl; + while (!SetupDiGetDeviceRegistryProperty(devs, &devInfo, SPDRP_DEVICEDESC, &dataType, (LPBYTE)buffer, size, &reqSize)) { + if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { + goto loop2; + } + size = reqSize; + delete[] buffer; + buffer = new TCHAR[(size / sizeof(TCHAR)) + 2]; + if (!buffer) { + goto failed; + } + } + std::wcout << L"Name: " << buffer << std::endl; + // Build a list of driver info items that we will retrieve below + if (!SetupDiBuildDriverInfoList(devs, &devInfo, SPDIT_COMPATDRIVER)) + printf("err - %x\n", GetLastError()); + + // Get the first info item for this driver + SP_DRVINFO_DATA drvInfo; + drvInfo.cbSize = sizeof(SP_DRVINFO_DATA); + if (!SetupDiEnumDriverInfo(devs, &devInfo, SPDIT_COMPATDRIVER, 0, &drvInfo)) + printf("err - %x\n", GetLastError()); // Still fails with "no more items" + else + // std::wcout << L"Version: 0x" << std::uppercase << std::setfill(L'0') << std::setw(4) << std::hex << (drvInfo.DriverVersion >> 32); + // std::wcout << L" 0x" << std::uppercase << std::setfill(L'0') << std::setw(4) << std::hex << (drvInfo.DriverVersion & 0xffffffffULL) << std::endl; + std::wcout << L"Version: " << std::to_wstring((drvInfo.DriverVersion >> 48) & 0xFFFF); + std::wcout << L"." << std::to_wstring((drvInfo.DriverVersion >> 32) & 0xFFFF); + std::wcout << L"." << std::to_wstring((drvInfo.DriverVersion >> 16) & 0xFFFF); + std::wcout << L"." << std::to_wstring(drvInfo.DriverVersion & 0x0000FFFF) << std::endl; + + // desc = GetDeviceStringProperty(Devs, DevInfo, SPDRP_DEVICEDESC);SPDRP_FRIENDLYNAME + + } + loop2: + retcode = 1; + //std::wcout << arraybuf[i] << std::endl; + } + + } + // hwIds = GetDevMultiSz(devs, &devInfo, SPDRP_HARDWAREID); + // compatIds = GetDevMultiSz(devs, &devInfo, SPDRP_COMPATIBLEIDS); + + // if (WildCompareHwIds(hwIds, templ[argIndex]) || + // WildCompareHwIds(compatIds, templ[argIndex])) { + // match = TRUE; + // } + + final: + failed: + if (devs != INVALID_HANDLE_VALUE) { + SetupDiDestroyDeviceInfoList(devs); + } + return TRUE; + +} + + // // Hooks MessageBoxW which is called if an error occurred, even when instructed to suppress any UI interaction // diff --git a/src/Devcon.h b/src/Devcon.h index ada6951..d7dd565 100644 --- a/src/Devcon.h +++ b/src/Devcon.h @@ -33,4 +33,6 @@ namespace devcon bool inf_default_install(const std::wstring& fullInfPath, bool* rebootRequired); bool inf_default_uninstall(const std::wstring& fullInfPath, bool* rebootRequired); + + bool find_hwid(const std::wstring& matchstring); }; diff --git a/src/NefConUtil.cpp b/src/NefConUtil.cpp index c35bf4d..53fef92 100644 --- a/src/NefConUtil.cpp +++ b/src/NefConUtil.cpp @@ -730,6 +730,22 @@ int main(int argc, char* argv[]) return EXIT_SUCCESS; } + if (cmdl[{ "--find-hwid" }]) + { + int errorCode; + // if (!IsAdmin(errorCode)) return errorCode; + + hwId = cmdl({ "--hardware-id" }).str(); + + if (hwId.empty()) { + logger->error("Hardware ID missing"); + std::cout << color(red) << "Hardware ID missing" << std::endl; + return EXIT_FAILURE; + } + devcon::find_hwid(ConvertAnsiToWide(hwId)); + return EXIT_SUCCESS; + } + #pragma endregion if (cmdl[{ "-v", "--version" }]) @@ -783,7 +799,9 @@ int main(int argc, char* argv[]) std::cout << " --inf-default-uninstall Uninstalls an INF file with a [DefaultUninstall] section" << std::endl; std::cout << " --inf-path Absolute path to the INF file to uninstall (required)" << std::endl; std::cout << " --delete-file-on-reboot Marks a given file to get deleted on next reboot" << std::endl; - std::cout << " --file-path The absolute path of the file to remove (required)" << std::endl; + std::cout << " --file-path The absolute path of the file to remove (required)" << std::endl; + std::cout << " --find-hwid Shows the devices matching the wildcard hwid" << std::endl; + std::cout << " ---hardware-id Hardware ID (or partial) of the device for matching (required)" << std::endl; std::cout << " -v, --version Display version of this utility" << std::endl; std::cout << std::endl; std::cout << " logging:" << std::endl; From 4099c325fff8558990ea5956781e4cb789e1503a Mon Sep 17 00:00:00 2001 From: Nagib Gulam Date: Tue, 13 Jun 2023 06:14:46 -0500 Subject: [PATCH 2/4] Clean up code + refactor --- src/Devcon.cpp | 266 ++++++++++++++++++++++++++----------------------- 1 file changed, 139 insertions(+), 127 deletions(-) diff --git a/src/Devcon.cpp b/src/Devcon.cpp index 96173b5..3ddf043 100644 --- a/src/Devcon.cpp +++ b/src/Devcon.cpp @@ -1139,148 +1139,160 @@ bool devcon::inf_default_uninstall(const std::wstring& fullInfPath, bool* reboot bool devcon::find_hwid(const std::wstring& matchstring) { - HDEVINFO devs = INVALID_HANDLE_VALUE; -// IdEntry* templ = NULL; -// int failcode = EXIT_FAIL; - int retcode; - int argIndex; - DWORD devIndex; - SP_DEVINFO_DATA devInfo; - SP_DEVINFO_LIST_DETAIL_DATA devInfoListDetail; - BOOL doSearch = FALSE; -// BOOL match; -// BOOL all = FALSE; -// GUID cls; -// DWORD numClass = 0; -// int skip = 0; - - LPTSTR buffer; - DWORD size; - DWORD reqSize; - DWORD dataType; - size = 8192; // initial guess, nothing magic about this - buffer = new TCHAR[(size / sizeof(TCHAR)) + 2]; - if (!buffer) { - return FALSE; - } + bool found = FALSE; + DWORD devIndex, err, total = 0; + HDEVINFO hDevInfo; + SP_DEVINFO_DATA spDevInfoData; - devs = SetupDiGetClassDevsW(NULL, - NULL, - NULL, - DIGCF_ALLCLASSES | DIGCF_PRESENT); + DWORD DataT; + LPTSTR buffer = nullptr; + DWORD buffersize = 0; - if (devs == INVALID_HANDLE_VALUE) { - return FALSE; + hDevInfo = SetupDiGetClassDevs( + nullptr, + nullptr, + nullptr, + DIGCF_ALLCLASSES | DIGCF_PRESENT + ); + if (hDevInfo == INVALID_HANDLE_VALUE) + { + return found; } -#if 0 - devInfoListDetail.cbSize = sizeof(devInfoListDetail); - if (!SetupDiGetDeviceInfoListDetail(devs, &devInfoListDetail)) { - goto final; - } -#endif - // - // now enumerate them - // + spDevInfoData.cbSize = sizeof(spDevInfoData); + for (devIndex = 0; SetupDiEnumDeviceInfo(hDevInfo, devIndex, &spDevInfoData); devIndex++) { + // get all devices info + while (!SetupDiGetDeviceRegistryProperty(hDevInfo, + &spDevInfoData, + SPDRP_HARDWAREID, + &DataT, + (PBYTE)buffer, + buffersize, + &buffersize)) + { + if (GetLastError() == ERROR_INVALID_DATA) + { + break; + } + if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) + { + if (buffer) + LocalFree(buffer); + buffer = static_cast(LocalAlloc(LPTR, buffersize)); + } + else + { + goto cleanup_DeviceInfo; + } + } + if (GetLastError() == ERROR_INVALID_DATA) + continue; - devInfo.cbSize = sizeof(devInfo); - for (devIndex = 0; SetupDiEnumDeviceInfo(devs, devIndex, &devInfo); devIndex++) { - // std::cout << "devIndex = " << devIndex << std::endl; -#if 0 - TCHAR devID[MAX_DEVICE_ID_LEN]; - LPTSTR* hwIds = NULL; - LPTSTR* compatIds = NULL; - // - // determine instance ID - // - if (CM_Get_Device_ID_Ex(devInfo.DevInst, devID, MAX_DEVICE_ID_LEN, 0, devInfoListDetail.RemoteMachineHandle) != CR_SUCCESS) { - devID[0] = TEXT('\0'); + std::vector arraybuf; + const TCHAR* p = buffer; + while (*p) + { + arraybuf.push_back(p); + p += _tcslen(p) + 1; + } + bool found_match = FALSE; + for (int i = 0; i < arraybuf.size(); i++) { + if (arraybuf[i].find(matchstring) != std::wstring::npos) { + found_match = TRUE; + break; + } + } + // If we have a match, print out the whole array + if (found_match) { + if (total++ > 0) + std::wcout << std::endl; + std::wcout << L"Hardware IDs: "; + for (int i = 0; i < arraybuf.size(); i++) { + std::wcout << arraybuf[i] << std::endl; + } + + //total++; + while (!SetupDiGetDeviceRegistryProperty(hDevInfo, + &spDevInfoData, + SPDRP_FRIENDLYNAME, + &DataT, + (PBYTE)buffer, + buffersize, + &buffersize)) + { + if (GetLastError() == ERROR_INVALID_DATA) + { + break; + } + if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) + { + if (buffer) + LocalFree(buffer); + buffer = static_cast(LocalAlloc(LPTR, buffersize)); } -#endif - while (!SetupDiGetDeviceRegistryProperty(devs, &devInfo, SPDRP_HARDWAREID, &dataType, (LPBYTE)buffer, size, &reqSize)) { - if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { - goto loop; + else + { + goto cleanup_DeviceInfo; + } + } + if (GetLastError() == ERROR_INVALID_DATA) + { + // Lets try SPDRP_DEVICEDESC + while (!SetupDiGetDeviceRegistryProperty(hDevInfo, + &spDevInfoData, + SPDRP_DEVICEDESC, + &DataT, + (PBYTE)buffer, + buffersize, + &buffersize)) + { + if (GetLastError() == ERROR_INVALID_DATA) + { + break; } - if (dataType != REG_MULTI_SZ) { - goto loop; + if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) + { + if (buffer) + LocalFree(buffer); + buffer = static_cast(LocalAlloc(LPTR, buffersize)); } - size = reqSize; - delete[] buffer; - buffer = new TCHAR[(size / sizeof(TCHAR)) + 2]; - if (!buffer) { - goto failed; + else + { + goto cleanup_DeviceInfo; } } - loop: - // std::wcout << "Buffer from SetupDiGetDeviceRegistryProperty is " << buffer << std::endl; - //CStringArray strings; - std::vector arraybuf; - const TCHAR* p = buffer; - while (*p) // while not at the end of strings + if (GetLastError() != ERROR_INVALID_DATA) { - arraybuf.push_back(p); - //strings.Add(p); // add string to array - p += _tcslen(p) + 1; // find next string + std::wcout << L"Name: " << buffer << std::endl; } + } + else + { + std::wcout << L"FriendlyName: " << buffer << std::endl; + } + // Build a list of driver info items that we will retrieve below + if (!SetupDiBuildDriverInfoList(hDevInfo, &spDevInfoData, SPDIT_COMPATDRIVER)) + goto cleanup_DeviceInfo; - //delete[] buffer; - // Print Strings stored in Vector - for (int i = 0; i < arraybuf.size(); i++) { - if (arraybuf[i].find(matchstring) != std::wstring::npos) { - // std::cout << "found!" << '\n'; - std::wcout << L"Hardware ID: " << arraybuf[i] << std::endl; - while (!SetupDiGetDeviceRegistryProperty(devs, &devInfo, SPDRP_DEVICEDESC, &dataType, (LPBYTE)buffer, size, &reqSize)) { - if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { - goto loop2; - } - size = reqSize; - delete[] buffer; - buffer = new TCHAR[(size / sizeof(TCHAR)) + 2]; - if (!buffer) { - goto failed; - } - } - std::wcout << L"Name: " << buffer << std::endl; - // Build a list of driver info items that we will retrieve below - if (!SetupDiBuildDriverInfoList(devs, &devInfo, SPDIT_COMPATDRIVER)) - printf("err - %x\n", GetLastError()); - - // Get the first info item for this driver - SP_DRVINFO_DATA drvInfo; - drvInfo.cbSize = sizeof(SP_DRVINFO_DATA); - if (!SetupDiEnumDriverInfo(devs, &devInfo, SPDIT_COMPATDRIVER, 0, &drvInfo)) - printf("err - %x\n", GetLastError()); // Still fails with "no more items" - else - // std::wcout << L"Version: 0x" << std::uppercase << std::setfill(L'0') << std::setw(4) << std::hex << (drvInfo.DriverVersion >> 32); - // std::wcout << L" 0x" << std::uppercase << std::setfill(L'0') << std::setw(4) << std::hex << (drvInfo.DriverVersion & 0xffffffffULL) << std::endl; - std::wcout << L"Version: " << std::to_wstring((drvInfo.DriverVersion >> 48) & 0xFFFF); - std::wcout << L"." << std::to_wstring((drvInfo.DriverVersion >> 32) & 0xFFFF); - std::wcout << L"." << std::to_wstring((drvInfo.DriverVersion >> 16) & 0xFFFF); - std::wcout << L"." << std::to_wstring(drvInfo.DriverVersion & 0x0000FFFF) << std::endl; - - // desc = GetDeviceStringProperty(Devs, DevInfo, SPDRP_DEVICEDESC);SPDRP_FRIENDLYNAME - - } - loop2: - retcode = 1; - //std::wcout << arraybuf[i] << std::endl; - } + // Get the first info item for this driver + SP_DRVINFO_DATA drvInfo; + drvInfo.cbSize = sizeof(SP_DRVINFO_DATA); + if (!SetupDiEnumDriverInfo(hDevInfo, &spDevInfoData, SPDIT_COMPATDRIVER, 0, &drvInfo)) + goto cleanup_DeviceInfo; // Still fails with "no more items" + std::wcout << L"Version: " << std::to_wstring((drvInfo.DriverVersion >> 48) & 0xFFFF); + std::wcout << L"." << std::to_wstring((drvInfo.DriverVersion >> 32) & 0xFFFF); + std::wcout << L"." << std::to_wstring((drvInfo.DriverVersion >> 16) & 0xFFFF); + std::wcout << L"." << std::to_wstring(drvInfo.DriverVersion & 0x0000FFFF) << std::endl; + } } - // hwIds = GetDevMultiSz(devs, &devInfo, SPDRP_HARDWAREID); - // compatIds = GetDevMultiSz(devs, &devInfo, SPDRP_COMPATIBLEIDS); - - // if (WildCompareHwIds(hwIds, templ[argIndex]) || - // WildCompareHwIds(compatIds, templ[argIndex])) { - // match = TRUE; - // } - - final: - failed: - if (devs != INVALID_HANDLE_VALUE) { - SetupDiDestroyDeviceInfoList(devs); - } - return TRUE; + + cleanup_DeviceInfo: + err = GetLastError(); + SetupDiDestroyDeviceInfoList(hDevInfo); + SetLastError(err); + std::wcout << L"Total Found: " << std::to_wstring(total) << std::endl; + return found; } From e6cbf872842d7ee138fae70fa7300eff31891c2f Mon Sep 17 00:00:00 2001 From: Nagib Gulam Date: Tue, 13 Jun 2023 11:19:52 -0500 Subject: [PATCH 3/4] Update to use error code & enable verbose --- src/Devcon.cpp | 33 +++++++++++++++++---------------- src/NefConUtil.cpp | 8 ++++---- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/Devcon.cpp b/src/Devcon.cpp index 3ddf043..4dd2f62 100644 --- a/src/Devcon.cpp +++ b/src/Devcon.cpp @@ -1139,6 +1139,7 @@ bool devcon::inf_default_uninstall(const std::wstring& fullInfPath, bool* reboot bool devcon::find_hwid(const std::wstring& matchstring) { + el::Logger* logger = el::Loggers::getLogger("default"); bool found = FALSE; DWORD devIndex, err, total = 0; HDEVINFO hDevInfo; @@ -1204,17 +1205,15 @@ bool devcon::find_hwid(const std::wstring& matchstring) } // If we have a match, print out the whole array if (found_match) { - if (total++ > 0) - std::wcout << std::endl; - std::wcout << L"Hardware IDs: "; + found = TRUE; + total++; + logger->verbose(1, "Hardware IDs: "); for (int i = 0; i < arraybuf.size(); i++) { - std::wcout << arraybuf[i] << std::endl; + logger->verbose(1, "%v", arraybuf[i]); } - - //total++; while (!SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData, - SPDRP_FRIENDLYNAME, + SPDRP_DEVICEDESC, &DataT, (PBYTE)buffer, buffersize, @@ -1240,7 +1239,7 @@ bool devcon::find_hwid(const std::wstring& matchstring) // Lets try SPDRP_DEVICEDESC while (!SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData, - SPDRP_DEVICEDESC, + SPDRP_FRIENDLYNAME, &DataT, (PBYTE)buffer, buffersize, @@ -1263,12 +1262,12 @@ bool devcon::find_hwid(const std::wstring& matchstring) } if (GetLastError() != ERROR_INVALID_DATA) { - std::wcout << L"Name: " << buffer << std::endl; + logger->verbose(1, "Name: %v", std::wstring(buffer)); } } else { - std::wcout << L"FriendlyName: " << buffer << std::endl; + logger->verbose(1, "Name: %v", std::wstring(buffer)); } // Build a list of driver info items that we will retrieve below if (!SetupDiBuildDriverInfoList(hDevInfo, &spDevInfoData, SPDIT_COMPATDRIVER)) @@ -1280,18 +1279,20 @@ bool devcon::find_hwid(const std::wstring& matchstring) if (!SetupDiEnumDriverInfo(hDevInfo, &spDevInfoData, SPDIT_COMPATDRIVER, 0, &drvInfo)) goto cleanup_DeviceInfo; // Still fails with "no more items" - std::wcout << L"Version: " << std::to_wstring((drvInfo.DriverVersion >> 48) & 0xFFFF); - std::wcout << L"." << std::to_wstring((drvInfo.DriverVersion >> 32) & 0xFFFF); - std::wcout << L"." << std::to_wstring((drvInfo.DriverVersion >> 16) & 0xFFFF); - std::wcout << L"." << std::to_wstring(drvInfo.DriverVersion & 0x0000FFFF) << std::endl; - } + logger->verbose(1, "Version: %v.%v.%v.%v", std::to_wstring((drvInfo.DriverVersion >> 48) & 0xFFFF), + std::to_wstring((drvInfo.DriverVersion >> 32) & 0xFFFF), + std::to_wstring((drvInfo.DriverVersion >> 16) & 0xFFFF), + std::to_wstring(drvInfo.DriverVersion & 0x0000FFFF)); + } } cleanup_DeviceInfo: err = GetLastError(); SetupDiDestroyDeviceInfoList(hDevInfo); + if (!found && err == ERROR_SUCCESS) + err = ERROR_FILE_NOT_FOUND; SetLastError(err); - std::wcout << L"Total Found: " << std::to_wstring(total) << std::endl; + logger->verbose(1, "Total Found:: %v", std::to_wstring(total)); return found; } diff --git a/src/NefConUtil.cpp b/src/NefConUtil.cpp index 53fef92..38bd5f1 100644 --- a/src/NefConUtil.cpp +++ b/src/NefConUtil.cpp @@ -732,9 +732,6 @@ int main(int argc, char* argv[]) if (cmdl[{ "--find-hwid" }]) { - int errorCode; - // if (!IsAdmin(errorCode)) return errorCode; - hwId = cmdl({ "--hardware-id" }).str(); if (hwId.empty()) { @@ -742,7 +739,10 @@ int main(int argc, char* argv[]) std::cout << color(red) << "Hardware ID missing" << std::endl; return EXIT_FAILURE; } - devcon::find_hwid(ConvertAnsiToWide(hwId)); + if (!devcon::find_hwid(ConvertAnsiToWide(hwId))) + { + return GetLastError(); + } return EXIT_SUCCESS; } From 980a84b8b0a3e65b14df423e5f7c4607eac9ba17 Mon Sep 17 00:00:00 2001 From: Nagib Gulam Date: Tue, 13 Jun 2023 11:28:00 -0500 Subject: [PATCH 4/4] Small errcode touchup --- src/Devcon.cpp | 3 --- src/NefConUtil.cpp | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Devcon.cpp b/src/Devcon.cpp index 4dd2f62..edea577 100644 --- a/src/Devcon.cpp +++ b/src/Devcon.cpp @@ -1289,12 +1289,9 @@ bool devcon::find_hwid(const std::wstring& matchstring) cleanup_DeviceInfo: err = GetLastError(); SetupDiDestroyDeviceInfoList(hDevInfo); - if (!found && err == ERROR_SUCCESS) - err = ERROR_FILE_NOT_FOUND; SetLastError(err); logger->verbose(1, "Total Found:: %v", std::to_wstring(total)); return found; - } diff --git a/src/NefConUtil.cpp b/src/NefConUtil.cpp index 38bd5f1..4b5bbb3 100644 --- a/src/NefConUtil.cpp +++ b/src/NefConUtil.cpp @@ -741,7 +741,7 @@ int main(int argc, char* argv[]) } if (!devcon::find_hwid(ConvertAnsiToWide(hwId))) { - return GetLastError(); + return ERROR_NOT_FOUND; } return EXIT_SUCCESS; }