-
Notifications
You must be signed in to change notification settings - Fork 7
/
creg_registry.cpp
1557 lines (1130 loc) · 40.3 KB
/
creg_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
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
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Imported, modified, and debugged by: Majdi Sobain <[email protected]> 2016
Modifications have comments started with *** or *****
Debugging was on Dev-C++ 5.11 and MSVC++ 10.0
All credit goes to the auther <Stuart Konen> and his CodeProject article on :
http://www.codeproject.com/Articles/8953/Registry-Wrapper-Class-CRegistry
-------------------------------------------------------------------------------------
// --- FILE INFORMATION -----------------------------
CRegistry.cpp *** All of filenames and headers have been renamed
Classes: CRegEntry and CRegistry *** for better cosmetic appearance in ring source files
Author: Stuart Konen
Email: [email protected]
Date: 12/1/2004 (MM/DD/YYYY)
Version: 1.00
(-!-) If you're going to use these classes please do not
remove these comments... To use these classes, simply #include
Registry.h . In MFC you should also turn off precompiled headers
for this file, in VC++ this can be done by navigating to:
Project->Settings->Project Name->CRegistry.cpp->C/C++->Precompiled Headers
*///----------------------------------------------------
#include "creg_registry.h"
#pragma warning (disable : 4706)
/* ===================================================
* CONSTRUCTOR
* =================================================*/
CRegEntry::CRegEntry(CRegistry *Owner) {
// *** assert(Owner); // To help ignore creating an object without owner (Owner = NULL)
InitData(Owner);
}
/* ===================================================
* CRegEntry::InitData(CRegistry *Owner)
*
* Initializes the entries default values and sets the entries
* owner (CRegistry). This is only called during construction.
*/
void CRegEntry::InitData(CRegistry *Owner) {
dwDWORD = iType = 0;
lpszName = lpszStr = NULL;
__bStored = false;
__bConvertable = true;
if (Owner) __cregOwner = Owner;
}
/* *** ===================================================
* CRegEntry::SetOwner(CRegistry *Owner)
*
* This method help in transfering the entry from one owner (CRegistry) to another
* *** This method has been updated to help real transfer(copy), not just the name
*/
void CRegEntry::SetOwner(CRegistry* Owner) {
switch (iType) {
case REG_DWORD: {
DWORD dwvalue = (DWORD) __cregOwner[0][lpszName];
assert(Owner);
__cregOwner = Owner;
__cregOwner[0][lpszName] = ( (dwvalue) ? dwvalue : 0 );
break;
}
case REG_SZ: {
LPCTSTR szvalue = __cregOwner[0][lpszName];
assert(Owner);
__cregOwner = Owner;
__cregOwner[0][lpszName] = ( (szvalue) ? szvalue : TEXT("") );
break;
}
case REG_MULTI_SZ: {
LPCTSTR szvalue = new _TCHAR[_MAX_REG_VALUE];
__cregOwner[0][lpszName].GetMulti((LPTSTR) szvalue);
assert(Owner);
__cregOwner = Owner;
__cregOwner[0][lpszName].SetMulti(szvalue, MultiLength());
break;
}
case REG_BINARY: {
size_t n = vBytes.size();
LPBYTE pbvalue = new BYTE[n];
__cregOwner[0][lpszName].GetBinary(pbvalue, n);
assert(Owner);
__cregOwner = Owner;
__cregOwner[0][lpszName].SetBinary(pbvalue, n);
break;
}
case REG_EXPAND_SZ: {
LPTSTR eszvalue = new _TCHAR[_MAX_REG_VALUE];
eszvalue = __cregOwner[0][lpszName].GetExpandSZ();
assert(Owner);
__cregOwner = Owner;
__cregOwner[0][lpszName].SetExpandSZ(eszvalue);
break;
}
case REG_QWORD: {
UINT64 value;
value = __cregOwner[0][lpszName].GetQWORD();
assert(Owner);
__cregOwner = Owner;
__cregOwner[0][lpszName].SetQWORD(value);
break;
}
}
}
/* ===================================================
* CRegEntry::ForceStr()
*
* Forces the memory allocation for the entry's string value, if it
* has not already been allocated.
*/
void CRegEntry::ForceStr() {
if (lpszStr == NULL) { lpszStr = new _TCHAR[_MAX_REG_VALUE]; lpszStr[0] = 0; }
}
/* *** ===================================================
* CRegEntry::SetName(CRegEntry* creg, LPCSTR name)
*
* *** This is newly created method to help change the name of the given entry
* *** This method refect the change directly into the registry
*/
void CRegEntry::SetName(LPCSTR name) {
// *** its very important to insure that name is not empty
// *** I will do that in the wrapper ring_vmwincreg.cpp for more control
switch (iType) {
case REG_DWORD: {
DWORD dwvalue = (DWORD) __cregOwner[0][lpszName];
this->Delete();
this->lpszName = (LPTSTR) name;
__cregOwner[0][lpszName] = ( (dwvalue) ? dwvalue : 0 );
break;
}
case REG_SZ: {
LPCTSTR value = __cregOwner[0][lpszName];
this->Delete();
this->lpszName = (LPTSTR) name;
__cregOwner[0][lpszName] = ( (value) ? value : TEXT("") );
break;
}
case REG_MULTI_SZ: {
LPCTSTR szvalue = new _TCHAR[_MAX_REG_VALUE];
__cregOwner[0][lpszName].GetMulti((LPTSTR) szvalue);
this->Delete();
this->lpszName = (LPTSTR) name;
__cregOwner[0][lpszName].SetMulti(szvalue, MultiLength());
break;
}
case REG_BINARY: {
size_t n = vBytes.size();
LPBYTE pbvalue = new BYTE[n];
__cregOwner[0][lpszName].GetBinary(pbvalue, n);
this->Delete();
this->lpszName = (LPTSTR) name;
__cregOwner[0][lpszName].SetBinary(pbvalue, n);
break;
}
case REG_EXPAND_SZ: {
LPTSTR eszvalue = new _TCHAR[_MAX_REG_VALUE];
eszvalue = __cregOwner[0][lpszName].GetExpandSZ();
this->Delete();
this->lpszName = (LPTSTR) name;
__cregOwner[0][lpszName].SetExpandSZ(eszvalue);
break;
}
case REG_QWORD: {
UINT64 value;
value = __cregOwner[0][lpszName].GetQWORD();
this->Delete();
this->lpszName = (LPTSTR) name;
__cregOwner[0][lpszName].SetQWORD(value);
break;
}
}
}
/* ===================================================
* CRegEntry::operator=(LPCTSTR lpszValue)
*
* OPERATOR: Assigns a const character array to the open
* registry value. The registry value type will be REG_SZ.
*/
CRegEntry& CRegEntry::operator=(LPCTSTR lpszValue) {
size_t nValueLen = (_tcslen(lpszValue) + 1)*sizeof(TCHAR);
assert(nValueLen <= _MAX_REG_VALUE);
ForceStr(); iType = REG_SZ;
_tcsncpy(lpszStr, lpszValue, nValueLen > _MAX_REG_VALUE ? _MAX_REG_VALUE : nValueLen);
REGENTRY_ALLOWCONV(true)
if (REGENTRY_NOTLOADING && REGENTRY_KEYVALID( KEY_SET_VALUE ))
RegSetValueEx(__cregOwner->hKey, lpszName, NULL, REG_SZ, (LPBYTE)lpszValue, nValueLen);
REGENTRY_TRYCLOSE;
__bStored = true;
return *this;
}
/* ===================================================
* CRegEntry::operator=(LPDWORD lpdwValue)
*
* OPERATOR: Assigns a DWORD to the open registry value.
* The registry value type will be REG_DWORD.
*/
CRegEntry& CRegEntry::operator=(LPDWORD lpdwValue) {
iType = REG_DWORD;
memcpy(&dwDWORD, lpdwValue, sizeof( DWORD ));
REGENTRY_ALLOWCONV(true)
if (REGENTRY_NOTLOADING && REGENTRY_KEYVALID( KEY_SET_VALUE ))
RegSetValueEx(__cregOwner->hKey, lpszName, NULL, REG_DWORD, (LPBYTE)&dwDWORD, sizeof( DWORD ));
REGENTRY_TRYCLOSE;
__bStored = true;
return *this;
}
/* ===================================================
* CRegEntry::operator=(CRegEntry& cregValue)
*
* OPERATOR: Copys value information from the specified
* registry entry (CRegEntry) into this entry.
*/
CRegEntry& CRegEntry::operator=(CRegEntry& cregValue) {
if (this == &cregValue)
return *this;
if (lpszName == NULL) {
size_t nNameLen = _tcslen(cregValue.lpszName) + 1;
lpszName = new _TCHAR[nNameLen]; _tcsncpy(lpszName, cregValue.lpszName, nNameLen);
}
switch ((iType = cregValue.iType)) {
case REG_SZ:
return (*this = (ForceStr(), cregValue.lpszStr));
break;
case REG_MULTI_SZ: {
LPTSTR lpszBuf = new _TCHAR[_MAX_REG_VALUE];
SetMulti(cregValue.GetMulti(lpszBuf), cregValue.MultiLength());
delete [] lpszBuf; return *this;
}
break;
case REG_BINARY: {
size_t n = cregValue.vBytes.size(); LPBYTE buf = new BYTE[n];
cregValue.GetBinary(buf, n); SetBinary(buf, n);
delete [] buf; return *this;
}
break;
case REG_EXPAND_SZ: {
SetExpandSZ(cregValue.GetExpandSZ());
return *this;
}
break;
case REG_QWORD: {
SetQWORD(cregValue.GetQWORD());
return *this;
}
break;
default:
return (*this = cregValue.dwDWORD);
}
}
/* ===================================================
* CRegEntry::operator LPTSTR()
*
* OPERATOR: Converts (if required) and returns the open registry
* value as a null terminated string.
*/
CRegEntry::operator LPTSTR() {
/* If caching is disabled, refresh the entries */
REGENTRY_REFRESH_IF_NOCACHE
assert(__bConvertable); // Check for conversion implementation
ForceStr();
switch (iType) {
case REG_DWORD:
_stprintf(lpszStr, _T("%lu"), dwDWORD);
break;
case REG_MULTI_SZ:
GetMulti(lpszStr);
break;
case REG_BINARY: {
_tcsncpy(lpszStr, (const _TCHAR*)&vBytes[0], vBytes.size());
lpszStr[vBytes.size()] = 0;
}
break;
case REG_EXPAND_SZ:
GetExpandSZ(true); // lpszStr will be prepared and then returned
break;
case REG_QWORD:
// Return lpszStr as it actually contains the value
break;
}
return lpszStr;
}
/* ===================================================
* CRegEntry::operator DWORD()
*
* OPERATOR: Converts (if required) and returns the open registry
* value as an unsigned 32-bit integer (unsigned long).
*/
CRegEntry::operator DWORD() {
/* If caching is disabled, refresh the entries */
REGENTRY_REFRESH_IF_NOCACHE
assert(__bConvertable); // Check for conversion implementation
REGENTRY_BINARYTOSTRING
return (REGENTRY_SZ_SAFE ? _tcstoul(lpszStr, NULL, NULL) : dwDWORD);
}
/* ===================================================
* *** newly added to substitute the automatically created one by
* REGENTRY_CONV_NUMERIC_STORAGETYPE MACRO
*
* OPERATOR: Assigns a double to the open registry value.
* The registry value type will be REG_SZ.
*/
CRegEntry& CRegEntry::operator=(double Value) {
_R_BUF(23); // *** 23 is because the upper limit of numbers in ring is 15 digit or point {pluse} .000000 of
// *** _stprintf_s in case all of 15 are numbers {plus} terminal 0 (Numeric Overflow! Error)
_stprintf_s(buffer, (23 * sizeof(TCHAR)) ,_T("%f"), Value);
for (int i = ( ( _tcslen(buffer) -1 ) * sizeof(TCHAR) ); i >= 0; i-- ) {
if ( !strcmp( &buffer[(i-1)] , (LPCTSTR) ".0" ) ) {
break; // *** This peice of code made to save such 18.000000 number to be just 18.0
}
if ( !strcmp( &buffer[i], (LPCTSTR) "0" ) ) {
buffer[i] = 0;
buffer[i+1] = -2;
} else {
/* if ( !strcmp( &buffer[i] , (LPCTSTR) "." ) ) {
buffer[i] = 0;
buffer[i+1] = -2; // *** This peice of code made to save such 18.000000 number to be just 18
} */
break;
}
}
return (*this = (LPCTSTR)(buffer));
}
/* ===================================================
* *** newly added to substitute the automatically created one by
* REGENTRY_CONV_NUMERIC_STORAGETYPE MACRO
*
* CRegEntry::operator double()
*
* OPERATOR: Converts (if required) and returns the open registry
* value as a double.
*/
CRegEntry::operator double() {
REGENTRY_BINARYTOSTRING
return (REGENTRY_SZ_SAFE ? _tcstod(lpszStr, NULL) :(iType == REG_DWORD ? (double)dwDWORD : 0));
}
/* ===================================================
* CRegEntry::GetBinary(LPBYTE lpbValue, size_t nLen)
*
* Sets the registry value to a binary value (REG_BINARY)
*
* Important Params:
*
* LPBYTE lpbDest: Pointer to the byte array to store *
* size_t nLen: Elements contained within the byte array.
*/
void CRegEntry::SetBinary(LPBYTE lpbValue, size_t nLen) {
// *** if (!nLen) { assert(nLen); return; } // needed to be deactivated to create empty binary value
iType = REG_BINARY;
if (REGENTRY_NOTLOADING && REGENTRY_KEYVALID ( KEY_SET_VALUE ) )
RegSetValueEx(__cregOwner->hKey, lpszName, NULL, REG_BINARY, lpbValue, nLen);
REGENTRY_TRYCLOSE;
__bStored = true;
if (vBytes.size() < nLen) vBytes.reserve(nLen);
vBytes.clear();
// *** change vBytes check to avoid fill the bytes list if there is no value in reality
while (vBytes.size() < nLen) { vBytes.push_back(lpbValue[vBytes.size()]); }
}
/* ===================================================
* *** updated Exists function to query even those values of unkown
* types to this class
*
* bool CRegEntry::Exists()
*
* Checks whether the value entry exists or not.
*
* Note: This function updated to return the result of RegQueryValueEx
*/
DWORD CRegEntry::Exists () {
DWORD Res;
assert( REGENTRY_KEYVALID ( KEY_QUERY_VALUE ) );
/* This piece of code used if we want to return ONLY (True) or (False)
if ( RegQueryValueEx(__cregOwner->hKey, lpszName, NULL, NULL, NULL, NULL); == ERROR_SUCCESS ) {
REGENTRY_TRYCLOSE ;
return true;
} else {
REGENTRY_TRYCLOSE ;
return false;
} */
Res = RegQueryValueEx(__cregOwner->hKey, lpszName, NULL, NULL, NULL, NULL);
__cregOwner->AutoClose();
return Res;
}
/* ===================================================
* *** unsigned char CRegEntry::Type()
*
* newly added function that returns the type of the entry as a number.
*
* Note: This function if succeed will return the type but if not it will return the error code Summated to 12
*/
DWORD CRegEntry::Type() {
LONG res;
DWORD type;
assert( REGENTRY_KEYVALID ( KEY_QUERY_VALUE ) );
res = RegQueryValueEx(__cregOwner->hKey, lpszName, NULL, &type, NULL, NULL);
REGENTRY_TRYCLOSE ;
if ( res == ERROR_SUCCESS ) {
return type;
} else {
return res + 12;
}
}
/* ===================================================
* CRegEntry::GetBinary(LPBYTE lpbDest, size_t nMaxLen)
*
* Gets the binary value of a value stored as REG_BINARY
*
* Important Params:
*
* LPBYTE lpbDest: Pointer to the byte array to fill
* size_t nMaxLen: The maximum bytes to copy to lpbDest
*
* Notes: This will only work for values that were saved
* using the binary registry type specification (REG_BINARY)
*/
void CRegEntry::GetBinary(LPBYTE lpbDest, size_t nMaxLen) {
assert(IsBinary()); // Must be stored as Binary
REGENTRY_REFRESH_IF_NOCACHE
/* *** adding this check to ignore retrieving data if nMazLen is 0 */
if (nMaxLen > 0)
if ((size_t)(&vBytes.back() - &vBytes.at(0)+1) == vBytes.size()*sizeof(BYTE))
memcpy(lpbDest, (LPBYTE)&vBytes.at(0), vBytes.size() > nMaxLen ? nMaxLen : vBytes.size());
else
for (size_t n=0; n < vBytes.size() && n < nMaxLen; n++)
lpbDest[n] = vBytes[n];
}
/* ===================================================
* CRegEntry::GetBinaryLength()
*
* Returns the size of the binary value in bytes.
*/
size_t CRegEntry::GetBinaryLength() {
assert(IsBinary());
REGENTRY_REFRESH_IF_NOCACHE
return vBytes.size();
}
/* ===================================================
* CRegEntry::SetMulti(LPCTSTR lpszValue, size_t nLen, bool bInternal)
*
* Stores an array of null-terminated string, terminated by two null characters.
* For Example: First String\0Second\Third\0\0
*
* Important Params:
*
* LPCTSTR lpszValue: The string consisting of the null-terminated string array
* size_t nLen: The number of characters in the string, including null characters
*
* Note: For inserting individual null-terminated strings into the array,
* use MultiAdd or MultiSetAt.
*/
void CRegEntry::SetMulti(LPCTSTR lpszValue, size_t nLen, bool bInternal) {
size_t nCur = 0, nPrev = 0, nShortLen = nLen;
/* When this is internal, there is no need to repopulate the vector. */
if (bInternal) goto SkipNoInternal;
iType = REG_MULTI_SZ; vMultiString.clear();
if (nLen <= 2) goto SkipNoInternal; // The string is empty : \0\0
if (*(lpszValue + nShortLen-1) == '\0')
nShortLen--;
/* Populate a vector with each string part for easy and quick access */
while ((nCur = (int)(_tcschr(lpszValue+nPrev, '\0')-lpszValue)) < nShortLen) {
vMultiString.push_back(lpszValue+nPrev);
nPrev = nCur+1;
}
SkipNoInternal:
if (REGENTRY_NOTLOADING && REGENTRY_KEYVALID ( KEY_SET_VALUE ) )
RegSetValueEx(__cregOwner->hKey, lpszName, NULL, REG_MULTI_SZ, (LPBYTE)lpszValue, nLen*sizeof(TCHAR));
REGENTRY_TRYCLOSE;
__bStored = true;
}
/* ===================================================
* CRegEntry::MultiLength(bool bInternal = false)
*
* Returns the number of characters (including null) stored
* in the full string. Don't confuse this with MultiCount()
* which returns the number of strings stored in the array.
*/
size_t CRegEntry::MultiLength(bool bInternal /*false*/) {
/*** nLen Definition seperation from for loop as it was like this:
*** for (size_t nLen = 0, nIndex = 0; nIndex < vMultiString.size(); nIndex++) */
size_t nLen = 0;
//Ensure correct values with no cache
if (!bInternal) REGENTRY_REFRESH_IF_NOCACHE
for (size_t nIndex = 0; nIndex < vMultiString.size(); nIndex++)
nLen += vMultiString[nIndex].length() + 1;
return nLen ? nLen+1 : 0;
}
/* ===================================================
* CRegEntry::MultiCount()
*
* Returns the number of strings located within the array.
*/
size_t CRegEntry::MultiCount() {
// Ensure correct values with no cache
REGENTRY_REFRESH_IF_NOCACHE
return vMultiString.size();
}
/* ===================================================
* CRegEntry::MultiRemoveAt(size_t nIndex)
*
* Simply removes the string stored at the zero-based index of nIndex
*/
void CRegEntry::MultiRemoveAt(size_t nIndex) {
// Ensure correct values with no cache
REGENTRY_REFRESH_IF_NOCACHE
assert(nIndex < vMultiString.size());
vMultiString.erase(vMultiString.begin()+nIndex);
// Update the registry
REGENTRY_UPDATE_MULTISTRING
}
/* ===================================================
* CRegEntry::MultiSetAt(size_t nIndex, LPCTSTR lpszVal)
*
* Alters the value of a string in the array located at
* the 0 based index of nIndex. The new value is lpszVal.
* The index must be within the bounds of the array, with
* the exception of being == the number of elements in
* which case calling this function is equal to calling
* MultiAdd.
*/
void CRegEntry::MultiSetAt(size_t nIndex, LPCTSTR lpszVal) {
// Ensure correct values with no cache
REGENTRY_REFRESH_IF_NOCACHE
// *** assert(nIndex > vMultiString.size()) ;
assert(nIndex <= vMultiString.size()) ;
iType = REG_MULTI_SZ;
// Add a new string element if == elements+1
if (nIndex == vMultiString.size())
vMultiString.push_back(lpszVal);
else
vMultiString[nIndex] = lpszVal;
// Update the registry
REGENTRY_UPDATE_MULTISTRING
}
/* ===================================================
* CRegEntry::MultiGetAt(size_t nIndex)
*
* Returns a constant pointer to the string located in
* the array at the zero-based index of nIndex. Note that
* the return value is not an STL string.
*/
LPCTSTR CRegEntry::MultiGetAt(size_t nIndex) {
// Ensure correct values with no cache
REGENTRY_REFRESH_IF_NOCACHE
assert(nIndex < vMultiString.size() && IsMultiString());
return vMultiString[nIndex].c_str();
}
/* ===================================================
* CRegEntry::GetMulti(LPCTSTR lpszDest, size_t nMax)
*
* Copys the entire null-terminated array string to lpszDest.
* For Example: First String\0Second\Third\0\0
*
* Important Params:
*
* LPCTSTR lpszDest: Pointer to the character array to fill.
* size_t nMax: The maximum characters to read, including null-characters
*
* Note: By default nMax is set to _MAX_REG_VALUE, you can retrieve
* the length of the entire string by calling MultiLength().
*/
LPTSTR CRegEntry::GetMulti(LPTSTR lpszDest, size_t nMax) {
LPCTSTR strBuf;
size_t nCur = 0, nLen = 0;
if (!lpszDest) lpszDest = new TCHAR[_MAX_REG_VALUE]; // *** added to help call this function without params
assert(IsMultiString());
// *** if (!IsMultiString()) return &(lpszDest[0] = 0);
/* If caching is disabled, refresh the entries */
REGENTRY_REFRESH_IF_NOCACHE
for (size_t n=0; n < vMultiString.size() && nCur < nMax; n++) {
strBuf = vMultiString[n].c_str();
nLen = vMultiString[n].length()+1;
_tcsncpy(lpszDest + nCur, strBuf, (nLen >= nMax ? (nMax-nCur) : nLen) * sizeof(_TCHAR));
nCur += nLen;
}
/* Add the final null termination */
*(lpszDest + nCur) = 0;
return lpszDest;
}
/* ===================================================
* *** newly added function
*
* CRegEntry::GetExpandSZ(bool Expandable)
*
* retrive the value (NULL terminated string) of REG_EXPAND_SZ type directly from registry
*
*/
LPTSTR CRegEntry::GetExpandSZ(bool Expandable) {
assert(IsExpandSZ());
REGENTRY_REFRESH_IF_NOCACHE
if (Expandable) {
HANDLE dToken;
LPTSTR tmpStr = new TCHAR[(_tcslen(lpszStr) *sizeof(TCHAR))];
DWORD vSize = _MAX_REG_VALUE * 16;
_tcscpy(tmpStr, lpszStr);
lpszStr = new TCHAR[vSize];
ExpandEnvironmentStrings(tmpStr, lpszStr, vSize);
} else {
__cregOwner->Refresh(); // Refresh lpszStr if it contains the expanded form
}
REGENTRY_TRYCLOSE ;
return lpszStr;
}
/* ===================================================
* *** newly added function
*
* CRegEntry::SetExpandSZ(LPTSTR value)
*
* set a value (NULL terminated string) of REG_EXPAND_SZ type directly to registry
*
* Note : this function also retreive the important information about the value in the time of loading
*/
DWORD CRegEntry::SetExpandSZ(LPTSTR value) {
DWORD svalue = ( _tcslen(value) + 1 ) * sizeof(TCHAR);
assert(svalue <= _MAX_REG_VALUE);
lpszStr = new TCHAR[_MAX_REG_VALUE];
_tcscpy(lpszStr, value);
iType = REG_EXPAND_SZ;
if (REGENTRY_NOTLOADING && REGENTRY_KEYVALID( KEY_SET_VALUE )) {
svalue = RegSetValueEx(__cregOwner->hKey, lpszName, NULL, REG_EXPAND_SZ, (LPBYTE) value, svalue);
}
__bStored = true;
REGENTRY_TRYCLOSE;
return svalue;
}
/* ===================================================
* *** newly added function
*
* CRegEntry::SetQWORD(bool Expandable)
*
* set a value of REG_QWORD (unsigned long long) type directly to registry
*
*/
DWORD CRegEntry::SetQWORD(UINT64 value) {
lpszStr = new TCHAR[21]; // limit of largest value of QWORD range (20) + NULL termination
DWORD lRes = 0;
iType = REG_QWORD;
_stprintf(lpszStr, _T("%llu"), value);
if (REGENTRY_NOTLOADING && REGENTRY_KEYVALID( KEY_SET_VALUE )) {
lRes = RegSetValueEx(__cregOwner->hKey, lpszName, NULL, REG_QWORD, (LPBYTE) &value, sizeof ( UINT64 ));
}
__bStored = true;
REGENTRY_TRYCLOSE;
return lRes;
}
/* ===================================================
* *** newly added function
*
* UINT64 CRegEntry::GetQWORD()
*
* retrive the value of REG_QWORD type directly from registry
*
*/
UINT64 CRegEntry::GetQWORD() {
UINT64 value = 0;
DWORD size = sizeof(UINT64);
assert(IsQWORD());
REGENTRY_REFRESH_IF_NOCACHE
value = _tcstoui64(lpszStr, NULL, 10);
REGENTRY_TRYCLOSE ;
return value;
}
/* ===================================================
* CRegEntry::Delete()
*
* Removes the value from the open registry key, returns
* true on success and false on failure.
*/
bool CRegEntry::Delete() {
__bStored = false;
if (REGENTRY_KEYVALID (KEY_SET_VALUE) ) {
RegDeleteValue(__cregOwner->hKey, lpszName) ;
__cregOwner->AutoClose();
return true;
}
return false;
}
// BEGIN CREGISTRY FUNCTIONS
/* ===================================================
* CRegistry CONSTRUCTOR
*
* Flags:
*
* CREG_CREATE (default) - When attempting to open a key that
* does not exist, create it.
*
* CREG_AUTOOPEN - Close the open registry key handle
* after an action has been performed with it. Opens the
* key whenever another action needs to be performed.
*
* ===================================================*/
CRegistry::CRegistry(DWORD flags,bool Acc64Tree) {
InitData();
Enable64Tree = Acc64Tree;
__dwFlags = flags;
}
/* ===================================================
* CRegistry::InitData()
* Initializes the variables related to key locations to NULL.
*/
void CRegistry::InitData() {
_lpszSubKey = NULL;
_hRootKey = hKey = NULL;
}
/* ===================================================
* CRegistry::operator []( LPCTSTR lpszVName)
*
* OPERATOR: This will return the Registry Entry (CRegEntry) associated
* with the given value name. If the value name does not exist in
* the open key, it will be created.
*
* Note: If the value name is created, it is only stored in the actual
* registry when the entry's value has been set.
*/
CRegEntry& CRegistry::operator []( LPCTSTR lpszVName) {
size_t nValueNameLen = _tcslen(lpszVName) + 1;
assert(nValueNameLen <= _MAX_REG_VALUE);
for (int i = _reEntries.size()-1; i >=0; i--) {
if (!_tcsicmp(lpszVName, _reEntries[i]->lpszName))
return *_reEntries[i];
}
/* Entry not found */
_reEntries.push_back(new CRegEntry(this));
_reEntries.back()->lpszName = new _TCHAR[nValueNameLen];
_tcsncpy(_reEntries.back()->lpszName, lpszVName, (nValueNameLen > _MAX_REG_VALUE ? _MAX_REG_VALUE : nValueNameLen));
return *_reEntries.back();
}