forked from NTDLS/NSWFL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSWFL_Registry.Cpp
486 lines (369 loc) · 12.7 KB
/
NSWFL_Registry.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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright © NetworkDLS 2023, All rights reserved
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef _NSWFL_REGISTRY_CPP_
#define _NSWFL_REGISTRY_CPP_
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "NSWFL.H"
#include <tchar.h>
#ifdef _USE_GLOBAL_MEMPOOL
extern NSWFL::Memory::MemoryPool *pMem; //pMem must be defined and initalized elsewhere.
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace NSWFL {
namespace Registry {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using namespace NSWFL;
using namespace NSWFL::Registry;
// Deletes a value from a given subkey and root
bool DeleteRegistryValue(HKEY hKeyRoot, LPCTSTR pszSubKey, LPCTSTR pszValue)
{
HKEY hKey;
LONG lRes;
lRes = RegOpenKeyEx(hKeyRoot, pszSubKey, 0, KEY_SET_VALUE, &hKey);
if (lRes != ERROR_SUCCESS)
{
SetLastError((DWORD)lRes);
return false;
}
lRes = RegDeleteValue(hKey, pszValue);
RegCloseKey(hKey);
if (lRes == ERROR_SUCCESS)
return true;
SetLastError(lRes);
return false;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int CreateRegKeyStructure(HKEY hKey, const char *sPath)
{
char sDir[MAX_PATH];
int iNameSz = (int)strlen(sPath);
int iCount = 0;
int iPos = 0;
for (iPos = 0; iPos < iNameSz; iPos++)
{
if (sPath[iPos] == '\\' || sPath[iPos] == '/')
{
sDir[iPos] = '\0';
if (CreateRegistryKey(hKey, sDir))
{
iCount++;
}
}
sDir[iPos] = sPath[iPos];
}
sDir[iPos] = '\0';
if (NSWFL::Registry::CreateRegistryKey(hKey, sDir))
{
iCount++;
}
return iCount;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Creates a key specified by pszSubKey - you can't create
// keys directly under HKEY_LOCAL_MACHINE in Windows NT or 2000
// just for an extra bit of info.
bool CreateRegistryKey(HKEY hKeyRoot, LPCTSTR pszSubKey)
{
HKEY hKey;
DWORD dwFunc;
LONG lRet;
//------------------------------------------------------------------------------
SECURITY_DESCRIPTOR SD;
SECURITY_ATTRIBUTES SA;
if (!InitializeSecurityDescriptor(&SD, SECURITY_DESCRIPTOR_REVISION))
return false;
if (!SetSecurityDescriptorDacl(&SD, true, 0, false))
return false;
SA.nLength = sizeof(SA);
SA.lpSecurityDescriptor = &SD;
SA.bInheritHandle = false;
//------------------------------------------------------------------------------
lRet = RegCreateKeyEx(
hKeyRoot,
pszSubKey,
0,
(LPTSTR)NULL,
REG_OPTION_NON_VOLATILE,
KEY_WRITE,
&SA,
&hKey,
&dwFunc
);
if (lRet == ERROR_SUCCESS)
{
RegCloseKey(hKey);
hKey = (HKEY)NULL;
return true;
}
SetLastError((DWORD)lRet);
return false;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool DeleteRegistryKey(HKEY hKeyRoot, LPCTSTR pszSubKey)
{
DWORD dwRet = ERROR_SUCCESS;
// WinNT/2K will not allow you to delete keys which have
// subkeys/values inside them. MS's platform SDK tells you
// to use the SHDeleteKey function in shlwapi.dll. This dll
// is not available on NT platforms without IE 4.0 or later.
// Because of this I first attempt to delete the key in the
// hope that it is empty. If that is not possible I load shlwapi
// and call the function in that. This prevents the app bombing
// out if the dll can't be found.
if (RegDeleteKey(hKeyRoot, pszSubKey) != ERROR_SUCCESS)
{
HINSTANCE hLibInst = LoadLibrary(_T("shlwapi.dll"));
if (!hLibInst)
{
//throw ERROR_NO_SHLWAPI_DLL;
}
typedef DWORD(__stdcall* SHDELKEYPROC)(HKEY, LPCTSTR);
#if defined(UNICODE) || defined(_UNICODE)
SHDELKEYPROC DeleteKeyRecursive = (SHDELKEYPROC)GetProcAddress(hLibInst, "SHDeleteKeyW");
#else
SHDELKEYPROC DeleteKeyRecursive = (SHDELKEYPROC)GetProcAddress(hLibInst, "SHDeleteKeyA");
#endif
if (!DeleteKeyRecursive)
{
FreeLibrary(hLibInst);
return false;
}
dwRet = DeleteKeyRecursive(hKeyRoot, pszSubKey);
FreeLibrary(hLibInst);
}
if (dwRet == ERROR_SUCCESS)
return true;
SetLastError(dwRet);
return false;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Fetch a binary value. If the size specified by rdwSize is too small, rdwSize will
// be set to the correct size.
bool Get_BinaryRegistryValue(HKEY hKeyRoot, LPCTSTR pszSubKey, LPCTSTR pszValue, PVOID pBuffer, DWORD& rdwSize)
{
HKEY hKey;
DWORD dwType = REG_BINARY;
DWORD dwSize = rdwSize;
LONG lRes = 0;
lRes = RegOpenKeyEx(hKeyRoot, pszSubKey, 0, KEY_READ, &hKey);
if (lRes != ERROR_SUCCESS)
{
SetLastError((DWORD)lRes);
return false;
}
lRes = RegQueryValueEx(hKey, pszValue, 0, &dwType, (LPBYTE)pBuffer, &dwSize);
rdwSize = dwSize;
RegCloseKey(hKey);
if (lRes != ERROR_SUCCESS)
{
SetLastError(lRes);
return false;
}
if (dwType != REG_BINARY)
{
//throw ERROR_WRONG_TYPE;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Fetch a little endian DWORD from the registry
//(see platform SDK "Registry Value Types")
bool Get_DWORDRegistryValue(HKEY hKeyRoot, LPCTSTR pszSubKey, LPCTSTR pszValue, unsigned __int64 &u64Buff)
{
HKEY hKey;
DWORD dwType = REG_QWORD;
DWORD dwSize = sizeof(u64Buff);
LONG lRes;
u64Buff = 0;
lRes = RegOpenKeyEx(hKeyRoot, pszSubKey, 0, KEY_READ, &hKey);
if (lRes != ERROR_SUCCESS)
{
SetLastError(lRes);
return false;
}
lRes = RegQueryValueEx(hKey, pszValue, 0, &dwType, (LPBYTE)&u64Buff, &dwSize);
RegCloseKey(hKey);
if (dwType != REG_QWORD)
//throw ERROR_WRONG_TYPE;
if (lRes != ERROR_SUCCESS)
{
SetLastError(lRes);
return false;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Fetch a little endian DWORD from the registry
//(see platform SDK "Registry Value Types")
bool Get_DWORDRegistryValue(HKEY hKeyRoot, LPCTSTR pszSubKey, LPCTSTR pszValue, unsigned long &ulBuff)
{
HKEY hKey;
DWORD dwType = REG_DWORD;
DWORD dwSize = sizeof(ulBuff);
LONG lRes;
ulBuff = 0;
lRes = RegOpenKeyEx(hKeyRoot, pszSubKey, 0, KEY_READ, &hKey);
if (lRes != ERROR_SUCCESS)
{
SetLastError(lRes);
return false;
}
lRes = RegQueryValueEx(hKey, pszValue, 0, &dwType, (LPBYTE)&ulBuff, &dwSize);
RegCloseKey(hKey);
if (dwType != REG_DWORD)
//throw ERROR_WRONG_TYPE;
if (lRes != ERROR_SUCCESS)
{
SetLastError(lRes);
return false;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Retrieve a string value. If the given buffer for the string is too small (specified
// by rdwSize), rdwSize is increased to the correct value. If the buffer is bigger than
// the retrieved string, rdwSize is set to the length of the string (in bytes) including
// the terminating null.
bool Get_StringRegistryValue(HKEY hKeyRoot, LPCTSTR pszSubKey, LPCTSTR pszValue, LPTSTR pszBuffer, DWORD& rdwSize)
{
HKEY hKey;
LONG lRes;
DWORD dwType = KEY_ALL_ACCESS;
lRes = RegOpenKeyEx(hKeyRoot, pszSubKey, 0, KEY_READ, &hKey);
if (lRes != ERROR_SUCCESS)
{
RegCloseKey(hKey);
SetLastError(lRes);
return false;
}
lRes = RegQueryValueEx(hKey, pszValue, NULL, &dwType, (unsigned char*)pszBuffer, &rdwSize);
if (lRes != ERROR_SUCCESS)
{
RegCloseKey(hKey);
SetLastError(lRes);
return false;
}
lRes = RegCloseKey(hKey);
if (lRes != ERROR_SUCCESS)
{
RegCloseKey(hKey);
SetLastError(lRes);
return false;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Writes a binary value to the registry
bool Set_BinaryRegistryValue(HKEY hKeyRoot, LPCTSTR pszSubKey, LPCTSTR pszValue, PVOID pData, DWORD dwSize)
{
HKEY hKey;
LONG lRes = 0;
lRes = RegOpenKeyEx(hKeyRoot, pszSubKey, 0, KEY_WRITE, &hKey);
if (lRes != ERROR_SUCCESS)
{
SetLastError(lRes);
return false;
}
lRes = RegSetValueEx(hKey, pszValue, 0, REG_BINARY, (unsigned char*)pData, dwSize);
RegCloseKey(hKey);
if (lRes != ERROR_SUCCESS)
{
SetLastError(lRes);
return false;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Writes a DWORD value to the registry
bool Set_DWORDRegistryValue(HKEY hKeyRoot, LPCTSTR pszSubKey, LPCTSTR pszValue, unsigned long ulValue)
{
HKEY hKey;
LONG lRes;
lRes = RegOpenKeyEx(hKeyRoot, pszSubKey, 0, KEY_WRITE, &hKey);
if (lRes != ERROR_SUCCESS)
{
SetLastError(lRes);
return false;
}
lRes = RegSetValueEx(hKey, pszValue, 0, REG_DWORD, (unsigned char*)&ulValue, sizeof(ulValue));
RegCloseKey(hKey);
if (lRes != ERROR_SUCCESS)
{
SetLastError(lRes);
return false;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Writes a DWORD_PTR value to the registry
bool Set_DWORDRegistryValue(HKEY hKeyRoot, LPCTSTR pszSubKey, LPCTSTR pszValue, unsigned __int64 u64Value)
{
HKEY hKey;
LONG lRes;
lRes = RegOpenKeyEx(hKeyRoot, pszSubKey, 0, KEY_WRITE, &hKey);
if (lRes != ERROR_SUCCESS)
{
SetLastError(lRes);
return false;
}
lRes = RegSetValueEx(hKey, pszValue, 0, REG_QWORD, (LPBYTE)&u64Value, sizeof(u64Value));
RegCloseKey(hKey);
if (lRes != ERROR_SUCCESS)
{
SetLastError(lRes);
return false;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Writes a string to the registry.
bool Set_StringRegistryValue(HKEY hKeyRoot, LPCTSTR pszSubKey, LPCTSTR pszValue, LPCTSTR pszString)
{
HKEY hKey;
LONG lRes;
DWORD dwSize = lstrlen(pszString) * sizeof(TCHAR);
lRes = RegOpenKeyEx(hKeyRoot, pszSubKey, 0, KEY_WRITE, &hKey);
if (lRes != ERROR_SUCCESS)
{
SetLastError(lRes);
return false;
}
lRes = RegSetValueEx(hKey, pszValue, 0, REG_SZ, (unsigned char*)pszString, dwSize);
RegCloseKey(hKey);
if (lRes != ERROR_SUCCESS)
{
SetLastError(lRes);
return false;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Set_BOOLRegistryValue(HKEY hKeyRoot, LPCTSTR pszSubKey, LPCTSTR pszValue, bool bVal)
{
DWORD dwVal = 0;
if (bVal)
{
dwVal = 1;
}
return Set_DWORDRegistryValue(hKeyRoot, pszSubKey, pszValue, dwVal);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Get_BOOLRegistryValue(HKEY hKeyRoot, LPCTSTR pszSubKey, LPCTSTR pszValue)
{
DWORD dwVal = 0;
if (Get_DWORDRegistryValue(hKeyRoot, pszSubKey, pszValue, dwVal))
{
return (dwVal > 0);
}
return false;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
} //namespace::Registry
} //namespace::NSWFL
#endif