-
Notifications
You must be signed in to change notification settings - Fork 0
/
MRegKey.hpp
863 lines (749 loc) · 25.9 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_ 7 /* Version 7 */
#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 <new> // std::nothrow
////////////////////////////////////////////////////////////////////////////
// NOTE: RegDeleteTreeDx deletes all value entries if pszSubKey == NULL.
// NOTE: RegDeleteTreeDx cannot delete opening keys.
LONG RegDeleteTreeDx(HKEY hKey, LPCTSTR pszSubKey/* = NULL*/);
////////////////////////////////////////////////////////////////////////////
class MRegKey
{
public:
MRegKey();
MRegKey(HKEY hKey);
MRegKey(MRegKey& key);
MRegKey(HKEY hBaseKey, LPCTSTR pszSubKey, BOOL bCreate = FALSE);
virtual ~MRegKey();
operator HKEY() const;
bool operator!() const;
bool operator==(HKEY hKey) const;
bool operator!=(HKEY hKey) const;
MRegKey& operator=(HKEY hKey);
MRegKey& operator=(MRegKey& key);
BOOL Attach(HKEY hKey);
HKEY Detach();
HKEY Handle() const;
LONG RegCreateKeyEx(HKEY hBaseKey, LPCTSTR pszSubKey, DWORD dwReserved = 0,
LPTSTR lpClass = NULL, DWORD dwOptions = 0,
REGSAM samDesired = KEY_ALL_ACCESS,
LPSECURITY_ATTRIBUTES lpsa = NULL,
LPDWORD lpdwDisposition = NULL);
LONG RegOpenKeyEx(HKEY hBaseKey, LPCTSTR pszSubKey, DWORD dwOptions = 0,
REGSAM samDesired = KEY_READ);
LONG RegConnectRegistry(LPCTSTR lpMachineName, HKEY hBaseKey);
LONG RegCloseKey();
LONG RegQueryValueEx(LPCTSTR pszValueName = NULL,
LPDWORD lpReserved = NULL, LPDWORD lpType = NULL,
LPBYTE lpData = NULL, LPDWORD lpcbData = NULL);
LONG QueryBinary(LPCTSTR pszValueName, LPVOID pvValue, DWORD cb);
LONG QueryDword(LPCTSTR pszValueName, DWORD& dw);
LONG QueryDwordLE(LPCTSTR pszValueName, DWORD& dw);
LONG QueryDwordBE(LPCTSTR pszValueName, DWORD& dw);
LONG QuerySz(LPCTSTR pszValueName, LPTSTR pszValue, DWORD cchValue);
LONG QueryExpandSz(LPCTSTR pszValueName, LPTSTR pszValue, DWORD cchValue);
LONG QueryMultiSz(LPCTSTR pszValueName, LPTSTR pszzValues, DWORD cchValues);
template <typename T_CONTAINER>
LONG QueryMultiSz(LPCTSTR pszValueName, T_CONTAINER& container);
template <typename T_STRUCT>
LONG QueryStruct(LPCTSTR pszValueName, T_STRUCT& data);
template <typename T_STRING>
LONG QuerySz(LPCTSTR pszValueName, T_STRING& strValue);
template <typename T_STRING>
LONG QueryExpandSz(LPCTSTR pszValueName, T_STRING& strValue);
LONG RegSetValueEx(LPCTSTR pszValueName, DWORD dwReserved,
DWORD dwType, CONST BYTE *lpData, DWORD cbData);
LONG SetBinary(LPCTSTR pszValueName, LPCVOID pvValue, DWORD cb);
LONG SetDword(LPCTSTR pszValueName, DWORD dw);
LONG SetDwordLE(LPCTSTR pszValueName, DWORD dw);
LONG SetDwordBE(LPCTSTR pszValueName, DWORD dw);
LONG SetSz(LPCTSTR pszValueName, LPCTSTR pszValue, DWORD cchValue);
LONG SetSz(LPCTSTR pszValueName, LPCTSTR pszValue);
LONG SetExpandSz(LPCTSTR pszValueName, LPCTSTR pszValue, DWORD cchValue);
LONG SetExpandSz(LPCTSTR pszValueName, LPCTSTR pszValue);
LONG SetMultiSz(LPCTSTR pszValueName, LPCTSTR pszzValues);
LONG SetMultiSz(LPCTSTR pszValueName, LPCTSTR pszzValues, DWORD cchValues);
template <typename T_CONTAINER>
LONG SetMultiSz(LPCTSTR pszValueName, const T_CONTAINER& container);
template <typename T_STRUCT>
LONG SetStruct(LPCTSTR pszValueName, const T_STRUCT& data);
LONG RegDeleteValue(LPCTSTR pszValueName);
LONG RegDeleteTreeDx(LPCTSTR pszSubKey);
LONG RegEnumKeyEx(DWORD dwIndex, LPTSTR lpName, LPDWORD lpcchName,
LPDWORD lpReserved = NULL, LPTSTR lpClass = NULL,
LPDWORD lpcchClass = NULL,
PFILETIME lpftLastWriteTime = NULL);
LONG RegEnumValue(DWORD dwIndex, LPTSTR lpName, LPDWORD lpcchName,
LPDWORD lpReserved = NULL, LPDWORD lpType = NULL,
LPBYTE lpData = NULL, LPDWORD lpcbData = NULL);
LONG RegFlushKey();
LONG RegGetKeySecurity(SECURITY_INFORMATION si,
PSECURITY_DESCRIPTOR pSD, LPDWORD pcbSD);
LONG RegNotifyChangeKeyValue(BOOL bWatchSubTree = TRUE,
DWORD dwFilter = REG_LEGAL_CHANGE_FILTER,
HANDLE hEvent = NULL, BOOL bAsyncronous = FALSE);
LONG RegQueryInfoKey(LPTSTR lpClass = NULL,
LPDWORD lpcchClass = NULL,
LPDWORD lpReserved = NULL,
LPDWORD lpcSubKeys = NULL,
LPDWORD lpcchMaxSubKeyLen = NULL,
LPDWORD lpcchMaxClassLen = NULL,
LPDWORD lpcValues = NULL,
LPDWORD lpcchMaxValueNameLen = NULL,
LPDWORD lpcbMaxValueLen = NULL,
LPDWORD lpcbSecurityDescriptor = NULL,
PFILETIME lpftLastWriteTime = NULL);
LONG RegQueryMultipleValues(PVALENT val_list, DWORD num_vals,
LPTSTR lpValueBuf, LPDWORD lpdwTotsize);
LONG RegSetKeySecurity(SECURITY_INFORMATION si, PSECURITY_DESCRIPTOR psd);
static LONG RegLoadKey(HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszFile);
static LONG RegUnLoadKey(HKEY hKey, LPCTSTR pszSubKey);
static LONG RegSaveKey(HKEY hKey, LPCTSTR pszFile,
LPSECURITY_ATTRIBUTES lpsa = NULL);
static LONG RegRestoreKey(HKEY hKey, LPCTSTR pszFile, DWORD dwFlags);
static LONG RegReplaceKey(HKEY hKey, LPCTSTR pszSubKey,
LPCTSTR pszNewFile, LPCTSTR pszOldFile);
static LONG RegDeleteTreeDx(HKEY hKey, LPCTSTR pszSubKey);
static DWORD MultiSzSizeDx(LPCTSTR pszz);
static HKEY CloneHandleDx(HKEY hKey);
protected:
HKEY m_hKey;
};
////////////////////////////////////////////////////////////////////////////
template <typename T_STRUCT>
inline LONG MRegKey::QueryStruct(LPCTSTR pszValueName, T_STRUCT& data)
{
assert(m_hKey);
DWORD cbData = static_cast<DWORD>(sizeof(data));
LONG result = ::RegQueryValueEx(m_hKey, pszValueName, NULL, NULL,
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)
{
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)
{
container.clear();
LONG result;
#ifndef NDEBUG
DWORD dwType;
result = RegQueryValueEx(pszValueName, NULL, &dwType, NULL, NULL);
assert(result == ERROR_SUCCESS);
assert(dwType == REG_MULTI_SZ);
#endif
DWORD cbData;
result = RegQueryValueEx(pszValueName, NULL, NULL, NULL, &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)
{
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)
{
LONG result;
strValue.clear();
#ifndef NDEBUG
DWORD dwType;
result = RegQueryValueEx(pszValueName, NULL, &dwType, NULL, NULL);
assert(result == ERROR_SUCCESS);
assert(dwType == REG_SZ);
#endif
DWORD cbData;
result = RegQueryValueEx(pszValueName, NULL, NULL, NULL, &cbData);
if (result != ERROR_SUCCESS)
return result;
LPTSTR psz = new(std::nothrow) TCHAR[cbData / sizeof(TCHAR) + 1];
assert(psz);
if (psz)
{
result = RegQueryValueEx(pszValueName, NULL, NULL,
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)
{
LONG result;
strValue.clear();
#ifndef NDEBUG
DWORD dwType;
result = RegQueryValueEx(pszValueName, NULL, &dwType, NULL, NULL);
assert(result == ERROR_SUCCESS);
assert(dwType == REG_EXPAND_SZ);
#endif
DWORD cbData;
result = RegQueryValueEx(pszValueName, NULL, NULL, NULL, &cbData);
if (result != ERROR_SUCCESS)
return result;
LPTSTR psz = new(std::nothrow) TCHAR[cbData / sizeof(TCHAR) + 1];
assert(psz);
if (psz)
{
result = RegQueryValueEx(pszValueName, NULL, NULL,
reinterpret_cast<LPBYTE>(psz), &cbData);
if (result != ERROR_SUCCESS)
{
strValue = psz;
}
delete[] psz;
}
return result;
}
inline MRegKey::MRegKey() : m_hKey(NULL)
{
}
inline MRegKey::MRegKey(HKEY hKey) : m_hKey(hKey)
{
}
inline MRegKey::MRegKey(
HKEY hBaseKey, LPCTSTR pszSubKey,
BOOL bCreate/* = FALSE*/) : m_hKey(NULL)
{
if (bCreate)
RegCreateKeyEx(hBaseKey, pszSubKey);
else
RegOpenKeyEx(hBaseKey, pszSubKey);
}
inline MRegKey::MRegKey(MRegKey& key) : m_hKey(CloneHandleDx(key))
{
}
inline /*virtual*/ MRegKey::~MRegKey()
{
RegCloseKey();
}
inline HKEY MRegKey::Handle() const
{
return (this ? m_hKey : NULL);
}
inline MRegKey::operator HKEY() const
{
return Handle();
}
inline bool MRegKey::operator!() const
{
return Handle() == NULL;
}
inline bool MRegKey::operator==(HKEY hKey) const
{
return Handle() == hKey;
}
inline bool MRegKey::operator!=(HKEY hKey) const
{
return Handle() != hKey;
}
inline MRegKey& MRegKey::operator=(HKEY hKey)
{
if (Handle() != hKey)
{
Attach(hKey);
}
return *this;
}
inline MRegKey& MRegKey::operator=(MRegKey& key)
{
if (Handle() != key.m_hKey)
{
HKEY hKey = CloneHandleDx(key);
Attach(hKey);
}
return *this;
}
inline BOOL MRegKey::Attach(HKEY hKey)
{
RegCloseKey();
m_hKey = hKey;
return m_hKey != NULL;
}
inline HKEY MRegKey::Detach()
{
HKEY hKey = m_hKey;
m_hKey = NULL;
return hKey;
}
inline LONG MRegKey::RegCreateKeyEx(HKEY hBaseKey, LPCTSTR pszSubKey,
DWORD dwReserved/* = 0*/, LPTSTR lpClass/* = NULL*/,
DWORD dwOptions/* = 0*/, REGSAM samDesired/* = KEY_ALL_ACCESS*/,
LPSECURITY_ATTRIBUTES lpsa/* = NULL*/,
LPDWORD lpdwDisposition/* = NULL*/)
{
assert(m_hKey == NULL);
return ::RegCreateKeyEx(hBaseKey, pszSubKey, dwReserved,
lpClass, dwOptions, samDesired, lpsa, &m_hKey, lpdwDisposition);
}
inline LONG MRegKey::RegOpenKeyEx(HKEY hBaseKey, LPCTSTR pszSubKey,
DWORD dwOptions/* = 0*/, REGSAM samDesired/* = KEY_READ*/)
{
assert(m_hKey == NULL);
return ::RegOpenKeyEx(hBaseKey, pszSubKey, dwOptions, samDesired,
&m_hKey);
}
inline LONG
MRegKey::RegConnectRegistry(LPCTSTR lpMachineName, HKEY hBaseKey)
{
assert(m_hKey == NULL);
return ::RegConnectRegistry(lpMachineName, hBaseKey, &m_hKey);
}
inline LONG MRegKey::RegCloseKey()
{
if (Handle())
{
LONG result = ::RegCloseKey(Detach());
return result;
}
return ERROR_INVALID_HANDLE;
}
inline LONG MRegKey::RegQueryValueEx(LPCTSTR pszValueName/* = NULL*/,
LPDWORD lpReserved/* = NULL*/, LPDWORD lpType/* = NULL*/,
LPBYTE lpData/* = NULL*/, LPDWORD lpcbData/* = NULL*/)
{
assert(m_hKey);
return ::RegQueryValueEx(m_hKey, pszValueName, lpReserved,
lpType, lpData, lpcbData);
}
inline LONG MRegKey::QueryBinary(
LPCTSTR pszValueName, LPVOID pvValue, DWORD cb)
{
#ifndef NDEBUG
DWORD dwType;
LONG result = RegQueryValueEx(pszValueName, NULL, &dwType,
NULL, NULL);
assert(result == ERROR_SUCCESS);
assert(dwType == REG_BINARY);
#endif
DWORD cbData = cb;
return RegQueryValueEx(pszValueName, NULL, NULL,
reinterpret_cast<LPBYTE>(pvValue),
&cbData);
}
inline LONG MRegKey::QueryDword(LPCTSTR pszValueName, DWORD& dw)
{
#ifndef NDEBUG
DWORD dwType;
LONG result = RegQueryValueEx(pszValueName, NULL, &dwType,
NULL, NULL);
assert(result == ERROR_SUCCESS);
assert(dwType == REG_DWORD);
#endif
DWORD cbData = sizeof(DWORD);
return RegQueryValueEx(pszValueName, NULL, NULL,
reinterpret_cast<LPBYTE>(&dw), &cbData);
}
inline LONG MRegKey::QueryDwordLE(LPCTSTR pszValueName, DWORD& dw)
{
#ifndef NDEBUG
DWORD dwType;
LONG result = RegQueryValueEx(pszValueName, NULL, &dwType,
NULL, NULL);
assert(result == ERROR_SUCCESS);
assert(dwType == REG_DWORD_LITTLE_ENDIAN);
#endif
DWORD cbData = sizeof(DWORD);
return RegQueryValueEx(pszValueName, NULL, NULL,
reinterpret_cast<LPBYTE>(&dw), &cbData);
}
inline LONG MRegKey::QueryDwordBE(LPCTSTR pszValueName, DWORD& dw)
{
#ifndef NDEBUG
DWORD dwType;
LONG result = RegQueryValueEx(pszValueName, NULL, &dwType,
NULL, NULL);
assert(result == ERROR_SUCCESS);
assert(dwType == REG_DWORD_BIG_ENDIAN);
#endif
DWORD cbData = sizeof(DWORD);
return RegQueryValueEx(pszValueName, NULL, NULL,
reinterpret_cast<LPBYTE>(&dw), &cbData);
}
inline LONG
MRegKey::QuerySz(LPCTSTR pszValueName, LPTSTR pszValue, DWORD cchValue)
{
#ifndef NDEBUG
DWORD dwType;
LONG result = RegQueryValueEx(pszValueName, NULL, &dwType,
NULL, NULL);
assert(result == ERROR_SUCCESS);
assert(dwType == REG_SZ);
#endif
DWORD cbData = cchValue * sizeof(TCHAR);
return RegQueryValueEx(pszValueName, NULL, NULL,
reinterpret_cast<LPBYTE>(pszValue), &cbData);
}
inline LONG MRegKey::QueryExpandSz(
LPCTSTR pszValueName, LPTSTR pszValue, DWORD cchValue)
{
#ifndef NDEBUG
DWORD dwType;
LONG result = RegQueryValueEx(pszValueName, NULL, &dwType, NULL, NULL);
assert(result == ERROR_SUCCESS);
assert(dwType == REG_EXPAND_SZ);
#endif
DWORD cbData = cchValue * sizeof(TCHAR);
return RegQueryValueEx(pszValueName, NULL, NULL,
reinterpret_cast<LPBYTE>(pszValue), &cbData);
}
inline LONG MRegKey::QueryMultiSz(
LPCTSTR pszValueName, LPTSTR pszzValues, DWORD cchValues)
{
#ifndef NDEBUG
DWORD dwType;
LONG result = RegQueryValueEx(pszValueName, NULL, &dwType, NULL, NULL);
assert(result == ERROR_SUCCESS);
assert(dwType == REG_MULTI_SZ);
#endif
DWORD cbData = sizeof(TCHAR) * cchValues;
return RegQueryValueEx(pszValueName, NULL, NULL,
reinterpret_cast<LPBYTE>(pszzValues), &cbData);
}
inline LONG MRegKey::RegSetValueEx(LPCTSTR pszValueName, DWORD dwReserved,
DWORD dwType, CONST BYTE *lpData, DWORD cbData)
{
assert(m_hKey);
return ::RegSetValueEx(m_hKey, pszValueName, dwReserved, dwType,
lpData, cbData);
}
inline LONG MRegKey::SetBinary(LPCTSTR pszValueName, LPCVOID pvValue, DWORD cb)
{
return RegSetValueEx(pszValueName, 0, REG_BINARY,
reinterpret_cast<const BYTE *>(pvValue), cb);
}
inline LONG MRegKey::SetDword(LPCTSTR pszValueName, DWORD dw)
{
DWORD dwValue = dw;
return RegSetValueEx(pszValueName, 0, REG_DWORD,
reinterpret_cast<const BYTE *>(&dwValue), sizeof(DWORD));
}
inline LONG MRegKey::SetDwordLE(LPCTSTR pszValueName, DWORD dw)
{
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)
{
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)
{
return RegSetValueEx(pszValueName, 0, REG_SZ,
reinterpret_cast<const BYTE *>(pszValue), cchValue * sizeof(TCHAR));
}
inline LONG
MRegKey::SetExpandSz(LPCTSTR pszValueName, LPCTSTR pszValue, DWORD cchValue)
{
return RegSetValueEx(pszValueName, 0, REG_EXPAND_SZ,
reinterpret_cast<const BYTE *>(pszValue), cchValue * sizeof(TCHAR));
}
inline LONG
MRegKey::SetMultiSz(LPCTSTR pszValueName, LPCTSTR pszzValues)
{
return RegSetValueEx(pszValueName, 0, REG_MULTI_SZ,
reinterpret_cast<const BYTE *>(pszzValues),
MRegKey::MultiSzSizeDx(pszzValues));
}
inline LONG
MRegKey::SetMultiSz(LPCTSTR pszValueName, LPCTSTR pszzValues, DWORD cchValues)
{
DWORD cb = static_cast<DWORD>(sizeof(TCHAR) * cchValues);
return RegSetValueEx(pszValueName, 0, REG_MULTI_SZ,
reinterpret_cast<const BYTE *>(pszzValues), cb);
}
inline LONG MRegKey::RegDeleteValue(LPCTSTR pszValueName)
{
assert(m_hKey);
return ::RegDeleteValue(m_hKey, pszValueName);
}
inline LONG MRegKey::RegEnumKeyEx(DWORD dwIndex, LPTSTR lpName,
LPDWORD lpcchName, LPDWORD lpReserved/* = NULL*/,
LPTSTR lpClass/* = NULL*/, LPDWORD lpcchClass/* = NULL*/,
PFILETIME lpftLastWriteTime/* = NULL*/)
{
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/* = NULL*/, LPDWORD lpType/* = NULL*/,
LPBYTE lpData/* = NULL*/, LPDWORD lpcbData/* = NULL*/)
{
assert(m_hKey);
return ::RegEnumValue(m_hKey, dwIndex, lpName, lpcchName, lpReserved,
lpType, lpData, lpcbData);
}
inline LONG MRegKey::RegFlushKey()
{
assert(m_hKey);
return ::RegFlushKey(m_hKey);
}
inline LONG MRegKey::RegGetKeySecurity(SECURITY_INFORMATION si,
PSECURITY_DESCRIPTOR pSD, LPDWORD pcbSD)
{
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/* = NULL*/, BOOL bAsyncronous/* = FALSE*/)
{
assert(m_hKey);
return ::RegNotifyChangeKeyValue(m_hKey, bWatchSubTree, dwFilter,
hEvent, bAsyncronous);
}
inline LONG MRegKey::RegQueryInfoKey(LPTSTR lpClass/* = NULL*/,
LPDWORD lpcchClass/* = NULL*/,
LPDWORD lpReserved/* = NULL*/,
LPDWORD lpcSubKeys/* = NULL*/,
LPDWORD lpcchMaxSubKeyLen/* = NULL*/,
LPDWORD lpcchMaxClassLen/* = NULL*/,
LPDWORD lpcValues/* = NULL*/,
LPDWORD lpcchMaxValueNameLen/* = NULL*/,
LPDWORD lpcbMaxValueLen/* = NULL*/,
LPDWORD lpcbSecurityDescriptor/* = NULL*/,
PFILETIME lpftLastWriteTime/* = NULL*/)
{
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)
{
assert(m_hKey);
return ::RegQueryMultipleValues(
m_hKey, val_list, num_vals, lpValueBuf, lpdwTotsize);
}
inline LONG
MRegKey::RegSetKeySecurity(SECURITY_INFORMATION si, PSECURITY_DESCRIPTOR psd)
{
assert(m_hKey);
return ::RegSetKeySecurity(m_hKey, si, psd);
}
inline /*static*/ LONG
MRegKey::RegLoadKey(HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszFile)
{
assert(pszSubKey);
assert(pszFile);
return ::RegLoadKey(hKey, pszSubKey, pszFile);
}
inline /*static*/ LONG MRegKey::RegUnLoadKey(HKEY hKey, LPCTSTR pszSubKey)
{
assert(pszSubKey);
return ::RegUnLoadKey(hKey, pszSubKey);
}
inline /*static*/ LONG
MRegKey::RegSaveKey(HKEY hKey, LPCTSTR pszFile,
LPSECURITY_ATTRIBUTES lpsa/* = NULL*/)
{
assert(pszFile);
return ::RegSaveKey(hKey, pszFile, lpsa);
}
inline /*static*/
LONG MRegKey::RegRestoreKey(HKEY hKey, LPCTSTR pszFile, DWORD dwFlags)
{
assert(pszFile);
return ::RegRestoreKey(hKey, pszFile, dwFlags);
}
inline /*static*/ LONG MRegKey::RegReplaceKey(
HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszNewFile, LPCTSTR pszOldFile)
{
assert(pszNewFile);
assert(pszOldFile);
return ::RegReplaceKey(hKey, pszSubKey, pszNewFile, pszOldFile);
}
inline /*static*/ LONG MRegKey::RegDeleteTreeDx(HKEY hKey, LPCTSTR pszSubKey)
{
return RegDeleteTreeDx(hKey, pszSubKey);
}
inline LONG MRegKey::SetSz(LPCTSTR pszValueName, LPCTSTR pszValue)
{
return SetSz(pszValueName, pszValue, lstrlen(pszValue) + 1);
}
inline LONG MRegKey::SetExpandSz(LPCTSTR pszValueName, LPCTSTR pszValue)
{
return SetExpandSz(pszValueName, pszValue, lstrlen(pszValue) + 1);
}
inline LONG MRegKey::RegDeleteTreeDx(LPCTSTR pszSubKey)
{
assert(m_hKey);
return RegDeleteTreeDx(m_hKey, pszSubKey);
}
inline LONG RegDeleteTreeDx(HKEY hKey, LPCTSTR pszSubKey/* = NULL*/)
{
LONG ret;
DWORD cchSubKeyMax, cchValueMax;
DWORD cchMax, cch;
TCHAR szNameBuf[MAX_PATH], *pszName = szNameBuf;
HKEY hSubKey = hKey;
if (pszSubKey != NULL)
{
ret = ::RegOpenKeyEx(hKey, pszSubKey, 0, KEY_READ, &hSubKey);
if (ret)
return ret;
}
ret = ::RegQueryInfoKey(hSubKey, NULL, NULL, NULL, NULL,
&cchSubKeyMax, NULL, NULL, &cchValueMax, NULL, NULL, NULL);
if (ret)
goto cleanup;
cchSubKeyMax++;
cchValueMax++;
if (cchSubKeyMax < cchValueMax)
cchMax = cchValueMax;
else
cchMax = cchSubKeyMax;
if (cchMax > sizeof(szNameBuf) / sizeof(TCHAR))
{
pszName = new(std::nothrow) TCHAR[cchMax * sizeof(TCHAR)];
if (pszName == NULL)
goto cleanup;
}
for(;;)
{
cch = cchMax;
if (::RegEnumKeyEx(hSubKey, 0, pszName, &cch, NULL,
NULL, NULL, NULL))
{
break;
}
ret = RegDeleteTreeDx(hSubKey, pszName);
if (ret)
goto cleanup;
}
if (pszSubKey != NULL)
{
ret = ::RegDeleteKey(hKey, pszSubKey);
}
else
{
// NOTE: if pszSubKey was NULL, then delete value entries.
for (;;)
{
cch = cchMax;
if (::RegEnumValue(hKey, 0, pszName, &cch,
NULL, NULL, NULL, NULL))
{
break;
}
ret = ::RegDeleteValue(hKey, pszName);
if (ret)
goto cleanup;
}
}
cleanup:
if (pszSubKey != NULL)
::RegCloseKey(hSubKey);
if (pszName != szNameBuf)
delete[] pszName;
return ret;
}
inline /*static*/ DWORD MRegKey::MultiSzSizeDx(LPCTSTR pszz)
{
DWORD siz = 0;
if (*pszz)
{
do
{
INT len = lstrlen(pszz);
siz += len + 1;
pszz += len + 1;
}
while (*pszz);
}
else
{
++siz;
}
++siz;
siz *= static_cast<DWORD>(sizeof(TCHAR));
return siz;
}
inline /*static*/ HKEY MRegKey::CloneHandleDx(HKEY hKey)
{
if (hKey == NULL)
return NULL;
HANDLE hProcess = ::GetCurrentProcess();
HANDLE hDup = NULL;
::DuplicateHandle(hProcess, hKey, hProcess, &hDup, 0,
FALSE, DUPLICATE_SAME_ACCESS);
return reinterpret_cast<HKEY>(hDup);
}
////////////////////////////////////////////////////////////////////////////
#endif // ndef MZC4_MREGKEY_HPP_