Support reading longer registry value by dynamically allocating buffer size (#42)

This commit is contained in:
Sandeep Somavarapu 2025-02-10 17:56:51 +01:00 committed by GitHub
parent 5ec382d087
commit 9b1e64530a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -74,18 +74,32 @@ private:
if (ERROR_SUCCESS != RegOpenKeyEx(root, registryKey.c_str(), 0, KEY_READ, &hKey))
return std::nullopt;
BYTE buffer[1024];
DWORD bufferSize = sizeof(buffer);
DWORD bufferSize = 0;
DWORD type;
auto readResult = RegQueryValueEx(hKey, name.c_str(), 0, &type, buffer, &bufferSize);
// First query to get required buffer size
auto result = RegQueryValueEx(hKey, name.c_str(), 0, &type, nullptr, &bufferSize);
if (ERROR_SUCCESS != result && ERROR_MORE_DATA != result)
{
RegCloseKey(hKey);
return std::nullopt;
}
if (std::find(supportedTypes.begin(), supportedTypes.end(), type) == supportedTypes.end())
{
RegCloseKey(hKey);
return std::nullopt;
}
std::vector<BYTE> buffer(bufferSize);
result = RegQueryValueEx(hKey, name.c_str(), 0, &type, buffer.data(), &bufferSize);
RegCloseKey(hKey);
if (ERROR_SUCCESS != readResult ||
std::find(supportedTypes.begin(), supportedTypes.end(), type) == supportedTypes.end())
if (ERROR_SUCCESS != result)
return std::nullopt;
return std::optional<T>{parseRegistryValue(buffer, bufferSize, type)};
return std::optional<T>{parseRegistryValue(buffer.data(), bufferSize, type)};
}
};