-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathRegistryKey.cpp
55 lines (43 loc) · 1.56 KB
/
RegistryKey.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
#include "RegistryKey.h"
#include "exceptions.h"
std::optional<DWORD> RegistryKey::TryGetDWORD(HKEY section, const std::wstring& subSectionName, const std::wstring& valueName, DWORD flags)
{
DWORD dwData = 0;
DWORD cbData = sizeof(dwData);
if (!CheckReturnValue(RegGetValue(section, subSectionName.c_str(), valueName.c_str(), RRF_RT_REG_DWORD | flags, nullptr, reinterpret_cast<LPBYTE>(&dwData), &cbData)))
{
return std::nullopt;
}
return dwData;
}
std::optional<std::wstring> RegistryKey::TryGetString(HKEY section, const std::wstring& subSectionName, const std::wstring& valueName)
{
DWORD cbData{};
if (!CheckReturnValue(RegGetValue(section, subSectionName.c_str(), valueName.c_str(), RRF_RT_REG_SZ, nullptr, nullptr, &cbData)))
{
return std::nullopt;
}
std::wstring data;
data.resize(cbData / sizeof(wchar_t));
if (!CheckReturnValue(RegGetValue(section, subSectionName.c_str(), valueName.c_str(), RRF_RT_REG_SZ, nullptr, data.data(), &cbData)))
{
return std::nullopt;
}
data.resize(cbData / sizeof(wchar_t) - 1);
return data;
}
bool RegistryKey::CheckReturnValue(int errorCode)
{
if (errorCode == NO_ERROR)
{
return true;
}
// NotFound result is expected, don't spam logs with failures
if (errorCode != ERROR_FILE_NOT_FOUND)
{
LOG_IF_FAILED(HRESULT_FROM_WIN32(errorCode));
}
return false;
}