Initialversion
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
#include "RegistryConfig.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
DWORD ReadDword(HKEY key, const wchar_t* name, DWORD defaultValue)
|
||||
{
|
||||
DWORD value = defaultValue;
|
||||
DWORD size = sizeof(value);
|
||||
DWORD type = 0;
|
||||
if (RegQueryValueExW(key, name, nullptr, &type, reinterpret_cast<BYTE*>(&value), &size) != ERROR_SUCCESS ||
|
||||
type != REG_DWORD)
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
std::wstring ReadString(HKEY key, const wchar_t* name)
|
||||
{
|
||||
DWORD type = 0;
|
||||
DWORD size = 0;
|
||||
if (RegQueryValueExW(key, name, nullptr, &type, nullptr, &size) != ERROR_SUCCESS ||
|
||||
(type != REG_SZ && type != REG_EXPAND_SZ) || size == 0)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<wchar_t> buffer((size / sizeof(wchar_t)) + 1, L'\0');
|
||||
if (RegQueryValueExW(key, name, nullptr, &type, reinterpret_cast<BYTE*>(buffer.data()), &size) != ERROR_SUCCESS)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
std::wstring value(buffer.data());
|
||||
if (type == REG_EXPAND_SZ)
|
||||
{
|
||||
return hook::ExpandEnvironment(value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
namespace hook
|
||||
{
|
||||
std::wstring ExpandEnvironment(const std::wstring& value)
|
||||
{
|
||||
if (value.empty())
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
DWORD needed = ExpandEnvironmentStringsW(value.c_str(), nullptr, 0);
|
||||
if (needed == 0)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
std::vector<wchar_t> buffer(needed, L'\0');
|
||||
if (ExpandEnvironmentStringsW(value.c_str(), buffer.data(), needed) == 0)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
return std::wstring(buffer.data());
|
||||
}
|
||||
|
||||
HookConfig LoadConfig()
|
||||
{
|
||||
HookConfig config;
|
||||
|
||||
HKEY key = nullptr;
|
||||
if (RegOpenKeyExW(HKEY_CURRENT_USER, kConfigKey, 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS)
|
||||
{
|
||||
return config;
|
||||
}
|
||||
|
||||
config.enabled = ReadDword(key, L"Enabled", 1) != 0;
|
||||
config.scriptPath = ReadString(key, L"ScriptPath");
|
||||
config.arguments = ExpandEnvironment(ReadString(key, L"Arguments"));
|
||||
config.workingDirectory = ReadString(key, L"WorkingDirectory");
|
||||
config.startOnConnect = ReadDword(key, L"StartOnConnect", 1) != 0;
|
||||
config.stopOnDisconnect = ReadDword(key, L"StopOnDisconnect", 0) != 0;
|
||||
config.hidden = ReadDword(key, L"Hidden", 1) != 0;
|
||||
config.preventDuplicates = ReadDword(key, L"PreventDuplicates", 1) != 0;
|
||||
config.enableLogging = ReadDword(key, L"EnableLogging", 1) != 0;
|
||||
|
||||
RegCloseKey(key);
|
||||
return config;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user