-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegCtrl.cpp
More file actions
145 lines (120 loc) · 3.97 KB
/
RegCtrl.cpp
File metadata and controls
145 lines (120 loc) · 3.97 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "RegCtrl.h"
CRegCtrl RegCtrl;
bool CRegCtrl::SetRegistry(HKEY hKey, const std::string& subKey, const std::string& valueName, const std::string& data)
{
return SetRegistry(hKey,
StrCtrl.AnsiStringToWideString(subKey),
StrCtrl.AnsiStringToWideString(valueName),
StrCtrl.AnsiStringToWideString(data)
);
}
bool CRegCtrl::SetRegistry(HKEY hKey, const std::wstring& subKey, const std::wstring& valueName, const std::wstring& data)
{
HKEY resultKey;
if (!OpenRegistryKey(hKey, subKey, KEY_ALL_ACCESS, resultKey))
{
DWORD disposition; // This will store the result of the operation (KEY_CREATED_NEW_KEY or KEY_OPENED_EXISTING_KEY).
if (RegCreateKeyExW(hKey, subKey.c_str(), 0, nullptr, 0, KEY_ALL_ACCESS, nullptr, &resultKey, &disposition) == ERROR_SUCCESS)
{
RegCloseKey(resultKey); // 일단 핸들 닫고 다시 연다.
OpenRegistryKey(hKey, subKey, KEY_ALL_ACCESS, resultKey);
}
else
{
// Failed to create the key.
return false;
}
}
DWORD dataSize = static_cast<DWORD>(data.size() * sizeof(wchar_t));
bool result = SetValue(resultKey, valueName, data, dataSize);
CloseRegistryKey(resultKey);
return result;
}
bool CRegCtrl::GetRegistry(HKEY hKey, const std::string& subKey, const std::string& valueName, std::string& data)
{
std::wstring wstrTemp;
auto ret = GetRegistry(hKey,
StrCtrl.AnsiStringToWideString(subKey),
StrCtrl.AnsiStringToWideString(valueName),
wstrTemp
);
data = StrCtrl.WideStringToAnsiString(wstrTemp);
return ret;
}
bool CRegCtrl::GetRegistry(HKEY hKey, const std::wstring& subKey, const std::wstring& valueName, std::wstring& data)
{
HKEY resultKey;
if (!OpenRegistryKey(hKey, subKey, KEY_READ, resultKey))
{
return false;
}
DWORD dataSize = 0;
bool result = QueryValue(resultKey, valueName, data, dataSize);
if (!result)
{
CloseRegistryKey(resultKey);
return false;
}
CloseRegistryKey(resultKey);
return true;
}
bool CRegCtrl::DeleteRegistry(HKEY hKey, const std::string& subKey, const std::string& valueName)
{
return DeleteRegistry(hKey,
StrCtrl.AnsiStringToWideString(subKey),
StrCtrl.AnsiStringToWideString(valueName)
);
}
bool CRegCtrl::DeleteRegistry(HKEY hKey, const std::wstring& subKey, const std::wstring& valueName)
{
HKEY resultKey;
if (!OpenRegistryKey(hKey, subKey, KEY_ALL_ACCESS, resultKey))
{
return false;
}
bool result = RegDeleteValueW(resultKey, valueName.c_str()) == ERROR_SUCCESS;
CloseRegistryKey(resultKey);
return result;
}
bool CRegCtrl::OpenRegistryKey(HKEY hKey, const std::wstring& subKey, REGSAM access, HKEY& resultKey)
{
if (RegOpenKeyExW(hKey, subKey.c_str(), 0, access, &resultKey) != ERROR_SUCCESS)
{
return false;
}
return true;
}
bool CRegCtrl::QueryValue(HKEY resultKey, const std::wstring& valueName, std::wstring& data, DWORD& dataSize)
{
DWORD type;
bool result = RegQueryValueExW(resultKey, valueName.c_str(), 0, &type, nullptr, &dataSize) == ERROR_SUCCESS;
if (!result)
{
return false;
}
if (type != REG_SZ && type != REG_EXPAND_SZ)
{
return false;
}
data.resize(dataSize / sizeof(wchar_t));
result = RegQueryValueExW(resultKey, valueName.c_str(), 0, &type, reinterpret_cast<BYTE*>(&data[0]), &dataSize) == ERROR_SUCCESS;
if (!result)
{
return false;
}
return true;
}
bool CRegCtrl::SetValue(HKEY resultKey, const std::wstring& valueName, const std::wstring& data, DWORD dataSize)
{
const BYTE* pData = reinterpret_cast<const BYTE*>(data.c_str());
bool result = RegSetValueExW(resultKey, valueName.c_str(), 0, REG_SZ, pData, dataSize) == ERROR_SUCCESS;
return result;
}
bool CRegCtrl::CloseRegistryKey(HKEY hKey)
{
if (RegCloseKey(hKey) != ERROR_SUCCESS)
{
return false;
}
return true;
}