-
Notifications
You must be signed in to change notification settings - Fork 0
/
dkstd_registry.hpp
397 lines (330 loc) · 13.3 KB
/
dkstd_registry.hpp
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// author: Khiêm Đoàn Hoà (KhiemDH)
// github: https://github.com/khiemdoan/dkstd
// created: 2017-04-26
// modified: 2019-08-03
#pragma once
#ifdef _WIN32
#include "windows.h"
#pragma comment (lib, "Advapi32.lib")
#include "string"
#include "vector"
#include "memory"
namespace dkstd
{
namespace registry
{
/* HKEY_CURRENT_USER */
bool Read_HKCU_Key(std::wstring sKey, std::wstring sValue, DWORD & dwNumber);
bool Set_HKCU_Key(std::wstring sKey, std::wstring sValue, const DWORD & dwNumber);
bool Read_HKCU_Key(std::wstring sKey, std::wstring sValue, std::wstring & sData);
bool Set_HKCU_Key(std::wstring sKey, std::wstring sValue, const std::wstring & sData);
bool Read_HKCU_Key(std::wstring sKey, std::wstring sValue, DWORD dwType, std::vector<uint8_t> & data);
bool Set_HKCU_Key(std::wstring sKey, std::wstring sValue, DWORD dwType, const std::vector<uint8_t> & data);
std::vector<std::wstring> EnumKey_HKCU_Key(std::wstring sKey);
std::vector<std::wstring> EnumValue_HKCU_Key(std::wstring sKey);
/* HKEY_LOCAL_MACHINE */
bool Read_HKLM_Key(std::wstring sKey, std::wstring sValue, DWORD & dwNumber);
bool Set_HKLM_Key(std::wstring sKey, std::wstring sValue, const DWORD & dwNumber);
bool Read_HKLM_Key(std::wstring sKey, std::wstring sValue, std::wstring & sData);
bool Set_HKLM_Key(std::wstring sKey, std::wstring sValue, const std::wstring & sData);
bool Read_HKLM_Key(std::wstring sKey, std::wstring sValue, DWORD dwType, std::vector<uint8_t> & data);
bool Set_HKLM_Key(std::wstring sKey, std::wstring sValue, DWORD dwType, const std::vector<uint8_t> & data);
std::vector<std::wstring> EnumKey_HKLM_Key(std::wstring sKey);
std::vector<std::wstring> EnumValue_HKLM_Key(std::wstring sKey);
/* Any key */
bool ReadKey(HKEY hRootKey, std::wstring sKey, std::wstring sValue, DWORD dwType, std::vector<uint8_t> & data);
bool SetKey(HKEY hRootKey, std::wstring sKey, std::wstring sValue, DWORD dwType, const std::vector<uint8_t> & data);
std::vector<std::wstring> EnumKey(HKEY hRootKey, std::wstring sKey);
std::vector<std::wstring> EnumValue(HKEY hRootKey, std::wstring sKey);
class registry_exception : public std::exception {};
};
}
// KhiemDH - 2017-04-26
inline bool dkstd::registry::Read_HKCU_Key(std::wstring sKey, std::wstring sValue, DWORD & dwNumber)
{
std::vector<uint8_t> buffer;
if (dkstd::registry::Read_HKCU_Key(sKey, sValue, REG_DWORD, buffer))
{
if (buffer.size() == sizeof(DWORD))
{
dwNumber = *((DWORD *)buffer.data());
return true;
}
}
return false;
}
// KhiemDH - 2017-04-26
inline bool dkstd::registry::Set_HKCU_Key(std::wstring sKey, std::wstring sValue, const DWORD & dwNumber)
{
std::vector<uint8_t> buffer;
buffer.resize(sizeof(DWORD));
std::memcpy(buffer.data(), &dwNumber, sizeof(DWORD));
return dkstd::registry::Set_HKCU_Key(sKey, sValue, REG_DWORD, buffer);
}
// KhiemDH - 2017-04-26
inline bool dkstd::registry::Read_HKCU_Key(std::wstring sKey, std::wstring sValue, std::wstring & sData)
{
std::vector<uint8_t> buffer;
if (dkstd::registry::Read_HKCU_Key(sKey, sValue, REG_SZ, buffer))
{
sData = (wchar_t *)buffer.data();
return true;
}
return false;
}
// KhiemDH - 2017-04-26
inline bool dkstd::registry::Set_HKCU_Key(std::wstring sKey, std::wstring sValue, const std::wstring & sData)
{
std::vector<uint8_t> buffer;
size_t size = sData.length() * sizeof(wchar_t);
buffer.resize(size);
std::memcpy(buffer.data(), sData.data(), size);
return dkstd::registry::Set_HKCU_Key(sKey, sValue, REG_SZ, buffer);
}
// KhiemDH - 2017-04-26
// Đọc các key trong HKEY_CURRENT_USER
inline bool dkstd::registry::Read_HKCU_Key(std::wstring sKey, std::wstring sValue, DWORD dwType, std::vector<uint8_t> & data)
{
return dkstd::registry::ReadKey(HKEY_CURRENT_USER, sKey, sValue, dwType, data);
}
// KhiemDH - 2017-04-26
// Gán giá trị cho các key trong HKEY_CURRENT_USER
inline bool dkstd::registry::Set_HKCU_Key(std::wstring sKey, std::wstring sValue, DWORD dwType, const std::vector<uint8_t> & data)
{
return dkstd::registry::SetKey(HKEY_CURRENT_USER, sKey, sValue, dwType, data);
}
// KhiemDH - 2019-08-03
inline std::vector<std::wstring> dkstd::registry::EnumKey_HKCU_Key(std::wstring sKey)
{
return EnumKey(HKEY_CURRENT_USER, sKey);
}
// KhiemDH - 2019-08-03
inline std::vector<std::wstring> dkstd::registry::EnumValue_HKCU_Key(std::wstring sKey)
{
return EnumValue(HKEY_CURRENT_USER, sKey);
}
// KhiemDH - 2017-04-26
inline bool dkstd::registry::Read_HKLM_Key(std::wstring sKey, std::wstring sValue, DWORD & dwNumber)
{
std::vector<uint8_t> buffer;
if (dkstd::registry::Read_HKLM_Key(sKey, sValue, REG_DWORD, buffer))
{
if (buffer.size() == sizeof(DWORD))
{
dwNumber = *((DWORD *)buffer.data());
return true;
}
}
return false;
}
// KhiemDH - 2017-04-26
inline bool dkstd::registry::Set_HKLM_Key(std::wstring sKey, std::wstring sValue, const DWORD & dwNumber)
{
std::vector<uint8_t> buffer;
buffer.resize(sizeof(DWORD));
std::memcpy(buffer.data(), &dwNumber, sizeof(DWORD));
return dkstd::registry::Set_HKLM_Key(sKey, sValue, REG_DWORD, buffer);
}
// KhiemDH - 2017-04-26
inline bool dkstd::registry::Read_HKLM_Key(std::wstring sKey, std::wstring sValue, std::wstring & sData)
{
std::vector<uint8_t> buffer;
if (dkstd::registry::Read_HKLM_Key(sKey, sValue, REG_SZ, buffer))
{
sData = (wchar_t *)buffer.data();
return true;
}
return false;
}
// KhiemDH - 2017-04-26
inline bool dkstd::registry::Set_HKLM_Key(std::wstring sKey, std::wstring sValue, const std::wstring & sData)
{
std::vector<uint8_t> buffer;
size_t size = sData.length() * sizeof(wchar_t);
buffer.resize(size);
std::memcpy(buffer.data(), sData.data(), size);
return dkstd::registry::Set_HKLM_Key(sKey, sValue, REG_SZ, buffer);
}
// KhiemDH - 2017-04-26
// Đọc các key trong HKEY_LOCAL_MACHINE
inline bool dkstd::registry::Read_HKLM_Key(std::wstring sKey, std::wstring sValue, DWORD dwType, std::vector<uint8_t> & data)
{
return dkstd::registry::ReadKey(HKEY_LOCAL_MACHINE, sKey, sValue, dwType, data);
}
// KhiemDH - 2017-04-26
// Gán giá trị cho các key trong HKEY_LOCAL_MACHINE
inline bool dkstd::registry::Set_HKLM_Key(std::wstring sKey, std::wstring sValue, DWORD dwType, const std::vector<uint8_t> & data)
{
return dkstd::registry::SetKey(HKEY_LOCAL_MACHINE, sKey, sValue, dwType, data);
}
// KhiemDH - 2019-08-03
inline std::vector<std::wstring> dkstd::registry::EnumKey_HKLM_Key(std::wstring sKey)
{
return EnumKey(HKEY_LOCAL_MACHINE, sKey);
}
// KhiemDH - 2019-08-03
inline std::vector<std::wstring> dkstd::registry::EnumValue_HKLM_Key(std::wstring sKey)
{
return EnumValue(HKEY_LOCAL_MACHINE, sKey);
}
// KhiemDH - 2018-07-27
// Đọc giá trị trong Registry
inline bool dkstd::registry::ReadKey(HKEY hRootKey, std::wstring sKey, std::wstring sValue, DWORD dwType, std::vector<uint8_t> & data)
{
HKEY hKey = NULL;
LONG lCreate = ERROR_SUCCESS;
LONG lQuery = ERROR_SUCCESS;
DWORD dwRegOpened = NULL;
DWORD dwSize = 0;
bool bReturn = false;
std::unique_ptr<uint8_t[]> buffer;
try
{
lCreate = RegCreateKeyExW(hRootKey, sKey.c_str(), NULL, NULL, REG_OPTION_NON_VOLATILE, KEY_READ, NULL, &hKey, &dwRegOpened);
if (lCreate != ERROR_SUCCESS)
throw registry_exception();
lQuery = RegQueryValueExW(hKey, sValue.c_str(), NULL, &dwType, nullptr, &dwSize);
if (lQuery != ERROR_SUCCESS)
throw registry_exception();
buffer = std::unique_ptr<uint8_t[]>(new uint8_t[dwSize]{ 0 });
lQuery = RegQueryValueExW(hKey, sValue.c_str(), NULL, &dwType, buffer.get(), &dwSize);
if (lQuery != ERROR_SUCCESS)
throw registry_exception();
data.resize(dwSize);
std::copy(buffer.get(), buffer.get() + dwSize, data.begin());
bReturn = true;
}
catch (registry_exception e) {}
if (lCreate == ERROR_SUCCESS && hKey != NULL)
{
RegCloseKey(hKey);
hKey = NULL;
}
return bReturn;
}
// KhiemDH - 2018-07-27
// Gán giá trị trong Registry
// Nếu key chưa tồn tại thì tạo mới
inline bool dkstd::registry::SetKey(HKEY hRootKey, std::wstring sKey, std::wstring sValue, DWORD dwType, const std::vector<uint8_t> & data)
{
HKEY hKey = NULL;
LONG lCreate = ERROR_SUCCESS;
LONG lQuery = ERROR_SUCCESS;
DWORD dwRegOpened = NULL;
DWORD dwSize = data.size();
bool bReturn = false;
try
{
lCreate = RegCreateKeyExW(hRootKey, sKey.c_str(), NULL, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, &dwRegOpened);
if (lCreate != ERROR_SUCCESS)
throw registry_exception();
lQuery = RegSetValueExW(hKey, sValue.c_str(), NULL, dwType, dwSize ? &data[0] : nullptr, dwSize);
if (lQuery != ERROR_SUCCESS)
throw registry_exception();
bReturn = true;
}
catch (registry_exception e) {}
if (lCreate == ERROR_SUCCESS && hKey != NULL)
{
RegCloseKey(hKey);
hKey = NULL;
}
return bReturn;
}
// KhiemDH - 2019-08-03
inline std::vector<std::wstring> dkstd::registry::EnumKey(HKEY hRootKey, std::wstring sKey)
{
DWORD cSubKeys = 0; // number of subkeys
DWORD cbMaxSubKey = 0; // longest subkey size
HKEY hKey;
LONG lCreate = ERROR_SUCCESS;
LONG lQuery = ERROR_SUCCESS;
std::vector<std::wstring> keys;
try
{
lCreate = RegOpenKeyExW(hRootKey, sKey.c_str(), 0, KEY_READ, &hKey);
if (lCreate != ERROR_SUCCESS)
throw registry_exception();
lQuery = RegQueryInfoKeyW(
hKey, // key handle
NULL, // buffer for class name
NULL, // size of class string
NULL, // reserved
&cSubKeys, // number of subkeys
&cbMaxSubKey, // longest subkey size
NULL, // longest class string
NULL, // number of values for this key
NULL, // longest value name
NULL, // longest value data
NULL, // security descriptor
NULL); // last write time
if (lQuery != ERROR_SUCCESS)
throw registry_exception();
std::vector<wchar_t> achKey; // buffer for subkey name
achKey.resize(cbMaxSubKey + 1);
for (DWORD i = 0; i < cSubKeys; i++)
{
DWORD cbName = cbMaxSubKey + 1; // size of name string
lQuery = RegEnumKeyW(hKey, i, achKey.data(), cbName);
if (lQuery != ERROR_SUCCESS)
continue;
keys.emplace_back(achKey.data());
}
}
catch (registry_exception) {}
if (lCreate == ERROR_SUCCESS && hKey != NULL)
{
RegCloseKey(hKey);
hKey = NULL;
}
return keys;
}
// KhiemDH - 2019-08-03
inline std::vector<std::wstring> dkstd::registry::EnumValue(HKEY hRootKey, std::wstring sKey)
{
DWORD cValues = 0; // number of values for this key
DWORD cchMaxValue = 0; // longest value name
HKEY hKey;
LONG lCreate = ERROR_SUCCESS;
LONG lQuery = ERROR_SUCCESS;
std::vector<std::wstring> values;
try
{
lCreate = RegOpenKeyExW(hRootKey, sKey.c_str(), 0, KEY_READ, &hKey);
if (lCreate != ERROR_SUCCESS)
throw registry_exception();
lQuery = RegQueryInfoKeyW(
hKey, // key handle
NULL, // buffer for class name
NULL, // size of class string
NULL, // reserved
NULL, // number of subkeys
NULL, // longest subkey size
NULL, // longest class string
&cValues, // number of values for this key
&cchMaxValue, // longest value name
NULL, // longest value data
NULL, // security descriptor
NULL); // last write time
if (lQuery != ERROR_SUCCESS)
throw registry_exception();
std::vector<wchar_t> achKey; // buffer for subkey name
achKey.resize(cchMaxValue + 1);
for (DWORD i = 0; i < cValues; i++)
{
DWORD cbName = cchMaxValue + 1; // size of name string
lQuery = RegEnumValueW(hKey, i, achKey.data(), &cbName, NULL, NULL, NULL, NULL);
if (lQuery != ERROR_SUCCESS)
continue;
values.emplace_back(achKey.data());
}
}
catch (registry_exception) {}
if (lCreate == ERROR_SUCCESS && hKey != NULL)
{
RegCloseKey(hKey);
hKey = NULL;
}
return values;
}
#endif