Implement reading path and unicode string from windows registry

This commit is contained in:
Sajid 2025-01-28 15:37:38 +06:00
parent 2b6013ad26
commit 4094690282
3 changed files with 52 additions and 19 deletions

View file

@ -176,7 +176,7 @@ int main(int argc, char *argv[])
if (!Registry::RootDirectoryPath.empty()) if (!Registry::RootDirectoryPath.empty())
{ {
if (!os::process::SetWorkingDirectory(std::filesystem::path(Registry::RootDirectoryPath))) if (!os::process::SetWorkingDirectory(std::filesystem::path(Registry::RootDirectoryPath)))
LOGFN_ERROR("Failed to set working directory: \"{}\"", Registry::RootDirectoryPath); LOGFN_ERROR("Failed to set working directory: \"{}\"", Registry::RootDirectoryPath.string());
} }
HostStartup(); HostStartup();

View file

@ -16,44 +16,77 @@ bool os::registry::ReadValue(const std::string& name, T& data)
if (RegOpenKeyExW(HKEY_CURRENT_USER, g_registryRoot, 0, KEY_READ, &hKey) != ERROR_SUCCESS) if (RegOpenKeyExW(HKEY_CURRENT_USER, g_registryRoot, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
return false; return false;
BYTE buffer[512]; wchar_t wideName[128];
DWORD bufferSize = sizeof(buffer); int wideNameSize = MultiByteToWideChar(CP_UTF8, 0, name.c_str(), name.size(), wideName, sizeof(wideName));
if (wideNameSize == 0)
{
return false;
}
wideName[wideNameSize] = 0;
DWORD bufferSize = 0;
DWORD dataType = 0; DWORD dataType = 0;
auto result = RegQueryValueExA(hKey, name.c_str(), NULL, &dataType, buffer, &bufferSize); auto result = RegQueryValueExW(hKey, wideName, NULL, &dataType, nullptr, &bufferSize);
RegCloseKey(hKey);
if (result != ERROR_SUCCESS) if (result != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return false; return false;
}
result = ERROR_INVALID_FUNCTION;
if constexpr (std::is_same_v<T, std::string>) if constexpr (std::is_same_v<T, std::string>)
{ {
if (dataType != REG_SZ) if (dataType == REG_SZ)
return false; {
std::vector<uint8_t> buffer{};
buffer.reserve(bufferSize);
result = RegQueryValueExW(hKey, wideName, nullptr, nullptr, buffer.data(), &bufferSize);
data = std::string((char*)buffer, bufferSize - 1); if (result == ERROR_SUCCESS)
{
int valueSize = WideCharToMultiByte(CP_UTF8, 0, (wchar_t*)buffer.data(), (bufferSize / sizeof(wchar_t)) - 1, nullptr, 0, nullptr, nullptr);
data.resize(valueSize);
WideCharToMultiByte(CP_UTF8, 0, (wchar_t*)buffer.data(), (bufferSize / sizeof(wchar_t)) - 1, data.data(), valueSize, nullptr, nullptr);
}
}
}
else if constexpr (std::is_same_v<T, std::filesystem::path>)
{
if (dataType == REG_SZ)
{
std::vector<uint8_t> buffer{};
buffer.reserve(bufferSize);
result = RegQueryValueExW(hKey, wideName, nullptr, nullptr, buffer.data(), &bufferSize);
if (result == ERROR_SUCCESS)
{
data = reinterpret_cast<wchar_t*>(buffer.data());
}
}
} }
else if constexpr (std::is_same_v<T, uint32_t>) else if constexpr (std::is_same_v<T, uint32_t>)
{ {
if (dataType != REG_DWORD) if (dataType == REG_DWORD)
return false; {
result = RegQueryValueExW(hKey, wideName, nullptr, nullptr, (BYTE*)&data, &bufferSize);
data = *(uint32_t*)buffer; }
} }
else if constexpr (std::is_same_v<T, uint64_t>) else if constexpr (std::is_same_v<T, uint64_t>)
{ {
if (dataType != REG_QWORD) if (dataType == REG_QWORD)
return false; {
result = RegQueryValueExW(hKey, wideName, nullptr, nullptr, (BYTE*)&data, &bufferSize);
data = *(uint32_t*)buffer; }
} }
else else
{ {
static_assert(false, "Unsupported data type."); static_assert(false, "Unsupported data type.");
} }
return true; RegCloseKey(hKey);
return result == ERROR_SUCCESS;
} }
template<typename T> template<typename T>

View file

@ -3,7 +3,7 @@
class Registry class Registry
{ {
public: public:
inline static std::string RootDirectoryPath; inline static std::filesystem::path RootDirectoryPath;
static void Load(); static void Load();
static void Save(); static void Save();