| 3 | #include <spdlog/spdlog.h> |
| 4 | |
| 5 | std::optional<std::string> RegistryUtils::GetStringValue(HKEY hKeyParent, const std::string &subKey, const std::string &valueName) { |
| 6 | HKEY hKey; |
| 7 | LONG result = RegOpenKeyExA(hKeyParent, subKey.c_str(), 0, KEY_READ, &hKey); |
| 8 | if(result != ERROR_SUCCESS) { |
| 9 | spdlog::error("Failed to open registry key. (Status={})", result); |
| 10 | return {}; |
| 11 | } |
| 12 | DWORD dataSize = 0; |
| 13 | result = RegQueryValueExA(hKey, valueName.c_str(), nullptr, nullptr, nullptr, &dataSize); |
| 14 | if(result != ERROR_SUCCESS) { |
| 15 | spdlog::error("Failed to query registry value. (Status={})", result); |
| 16 | RegCloseKey(hKey); |
| 17 | return {}; |
| 18 | } |
| 19 | std::vector<char> data(dataSize); |
| 20 | result = RegQueryValueExA(hKey, valueName.c_str(), nullptr, nullptr, reinterpret_cast<BYTE *>(data.data()), &dataSize); |
| 21 | if(result != ERROR_SUCCESS) { |
| 22 | spdlog::error("Failed to query registry value. (Status={})", result); |
| 23 | RegCloseKey(hKey); |
| 24 | return {}; |
| 25 | } |
| 26 | |
| 27 | RegCloseKey(hKey); |
| 28 | if(!data.empty() && data.back() == '\0') |
| 29 | data.pop_back(); |
| 30 | return std::string{data.begin(), data.end()}; |
| 31 | } |
| 32 | |
| 33 | bool RegistryUtils::SetStringValue(HKEY hKeyParent, const std::string &subKey, const std::string &valueName, const std::string &newValue) { |
| 34 | HKEY hKey; |