-
Notifications
You must be signed in to change notification settings - Fork 1
/
regkey.h
243 lines (210 loc) · 6.1 KB
/
regkey.h
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
** ×¢²á±íÏà¹Ø
** author
** taoabc@gmail.com
*/
#ifndef ULT_REGKEY_H_
#define ULT_REGKEY_H_
#include <string>
#include <vector>
#include <cassert>
#include <Windows.h>
namespace ult {
class RegKey {
public:
static bool ToParentAndSub(const std::wstring& key, HKEY* parent, std::wstring* sub) {
int count = sizeof(key_map_) / sizeof (key_map_[0]);
bool result = false;
for (int i = 0; i < count; ++i) {
if (0 == key.find(key_map_[i].fullname) || 0 == key.find(key_map_[i].shortname)) {
*parent = key_map_[i].hkey;
size_t pos = key.find(L'\\');
if (pos != std::wstring::npos) {
*sub = key.substr(pos+1);
}
result = true;
break;
}
}
return result;
}
RegKey(void) :
hkey_(NULL) {
}
~RegKey(void) {
Close();
}
void Attach(HKEY hkey) {
Close();
hkey_ = hkey;
}
HKEY Detach(void) {
HKEY t = hkey_;
hkey_ = NULL;
return t;
}
operator HKEY(void) const {
return hkey_;
}
bool IsOpened(void) const {
return hkey_ != NULL;
}
bool Open(HKEY hkey, const std::wstring& subkey, REGSAM desired = KEY_READ) {
if (!Close()) {
return false;
}
HKEY temp;
if (ERROR_SUCCESS != ::RegOpenKeyEx(hkey, subkey.c_str(), 0, desired, &temp)) {
return false;
}
hkey_ = temp;
return true;
}
bool Open(const std::wstring& key, REGSAM desired = KEY_READ) {
HKEY hkey;
std::wstring subkey;
if (!ToParentAndSub(key, &hkey, &subkey)) {
return false;
}
return Open(hkey, subkey, desired);
}
bool Close(void) {
if (hkey_ == NULL) {
return true;
}
bool result = (ERROR_SUCCESS == ::RegCloseKey(hkey_));
hkey_ = NULL;
return result;
}
bool Create(HKEY hkey, const std::wstring& subkey, DWORD options = REG_OPTION_NON_VOLATILE,
DWORD desired = KEY_READ | KEY_WRITE, LPDWORD disposition = NULL) {
HKEY temp;
if (ERROR_SUCCESS != ::RegCreateKeyEx(hkey, subkey.c_str(), 0, NULL, options, desired,
NULL, &temp, disposition)) {
return false;
}
hkey_ = temp;
return true;
}
bool Create(const std::wstring& key, DWORD options = REG_OPTION_NON_VOLATILE,
DWORD desired = KEY_READ | KEY_WRITE, LPDWORD disposition = NULL) {
HKEY hkey;
std::wstring subkey;
if (!ToParentAndSub(key, &hkey, &subkey)) {
return false;
}
return Create(hkey, subkey, options, desired, disposition);
}
bool DeleteValue(const std::wstring& value) {
assert(hkey_ != NULL);
return ERROR_SUCCESS == ::RegDeleteValue(hkey_, value.c_str());
}
bool DeleteSubKey(const std::wstring& subkey) {
assert(hkey_ != NULL);
return ERROR_SUCCESS == ::RegDeleteKey(hkey_, subkey.c_str());
}
bool RecurseDeleteKey(const std::wstring& key) {
assert(hkey_ != NULL);
ult::RegKey regkey;
if (!regkey.Open(hkey_, key, KEY_READ | KEY_WRITE)) {
return false;
}
std::wstring name;
while (regkey.EnumKey(0, &name)) {
if (!regkey.RecurseDeleteKey(name)) {
return false;
}
}
return DeleteSubKey(key);
}
bool Flush(void) {
assert(hkey_ != NULL);
return ERROR_SUCCESS == ::RegFlushKey(hkey_);
}
bool EnumKey(DWORD index, std::wstring* name, std::wstring* defined_class = NULL,
PFILETIME writetime = NULL) {
assert(hkey_ != NULL);
wchar_t buf_name[MAX_PATH];
DWORD buf_name_len = MAX_PATH;
wchar_t buf_class[MAX_PATH];
DWORD buf_class_len = MAX_PATH;
if (ERROR_SUCCESS != ::RegEnumKeyEx(hkey_, index, buf_name, &buf_name_len, NULL,
buf_class, &buf_class_len, writetime)) {
return false;
}
name->assign(buf_name, buf_name_len);
if (defined_class != NULL) {
defined_class->assign(buf_class, buf_class_len);
}
return true;
}
bool GetValue(const std::wstring& field, LPVOID data = NULL, LPDWORD data_len = NULL, LPDWORD type = NULL) {
assert(hkey_ != NULL);
LONG ret = ::RegQueryValueEx(hkey_, field.c_str(), NULL, type, (LPBYTE)data, data_len);
return ret == ERROR_SUCCESS;
}
bool SetValue(const std::wstring& field, DWORD type, LPCVOID data, DWORD data_len) {
assert(hkey_ != NULL);
LONG ret = ::RegSetValueEx(hkey_, field.c_str(), 0, type, (const BYTE*)data, data_len);
return ret == ERROR_SUCCESS;
}
DWORD GetDwordValue(const std::wstring& field) {
DWORD type;
DWORD data;
DWORD len = sizeof (data);
if (!GetValue(field, &data, &len, &type)) {
return (DWORD)-1;
}
if (type != REG_DWORD) {
return (DWORD)-1;
}
return data;
}
bool SetDwordValue(const std::wstring& field, DWORD value) {
DWORD len = sizeof (value);
if (!SetValue(field, REG_DWORD, &value, len)) {
return false;
}
return true;
}
std::wstring GetStringValue(const std::wstring& field) {
std::wstring result;
do {
DWORD type;
std::vector<char> buffer(2048);
DWORD len = buffer.capacity();
LONG ret = ::RegQueryValueEx(hkey_, field.c_str(), NULL, &type, (LPBYTE)buffer.data(), &len);
if (ret == ERROR_MORE_DATA) {
buffer.reserve(len);
ret = ::RegQueryValueEx(hkey_, field.c_str(), NULL, &type, (LPBYTE)buffer.data(), &len);
}
if (ret != ERROR_SUCCESS || type != REG_SZ) break;
result.assign((wchar_t*)buffer.data(), len/sizeof(wchar_t));
} while (false);
return result;
}
bool SetStringValue(const std::wstring& field, const std::wstring& value) {
DWORD len = value.length() * sizeof (wchar_t);
if (!SetValue(field, REG_SZ, value.c_str(), len)) {
return false;
}
return true;
}
private:
struct ParentKeyMap {
LPCWSTR fullname;
LPCWSTR shortname;
HKEY hkey;
};
static const ParentKeyMap key_map_[5];
HKEY hkey_;
}; // class RegKey
__declspec(selectany) const RegKey::ParentKeyMap RegKey::key_map_[5] = {
{L"HKEY_CLASSES_ROOT", L"HKCR", HKEY_CLASSES_ROOT},
{L"HKEY_CURRENT_CONFIG", L"HKCC", HKEY_CURRENT_CONFIG},
{L"HKEY_CURRENT_USER", L"HKCU", HKEY_CURRENT_USER},
{L"HKEY_LOCAL_MACHINE", L"HKLM", HKEY_LOCAL_MACHINE},
{L"HKEY_USERS", L"HKU", HKEY_USERS},
};
} //namespace ult
#endif