Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 0.8, 0.8.1, and 0.8.2

shellext: remove unused Registry.h and .cpp

Changeset 925ccc0902ae

Parent 8f3b519cb416

by Adrian Buehlmann

Changes to 2 files · Browse files at 925ccc0902ae Showing diff from parent 8f3b519cb416 Diff from another changeset...

Change 1 of 1 Show Entire File win32/​shellext/​Registry.cpp Stacked
 
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
@@ -1,799 +0,0 @@
- - -//#include "stdafx.h" -#include <windows.h> -#include <winreg.h> -#include "Registry.h" - -#define CLASS_NAME_LENGTH 255 - -/* IMPORTANT NOTES ABOUT CREGISTRY: - - CRegistry never keeps a key open past the end of a function call. - This is incase the application crashes before the next call to close - the registry - - INCLUDE FILES - "winreg.h" and "afxdisp.h" must be included in "stdafx.h" - - KEY NAMES: - Key names must not begin with a \ and only absolute strings are accepted - -*/ - - - -CRegistry::CRegistry() -{ - m_hRootKey = HKEY_CURRENT_USER; - m_bLazyWrite = TRUE; - m_nLastError = ERROR_SUCCESS; -} - -CRegistry::~CRegistry() -{ - ClearKey(); -} - - -BOOL CRegistry::ClearKey() -{ - /* Call CloseKey to write the current key to the registry and close the - key. An application should not keep keys open any longer than necessary. - Calling CloseKey when there is no current key has no effect.*/ - - m_strCurrentPath.Empty(); - m_hRootKey = HKEY_CURRENT_USER; - m_bLazyWrite = TRUE; - return TRUE; -} - - - -BOOL CRegistry::SetRootKey(HKEY hRootKey) -{ - // sets the root key - // make sure to set it to a valid key - if (hRootKey != HKEY_CLASSES_ROOT && - hRootKey != HKEY_CURRENT_USER && - hRootKey != HKEY_LOCAL_MACHINE && - hRootKey != HKEY_USERS) return FALSE; - - m_hRootKey = hRootKey; - return TRUE; -} - - -BOOL CRegistry::CreateKey(CString strKey) -{ - /* Use CreateKey to add a new key to the registry. - Key is the name of the key to create. Key must be - an absolute name. An absolute key - begins with a backslash (\) and is a subkey of - the root key. */ - - ASSERT(strKey[0] != '\\'); - HKEY hKey; - - DWORD dwDisposition = 0; - - if (::RegCreateKeyEx(m_hRootKey, LPCTSTR(strKey), 0, NULL, - REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, - &dwDisposition) != ERROR_SUCCESS) return FALSE; - - if (!m_bLazyWrite) ::RegFlushKey(hKey); - ::RegCloseKey(hKey); - m_strCurrentPath = strKey; - return TRUE; -} - - -BOOL CRegistry::DeleteKey(CString strKey) -{ - /* Call DeleteKey to remove a specified key and its associated data, - if any, from the registry. Returns FALSE is there are subkeys - Subkeys must be explicitly deleted by separate calls to DeleteKey. - DeleteKey returns True if key deletion is successful. On error, - DeleteKey returns False. */ - - // need to open the key first with RegOpenKeyEx - ASSERT(FALSE); // not yet implemented - ASSERT(strKey[0] != '\\'); - - if (!KeyExists(strKey)) return TRUE; - if (::RegDeleteKey(m_hRootKey, strKey) != ERROR_SUCCESS) return FALSE; - return TRUE; -} - - - -BOOL CRegistry::DeleteValue(CString strName) -{ - /* Call DeleteValue to remove a specific data value - associated with the current key. Name is string - containing the name of the value to delete. Keys can contain - multiple data values, and every value associated with a key - has a unique name. */ - - ASSERT(m_strCurrentPath.GetLength() > 0); - HKEY hKey; - LONG lResult; - - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_SET_VALUE, &hKey) != ERROR_SUCCESS) return FALSE; - - lResult = ::RegDeleteValue(hKey, LPCTSTR(strName)); - ::RegCloseKey(hKey); - - if (lResult == ERROR_SUCCESS) return TRUE; - return FALSE; -} - - -int CRegistry::GetDataSize(CString strValueName) -{ - /* Call GetDataSize to determine the size, in bytes, of - a data value associated with the current key. ValueName - is a string containing the name of the data value to query. - On success, GetDataSize returns the size of the data value. - On failure, GetDataSize returns -1. */ - - HKEY hKey; - ASSERT(m_strCurrentPath.GetLength() > 0); - LONG lResult; - - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS) return -1; - - DWORD dwSize = 1; - lResult = ::RegQueryValueEx(hKey, LPCTSTR(strValueName), - NULL, NULL, NULL, &dwSize); - ::RegCloseKey(hKey); - - if (lResult != ERROR_SUCCESS) return -1; - return (int)dwSize; -} - -DWORD CRegistry::GetDataType(CString strValueName) -{ - HKEY hKey; - ASSERT(m_strCurrentPath.GetLength() > 0); - - m_nLastError = ::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_QUERY_VALUE, &hKey); - - if (m_nLastError != ERROR_SUCCESS) return 0; - - DWORD dwType = 1; - m_nLastError = ::RegQueryValueEx(hKey, LPCTSTR(strValueName), - NULL, &dwType, NULL, NULL); - ::RegCloseKey(hKey); - - if (m_nLastError == ERROR_SUCCESS) return dwType; - - return 0; -} - - - -int CRegistry::GetSubKeyCount() -{ - /* Call this function to determine the number of subkeys. - the function returns -1 on error */ - HKEY hKey; - ASSERT(m_strCurrentPath.GetLength() > 0); - - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS) return -1; - - LONG lResult; - DWORD dwSubKeyCount, dwValueCount, dwClassNameLength, - dwMaxSubKeyName, dwMaxValueName, dwMaxValueLength; - FILETIME ftLastWritten; - - _TCHAR szClassBuffer[CLASS_NAME_LENGTH]; - - dwClassNameLength = CLASS_NAME_LENGTH; - lResult = ::RegQueryInfoKey(hKey, szClassBuffer, &dwClassNameLength, - NULL, &dwSubKeyCount, &dwMaxSubKeyName, NULL, &dwValueCount, - &dwMaxValueName, &dwMaxValueLength, NULL, &ftLastWritten); - - ::RegCloseKey(hKey); - if (lResult != ERROR_SUCCESS) return -1; - - return (int)dwSubKeyCount; -} - - -int CRegistry::GetValueCount() -{ - /* Call this function to determine the number of subkeys. - the function returns -1 on error */ - HKEY hKey; - ASSERT(m_strCurrentPath.GetLength() > 0); - - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS) return -1; - - LONG lResult; - DWORD dwSubKeyCount, dwValueCount, dwClassNameLength, - dwMaxSubKeyName, dwMaxValueName, dwMaxValueLength; - FILETIME ftLastWritten; - - _TCHAR szClassBuffer[CLASS_NAME_LENGTH]; - - dwClassNameLength = CLASS_NAME_LENGTH; - lResult = ::RegQueryInfoKey(hKey, szClassBuffer, &dwClassNameLength, - NULL, &dwSubKeyCount, &dwMaxSubKeyName, NULL, &dwValueCount, - &dwMaxValueName, &dwMaxValueLength, NULL, &ftLastWritten); - - ::RegCloseKey(hKey); - if (lResult != ERROR_SUCCESS) return -1; - - return (int)dwValueCount; -} - - -BOOL CRegistry::KeyExists(CString strKey, HKEY hRootKey) -{ - /* Call KeyExists to determine if a key of a specified name exists. - Key is the name of the key for which to search. */ - - ASSERT(strKey[0] != '\\'); - HKEY hKey; - - if (hRootKey == NULL) hRootKey = m_hRootKey; - - LONG lResult = ::RegOpenKeyEx(hRootKey, LPCTSTR(strKey), 0, - KEY_ALL_ACCESS, &hKey); - ::RegCloseKey(hKey); - if (lResult == ERROR_SUCCESS) return TRUE; - return FALSE; -} - -BOOL CRegistry::SetKey(CString strKey, BOOL bCanCreate) -{ - /* Call SetKey to make a specified key the current key. Key is the - name of the key to open. If Key is null, the CurrentKey property - is set to the key specified by the RootKey property. - - CanCreate specifies whether to create the specified key if it does - not exist. If CanCreate is True, the key is created if necessary. - - Key is opened or created with the security access value KEY_ALL_ACCESS. - OpenKey only creates non-volatile keys, A non-volatile key is stored in - the registry and is preserved when the system is restarted. - - OpenKey returns True if the key is successfully opened or created */ - - ASSERT(strKey[0] != '\\'); - HKEY hKey; - - - // close the current key if it is open - if (strKey.GetLength() == 0) - { - m_strCurrentPath.Empty(); - return TRUE; - } - - DWORD dwDisposition; - if (bCanCreate) // open the key with RegCreateKeyEx - { - if (::RegCreateKeyEx(m_hRootKey, LPCTSTR(strKey), 0, NULL, - REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, - &dwDisposition) != ERROR_SUCCESS) return FALSE; - m_strCurrentPath = strKey; - if (!m_bLazyWrite) ::RegFlushKey(hKey); - ::RegCloseKey(hKey); - return TRUE; - } - - // otherwise, open the key without creating - // open key requires no initial slash - m_nLastError = ::RegOpenKeyEx(m_hRootKey, LPCTSTR(strKey), 0, - KEY_ALL_ACCESS, &hKey); - if (m_nLastError != ERROR_SUCCESS) return FALSE; - m_strCurrentPath = strKey; - if (!m_bLazyWrite) ::RegFlushKey(hKey); - ::RegCloseKey(hKey); - return TRUE; -} - - -BOOL CRegistry::ValueExists(CString strName) -{ - /* Call ValueExists to determine if a particular key exists in - the registry. Calling Value Exists is especially useful before - calling other TRegistry methods that operate only on existing keys. - - Name is the name of the data value for which to check. - ValueExists returns True if a match if found, False otherwise. */ - - HKEY hKey; - LONG lResult; - ASSERT(m_strCurrentPath.GetLength() > 0); - - - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS) return FALSE; - - lResult = ::RegQueryValueEx(hKey, LPCTSTR(strName), NULL, - NULL, NULL, NULL); - ::RegCloseKey(hKey); - - if (lResult == ERROR_SUCCESS) return TRUE; - return FALSE; -} - - -void CRegistry::RenameValue(CString strOldName, CString strNewName) -{ - /* Call RenameValue to change the name of a data value associated - with the current key. OldName is a string containing the current - name of the data value. NewName is a string containing the replacement - name for the data value. - - If OldName is the name of an existing data value for the current key, - and NewName is not the name of an existing data value for the current - key, RenameValue changes the data value name as specified. Otherwise - the current name remains unchanged. - */ - ASSERT(FALSE); // functionality not yet implemented -} - - - - -COleDateTime CRegistry::ReadDateTime(CString strName, COleDateTime dtDefault) -{ - /* Call ReadDate to read a date value from a specified data value - associated with the current key. Name is the name of the data value to read. - If successful, ReadDate returns a Delphi TDateTime value. The integral part - of a TDateTime value is the number of days that have passed since 12/30/1899. - The fractional part of a TDateTime value is the time of day. - On error, an exception is raised, and the value returned by this function - should be discarded. */ - - DWORD dwType = REG_BINARY; - COleDateTime dt; - DWORD dwSize = sizeof(dt); - HKEY hKey; - - ASSERT(m_strCurrentPath.GetLength() > 0); - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_READ, &hKey) != ERROR_SUCCESS) return dtDefault; - if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL, - &dwType, (LPBYTE)&dt, &dwSize) != ERROR_SUCCESS) dt = dtDefault; - ::RegCloseKey(hKey); - return dt; -} - - -double CRegistry::ReadFloat(CString strName, double fDefault) -{ - /* Call ReadFloat to read a float value from a specified - data value associated with the current key. Name is the name - of the data value to read. - - If successful, ReadFloat returns a double value. - On error, an exception is raised, and the value returned by - this function should be discarded. */ - - DWORD dwType = REG_BINARY; - double d; - DWORD dwSize = sizeof(d); - HKEY hKey; - - ASSERT(m_strCurrentPath.GetLength() > 0); - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_READ, &hKey) != ERROR_SUCCESS) return fDefault; - - if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL, - &dwType, (LPBYTE)&d, &dwSize) != ERROR_SUCCESS) d = fDefault; - ::RegCloseKey(hKey); - return d; -} - -CString CRegistry::ReadString(CString strName, CString strDefault) -{ - DWORD dwType = REG_SZ; - DWORD dwSize = 255; - BOOL bSuccess = TRUE; - _TCHAR sz[255]; - HKEY hKey; - - - ASSERT(m_strCurrentPath.GetLength() > 0); - - // make sure it is the proper type - dwType = GetDataType(strName); - - if (dwType != REG_SZ && dwType != REG_EXPAND_SZ) - { - return strDefault; - } - - m_nLastError = ::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_READ, &hKey); - if (m_nLastError != ERROR_SUCCESS) return strDefault; - - m_nLastError = ::RegQueryValueEx(hKey, LPCTSTR(strName), NULL, - &dwType, (LPBYTE)sz, &dwSize); - if (m_nLastError != ERROR_SUCCESS) bSuccess = FALSE; - ::RegCloseKey(hKey); - - if (!bSuccess) return strDefault; - return CString((LPCTSTR)sz); -} - -DWORD CRegistry::ReadDword(CString strName, DWORD dwDefault) -{ - DWORD dwType = REG_DWORD; - DWORD dw; - DWORD dwSize = sizeof(dw); - HKEY hKey; - - ASSERT(m_strCurrentPath.GetLength() > 0); - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_READ, &hKey) != ERROR_SUCCESS) return dwDefault; - - if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL, - &dwType, (LPBYTE)&dw, &dwSize) != ERROR_SUCCESS) dw = dwDefault; - ::RegCloseKey(hKey); - return dw; -} - - - -int CRegistry::ReadInt(CString strName, int nDefault) -{ - DWORD dwType = REG_BINARY; - int n; - DWORD dwSize = sizeof(n); - HKEY hKey; - - ASSERT(m_strCurrentPath.GetLength() > 0); - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_READ, &hKey) != ERROR_SUCCESS) return nDefault; - - if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL, - &dwType, (LPBYTE)&n, &dwSize) != ERROR_SUCCESS) n = nDefault; - ::RegCloseKey(hKey); - return n; -} - -BOOL CRegistry::ReadBool(CString strName, BOOL bDefault) -{ - DWORD dwType = REG_BINARY; - BOOL b; - DWORD dwSize = sizeof(b); - HKEY hKey; - - ASSERT(m_strCurrentPath.GetLength() > 0); - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_READ, &hKey) != ERROR_SUCCESS) return bDefault; - - if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL, - &dwType, (LPBYTE)&b, &dwSize) != ERROR_SUCCESS) b = bDefault; - ::RegCloseKey(hKey); - return b; -} - - -COLORREF CRegistry::ReadColor(CString strName, COLORREF rgbDefault) -{ - DWORD dwType = REG_BINARY; - COLORREF rgb; - DWORD dwSize = sizeof(rgb); - HKEY hKey; - - ASSERT(m_strCurrentPath.GetLength() > 0); - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_READ, &hKey) != ERROR_SUCCESS) return rgbDefault; - - if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL, - &dwType, (LPBYTE)&rgb, &dwSize) != ERROR_SUCCESS) rgb = rgbDefault; - ::RegCloseKey(hKey); - return rgb; -} - -BOOL CRegistry::ReadFont(CString strName, CFont* pFont) -{ - DWORD dwType = REG_BINARY; - DWORD dwSize = sizeof(LOGFONT); - BOOL bSuccess = TRUE; - HKEY hKey; - LOGFONT lf; - - ASSERT(m_strCurrentPath.GetLength() > 0); - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_READ, &hKey) != ERROR_SUCCESS) return FALSE; - - if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL, - &dwType, (LPBYTE)&lf, &dwSize) != ERROR_SUCCESS) bSuccess = FALSE; - ::RegCloseKey(hKey); - if (bSuccess) - { - pFont->Detach(); - pFont->CreateFontIndirect(&lf); - } - return bSuccess; -} - - -BOOL CRegistry::ReadPoint(CString strName, CPoint* pPoint) -{ - DWORD dwType = REG_BINARY; - DWORD dwSize = sizeof(CPoint); - BOOL bSuccess = TRUE; - HKEY hKey; - - ASSERT(m_strCurrentPath.GetLength() > 0); - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_READ, &hKey) != ERROR_SUCCESS) return FALSE; - - if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL, - &dwType, (LPBYTE)pPoint, &dwSize) != ERROR_SUCCESS) bSuccess = FALSE; - ::RegCloseKey(hKey); - return bSuccess; -} - -BOOL CRegistry::ReadSize(CString strName, CSize* pSize) -{ - DWORD dwType = REG_BINARY; - DWORD dwSize = sizeof(CSize); - BOOL bSuccess = TRUE; - HKEY hKey; - - ASSERT(m_strCurrentPath.GetLength() > 0); - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_READ, &hKey) != ERROR_SUCCESS) return FALSE; - - if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL, - &dwType, (LPBYTE)pSize, &dwSize) != ERROR_SUCCESS) bSuccess = FALSE; - ::RegCloseKey(hKey); - return bSuccess; -} - -BOOL CRegistry::ReadRect(CString strName, CRect* pRect) -{ - DWORD dwType = REG_BINARY; - DWORD dwSize = sizeof(CRect); - BOOL bSuccess = TRUE; - HKEY hKey; - - ASSERT(m_strCurrentPath.GetLength() > 0); - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_READ, &hKey) != ERROR_SUCCESS) return FALSE; - - if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL, - &dwType, (LPBYTE)pRect, &dwSize) != ERROR_SUCCESS) bSuccess = FALSE; - ::RegCloseKey(hKey); - return bSuccess; -} - - - - -BOOL CRegistry::WriteBool(CString strName, BOOL bValue) -{ - ASSERT(m_strCurrentPath.GetLength() > 0); - BOOL bSuccess = TRUE; - HKEY hKey; - - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE; - - if (::RegSetValueEx(hKey, LPCTSTR(strName), 0, - REG_BINARY, (LPBYTE)&bValue, sizeof(bValue)) - != ERROR_SUCCESS) bSuccess = FALSE; - - if (!m_bLazyWrite) ::RegFlushKey(hKey); - ::RegCloseKey(hKey); - return bSuccess; -} - -BOOL CRegistry::WriteDateTime(CString strName, COleDateTime dtValue) -{ - ASSERT(m_strCurrentPath.GetLength() > 0); - BOOL bSuccess = TRUE; - HKEY hKey; - - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE; - - if (::RegSetValueEx(hKey, LPCTSTR(strName), 0, - REG_BINARY, (LPBYTE)&dtValue, sizeof(dtValue)) - != ERROR_SUCCESS) bSuccess = FALSE; - - if (!m_bLazyWrite) ::RegFlushKey(hKey); - ::RegCloseKey(hKey); - return bSuccess; -} - - -BOOL CRegistry::WriteString(CString strName, CString strValue) -{ - ASSERT(m_strCurrentPath.GetLength() > 0); - BOOL bSuccess = TRUE; - HKEY hKey; - _TCHAR sz[255]; - - if (strValue.GetLength() > 254) return FALSE; - -#ifdef _UNICODE - wstrcpy(sz, LPCTSTR(strValue)); -#else - strcpy(sz, LPCTSTR(strValue)); -#endif - - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE; - -#ifdef _UNICODE - if (::RegSetValueEx(hKey, LPCTSTR(strName), 0, - REG_SZ, (LPBYTE)sz, wstrlen(sz) + 1) - != ERROR_SUCCESS) bSuccess = FALSE; -#else - if (::RegSetValueEx(hKey, LPCTSTR(strName), 0, - REG_SZ, (LPBYTE)sz, strlen(sz) + 1) - != ERROR_SUCCESS) bSuccess = FALSE; -#endif - - if (!m_bLazyWrite) ::RegFlushKey(hKey); - ::RegCloseKey(hKey); - return bSuccess; -} - - -BOOL CRegistry::WriteFloat(CString strName, double fValue) -{ - ASSERT(m_strCurrentPath.GetLength() > 0); - BOOL bSuccess = TRUE; - HKEY hKey; - - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE; - - if (::RegSetValueEx(hKey, LPCTSTR(strName), 0, - REG_BINARY, (LPBYTE)&fValue, sizeof(fValue)) - != ERROR_SUCCESS) bSuccess = FALSE; - - if (!m_bLazyWrite) ::RegFlushKey(hKey); - ::RegCloseKey(hKey); - return bSuccess; -} - -BOOL CRegistry::WriteInt(CString strName, int nValue) -{ - ASSERT(m_strCurrentPath.GetLength() > 0); - BOOL bSuccess = TRUE; - HKEY hKey; - - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE; - - if (::RegSetValueEx(hKey, LPCTSTR(strName), 0, - REG_BINARY, (LPBYTE)&nValue, sizeof(nValue)) - != ERROR_SUCCESS) bSuccess = FALSE; - - if (!m_bLazyWrite) ::RegFlushKey(hKey); - ::RegCloseKey(hKey); - return bSuccess; -} - -BOOL CRegistry::WriteDword(CString strName, DWORD dwValue) -{ - ASSERT(m_strCurrentPath.GetLength() > 0); - BOOL bSuccess = TRUE; - HKEY hKey; - - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE; - - if (::RegSetValueEx(hKey, LPCTSTR(strName), 0, - REG_BINARY, (LPBYTE)&dwValue, sizeof(dwValue)) - != ERROR_SUCCESS) bSuccess = FALSE; - - if (!m_bLazyWrite) ::RegFlushKey(hKey); - ::RegCloseKey(hKey); - return bSuccess; -} - -BOOL CRegistry::WriteColor(CString strName, COLORREF rgbValue) -{ - ASSERT(m_strCurrentPath.GetLength() > 0); - BOOL bSuccess = TRUE; - HKEY hKey; - - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE; - - if (::RegSetValueEx(hKey, LPCTSTR(strName), 0, - REG_BINARY, (LPBYTE)&rgbValue, sizeof(rgbValue)) - != ERROR_SUCCESS) bSuccess = FALSE; - - if (!m_bLazyWrite) ::RegFlushKey(hKey); - ::RegCloseKey(hKey); - return bSuccess; -} - - -BOOL CRegistry::WriteFont(CString strName, CFont* pFont) -{ - ASSERT(m_strCurrentPath.GetLength() > 0); - BOOL bSuccess = TRUE; - HKEY hKey; - - LOGFONT lf; - pFont->GetLogFont(&lf); - - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE; - - if (::RegSetValueEx(hKey, LPCTSTR(strName), 0, - REG_BINARY, (LPBYTE)&lf, sizeof(lf)) - != ERROR_SUCCESS) bSuccess = FALSE; - - if (!m_bLazyWrite) ::RegFlushKey(hKey); - ::RegCloseKey(hKey); - return bSuccess; -} - - -BOOL CRegistry::WritePoint(CString strName, CPoint* pPoint) -{ - ASSERT(m_strCurrentPath.GetLength() > 0); - BOOL bSuccess = TRUE; - HKEY hKey; - - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE; - - if (::RegSetValueEx(hKey, LPCTSTR(strName), 0, - REG_BINARY, (LPBYTE)pPoint, sizeof(CPoint)) - != ERROR_SUCCESS) bSuccess = FALSE; - - if (!m_bLazyWrite) ::RegFlushKey(hKey); - ::RegCloseKey(hKey); - return bSuccess; -} - - -BOOL CRegistry::WriteSize(CString strName, CSize* pSize) -{ - ASSERT(m_strCurrentPath.GetLength() > 0); - BOOL bSuccess = TRUE; - HKEY hKey; - - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE; - - if (::RegSetValueEx(hKey, LPCTSTR(strName), 0, - REG_BINARY, (LPBYTE)pSize, sizeof(CSize)) - != ERROR_SUCCESS) bSuccess = FALSE; - - if (!m_bLazyWrite) ::RegFlushKey(hKey); - ::RegCloseKey(hKey); - return bSuccess; -} - -BOOL CRegistry::WriteRect(CString strName, CRect* pRect) -{ - ASSERT(m_strCurrentPath.GetLength() > 0); - BOOL bSuccess = TRUE; - HKEY hKey; - - if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0, - KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE; - - if (::RegSetValueEx(hKey, LPCTSTR(strName), 0, - REG_BINARY, (LPBYTE)pRect, sizeof(CRect)) - != ERROR_SUCCESS) bSuccess = FALSE; - - if (!m_bLazyWrite) ::RegFlushKey(hKey); - ::RegCloseKey(hKey); - return bSuccess; -} -
Change 1 of 1 Show Entire File win32/​shellext/​Registry.h Stacked
 
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
@@ -1,77 +0,0 @@
- - -#ifndef __REGISTRY_H__ -#define __REGISTRY_H__ - -#include <string> -typedef std::string CString; - -class CRegistry -{ -public: - CRegistry(); - ~CRegistry(); - -int m_nLastError; - -// CRegistry properties -protected: - HKEY m_hRootKey; - BOOL m_bLazyWrite; - CString m_strCurrentPath; - -public: - inline BOOL PathIsValid() { - return (m_strCurrentPath.GetLength() > 0); } - inline CString GetCurrentPath() { - return m_strCurrentPath; } - inline HKEY GetRootKey() { - return m_hRootKey; } - - -//CRegistry methods -public: - BOOL ClearKey(); - BOOL SetRootKey(HKEY hRootKey); - BOOL CreateKey(CString strKey); - BOOL DeleteKey(CString strKey); - BOOL DeleteValue(CString strName); - int GetDataSize(CString strValueName); - DWORD GetDataType(CString strValueName); - int GetSubKeyCount(); - int GetValueCount(); - BOOL KeyExists(CString strKey, HKEY hRootKey = NULL); - BOOL SetKey(CString strKey, BOOL bCanCreate); - BOOL ValueExists(CString strName); - void RenameValue(CString strOldName, CString strNewName); - - // data reading functions - COleDateTime ReadDateTime(CString strName, COleDateTime dtDefault); - double ReadFloat(CString strName, double fDefault); - CString ReadString(CString strName, CString strDefault); - int ReadInt(CString strName, int nDefault); - BOOL ReadBool(CString strName, BOOL bDefault); - COLORREF ReadColor(CString strName, COLORREF rgbDefault); - BOOL ReadFont(CString strName, CFont* pFont); - BOOL ReadPoint(CString strName, CPoint* pPoint); - BOOL ReadSize(CString strName, CSize* pSize); - BOOL ReadRect(CString strName, CRect* pRect); - DWORD ReadDword(CString strName, DWORD dwDefault); - - // data writing functions - BOOL WriteBool(CString strName, BOOL bValue); - BOOL WriteDateTime(CString strName, COleDateTime dtValue); - BOOL WriteString(CString strName, CString strValue); - BOOL WriteFloat(CString strName, double fValue); - BOOL WriteInt(CString strName, int nValue); - BOOL WriteColor(CString strName, COLORREF rgbValue); - BOOL WriteFont(CString strName, CFont* pFont); - BOOL WritePoint(CString strName, CPoint* pPoint); - BOOL WriteSize(CString strName, CSize* pSize); - BOOL WriteRect(CString strName, CRect* pRect); - BOOL WriteDword(CString strName, DWORD dwValue); - -};// end of CRegistry class definition - - -#endif \ No newline at end of file