-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
MRegKey.hpp
863 lines (749 loc) · 27.4 KB
/
MRegKey.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
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
// MRegKey.hpp -- Win32API registry key manipulator -*- C++ -*-
// This file is part of MZC4. See file "ReadMe.txt" and "License.txt".
////////////////////////////////////////////////////////////////////////////
#ifndef MZC4_MREGKEY_HPP_
#define MZC4_MREGKEY_HPP_ 101 /* Version 101 */
#ifndef HKCR
#define HKCR HKEY_CLASSES_ROOT
#define HKCU HKEY_CURRENT_USER
#define HKLM HKEY_LOCAL_MACHINE
#define HKU HKEY_USERS
#define HKPD HKEY_PERFORMANCE_DATA
#define HKCC HKEY_CURRENT_CONFIG
#define HKDD HKEY_DYN_DATA
#endif
class MRegKey;
////////////////////////////////////////////////////////////////////////////
#ifndef _INC_WINDOWS
#include <windows.h> // Win32API
#endif
#include <cassert> // assert
#include <malloc.h> // _malloca/_freea
////////////////////////////////////////////////////////////////////////////
// NOTE: RegDeleteTreeDx deletes all value entries if pszSubKey == nullptr.
// NOTE: RegDeleteTreeDx cannot delete opening keys.
LONG RegDeleteTreeDx(HKEY hKey, LPCTSTR pszSubKey/* = nullptr*/) noexcept;
////////////////////////////////////////////////////////////////////////////
class MRegKey
{
public:
MRegKey() noexcept;
MRegKey(HKEY hKey) noexcept;
explicit MRegKey(MRegKey& key) noexcept;
MRegKey(HKEY hBaseKey, LPCTSTR pszSubKey, BOOL bCreate = FALSE) noexcept;
virtual ~MRegKey() noexcept;
operator HKEY() const noexcept;
bool operator!() const noexcept;
bool operator==(HKEY hKey) const noexcept;
bool operator!=(HKEY hKey) const noexcept;
MRegKey& operator=(HKEY hKey) noexcept;
MRegKey& operator=(MRegKey& key) noexcept;
BOOL Attach(HKEY hKey) noexcept;
HKEY Detach() noexcept;
HKEY Handle() const noexcept;
LONG RegCreateKeyEx(HKEY hBaseKey, LPCTSTR pszSubKey, DWORD dwReserved = 0,
LPTSTR lpClass = nullptr, DWORD dwOptions = 0,
REGSAM samDesired = KEY_ALL_ACCESS,
LPSECURITY_ATTRIBUTES lpsa = nullptr,
LPDWORD lpdwDisposition = nullptr) noexcept;
LONG RegOpenKeyEx(HKEY hBaseKey, LPCTSTR pszSubKey, DWORD dwOptions = 0,
REGSAM samDesired = KEY_READ) noexcept;
LONG RegConnectRegistry(LPCTSTR lpMachineName, HKEY hBaseKey) noexcept;
LONG RegCloseKey() noexcept;
LONG RegQueryValueEx(LPCTSTR pszValueName = nullptr,
LPDWORD lpReserved = nullptr, LPDWORD lpType = nullptr,
LPBYTE lpData = nullptr, LPDWORD lpcbData = nullptr) noexcept;
LONG QueryBinary(LPCTSTR pszValueName, LPVOID pvValue, DWORD cb) noexcept;
LONG QueryDword(LPCTSTR pszValueName, DWORD& dw) noexcept;
LONG QueryDwordLE(LPCTSTR pszValueName, DWORD& dw) noexcept;
LONG QueryDwordBE(LPCTSTR pszValueName, DWORD& dw) noexcept;
LONG QuerySz(LPCTSTR pszValueName, LPTSTR pszValue, DWORD cchValue) noexcept;
LONG QueryExpandSz(LPCTSTR pszValueName, LPTSTR pszValue, DWORD cchValue) noexcept;
LONG QueryMultiSz(LPCTSTR pszValueName, LPTSTR pszzValues, DWORD cchValues) noexcept;
template <typename T_CONTAINER>
LONG QueryMultiSz(LPCTSTR pszValueName, T_CONTAINER& container) noexcept;
template <typename T_STRUCT>
LONG QueryStruct(LPCTSTR pszValueName, T_STRUCT& data) noexcept;
template <typename T_STRING>
LONG QuerySz(LPCTSTR pszValueName, T_STRING& strValue) noexcept;
template <typename T_STRING>
LONG QueryExpandSz(LPCTSTR pszValueName, T_STRING& strValue) noexcept;
LONG RegSetValueEx(LPCTSTR pszValueName, DWORD dwReserved,
DWORD dwType, CONST BYTE *lpData, DWORD cbData) noexcept;
LONG SetBinary(LPCTSTR pszValueName, LPCVOID pvValue, DWORD cb) noexcept;
LONG SetDword(LPCTSTR pszValueName, DWORD dw) noexcept;
LONG SetDwordLE(LPCTSTR pszValueName, DWORD dw) noexcept;
LONG SetDwordBE(LPCTSTR pszValueName, DWORD dw) noexcept;
LONG SetSz(LPCTSTR pszValueName, LPCTSTR pszValue, DWORD cchValue) noexcept;
LONG SetSz(LPCTSTR pszValueName, LPCTSTR pszValue) noexcept;
LONG SetExpandSz(LPCTSTR pszValueName, LPCTSTR pszValue, DWORD cchValue) noexcept;
LONG SetExpandSz(LPCTSTR pszValueName, LPCTSTR pszValue) noexcept;
LONG SetMultiSz(LPCTSTR pszValueName, LPCTSTR pszzValues) noexcept;
LONG SetMultiSz(LPCTSTR pszValueName, LPCTSTR pszzValues, DWORD cchValues) noexcept;
template <typename T_CONTAINER>
LONG SetMultiSz(LPCTSTR pszValueName, const T_CONTAINER& container) noexcept;
template <typename T_STRUCT>
LONG SetStruct(LPCTSTR pszValueName, const T_STRUCT& data) noexcept;
LONG RegDeleteValue(LPCTSTR pszValueName) noexcept;
LONG RegDeleteTreeDx(LPCTSTR pszSubKey) noexcept;
LONG RegEnumKeyEx(DWORD dwIndex, LPTSTR lpName, LPDWORD lpcchName,
LPDWORD lpReserved = nullptr, LPTSTR lpClass = nullptr,
LPDWORD lpcchClass = nullptr,
PFILETIME lpftLastWriteTime = nullptr) noexcept;
LONG RegEnumValue(DWORD dwIndex, LPTSTR lpName, LPDWORD lpcchName,
LPDWORD lpReserved = nullptr, LPDWORD lpType = nullptr,
LPBYTE lpData = nullptr, LPDWORD lpcbData = nullptr) noexcept;
LONG RegFlushKey() noexcept;
LONG RegGetKeySecurity(SECURITY_INFORMATION si,
PSECURITY_DESCRIPTOR pSD, LPDWORD pcbSD) noexcept;
LONG RegNotifyChangeKeyValue(BOOL bWatchSubTree = TRUE,
DWORD dwFilter = REG_LEGAL_CHANGE_FILTER,
HANDLE hEvent = nullptr, BOOL bAsyncronous = FALSE) noexcept;
LONG RegQueryInfoKey(LPTSTR lpClass = nullptr,
LPDWORD lpcchClass = nullptr,
LPDWORD lpReserved = nullptr,
LPDWORD lpcSubKeys = nullptr,
LPDWORD lpcchMaxSubKeyLen = nullptr,
LPDWORD lpcchMaxClassLen = nullptr,
LPDWORD lpcValues = nullptr,
LPDWORD lpcchMaxValueNameLen = nullptr,
LPDWORD lpcbMaxValueLen = nullptr,
LPDWORD lpcbSecurityDescriptor = nullptr,
PFILETIME lpftLastWriteTime = nullptr) noexcept;
LONG RegQueryMultipleValues(PVALENT val_list, DWORD num_vals,
LPTSTR lpValueBuf, LPDWORD lpdwTotsize) noexcept;
LONG RegSetKeySecurity(SECURITY_INFORMATION si, PSECURITY_DESCRIPTOR psd) noexcept;
static LONG RegLoadKey(HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszFile) noexcept;
static LONG RegUnLoadKey(HKEY hKey, LPCTSTR pszSubKey) noexcept;
static LONG RegSaveKey(HKEY hKey, LPCTSTR pszFile,
LPSECURITY_ATTRIBUTES lpsa = nullptr) noexcept;
static LONG RegRestoreKey(HKEY hKey, LPCTSTR pszFile, DWORD dwFlags) noexcept;
static LONG RegReplaceKey(HKEY hKey, LPCTSTR pszSubKey,
LPCTSTR pszNewFile, LPCTSTR pszOldFile) noexcept;
static LONG RegDeleteTreeDx(HKEY hKey, LPCTSTR pszSubKey) noexcept;
static size_t MultiSzSizeDx(LPCTSTR pszz) noexcept;
static HKEY CloneHandleDx(HKEY hKey) noexcept;
protected:
HKEY m_hKey;
};
////////////////////////////////////////////////////////////////////////////
template <typename T_STRUCT>
inline LONG MRegKey::QueryStruct(LPCTSTR pszValueName, T_STRUCT& data) noexcept
{
assert(m_hKey);
DWORD cbData = static_cast<DWORD>(sizeof(data));
LONG result = ::RegQueryValueEx(m_hKey, pszValueName, nullptr, nullptr,
reinterpret_cast<LPBYTE>(&data), &cbData);
if (result == ERROR_SUCCESS && cbData != sizeof(data))
result = ERROR_INVALID_DATA;
return result;
}
template <typename T_STRUCT>
inline LONG MRegKey::SetStruct(LPCTSTR pszValueName, const T_STRUCT& data) noexcept
{
assert(m_hKey);
const DWORD cbData = static_cast<DWORD>(sizeof(data));
return ::RegSetValueEx(m_hKey, pszValueName, 0, REG_BINARY,
reinterpret_cast<const BYTE *>(&data), cbData);
}
template <typename T_CONTAINER>
LONG MRegKey::QueryMultiSz(LPCTSTR pszValueName, T_CONTAINER& container) noexcept
{
container.clear();
LONG result;
#ifndef NDEBUG
DWORD dwType;
result = RegQueryValueEx(pszValueName, nullptr, &dwType, nullptr, nullptr);
if (result == ERROR_SUCCESS)
assert(dwType == REG_MULTI_SZ);
#endif
DWORD cbData;
result = RegQueryValueEx(pszValueName, nullptr, nullptr, nullptr, &cbData);
if (result != ERROR_SUCCESS)
return result;
const DWORD cch = static_cast<DWORD>(cbData / sizeof(TCHAR) + 1);
LPTSTR pszz = new(std::nothrow) TCHAR[cch];
if (pszz)
{
result = QueryMultiSz(pszValueName, pszz, cch);
if (result == ERROR_SUCCESS)
{
for (LPTSTR pch = pszz; *pch; pch += lstrlen(pch) + 1)
{
#if (__cplusplus >= 201103L)
container.emplace_back(pch);
#else
container.push_back(pch);
#endif
}
}
delete[] pszz;
}
else
result = ERROR_OUTOFMEMORY;
return result;
}
template <typename T_CONTAINER>
inline LONG MRegKey::SetMultiSz(
LPCTSTR pszValueName, const T_CONTAINER& container) noexcept
{
typename T_CONTAINER::value_type str;
typename T_CONTAINER::const_iterator it, end;
it = container.begin();
end = container.end();
if (it != end)
{
for (; it != end; ++it)
{
str += *it;
str += TEXT('\0');
}
}
else
{
str += TEXT('\0');
}
const DWORD cchValues = static_cast<DWORD>(str.size() + 1);
return SetMultiSz(pszValueName, str.c_str(), cchValues);
}
template <typename T_STRING>
LONG MRegKey::QuerySz(LPCTSTR pszValueName, T_STRING& strValue) noexcept
{
LONG result;
strValue.clear();
#ifndef NDEBUG
DWORD dwType;
result = RegQueryValueEx(pszValueName, nullptr, &dwType, nullptr, nullptr);
if (result == ERROR_SUCCESS)
assert(dwType == REG_SZ);
#endif
DWORD cbData;
result = RegQueryValueEx(pszValueName, nullptr, nullptr, nullptr, &cbData);
if (result != ERROR_SUCCESS)
return result;
LPTSTR psz = new(std::nothrow) TCHAR[cbData / sizeof(TCHAR) + 1];
assert(psz);
if (psz)
{
result = RegQueryValueEx(pszValueName, nullptr, nullptr,
reinterpret_cast<LPBYTE>(psz), &cbData);
if (result == ERROR_SUCCESS)
{
strValue = psz;
}
delete[] psz;
}
return result;
}
template <typename T_STRING>
LONG MRegKey::QueryExpandSz(LPCTSTR pszValueName, T_STRING& strValue) noexcept
{
LONG result;
strValue.clear();
#ifndef NDEBUG
DWORD dwType;
result = RegQueryValueEx(pszValueName, nullptr, &dwType, nullptr, nullptr);
if (result == ERROR_SUCCESS)
assert(dwType == REG_EXPAND_SZ);
#endif
DWORD cbData;
result = RegQueryValueEx(pszValueName, nullptr, nullptr, nullptr, &cbData);
if (result != ERROR_SUCCESS)
return result;
LPTSTR psz = new(std::nothrow) TCHAR[cbData / sizeof(TCHAR) + 1];
assert(psz);
if (psz)
{
result = RegQueryValueEx(pszValueName, nullptr, nullptr,
reinterpret_cast<LPBYTE>(psz), &cbData);
if (result == ERROR_SUCCESS)
{
strValue = psz;
}
delete[] psz;
}
return result;
}
inline MRegKey::MRegKey() noexcept : m_hKey(nullptr)
{
}
inline MRegKey::MRegKey(HKEY hKey) noexcept : m_hKey(hKey)
{
}
inline MRegKey::MRegKey(
HKEY hBaseKey, LPCTSTR pszSubKey,
BOOL bCreate/* = FALSE*/) noexcept : m_hKey(nullptr)
{
if (bCreate)
RegCreateKeyEx(hBaseKey, pszSubKey);
else
RegOpenKeyEx(hBaseKey, pszSubKey);
}
inline MRegKey::MRegKey(MRegKey& key) noexcept : m_hKey(CloneHandleDx(key))
{
}
inline /*virtual*/ MRegKey::~MRegKey() noexcept
{
RegCloseKey();
}
inline HKEY MRegKey::Handle() const noexcept
{
return m_hKey;
}
inline MRegKey::operator HKEY() const noexcept
{
return Handle();
}
inline bool MRegKey::operator!() const noexcept
{
return Handle() == nullptr;
}
inline bool MRegKey::operator==(HKEY hKey) const noexcept
{
return Handle() == hKey;
}
inline bool MRegKey::operator!=(HKEY hKey) const noexcept
{
return Handle() != hKey;
}
inline MRegKey& MRegKey::operator=(HKEY hKey) noexcept
{
if (Handle() != hKey)
{
Attach(hKey);
}
return *this;
}
inline MRegKey& MRegKey::operator=(MRegKey& key) noexcept
{
if (Handle() != key.m_hKey)
{
HKEY hKey = CloneHandleDx(key);
Attach(hKey);
}
return *this;
}
inline BOOL MRegKey::Attach(HKEY hKey) noexcept
{
RegCloseKey();
m_hKey = hKey;
return m_hKey != nullptr;
}
inline HKEY MRegKey::Detach() noexcept
{
HKEY hKey = m_hKey;
m_hKey = nullptr;
return hKey;
}
inline LONG MRegKey::RegCreateKeyEx(HKEY hBaseKey, LPCTSTR pszSubKey,
DWORD dwReserved/* = 0*/, LPTSTR lpClass/* = nullptr*/,
DWORD dwOptions/* = 0*/, REGSAM samDesired/* = KEY_ALL_ACCESS*/,
LPSECURITY_ATTRIBUTES lpsa/* = nullptr*/,
LPDWORD lpdwDisposition/* = nullptr*/) noexcept
{
UNREFERENCED_PARAMETER(dwReserved);
assert(m_hKey == nullptr);
return ::RegCreateKeyEx(hBaseKey, pszSubKey, 0,
lpClass, dwOptions, samDesired, lpsa, &m_hKey, lpdwDisposition);
}
inline LONG MRegKey::RegOpenKeyEx(HKEY hBaseKey, LPCTSTR pszSubKey,
DWORD dwOptions/* = 0*/, REGSAM samDesired/* = KEY_READ*/) noexcept
{
assert(m_hKey == nullptr);
return ::RegOpenKeyEx(hBaseKey, pszSubKey, dwOptions, samDesired,
&m_hKey);
}
inline LONG
MRegKey::RegConnectRegistry(LPCTSTR lpMachineName, HKEY hBaseKey) noexcept
{
assert(m_hKey == nullptr);
return ::RegConnectRegistry(lpMachineName, hBaseKey, &m_hKey);
}
inline LONG MRegKey::RegCloseKey() noexcept
{
if (Handle())
{
return ::RegCloseKey(Detach());
}
return ERROR_INVALID_HANDLE;
}
inline LONG MRegKey::RegQueryValueEx(LPCTSTR pszValueName/* = nullptr*/,
LPDWORD lpReserved/* = nullptr*/, LPDWORD lpType/* = nullptr*/,
LPBYTE lpData/* = nullptr*/, LPDWORD lpcbData/* = nullptr*/) noexcept
{
assert(m_hKey);
return ::RegQueryValueEx(m_hKey, pszValueName, lpReserved,
lpType, lpData, lpcbData);
}
inline LONG MRegKey::QueryBinary(
LPCTSTR pszValueName, LPVOID pvValue, DWORD cb) noexcept
{
#ifndef NDEBUG
DWORD dwType;
const LONG result = RegQueryValueEx(pszValueName, nullptr, &dwType,
nullptr, nullptr);
if (result == ERROR_SUCCESS)
assert(dwType == REG_BINARY);
#endif
DWORD cbData = cb;
return RegQueryValueEx(pszValueName, nullptr, nullptr,
static_cast<LPBYTE>(pvValue),
&cbData);
}
inline LONG MRegKey::QueryDword(LPCTSTR pszValueName, DWORD& dw) noexcept
{
#ifndef NDEBUG
DWORD dwType;
const LONG result = RegQueryValueEx(pszValueName, nullptr, &dwType,
nullptr, nullptr);
if (result == ERROR_SUCCESS)
assert(dwType == REG_DWORD);
#endif
DWORD cbData = sizeof(DWORD);
return RegQueryValueEx(pszValueName, nullptr, nullptr,
reinterpret_cast<LPBYTE>(&dw), &cbData);
}
inline LONG MRegKey::QueryDwordLE(LPCTSTR pszValueName, DWORD& dw) noexcept
{
#ifndef NDEBUG
DWORD dwType;
const LONG result = RegQueryValueEx(pszValueName, nullptr, &dwType,
nullptr, nullptr);
if (result == ERROR_SUCCESS)
assert(dwType == REG_DWORD_LITTLE_ENDIAN);
#endif
DWORD cbData = sizeof(DWORD);
return RegQueryValueEx(pszValueName, nullptr, nullptr,
reinterpret_cast<LPBYTE>(&dw), &cbData);
}
inline LONG MRegKey::QueryDwordBE(LPCTSTR pszValueName, DWORD& dw) noexcept
{
#ifndef NDEBUG
DWORD dwType;
const LONG result = RegQueryValueEx(pszValueName, nullptr, &dwType,
nullptr, nullptr);
if (result == ERROR_SUCCESS)
assert(dwType == REG_DWORD_BIG_ENDIAN);
#endif
DWORD cbData = sizeof(DWORD);
return RegQueryValueEx(pszValueName, nullptr, nullptr,
reinterpret_cast<LPBYTE>(&dw), &cbData);
}
inline LONG
MRegKey::QuerySz(LPCTSTR pszValueName, LPTSTR pszValue, DWORD cchValue) noexcept
{
#ifndef NDEBUG
DWORD dwType;
const LONG result = RegQueryValueEx(pszValueName, nullptr, &dwType,
nullptr, nullptr);
if (result == ERROR_SUCCESS)
assert(dwType == REG_SZ);
#endif
DWORD cbData = cchValue * sizeof(TCHAR);
return RegQueryValueEx(pszValueName, nullptr, nullptr,
reinterpret_cast<LPBYTE>(pszValue), &cbData);
}
inline LONG MRegKey::QueryExpandSz(
LPCTSTR pszValueName, LPTSTR pszValue, DWORD cchValue) noexcept
{
#ifndef NDEBUG
DWORD dwType;
const LONG result = RegQueryValueEx(pszValueName, nullptr, &dwType, nullptr, nullptr);
if (result == ERROR_SUCCESS)
assert(dwType == REG_EXPAND_SZ);
#endif
DWORD cbData = cchValue * sizeof(TCHAR);
return RegQueryValueEx(pszValueName, nullptr, nullptr,
reinterpret_cast<LPBYTE>(pszValue), &cbData);
}
inline LONG MRegKey::QueryMultiSz(
LPCTSTR pszValueName, LPTSTR pszzValues, DWORD cchValues) noexcept
{
#ifndef NDEBUG
DWORD dwType;
const LONG result = RegQueryValueEx(pszValueName, nullptr, &dwType, nullptr, nullptr);
if (result == ERROR_SUCCESS)
assert(dwType == REG_MULTI_SZ);
#endif
DWORD cbData = sizeof(TCHAR) * cchValues;
return RegQueryValueEx(pszValueName, nullptr, nullptr,
reinterpret_cast<LPBYTE>(pszzValues), &cbData);
}
inline LONG MRegKey::RegSetValueEx(LPCTSTR pszValueName, DWORD dwReserved,
DWORD dwType, CONST BYTE *lpData, DWORD cbData) noexcept
{
UNREFERENCED_PARAMETER(dwReserved);
assert(m_hKey);
return ::RegSetValueEx(m_hKey, pszValueName, 0, dwType,
lpData, cbData);
}
inline LONG MRegKey::SetBinary(LPCTSTR pszValueName, LPCVOID pvValue, DWORD cb) noexcept
{
return RegSetValueEx(pszValueName, 0, REG_BINARY,
reinterpret_cast<const BYTE *>(pvValue), cb);
}
inline LONG MRegKey::SetDword(LPCTSTR pszValueName, DWORD dw) noexcept
{
const DWORD dwValue = dw;
return RegSetValueEx(pszValueName, 0, REG_DWORD,
reinterpret_cast<const BYTE *>(&dwValue), sizeof(DWORD));
}
inline LONG MRegKey::SetDwordLE(LPCTSTR pszValueName, DWORD dw) noexcept
{
const DWORD dwValue = dw;
return RegSetValueEx(pszValueName, 0, REG_DWORD_LITTLE_ENDIAN,
reinterpret_cast<const BYTE *>(&dwValue), sizeof(DWORD));
}
inline LONG MRegKey::SetDwordBE(LPCTSTR pszValueName, DWORD dw) noexcept
{
const DWORD dwValue = dw;
return RegSetValueEx(pszValueName, 0, REG_DWORD_BIG_ENDIAN,
reinterpret_cast<const BYTE *>(&dwValue), sizeof(DWORD));
}
inline LONG
MRegKey::SetSz(LPCTSTR pszValueName, LPCTSTR pszValue, DWORD cchValue) noexcept
{
return RegSetValueEx(pszValueName, 0, REG_SZ,
reinterpret_cast<const BYTE *>(pszValue), cchValue * sizeof(TCHAR));
}
inline LONG
MRegKey::SetExpandSz(LPCTSTR pszValueName, LPCTSTR pszValue, DWORD cchValue) noexcept
{
return RegSetValueEx(pszValueName, 0, REG_EXPAND_SZ,
reinterpret_cast<const BYTE *>(pszValue), cchValue * sizeof(TCHAR));
}
inline LONG
MRegKey::SetMultiSz(LPCTSTR pszValueName, LPCTSTR pszzValues) noexcept
{
return RegSetValueEx(pszValueName, 0, REG_MULTI_SZ,
reinterpret_cast<const BYTE *>(pszzValues),
static_cast<DWORD>(MRegKey::MultiSzSizeDx(pszzValues)));
}
inline LONG
MRegKey::SetMultiSz(LPCTSTR pszValueName, LPCTSTR pszzValues, DWORD cchValues) noexcept
{
return RegSetValueEx(pszValueName, 0, REG_MULTI_SZ,
reinterpret_cast<const BYTE *>(pszzValues),
static_cast<DWORD>(sizeof(TCHAR) * cchValues));
}
inline LONG MRegKey::RegDeleteValue(LPCTSTR pszValueName) noexcept
{
assert(m_hKey);
return ::RegDeleteValue(m_hKey, pszValueName);
}
inline LONG MRegKey::RegEnumKeyEx(DWORD dwIndex, LPTSTR lpName,
LPDWORD lpcchName, LPDWORD lpReserved/* = nullptr*/,
LPTSTR lpClass/* = nullptr*/, LPDWORD lpcchClass/* = nullptr*/,
PFILETIME lpftLastWriteTime/* = nullptr*/) noexcept
{
assert(m_hKey);
return ::RegEnumKeyEx(m_hKey, dwIndex, lpName, lpcchName,
lpReserved, lpClass, lpcchClass, lpftLastWriteTime);
}
inline LONG MRegKey::RegEnumValue(DWORD dwIndex, LPTSTR lpName,
LPDWORD lpcchName, LPDWORD lpReserved/* = nullptr*/, LPDWORD lpType/* = nullptr*/,
LPBYTE lpData/* = nullptr*/, LPDWORD lpcbData/* = nullptr*/) noexcept
{
assert(m_hKey);
return ::RegEnumValue(m_hKey, dwIndex, lpName, lpcchName, lpReserved,
lpType, lpData, lpcbData);
}
inline LONG MRegKey::RegFlushKey() noexcept
{
assert(m_hKey);
return ::RegFlushKey(m_hKey);
}
inline LONG MRegKey::RegGetKeySecurity(SECURITY_INFORMATION si,
PSECURITY_DESCRIPTOR pSD, LPDWORD pcbSD) noexcept
{
assert(m_hKey);
return ::RegGetKeySecurity(m_hKey, si, pSD, pcbSD);
}
inline LONG MRegKey::RegNotifyChangeKeyValue(BOOL bWatchSubTree/* = TRUE*/,
DWORD dwFilter/* = REG_LEGAL_CHANGE_FILTER*/,
HANDLE hEvent/* = nullptr*/, BOOL bAsyncronous/* = FALSE*/) noexcept
{
assert(m_hKey);
return ::RegNotifyChangeKeyValue(m_hKey, bWatchSubTree, dwFilter,
hEvent, bAsyncronous);
}
inline LONG MRegKey::RegQueryInfoKey(LPTSTR lpClass/* = nullptr*/,
LPDWORD lpcchClass/* = nullptr*/,
LPDWORD lpReserved/* = nullptr*/,
LPDWORD lpcSubKeys/* = nullptr*/,
LPDWORD lpcchMaxSubKeyLen/* = nullptr*/,
LPDWORD lpcchMaxClassLen/* = nullptr*/,
LPDWORD lpcValues/* = nullptr*/,
LPDWORD lpcchMaxValueNameLen/* = nullptr*/,
LPDWORD lpcbMaxValueLen/* = nullptr*/,
LPDWORD lpcbSecurityDescriptor/* = nullptr*/,
PFILETIME lpftLastWriteTime/* = nullptr*/) noexcept
{
assert(m_hKey);
return ::RegQueryInfoKey(m_hKey, lpClass, lpcchClass,
lpReserved, lpcSubKeys, lpcchMaxSubKeyLen, lpcchMaxClassLen,
lpcValues, lpcchMaxValueNameLen, lpcbMaxValueLen,
lpcbSecurityDescriptor, lpftLastWriteTime);
}
inline LONG MRegKey::RegQueryMultipleValues(PVALENT val_list, DWORD num_vals,
LPTSTR lpValueBuf, LPDWORD lpdwTotsize) noexcept
{
assert(m_hKey);
return ::RegQueryMultipleValues(
m_hKey, val_list, num_vals, lpValueBuf, lpdwTotsize);
}
inline LONG
MRegKey::RegSetKeySecurity(SECURITY_INFORMATION si, PSECURITY_DESCRIPTOR psd) noexcept
{
assert(m_hKey);
return ::RegSetKeySecurity(m_hKey, si, psd);
}
inline /*static*/ LONG
MRegKey::RegLoadKey(HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszFile) noexcept
{
assert(pszSubKey);
assert(pszFile);
return ::RegLoadKey(hKey, pszSubKey, pszFile);
}
inline /*static*/ LONG MRegKey::RegUnLoadKey(HKEY hKey, LPCTSTR pszSubKey) noexcept
{
assert(pszSubKey);
return ::RegUnLoadKey(hKey, pszSubKey);
}
inline /*static*/ LONG
MRegKey::RegSaveKey(HKEY hKey, LPCTSTR pszFile,
LPSECURITY_ATTRIBUTES lpsa/* = nullptr*/) noexcept
{
assert(pszFile);
return ::RegSaveKey(hKey, pszFile, lpsa);
}
inline /*static*/
LONG MRegKey::RegRestoreKey(HKEY hKey, LPCTSTR pszFile, DWORD dwFlags) noexcept
{
assert(pszFile);
return ::RegRestoreKey(hKey, pszFile, dwFlags);
}
inline /*static*/ LONG MRegKey::RegReplaceKey(
HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszNewFile, LPCTSTR pszOldFile) noexcept
{
assert(pszNewFile);
assert(pszOldFile);
return ::RegReplaceKey(hKey, pszSubKey, pszNewFile, pszOldFile);
}
inline /*static*/ LONG MRegKey::RegDeleteTreeDx(HKEY hKey, LPCTSTR pszSubKey) noexcept
{
return ::RegDeleteTreeDx(hKey, pszSubKey);
}
inline LONG MRegKey::SetSz(LPCTSTR pszValueName, LPCTSTR pszValue) noexcept
{
return SetSz(pszValueName, pszValue, lstrlen(pszValue) + 1);
}
inline LONG MRegKey::SetExpandSz(LPCTSTR pszValueName, LPCTSTR pszValue) noexcept
{
return SetExpandSz(pszValueName, pszValue, lstrlen(pszValue) + 1);
}
inline LONG MRegKey::RegDeleteTreeDx(LPCTSTR pszSubKey) noexcept
{
assert(m_hKey);
return RegDeleteTreeDx(m_hKey, pszSubKey);
}
inline LONG RegDeleteTreeDx(HKEY hKey, LPCTSTR pszSubKey/* = nullptr*/) noexcept
{
DWORD cchSubKeyMax, cchValueMax;
DWORD cchMax, cch;
TCHAR szNameBuf[MAX_PATH], *pszName = szNameBuf;
HKEY hSubKey = hKey;
if (pszSubKey != nullptr)
{
const LONG ret = ::RegOpenKeyEx(hKey, pszSubKey, 0, KEY_READ, &hSubKey);
if (ret)
return ret;
}
LONG ret = ::RegQueryInfoKey(hSubKey, nullptr, nullptr, nullptr, nullptr,
&cchSubKeyMax, nullptr, nullptr, &cchValueMax, nullptr, nullptr, nullptr);
if (ret)
goto cleanup;
cchSubKeyMax++;
cchValueMax++;
if (cchSubKeyMax < cchValueMax)
cchMax = cchValueMax;
else
cchMax = cchSubKeyMax;
if (cchMax > sizeof(szNameBuf) / sizeof(TCHAR))
{
pszName = (TCHAR *)_malloca(cchMax * sizeof(TCHAR));
if (pszName == nullptr)
goto cleanup;
}
for(;;)
{
cch = cchMax;
if (::RegEnumKeyEx(hSubKey, 0, pszName, &cch, nullptr,
nullptr, nullptr, nullptr))
{
break;
}
ret = RegDeleteTreeDx(hSubKey, pszName);
if (ret)
goto cleanup;
}
if (pszSubKey != nullptr)
{
ret = ::RegDeleteKey(hKey, pszSubKey);
}
else
{
// NOTE: if pszSubKey was nullptr, then delete value entries.
for (;;)
{
cch = cchMax;
if (::RegEnumValue(hKey, 0, pszName, &cch,
nullptr, nullptr, nullptr, nullptr))
{
break;
}
ret = ::RegDeleteValue(hKey, pszName);
if (ret)
goto cleanup;
}
}
cleanup:
if (pszSubKey != nullptr)
::RegCloseKey(hSubKey);
if (pszName != szNameBuf)
_freea(pszName);
return ret;
}
inline /*static*/ size_t MRegKey::MultiSzSizeDx(LPCTSTR pszz) noexcept
{
size_t siz = 0;
if (*pszz)
{
do
{
const size_t len = lstrlen(pszz);
siz += len + 1;
pszz += len + 1;
}
while (*pszz);
}
else
{
++siz;
}
++siz;
siz *= sizeof(TCHAR);
return siz;
}
inline /*static*/ HKEY MRegKey::CloneHandleDx(HKEY hKey) noexcept
{
if (hKey == nullptr)
return nullptr;
HANDLE hProcess = ::GetCurrentProcess();
HANDLE hDup = nullptr;
::DuplicateHandle(hProcess, hKey, hProcess, &hDup, 0,
FALSE, DUPLICATE_SAME_ACCESS);
return static_cast<HKEY>(hDup);
}
////////////////////////////////////////////////////////////////////////////
#endif // ndef MZC4_MREGKEY_HPP_