27 if (status != ERROR_SUCCESS)
30 OPENVPN_THROW(E,
"registry key " << key <<
" error: " << err.message());
59 Key(HKEY key,
const std::wstring &subkey,
bool create =
false)
64 error = ::RegCreateKeyExW(key,
76 error = ::RegOpenKeyExW(key,
84 key_ =
static_cast<HKEY
>(INVALID_HANDLE_VALUE);
87 Key(
Key &key,
const std::wstring &subkey,
bool create =
false)
88 :
Key(key(), subkey, create)
99 Key(
const std::wstring &subkey,
bool create =
false)
100 :
Key(HKEY_LOCAL_MACHINE, subkey, create)
111 key_ =
static_cast<HKEY
>(INVALID_HANDLE_VALUE);
113 std::swap(
key_, rhs.key_);
117 Key copy{std::move(rhs)};
118 std::swap(copy.key_, this->key_);
138 return key_ != INVALID_HANDLE_VALUE;
161 HKEY
key_ =
static_cast<HKEY
>(INVALID_HANDLE_VALUE);
186 status = ::RegQueryInfoKeyA(key(),
199 if (status != ERROR_SUCCESS)
202 constexpr int MAX_KEY_LENGTH = 255;
203 for (DWORD i = 0; i < subkeys_num; ++i)
205 DWORD subkey_size = MAX_KEY_LENGTH;
206 WCHAR subkey[MAX_KEY_LENGTH];
207 status = ::RegEnumKeyExW(key(),
215 if (status == ERROR_SUCCESS)
224 static constexpr PCWSTR
gpol_nrpt_subkey = LR
"(SOFTWARE\Policies\Microsoft\Windows NT\DNSClient\DnsPolicyConfig)";
225 static constexpr PCWSTR
local_nrpt_subkey = LR
"(SYSTEM\CurrentControlSet\Services\Dnscache\Parameters\DnsPolicyConfig)";
230 static constexpr WCHAR
subkey_ipv4_itfs[] = LR
"(SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces)";
231 static constexpr WCHAR
subkey_ipv6_itfs[] = LR
"(SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters\Interfaces)";
246 DWORD size =
sizeof(value);
247 PBYTE data =
reinterpret_cast<PBYTE
>(&value);
250 err = ::RegGetValueW(key(),
nullptr, name, RRF_RT_REG_DWORD, &type, data, &size);
255 else if (type != REG_DWORD)
257 return {0, ERROR_DATATYPE_MISMATCH};
277 err = ::RegGetValueW(key(),
nullptr, name, RRF_RT_REG_SZ, &type,
nullptr, &size);
282 else if (type != REG_SZ)
284 return {{}, ERROR_DATATYPE_MISMATCH};
287 std::wstring
str(size /
sizeof(std::wstring::value_type) + 1,
'\0');
288 PBYTE data =
reinterpret_cast<PBYTE
>(
str.data());
289 err = ::RegGetValueW(key(),
nullptr, name, RRF_RT_REG_SZ,
nullptr, data, &size);
295 str.resize(::wcslen(
str.c_str()));
314 err = ::RegGetValueW(key(),
nullptr, name, RRF_RT_REG_BINARY, &type,
nullptr, &size);
319 else if (type != REG_BINARY)
321 return {{}, ERROR_DATATYPE_MISMATCH};
324 std::string
str(size,
'\0');
325 PBYTE data =
reinterpret_cast<PBYTE
>(
str.data());
326 err = ::RegGetValueW(key(),
nullptr, name, RRF_RT_REG_BINARY,
nullptr, data, &size);
345 const BYTE *bytes =
reinterpret_cast<const BYTE *
>(&value);
346 DWORD size =
static_cast<DWORD
>(
sizeof(value));
347 return ::RegSetValueExW(key(), name, 0, REG_DWORD, bytes, size);
360 const BYTE *bytes =
reinterpret_cast<const BYTE *
>(value.c_str());
361 DWORD size =
static_cast<DWORD
>((value.size() + 1) *
sizeof(std::wstring::value_type));
362 return ::RegSetValueExW(key(), name, 0, REG_SZ, bytes, size);
380 const BYTE *bytes =
reinterpret_cast<const BYTE *
>(value.data());
381 DWORD size =
static_cast<DWORD
>(value.size() *
sizeof(std::wstring::value_type));
382 return ::RegSetValueExW(key(), name, 0, REG_MULTI_SZ, bytes, size);
396 return ::RegDeleteTreeW(HKEY_LOCAL_MACHINE, subkey.c_str());
408 return ::RegDeleteValueW(key(), name);
Wrapper for Registry subkey enumeration.
KeyEnumerator(Key &key)
Construct a new Key Enumerator object.
Wrapper class for a Registry key handle.
HKEY operator()()
Return the Registry key handle.
bool defined() const
Check for a valid key handle.
Key(Key &key, const std::wstring &subkey, bool create=false)
Key & operator=(Key &&rhs)
Key(HKEY key, const std::wstring &subkey, bool create=false)
Construct a Key with an open handle for a subkey under key.
Key(const std::wstring &subkey, bool create=false)
Construct a Key with an open handle for a subkey under HKLM.
Key & operator=(const Key &)=delete
PHKEY ref()
Retrun a pointer to the Registry key handle.
#define OPENVPN_THROW(exc, stuff)
static void check_reg_error(DWORD status, const std::string &key)
Abstraction of Windows Registry operations.
static constexpr WCHAR subkey_ipv6_itfs[]
static LSTATUS delete_subkey(const std::wstring &subkey)
Delete a subkey from the Registry.
static LSTATUS delete_value(Key &key, PCWSTR name)
Delete a value from the Registry.
static constexpr PCWSTR gpol_nrpt_subkey
static LSTATUS set_string(Key &key, PCWSTR name, const std::wstring &value)
Set a REG_SZ value in the Registy.
static std::pair< std::string, LSTATUS > get_binary(Key &key, PCWSTR name)
Read a REG_BINARY value from the Windows registry.
static constexpr PCWSTR local_nrpt_subkey
static constexpr WCHAR subkey_ipv4_itfs[]
static std::pair< std::wstring, LSTATUS > get_string(Key &key, PCWSTR name)
Read a REG_SZ value from the Windows registry.
static LSTATUS set_multi_string(Key &key, PCWSTR name, const std::wstring &value)
Set a REG_MULTI_SZ value in the Registry.
static std::pair< DWORD, LSTATUS > get_dword(Key &key, PCWSTR name)
Read a REG_DWORD value from the Windows registry.
static LSTATUS set_dword(Key &key, PCWSTR name, DWORD value)
Set a DWORD value in the Registry.
os<< "Session Name: "<< tbc-> session_name<< '\n';os<< "Layer: "<< tbc-> layer str()<< '\n'
server addresses push_back({address, port})