root/trunk/Registry.cpp

Revision 5, 21.5 kB (checked in by qbert, 6 years ago)

Initial ( and last :( ) commit

Line 
1 #include "stdafx.h"
2 #include <winreg.h>
3 #include "Registry.h"
4
5 #define CLASS_NAME_LENGTH 255
6
7 /* IMPORTANT NOTES ABOUT CREGISTRY:
8     
9     CRegistry never keeps a key open past the end of a function call.
10     This is incase the application crashes before the next call to close
11     the registry
12     
13     INCLUDE FILES
14     "winreg.h" and "afxdisp.h" must be included in "stdafx.h"
15
16     KEY NAMES:
17     Key names must not begin with a \ and only absolute strings are accepted
18     
19 */
20
21
22
23 CRegistry::CRegistry()
24 {
25     m_hRootKey = HKEY_CURRENT_USER;
26     m_bLazyWrite = TRUE;
27     m_nLastError = ERROR_SUCCESS;
28 }
29
30 CRegistry::~CRegistry()
31 {
32     ClearKey();
33 }
34
35
36 BOOL CRegistry::ClearKey()
37 {
38     /* Call CloseKey to write the current key to the registry and close the
39     key. An application should not keep keys open any longer than necessary.
40     Calling CloseKey when there is no current key has no effect.*/
41
42     m_strCurrentPath.Empty();
43     m_hRootKey = HKEY_CURRENT_USER;
44     m_bLazyWrite = TRUE;
45     return TRUE;
46 }
47
48
49
50 BOOL CRegistry::SetRootKey(HKEY hRootKey)
51 {
52     // sets the root key
53     // make sure to set it to a valid key
54     if (hRootKey != HKEY_CLASSES_ROOT &&
55             hRootKey != HKEY_CURRENT_USER &&
56             hRootKey != HKEY_LOCAL_MACHINE &&
57             hRootKey != HKEY_USERS) return FALSE;
58
59     m_hRootKey = hRootKey;
60     return TRUE;
61 }
62
63
64 BOOL CRegistry::CreateKey(CString strKey)
65 {
66     /* Use CreateKey to add a new key to the registry.
67         Key is the name of the key to create. Key must be
68         an absolute name. An absolute key
69         begins with a backslash (\) and is a subkey of
70         the root key. */
71
72     ASSERT(strKey[0] != '\\');
73     HKEY hKey;
74
75     DWORD dwDisposition = 0;
76
77     if (::RegCreateKeyEx(m_hRootKey, LPCTSTR(strKey), 0, NULL,
78         REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,
79             &dwDisposition) != ERROR_SUCCESS) return FALSE;
80    
81     if (!m_bLazyWrite) ::RegFlushKey(hKey);
82     ::RegCloseKey(hKey);
83     m_strCurrentPath = strKey;
84     return TRUE;
85 }
86
87
88 BOOL CRegistry::DeleteKey(CString strKey)
89 {
90     /* Call DeleteKey to remove a specified key and its associated data,
91     if any, from the registry. Returns FALSE is there are subkeys
92     Subkeys must be explicitly deleted by separate calls to DeleteKey.
93     DeleteKey returns True if key deletion is successful. On error,
94     DeleteKey returns False. */
95    
96     // need to open the key first with RegOpenKeyEx
97     ASSERT(FALSE); // not yet implemented
98     ASSERT(strKey[0] != '\\');
99
100     if (!KeyExists(strKey)) return TRUE;
101     if (::RegDeleteKey(m_hRootKey, strKey) != ERROR_SUCCESS) return FALSE;
102     return TRUE;
103 }
104
105
106
107 BOOL CRegistry::DeleteValue(CString strName)
108 {
109     /* Call DeleteValue to remove a specific data value
110         associated with the current key. Name is string
111         containing the name of the value to delete. Keys can contain
112         multiple data values, and every value associated with a key
113         has a unique name. */
114
115     ASSERT(m_strCurrentPath.GetLength() > 0);
116     HKEY hKey;
117     LONG lResult;
118
119     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
120         KEY_SET_VALUE, &hKey) != ERROR_SUCCESS) return FALSE;
121
122     lResult = ::RegDeleteValue(hKey, LPCTSTR(strName));
123     ::RegCloseKey(hKey);
124
125     if (lResult == ERROR_SUCCESS) return TRUE;
126     return FALSE;
127 }
128
129
130 int CRegistry::GetDataSize(CString strValueName)
131 {
132     /* Call GetDataSize to determine the size, in bytes, of
133     a data value associated with the current key. ValueName
134     is a string containing the name of the data value to query.
135     On success, GetDataSize returns the size of the data value.
136     On failure, GetDataSize returns -1. */
137
138     HKEY hKey;
139     ASSERT(m_strCurrentPath.GetLength() > 0);
140     LONG lResult;
141
142     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
143         KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS) return -1;
144
145     DWORD dwSize = 1;
146     lResult = ::RegQueryValueEx(hKey, LPCTSTR(strValueName),
147         NULL, NULL, NULL, &dwSize);
148     ::RegCloseKey(hKey);
149
150     if (lResult != ERROR_SUCCESS) return -1;
151     return (int)dwSize;
152 }
153
154 DWORD CRegistry::GetDataType(CString strValueName)
155 {
156     HKEY hKey;
157     ASSERT(m_strCurrentPath.GetLength() > 0);
158
159     m_nLastError = ::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
160         KEY_QUERY_VALUE, &hKey);
161
162     if (m_nLastError != ERROR_SUCCESS) return 0;
163
164     DWORD dwType = 1;
165     m_nLastError = ::RegQueryValueEx(hKey, LPCTSTR(strValueName),
166         NULL, &dwType, NULL, NULL);
167     ::RegCloseKey(hKey);       
168
169     if (m_nLastError == ERROR_SUCCESS) return dwType;
170
171     return 0;
172 }
173
174
175
176 int CRegistry::GetSubKeyCount()
177 {
178     /* Call this function to determine the number of subkeys.
179         the function returns -1 on error */
180     HKEY hKey;
181     ASSERT(m_strCurrentPath.GetLength() > 0);
182
183     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
184         KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS) return -1;
185
186     LONG lResult;
187     DWORD dwSubKeyCount, dwValueCount, dwClassNameLength,
188         dwMaxSubKeyName, dwMaxValueName, dwMaxValueLength;
189     FILETIME ftLastWritten;
190
191     _TCHAR szClassBuffer[CLASS_NAME_LENGTH];
192        
193     dwClassNameLength = CLASS_NAME_LENGTH;
194     lResult = ::RegQueryInfoKey(hKey, szClassBuffer, &dwClassNameLength,
195         NULL, &dwSubKeyCount, &dwMaxSubKeyName, NULL, &dwValueCount,
196         &dwMaxValueName, &dwMaxValueLength, NULL, &ftLastWritten);
197                
198     ::RegCloseKey(hKey);
199     if (lResult != ERROR_SUCCESS) return -1;
200
201     return (int)dwSubKeyCount;
202 }
203
204
205 int CRegistry::GetValueCount()
206 {
207     /* Call this function to determine the number of subkeys.
208         the function returns -1 on error */
209     HKEY hKey;
210     ASSERT(m_strCurrentPath.GetLength() > 0);
211
212     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
213         KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS) return -1;
214
215     LONG lResult;
216     DWORD dwSubKeyCount, dwValueCount, dwClassNameLength,
217         dwMaxSubKeyName, dwMaxValueName, dwMaxValueLength;
218     FILETIME ftLastWritten;
219
220     _TCHAR szClassBuffer[CLASS_NAME_LENGTH];
221        
222     dwClassNameLength = CLASS_NAME_LENGTH;
223     lResult = ::RegQueryInfoKey(hKey, szClassBuffer, &dwClassNameLength,
224         NULL, &dwSubKeyCount, &dwMaxSubKeyName, NULL, &dwValueCount,
225         &dwMaxValueName, &dwMaxValueLength, NULL, &ftLastWritten);
226                
227     ::RegCloseKey(hKey);
228     if (lResult != ERROR_SUCCESS) return -1;
229
230     return (int)dwValueCount;
231 }
232
233
234 BOOL CRegistry::KeyExists(CString strKey, HKEY hRootKey)
235 {
236     /* Call KeyExists to determine if a key of a specified name exists.
237          Key is the name of the key for which to search. */
238
239     ASSERT(strKey[0] != '\\');
240     HKEY hKey;
241
242     if (hRootKey == NULL) hRootKey = m_hRootKey;
243    
244     LONG lResult = ::RegOpenKeyEx(hRootKey, LPCTSTR(strKey), 0,
245         KEY_ALL_ACCESS, &hKey);
246     ::RegCloseKey(hKey);
247     if (lResult == ERROR_SUCCESS) return TRUE;
248     return FALSE;
249 }
250
251 BOOL CRegistry::SetKey(CString strKey, BOOL bCanCreate)
252 {
253     /* Call SetKey to make a specified key the current key. Key is the
254         name of the key to open. If Key is null, the CurrentKey property
255         is set to the key specified by the RootKey property.
256
257         CanCreate specifies whether to create the specified key if it does
258         not exist. If CanCreate is True, the key is created if necessary.
259
260         Key is opened or created with the security access value KEY_ALL_ACCESS.
261         OpenKey only creates non-volatile keys, A non-volatile key is stored in
262         the registry and is preserved when the system is restarted.
263
264         OpenKey returns True if the key is successfully opened or created */
265
266     ASSERT(strKey[0] != '\\');
267     HKEY hKey;
268
269
270     // close the current key if it is open
271     if (strKey.GetLength() == 0)
272     {
273         m_strCurrentPath.Empty();
274         return TRUE;
275     }
276
277     DWORD dwDisposition;
278     if (bCanCreate) // open the key with RegCreateKeyEx
279     {
280         if (::RegCreateKeyEx(m_hRootKey, LPCTSTR(strKey), 0, NULL,
281             REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,
282                 &dwDisposition) != ERROR_SUCCESS) return FALSE;
283         m_strCurrentPath = strKey;
284         if (!m_bLazyWrite) ::RegFlushKey(hKey);
285         ::RegCloseKey(hKey);   
286         return TRUE;
287     }
288
289     // otherwise, open the key without creating
290     // open key requires no initial slash
291     m_nLastError = ::RegOpenKeyEx(m_hRootKey, LPCTSTR(strKey), 0,
292         KEY_ALL_ACCESS, &hKey);
293     if (m_nLastError != ERROR_SUCCESS) return FALSE;
294     m_strCurrentPath = strKey;
295     if (!m_bLazyWrite) ::RegFlushKey(hKey);
296     ::RegCloseKey(hKey);
297     return TRUE;
298 }
299
300
301 BOOL CRegistry::ValueExists(CString strName)
302 {
303     /* Call ValueExists to determine if a particular key exists in
304         the registry. Calling Value Exists is especially useful before
305         calling other TRegistry methods that operate only on existing keys.
306
307         Name is the name of the data value for which to check.
308     ValueExists returns True if a match if found, False otherwise. */
309
310     HKEY hKey;
311     LONG lResult;
312     ASSERT(m_strCurrentPath.GetLength() > 0);
313
314    
315     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
316         KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS) return FALSE;
317
318     lResult = ::RegQueryValueEx(hKey, LPCTSTR(strName), NULL,
319         NULL, NULL, NULL);
320     ::RegCloseKey(hKey);
321
322     if (lResult == ERROR_SUCCESS) return TRUE;
323     return FALSE;
324 }
325
326
327 void CRegistry::RenameValue(CString strOldName, CString strNewName)
328 {
329     /* Call RenameValue to change the name of a data value associated
330         with the current key. OldName is a string containing the current
331         name of the data value. NewName is a string containing the replacement
332         name for the data value.
333         
334         If OldName is the name of an existing data value for the current key,
335         and NewName is not the name of an existing data value for the current
336         key, RenameValue changes the data value name as specified. Otherwise
337         the current name remains unchanged.
338     */
339     ASSERT(FALSE); // functionality not yet implemented
340 }
341
342
343
344
345 COleDateTime CRegistry::ReadDateTime(CString strName, COleDateTime dtDefault)
346 {
347     /* Call ReadDate to read a date value from a specified data value
348     associated with the current key. Name is the name of the data value to read.
349     If successful, ReadDate returns a Delphi TDateTime value. The integral part
350     of a TDateTime value is the number of days that have passed since 12/30/1899.
351     The fractional part of a TDateTime value is the time of day.
352     On error, an exception is raised, and the value returned by this function
353     should be discarded. */
354
355     DWORD dwType = REG_BINARY;
356     COleDateTime dt;
357     DWORD dwSize = sizeof(dt);
358     HKEY hKey;
359
360     ASSERT(m_strCurrentPath.GetLength() > 0);
361     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
362         KEY_READ, &hKey) != ERROR_SUCCESS) return dtDefault;
363     if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL,
364         &dwType, (LPBYTE)&dt, &dwSize) != ERROR_SUCCESS) dt = dtDefault;
365     ::RegCloseKey(hKey);   
366     return dt;
367 }
368
369
370 double CRegistry::ReadFloat(CString strName, double fDefault)
371 {
372     /* Call ReadFloat to read a float value from a specified
373         data value associated with the current key. Name is the name
374         of the data value to read.
375         
376         If successful, ReadFloat returns a double value.
377         On error, an exception is raised, and the value returned by
378         this function should be discarded. */
379
380     DWORD dwType = REG_BINARY;
381     double d;
382     DWORD dwSize = sizeof(d);
383     HKEY hKey;
384
385     ASSERT(m_strCurrentPath.GetLength() > 0);
386     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
387         KEY_READ, &hKey) != ERROR_SUCCESS) return fDefault;
388
389     if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL,
390         &dwType, (LPBYTE)&d, &dwSize) != ERROR_SUCCESS) d = fDefault;
391     ::RegCloseKey(hKey);   
392     return d;
393 }
394
395 CString CRegistry::ReadString(CString strName, CString strDefault)
396 {
397     DWORD dwType = REG_SZ;
398     DWORD dwSize = 255;
399     BOOL bSuccess = TRUE;
400     _TCHAR sz[255];
401     HKEY hKey;
402    
403                                  
404     ASSERT(m_strCurrentPath.GetLength() > 0);
405
406     // make sure it is the proper type
407     dwType = GetDataType(strName);
408    
409     if (dwType != REG_SZ && dwType != REG_EXPAND_SZ)
410     {
411         return strDefault;
412     }
413
414     m_nLastError = ::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
415         KEY_READ, &hKey);
416     if (m_nLastError != ERROR_SUCCESS) return strDefault;
417
418     m_nLastError = ::RegQueryValueEx(hKey, LPCTSTR(strName), NULL,
419         &dwType, (LPBYTE)sz, &dwSize);
420     if (m_nLastError != ERROR_SUCCESS) bSuccess = FALSE;
421     ::RegCloseKey(hKey);   
422    
423     if (!bSuccess) return strDefault;
424     return CString((LPCTSTR)sz);
425 }
426
427 DWORD CRegistry::ReadDword(CString strName, DWORD dwDefault)
428 {
429     DWORD dwType = REG_DWORD;
430     DWORD dw;
431     DWORD dwSize = sizeof(dw);
432     HKEY hKey;
433
434     ASSERT(m_strCurrentPath.GetLength() > 0);
435     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
436         KEY_READ, &hKey) != ERROR_SUCCESS) return dwDefault;
437
438     if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL,
439         &dwType, (LPBYTE)&dw, &dwSize) != ERROR_SUCCESS) dw = dwDefault;
440     ::RegCloseKey(hKey);   
441     return dw;
442 }
443
444
445
446 int CRegistry::ReadInt(CString strName, int nDefault)
447 {
448     DWORD dwType = REG_BINARY;
449     int n;
450     DWORD dwSize = sizeof(n);
451     HKEY hKey;
452
453     ASSERT(m_strCurrentPath.GetLength() > 0);
454     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
455         KEY_READ, &hKey) != ERROR_SUCCESS) return nDefault;
456
457     if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL,
458         &dwType, (LPBYTE)&n, &dwSize) != ERROR_SUCCESS) n = nDefault;
459     ::RegCloseKey(hKey);   
460     return n;
461 }
462
463 BOOL CRegistry::ReadBool(CString strName, BOOL bDefault)
464 {
465     DWORD dwType = REG_BINARY;
466     BOOL b;
467     DWORD dwSize = sizeof(b);
468     HKEY hKey;
469
470     ASSERT(m_strCurrentPath.GetLength() > 0);
471     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
472         KEY_READ, &hKey) != ERROR_SUCCESS) return bDefault;
473
474     if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL,
475         &dwType, (LPBYTE)&b, &dwSize) != ERROR_SUCCESS) b = bDefault;
476     ::RegCloseKey(hKey);   
477     return b;
478 }
479
480
481 COLORREF CRegistry::ReadColor(CString strName, COLORREF rgbDefault)
482 {
483     DWORD dwType = REG_BINARY;
484     COLORREF rgb;
485     DWORD dwSize = sizeof(rgb);
486     HKEY hKey;
487
488     ASSERT(m_strCurrentPath.GetLength() > 0);
489     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
490         KEY_READ, &hKey) != ERROR_SUCCESS) return rgbDefault;
491
492     if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL,
493         &dwType, (LPBYTE)&rgb, &dwSize) != ERROR_SUCCESS) rgb = rgbDefault;
494     ::RegCloseKey(hKey);   
495     return rgb;
496 }
497
498 BOOL CRegistry::ReadFont(CString strName, CFont* pFont)
499 {
500     DWORD dwType = REG_BINARY;
501     DWORD dwSize = sizeof(LOGFONT);
502     BOOL bSuccess = TRUE;
503     HKEY hKey;
504     LOGFONT lf;
505
506     ASSERT(m_strCurrentPath.GetLength() > 0);
507     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
508         KEY_READ, &hKey) != ERROR_SUCCESS) return FALSE;
509
510     if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL,
511         &dwType, (LPBYTE)&lf, &dwSize) != ERROR_SUCCESS) bSuccess = FALSE;
512     ::RegCloseKey(hKey);   
513     if (bSuccess)
514     {
515         pFont->Detach();
516         pFont->CreateFontIndirect(&lf);
517     }
518     return bSuccess;
519 }
520
521
522 BOOL CRegistry::ReadPoint(CString strName, CPoint* pPoint)
523 {
524     DWORD dwType = REG_BINARY;
525     DWORD dwSize = sizeof(CPoint);
526     BOOL bSuccess = TRUE;
527     HKEY hKey;
528
529     ASSERT(m_strCurrentPath.GetLength() > 0);
530     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
531         KEY_READ, &hKey) != ERROR_SUCCESS) return FALSE;
532
533     if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL,
534         &dwType, (LPBYTE)pPoint, &dwSize) != ERROR_SUCCESS) bSuccess = FALSE;
535     ::RegCloseKey(hKey);   
536     return bSuccess;
537 }
538
539 BOOL CRegistry::ReadSize(CString strName, CSize* pSize)
540 {
541     DWORD dwType = REG_BINARY;
542     DWORD dwSize = sizeof(CSize);
543     BOOL bSuccess = TRUE;
544     HKEY hKey;
545
546     ASSERT(m_strCurrentPath.GetLength() > 0);
547     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
548         KEY_READ, &hKey) != ERROR_SUCCESS) return FALSE;
549
550     if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL,
551         &dwType, (LPBYTE)pSize, &dwSize) != ERROR_SUCCESS) bSuccess = FALSE;
552     ::RegCloseKey(hKey);   
553     return bSuccess;
554 }
555
556 BOOL CRegistry::ReadRect(CString strName, CRect* pRect)
557 {
558     DWORD dwType = REG_BINARY;
559     DWORD dwSize = sizeof(CRect);
560     BOOL bSuccess = TRUE;
561     HKEY hKey;
562
563     ASSERT(m_strCurrentPath.GetLength() > 0);
564     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
565         KEY_READ, &hKey) != ERROR_SUCCESS) return FALSE;
566
567     if (::RegQueryValueEx(hKey, LPCTSTR(strName), NULL,
568         &dwType, (LPBYTE)pRect, &dwSize) != ERROR_SUCCESS) bSuccess = FALSE;
569     ::RegCloseKey(hKey);   
570     return bSuccess;
571 }
572
573
574
575
576 BOOL CRegistry::WriteBool(CString strName, BOOL bValue)
577 {
578     ASSERT(m_strCurrentPath.GetLength() > 0);
579     BOOL bSuccess = TRUE;
580     HKEY hKey;
581
582     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
583         KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE;
584    
585     if (::RegSetValueEx(hKey, LPCTSTR(strName), 0,
586         REG_BINARY, (LPBYTE)&bValue, sizeof(bValue))
587          != ERROR_SUCCESS) bSuccess = FALSE;
588        
589     if (!m_bLazyWrite) ::RegFlushKey(hKey);
590     ::RegCloseKey(hKey);
591     return bSuccess;
592 }
593
594 BOOL CRegistry::WriteDateTime(CString strName, COleDateTime dtValue)
595 {
596     ASSERT(m_strCurrentPath.GetLength() > 0);
597     BOOL bSuccess = TRUE;
598     HKEY hKey;
599
600     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
601         KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE;
602    
603     if (::RegSetValueEx(hKey, LPCTSTR(strName), 0,
604         REG_BINARY, (LPBYTE)&dtValue, sizeof(dtValue))
605          != ERROR_SUCCESS) bSuccess = FALSE;
606        
607     if (!m_bLazyWrite) ::RegFlushKey(hKey);
608     ::RegCloseKey(hKey);
609     return bSuccess;
610 }
611
612
613 BOOL CRegistry::WriteString(CString strName, CString strValue)
614 {
615     ASSERT(m_strCurrentPath.GetLength() > 0);
616     BOOL bSuccess = TRUE;
617     HKEY hKey;
618     _TCHAR sz[255];
619    
620     if (strValue.GetLength() > 254) return FALSE;
621
622 #ifdef _UNICODE
623     wstrcpy(sz, LPCTSTR(strValue));
624 #else
625     strcpy(sz, LPCTSTR(strValue));
626 #endif
627
628     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
629         KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE;
630     
631 #ifdef _UNICODE
632     if (::RegSetValueEx(hKey, LPCTSTR(strName), 0,
633         REG_SZ, (LPBYTE)sz, wstrlen(sz) + 1)
634          != ERROR_SUCCESS) bSuccess = FALSE;
635 #else
636     if (::RegSetValueEx(hKey, LPCTSTR(strName), 0,
637         REG_SZ, (LPBYTE)sz, strlen(sz) + 1)
638          != ERROR_SUCCESS) bSuccess = FALSE;
639 #endif
640        
641     if (!m_bLazyWrite) ::RegFlushKey(hKey);
642     ::RegCloseKey(hKey);
643     return bSuccess;
644 }
645
646
647 BOOL CRegistry::WriteFloat(CString strName, double fValue)
648 {
649     ASSERT(m_strCurrentPath.GetLength() > 0);
650     BOOL bSuccess = TRUE;
651     HKEY hKey;
652
653     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
654         KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE;
655    
656     if (::RegSetValueEx(hKey, LPCTSTR(strName), 0,
657         REG_BINARY, (LPBYTE)&fValue, sizeof(fValue))
658          != ERROR_SUCCESS) bSuccess = FALSE;
659        
660     if (!m_bLazyWrite) ::RegFlushKey(hKey);
661     ::RegCloseKey(hKey);
662     return bSuccess;
663 }
664
665 BOOL CRegistry::WriteInt(CString strName, int nValue)
666 {
667     ASSERT(m_strCurrentPath.GetLength() > 0);
668     BOOL bSuccess = TRUE;
669     HKEY hKey;
670
671     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
672         KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE;
673    
674     if (::RegSetValueEx(hKey, LPCTSTR(strName), 0,
675         REG_BINARY, (LPBYTE)&nValue, sizeof(nValue))
676          != ERROR_SUCCESS) bSuccess = FALSE;
677        
678     if (!m_bLazyWrite) ::RegFlushKey(hKey);
679     ::RegCloseKey(hKey);
680     return bSuccess;
681 }
682
683 BOOL CRegistry::WriteDword(CString strName, DWORD dwValue)
684 {
685     ASSERT(m_strCurrentPath.GetLength() > 0);
686     BOOL bSuccess = TRUE;
687     HKEY hKey;
688
689     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
690         KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE;
691    
692     if (::RegSetValueEx(hKey, LPCTSTR(strName), 0,
693         REG_BINARY, (LPBYTE)&dwValue, sizeof(dwValue))
694          != ERROR_SUCCESS) bSuccess = FALSE;
695        
696     if (!m_bLazyWrite) ::RegFlushKey(hKey);
697     ::RegCloseKey(hKey);
698     return bSuccess;
699 }
700
701 BOOL CRegistry::WriteColor(CString strName, COLORREF rgbValue)
702 {
703     ASSERT(m_strCurrentPath.GetLength() > 0);
704     BOOL bSuccess = TRUE;
705     HKEY hKey;
706
707     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
708         KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE;
709    
710     if (::RegSetValueEx(hKey, LPCTSTR(strName), 0,
711         REG_BINARY, (LPBYTE)&rgbValue, sizeof(rgbValue))
712          != ERROR_SUCCESS) bSuccess = FALSE;
713        
714     if (!m_bLazyWrite) ::RegFlushKey(hKey);
715     ::RegCloseKey(hKey);
716     return bSuccess;
717 }
718
719
720 BOOL CRegistry::WriteFont(CString strName, CFont* pFont)
721 {
722     ASSERT(m_strCurrentPath.GetLength() > 0);
723     BOOL bSuccess = TRUE;
724     HKEY hKey;
725
726     LOGFONT lf;
727     pFont->GetLogFont(&lf);
728
729     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
730         KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE;
731    
732     if (::RegSetValueEx(hKey, LPCTSTR(strName), 0,
733         REG_BINARY, (LPBYTE)&lf, sizeof(lf))
734          != ERROR_SUCCESS) bSuccess = FALSE;
735        
736     if (!m_bLazyWrite) ::RegFlushKey(hKey);
737     ::RegCloseKey(hKey);
738     return bSuccess;
739 }
740
741
742 BOOL CRegistry::WritePoint(CString strName, CPoint* pPoint)
743 {
744     ASSERT(m_strCurrentPath.GetLength() > 0);
745     BOOL bSuccess = TRUE;
746     HKEY hKey;
747
748     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
749         KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE;
750    
751     if (::RegSetValueEx(hKey, LPCTSTR(strName), 0,
752         REG_BINARY, (LPBYTE)pPoint, sizeof(CPoint))
753          != ERROR_SUCCESS) bSuccess = FALSE;
754        
755     if (!m_bLazyWrite) ::RegFlushKey(hKey);
756     ::RegCloseKey(hKey);
757     return bSuccess;
758 }
759
760
761 BOOL CRegistry::WriteSize(CString strName, CSize* pSize)
762 {
763     ASSERT(m_strCurrentPath.GetLength() > 0);
764     BOOL bSuccess = TRUE;
765     HKEY hKey;
766
767     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
768         KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE;
769    
770     if (::RegSetValueEx(hKey, LPCTSTR(strName), 0,
771         REG_BINARY, (LPBYTE)pSize, sizeof(CSize))
772          != ERROR_SUCCESS) bSuccess = FALSE;
773        
774     if (!m_bLazyWrite) ::RegFlushKey(hKey);
775     ::RegCloseKey(hKey);
776     return bSuccess;
777 }
778
779 BOOL CRegistry::WriteRect(CString strName, CRect* pRect)
780 {
781     ASSERT(m_strCurrentPath.GetLength() > 0);
782     BOOL bSuccess = TRUE;
783     HKEY hKey;
784
785     if (::RegOpenKeyEx(m_hRootKey, LPCTSTR(m_strCurrentPath), 0,
786         KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE;
787    
788     if (::RegSetValueEx(hKey, LPCTSTR(strName), 0,
789         REG_BINARY, (LPBYTE)pRect, sizeof(CRect))
790          != ERROR_SUCCESS) bSuccess = FALSE;
791        
792     if (!m_bLazyWrite) ::RegFlushKey(hKey);
793     ::RegCloseKey(hKey);
794     return bSuccess;
795 }
Note: See TracBrowser for help on using the browser.