]> git.saurik.com Git - wxWidgets.git/blob - src/msw/registry.cpp
Restored sane way of using BC++ linker
[wxWidgets.git] / src / msw / registry.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/registry.cpp
3 // Purpose: implementation of registry classes and functions
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 03.04.98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 // TODO: - parsing of registry key names
11 // - support of other (than REG_SZ/REG_DWORD) registry types
12 // - add high level functions (RegisterOleServer, ...)
13 ///////////////////////////////////////////////////////////////////////////////
14
15 #ifdef __GNUG__
16 #pragma implementation "registry.h"
17 #endif
18
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 // other wxWindows headers
27 #include "wx/string.h"
28 #include "wx/intl.h"
29 #include "wx/log.h"
30 #include "wx/config.h" // for wxExpandEnvVars
31
32 #ifndef __WIN16__
33
34 // Windows headers
35 /*
36 #define STRICT
37 #define WIN32_LEAN_AND_MEAN
38 */
39
40 #include <windows.h>
41
42 // other std headers
43 #include <stdlib.h> // for _MAX_PATH
44
45 #ifndef _MAX_PATH
46 #define _MAX_PATH 512
47 #endif
48
49 // our header
50 #define HKEY_DEFINED // already defined in windows.h
51 #include "wx/msw/registry.h"
52
53 // some registry functions don't like signed chars
54 typedef unsigned char *RegString;
55
56 // ----------------------------------------------------------------------------
57 // constants
58 // ----------------------------------------------------------------------------
59
60 // the standard key names, short names and handles all bundled together for
61 // convenient access
62 static struct
63 {
64 HKEY hkey;
65 const wxChar *szName;
66 const wxChar *szShortName;
67 }
68 aStdKeys[] =
69 {
70 { HKEY_CLASSES_ROOT, wxT("HKEY_CLASSES_ROOT"), wxT("HKCR") },
71 #ifdef __WIN32__
72 { HKEY_CURRENT_USER, wxT("HKEY_CURRENT_USER"), wxT("HKCU") },
73 { HKEY_LOCAL_MACHINE, wxT("HKEY_LOCAL_MACHINE"), wxT("HKLM") },
74 { HKEY_USERS, wxT("HKEY_USERS"), wxT("HKU") }, // short name?
75 { HKEY_PERFORMANCE_DATA, wxT("HKEY_PERFORMANCE_DATA"), wxT("HKPD") },
76 #if WINVER >= 0x0400
77 { HKEY_CURRENT_CONFIG, wxT("HKEY_CURRENT_CONFIG"), wxT("HKCC") },
78 #ifndef __GNUWIN32__
79 { HKEY_DYN_DATA, wxT("HKEY_DYN_DATA"), wxT("HKDD") }, // short name?
80 #endif //GNUWIN32
81 #endif //WINVER >= 4.0
82 #endif //WIN32
83 };
84
85 // the registry name separator (perhaps one day MS will change it to '/' ;-)
86 #define REG_SEPARATOR wxT('\\')
87
88 // useful for Windows programmers: makes somewhat more clear all these zeroes
89 // being passed to Windows APIs
90 #define RESERVED (NULL)
91
92 // ----------------------------------------------------------------------------
93 // macros
94 // ----------------------------------------------------------------------------
95 // @ const_cast<> is not yet supported by all compilers
96 #define CONST_CAST ((wxRegKey *)this)->
97
98 #if !USE_MUTABLE
99 #define m_dwLastError CONST_CAST m_dwLastError
100 #endif
101
102 // ----------------------------------------------------------------------------
103 // non member functions
104 // ----------------------------------------------------------------------------
105
106 // removes the trailing backslash from the string if it has one
107 static inline void RemoveTrailingSeparator(wxString& str);
108
109 // returns TRUE if given registry key exists
110 static bool KeyExists(WXHKEY hRootKey, const wxChar *szKey);
111
112 // combines value and key name (uses static buffer!)
113 static const wxChar *GetFullName(const wxRegKey *pKey,
114 const wxChar *szValue = NULL);
115
116 // ============================================================================
117 // implementation of wxRegKey class
118 // ============================================================================
119
120 // ----------------------------------------------------------------------------
121 // static functions and variables
122 // ----------------------------------------------------------------------------
123
124 const size_t wxRegKey::nStdKeys = WXSIZEOF(aStdKeys);
125
126 // @@ should take a `StdKey key', but as it's often going to be used in loops
127 // it would require casts in user code.
128 const wxChar *wxRegKey::GetStdKeyName(size_t key)
129 {
130 // return empty string if key is invalid
131 wxCHECK_MSG( key < nStdKeys, wxT(""), wxT("invalid key in wxRegKey::GetStdKeyName") );
132
133 return aStdKeys[key].szName;
134 }
135
136 const wxChar *wxRegKey::GetStdKeyShortName(size_t key)
137 {
138 // return empty string if key is invalid
139 wxCHECK( key < nStdKeys, wxT("") );
140
141 return aStdKeys[key].szShortName;
142 }
143
144 wxRegKey::StdKey wxRegKey::ExtractKeyName(wxString& strKey)
145 {
146 wxString strRoot = strKey.BeforeFirst(REG_SEPARATOR);
147
148 HKEY hRootKey = 0;
149 size_t ui;
150 for ( ui = 0; ui < nStdKeys; ui++ ) {
151 if ( strRoot.CmpNoCase(aStdKeys[ui].szName) == 0 ||
152 strRoot.CmpNoCase(aStdKeys[ui].szShortName) == 0 ) {
153 hRootKey = aStdKeys[ui].hkey;
154 break;
155 }
156 }
157
158 if ( ui == nStdKeys ) {
159 wxFAIL_MSG(wxT("invalid key prefix in wxRegKey::ExtractKeyName."));
160
161 hRootKey = HKEY_CLASSES_ROOT;
162 }
163 else {
164 strKey = strKey.After(REG_SEPARATOR);
165 if ( !strKey.IsEmpty() && strKey.Last() == REG_SEPARATOR )
166 strKey.Truncate(strKey.Len() - 1);
167 }
168
169 return (wxRegKey::StdKey)(int)hRootKey;
170 }
171
172 wxRegKey::StdKey wxRegKey::GetStdKeyFromHkey(WXHKEY hkey)
173 {
174 for ( size_t ui = 0; ui < nStdKeys; ui++ ) {
175 if ( (int) aStdKeys[ui].hkey == (int) hkey )
176 return (StdKey)ui;
177 }
178
179 wxFAIL_MSG(wxT("non root hkey passed to wxRegKey::GetStdKeyFromHkey."));
180
181 return HKCR;
182 }
183
184 // ----------------------------------------------------------------------------
185 // ctors and dtor
186 // ----------------------------------------------------------------------------
187
188 wxRegKey::wxRegKey()
189 {
190 m_hKey = 0;
191 m_hRootKey = (WXHKEY) aStdKeys[HKCR].hkey;
192 m_dwLastError = 0;
193 }
194
195 wxRegKey::wxRegKey(const wxString& strKey) : m_strKey(strKey)
196 {
197 m_hRootKey = (WXHKEY) aStdKeys[ExtractKeyName(m_strKey)].hkey;
198 m_hKey = (WXHKEY) NULL;
199 m_dwLastError = 0;
200 }
201
202 // parent is a predefined (and preopened) key
203 wxRegKey::wxRegKey(StdKey keyParent, const wxString& strKey) : m_strKey(strKey)
204 {
205 RemoveTrailingSeparator(m_strKey);
206 m_hRootKey = (WXHKEY) aStdKeys[keyParent].hkey;
207 m_hKey = (WXHKEY) NULL;
208 m_dwLastError = 0;
209 }
210
211 // parent is a normal regkey
212 wxRegKey::wxRegKey(const wxRegKey& keyParent, const wxString& strKey)
213 : m_strKey(keyParent.m_strKey)
214 {
215 // combine our name with parent's to get the full name
216 if ( !m_strKey.IsEmpty() &&
217 (strKey.IsEmpty() || strKey[0] != REG_SEPARATOR) ) {
218 m_strKey += REG_SEPARATOR;
219 }
220
221 m_strKey += strKey;
222 RemoveTrailingSeparator(m_strKey);
223
224 m_hRootKey = keyParent.m_hRootKey;
225 m_hKey = (WXHKEY) NULL;
226 m_dwLastError = 0;
227 }
228
229 // dtor closes the key releasing system resource
230 wxRegKey::~wxRegKey()
231 {
232 Close();
233 }
234
235 // ----------------------------------------------------------------------------
236 // change the key name/hkey
237 // ----------------------------------------------------------------------------
238
239 // set the full key name
240 void wxRegKey::SetName(const wxString& strKey)
241 {
242 Close();
243
244 m_strKey = strKey;
245 m_hRootKey = (WXHKEY) aStdKeys[ExtractKeyName(m_strKey)].hkey;
246 }
247
248 // the name is relative to the parent key
249 void wxRegKey::SetName(StdKey keyParent, const wxString& strKey)
250 {
251 Close();
252
253 m_strKey = strKey;
254 RemoveTrailingSeparator(m_strKey);
255 m_hRootKey = (WXHKEY) aStdKeys[keyParent].hkey;
256 }
257
258 // the name is relative to the parent key
259 void wxRegKey::SetName(const wxRegKey& keyParent, const wxString& strKey)
260 {
261 Close();
262
263 // combine our name with parent's to get the full name
264 m_strKey = keyParent.m_strKey;
265 if ( !strKey.IsEmpty() && strKey[0] != REG_SEPARATOR )
266 m_strKey += REG_SEPARATOR;
267 m_strKey += strKey;
268
269 RemoveTrailingSeparator(m_strKey);
270
271 m_hRootKey = keyParent.m_hRootKey;
272 }
273
274 // hKey should be opened and will be closed in wxRegKey dtor
275 void wxRegKey::SetHkey(WXHKEY hKey)
276 {
277 Close();
278
279 m_hKey = hKey;
280 }
281
282 // ----------------------------------------------------------------------------
283 // info about the key
284 // ----------------------------------------------------------------------------
285
286 // returns TRUE if the key exists
287 bool wxRegKey::Exists() const
288 {
289 // opened key has to exist, try to open it if not done yet
290 return IsOpened() ? TRUE : KeyExists(m_hRootKey, m_strKey);
291 }
292
293 // returns the full name of the key (prefix is abbreviated if bShortPrefix)
294 wxString wxRegKey::GetName(bool bShortPrefix) const
295 {
296 StdKey key = GetStdKeyFromHkey((StdKey) m_hRootKey);
297 wxString str = bShortPrefix ? aStdKeys[key].szShortName
298 : aStdKeys[key].szName;
299 if ( !m_strKey.IsEmpty() )
300 str << "\\" << m_strKey;
301
302 return str;
303 }
304
305 bool wxRegKey::GetKeyInfo(size_t *pnSubKeys,
306 size_t *pnMaxKeyLen,
307 size_t *pnValues,
308 size_t *pnMaxValueLen) const
309 {
310 #if defined(__WIN32__) && !defined(__TWIN32__)
311
312 // old gcc headers incorrectly prototype RegQueryInfoKey()
313 #ifdef __GNUWIN32_OLD__
314 #define REG_PARAM (size_t *)
315 #else
316 #define REG_PARAM (LPDWORD)
317 #endif
318
319 m_dwLastError = ::RegQueryInfoKey
320 (
321 (HKEY) m_hKey,
322 NULL, // class name
323 NULL, // (ptr to) size of class name buffer
324 RESERVED,
325 REG_PARAM
326 pnSubKeys, // [out] number of subkeys
327 REG_PARAM
328 pnMaxKeyLen, // [out] max length of a subkey name
329 NULL, // longest subkey class name
330 REG_PARAM
331 pnValues, // [out] number of values
332 REG_PARAM
333 pnMaxValueLen, // [out] max length of a value name
334 NULL, // longest value data
335 NULL, // security descriptor
336 NULL // time of last modification
337 );
338
339 #undef REG_PARAM
340
341 if ( m_dwLastError != ERROR_SUCCESS ) {
342 wxLogSysError(m_dwLastError, _("Can't get info about registry key '%s'"),
343 GetName().c_str());
344 return FALSE;
345 }
346 else
347 return TRUE;
348 #else // Win16
349 wxFAIL_MSG("GetKeyInfo() not implemented");
350
351 return FALSE;
352 #endif
353 }
354
355 // ----------------------------------------------------------------------------
356 // operations
357 // ----------------------------------------------------------------------------
358
359 // opens key (it's not an error to call Open() on an already opened key)
360 bool wxRegKey::Open()
361 {
362 if ( IsOpened() )
363 return TRUE;
364
365 HKEY tmpKey;
366 m_dwLastError = RegOpenKey((HKEY) m_hRootKey, m_strKey, &tmpKey);
367 if ( m_dwLastError != ERROR_SUCCESS ) {
368 wxLogSysError(m_dwLastError, _("Can't open registry key '%s'"),
369 GetName().c_str());
370 return FALSE;
371 }
372 else
373 {
374 m_hKey = (WXHKEY) tmpKey;
375 return TRUE;
376 }
377 }
378
379 // creates key, failing if it exists and !bOkIfExists
380 bool wxRegKey::Create(bool bOkIfExists)
381 {
382 // check for existence only if asked (i.e. order is important!)
383 if ( !bOkIfExists && Exists() ) {
384 return FALSE;
385 }
386
387 if ( IsOpened() )
388 return TRUE;
389
390 HKEY tmpKey;
391 m_dwLastError = RegCreateKey((HKEY) m_hRootKey, m_strKey, &tmpKey);
392 if ( m_dwLastError != ERROR_SUCCESS ) {
393 wxLogSysError(m_dwLastError, _("Can't create registry key '%s'"),
394 GetName().c_str());
395 return FALSE;
396 }
397 else
398 {
399 m_hKey = (WXHKEY) tmpKey;
400 return TRUE;
401 }
402 }
403
404 // close the key, it's not an error to call it when not opened
405 bool wxRegKey::Close()
406 {
407 if ( IsOpened() ) {
408 m_dwLastError = RegCloseKey((HKEY) m_hKey);
409 if ( m_dwLastError != ERROR_SUCCESS ) {
410 wxLogSysError(m_dwLastError, _("Can't close registry key '%s'"),
411 GetName().c_str());
412
413 m_hKey = 0;
414 return FALSE;
415 }
416 else {
417 m_hKey = 0;
418 }
419 }
420
421 return TRUE;
422 }
423
424 bool wxRegKey::RenameValue(const wxChar *szValueOld, const wxChar *szValueNew)
425 {
426 bool ok = TRUE;
427 if ( HasValue(szValueNew) ) {
428 wxLogError(_("Registry value '%s' already exists."), szValueNew);
429
430 ok = FALSE;
431 }
432
433 if ( !ok ||
434 !CopyValue(szValueOld, *this, szValueNew) ||
435 !DeleteValue(szValueOld) ) {
436 wxLogError(_("Failed to rename registry value '%s' to '%s'."),
437 szValueOld, szValueNew);
438
439 return FALSE;
440 }
441
442 return TRUE;
443 }
444
445 bool wxRegKey::CopyValue(const wxChar *szValue,
446 wxRegKey& keyDst,
447 const wxChar *szValueNew)
448 {
449 if ( !szValueNew ) {
450 // by default, use the same name
451 szValueNew = szValue;
452 }
453
454 switch ( GetValueType(szValue) ) {
455 case Type_String:
456 {
457 wxString strVal;
458 return QueryValue(szValue, strVal) &&
459 keyDst.SetValue(szValueNew, strVal);
460 }
461
462 case Type_Dword:
463 /* case Type_Dword_little_endian: == Type_Dword */
464 {
465 long dwVal;
466 return QueryValue(szValue, &dwVal) &&
467 keyDst.SetValue(szValueNew, dwVal);
468 }
469
470 // these types are unsupported because I am not sure about how
471 // exactly they should be copied and because they shouldn't
472 // occur among the application keys (supposedly created with
473 // this class)
474 #ifdef __WIN32__
475 case Type_None:
476 case Type_Expand_String:
477 case Type_Binary:
478 case Type_Dword_big_endian:
479 case Type_Link:
480 case Type_Multi_String:
481 case Type_Resource_list:
482 case Type_Full_resource_descriptor:
483 case Type_Resource_requirements_list:
484 #endif // Win32
485 default:
486 wxLogError(_("Can't copy values of unsupported type %d."),
487 GetValueType(szValue));
488 return FALSE;
489 }
490 }
491
492 bool wxRegKey::Rename(const wxChar *szNewName)
493 {
494 wxCHECK_MSG( !!m_strKey, FALSE, _T("registry hives can't be renamed") );
495
496 if ( !Exists() ) {
497 wxLogError(_("Registry key '%s' does not exist, cannot rename it."),
498 GetFullName(this));
499
500 return FALSE;
501 }
502
503 // do we stay in the same hive?
504 bool inSameHive = !wxStrchr(szNewName, REG_SEPARATOR);
505
506 // construct the full new name of the key
507 wxRegKey keyDst;
508
509 if ( inSameHive ) {
510 // rename the key to the new name under the same parent
511 wxString strKey = m_strKey.BeforeLast(REG_SEPARATOR);
512 if ( !!strKey ) {
513 // don't add '\\' in the start if strFullNewName is empty
514 strKey += REG_SEPARATOR;
515 }
516
517 strKey += szNewName;
518
519 keyDst.SetName(GetStdKeyFromHkey(m_hRootKey), strKey);
520 }
521 else {
522 // this is the full name already
523 keyDst.SetName(szNewName);
524 }
525
526 bool ok = keyDst.Create(FALSE /* fail if alredy exists */);
527 if ( !ok ) {
528 wxLogError(_("Registry key '%s' already exists."),
529 GetFullName(&keyDst));
530 }
531 else {
532 ok = Copy(keyDst) && DeleteSelf();
533 }
534
535 if ( !ok ) {
536 wxLogError(_("Failed to rename the registry key '%s' to '%s'."),
537 GetFullName(this), GetFullName(&keyDst));
538 }
539 else {
540 m_hRootKey = keyDst.m_hRootKey;
541 m_strKey = keyDst.m_strKey;
542 }
543
544 return ok;
545 }
546
547 bool wxRegKey::Copy(const wxChar *szNewName)
548 {
549 // create the new key first
550 wxRegKey keyDst(szNewName);
551 bool ok = keyDst.Create(FALSE /* fail if alredy exists */);
552 if ( ok ) {
553 ok = Copy(keyDst);
554
555 // we created the dest key but copying to it failed - delete it
556 if ( !ok ) {
557 (void)keyDst.DeleteSelf();
558 }
559 }
560
561 return ok;
562 }
563
564 bool wxRegKey::Copy(wxRegKey& keyDst)
565 {
566 bool ok = TRUE;
567
568 // copy all sub keys to the new location
569 wxString strKey;
570 long lIndex;
571 bool bCont = GetFirstKey(strKey, lIndex);
572 while ( ok && bCont ) {
573 wxRegKey key(*this, strKey);
574 wxString keyName;
575 keyName << GetFullName(&keyDst) << REG_SEPARATOR << strKey;
576 ok = key.Copy((const char*) keyName);
577
578 if ( ok )
579 bCont = GetNextKey(strKey, lIndex);
580 }
581
582 // copy all values
583 wxString strVal;
584 bCont = GetFirstValue(strVal, lIndex);
585 while ( ok && bCont ) {
586 ok = CopyValue(strVal, keyDst);
587
588 if ( !ok ) {
589 wxLogSysError(m_dwLastError,
590 _("Failed to copy registry value '%s'"),
591 strVal.c_str());
592 }
593 else {
594 bCont = GetNextValue(strVal, lIndex);
595 }
596 }
597
598 if ( !ok ) {
599 wxLogError(_("Failed to copy the contents of registry key '%s' to "
600 "'%s'."), GetFullName(this), GetFullName(&keyDst));
601 }
602
603 return ok;
604 }
605
606 // ----------------------------------------------------------------------------
607 // delete keys/values
608 // ----------------------------------------------------------------------------
609 bool wxRegKey::DeleteSelf()
610 {
611 {
612 wxLogNull nolog;
613 if ( !Open() ) {
614 // it already doesn't exist - ok!
615 return TRUE;
616 }
617 }
618
619 // prevent a buggy program from erasing one of the root registry keys or an
620 // immediate subkey (i.e. one which doesn't have '\\' inside) of any other
621 // key except HKCR (HKCR has some "deleteable" subkeys)
622 if ( m_strKey.IsEmpty() || (m_hRootKey != HKCR &&
623 m_strKey.Find(REG_SEPARATOR) == wxNOT_FOUND) ) {
624 wxLogError(_("Registry key '%s' is needed for normal system operation,\n"
625 "deleting it will leave your system in unusable state:\n"
626 "operation aborted."), GetFullName(this));
627
628 return FALSE;
629 }
630
631 // we can't delete keys while enumerating because it confuses GetNextKey, so
632 // we first save the key names and then delete them all
633 wxArrayString astrSubkeys;
634
635 wxString strKey;
636 long lIndex;
637 bool bCont = GetFirstKey(strKey, lIndex);
638 while ( bCont ) {
639 astrSubkeys.Add(strKey);
640
641 bCont = GetNextKey(strKey, lIndex);
642 }
643
644 size_t nKeyCount = astrSubkeys.Count();
645 for ( size_t nKey = 0; nKey < nKeyCount; nKey++ ) {
646 wxRegKey key(*this, astrSubkeys[nKey]);
647 if ( !key.DeleteSelf() )
648 return FALSE;
649 }
650
651 // now delete this key itself
652 Close();
653
654 m_dwLastError = RegDeleteKey((HKEY) m_hRootKey, m_strKey);
655 if ( m_dwLastError != ERROR_SUCCESS ) {
656 wxLogSysError(m_dwLastError, _("Can't delete key '%s'"),
657 GetName().c_str());
658 return FALSE;
659 }
660
661 return TRUE;
662 }
663
664 bool wxRegKey::DeleteKey(const wxChar *szKey)
665 {
666 if ( !Open() )
667 return FALSE;
668
669 wxRegKey key(*this, szKey);
670 return key.DeleteSelf();
671 }
672
673 bool wxRegKey::DeleteValue(const wxChar *szValue)
674 {
675 if ( !Open() )
676 return FALSE;
677
678 #if defined(__WIN32__) && !defined(__TWIN32__)
679 m_dwLastError = RegDeleteValue((HKEY) m_hKey, WXSTRINGCAST szValue);
680 if ( m_dwLastError != ERROR_SUCCESS ) {
681 wxLogSysError(m_dwLastError, _("Can't delete value '%s' from key '%s'"),
682 szValue, GetName().c_str());
683 return FALSE;
684 }
685 #else //WIN16
686 // named registry values don't exist in Win16 world
687 wxASSERT( IsEmpty(szValue) );
688
689 // just set the (default and unique) value of the key to ""
690 m_dwLastError = RegSetValue((HKEY) m_hKey, NULL, REG_SZ, "", RESERVED);
691 if ( m_dwLastError != ERROR_SUCCESS ) {
692 wxLogSysError(m_dwLastError, _("Can't delete value of key '%s'"),
693 GetName().c_str());
694 return FALSE;
695 }
696 #endif //WIN16/32
697
698 return TRUE;
699 }
700
701 // ----------------------------------------------------------------------------
702 // access to values and subkeys
703 // ----------------------------------------------------------------------------
704
705 // return TRUE if value exists
706 bool wxRegKey::HasValue(const wxChar *szValue) const
707 {
708 // this function should be silent, so suppress possible messages from Open()
709 wxLogNull nolog;
710
711 #ifdef __WIN32__
712 if ( CONST_CAST Open() ) {
713 return RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED,
714 NULL, NULL, NULL) == ERROR_SUCCESS;
715 }
716 else
717 return FALSE;
718 #else // WIN16
719 // only unnamed value exists
720 return IsEmpty(szValue);
721 #endif // WIN16/32
722 }
723
724 // returns TRUE if this key has any values
725 bool wxRegKey::HasValues() const
726 {
727 // suppress possible messages from GetFirstValue()
728 wxLogNull nolog;
729
730 // just call GetFirstValue with dummy parameters
731 wxString str;
732 long l;
733 return CONST_CAST GetFirstValue(str, l);
734 }
735
736 // returns TRUE if this key has any subkeys
737 bool wxRegKey::HasSubkeys() const
738 {
739 // suppress possible messages from GetFirstKey()
740 wxLogNull nolog;
741
742 // just call GetFirstKey with dummy parameters
743 wxString str;
744 long l;
745 return CONST_CAST GetFirstKey(str, l);
746 }
747
748 // returns TRUE if given subkey exists
749 bool wxRegKey::HasSubKey(const wxChar *szKey) const
750 {
751 // this function should be silent, so suppress possible messages from Open()
752 wxLogNull nolog;
753
754 if ( CONST_CAST Open() )
755 return KeyExists(m_hKey, szKey);
756 else
757 return FALSE;
758 }
759
760 wxRegKey::ValueType wxRegKey::GetValueType(const wxChar *szValue) const
761 {
762 #ifdef __WIN32__
763 if ( ! CONST_CAST Open() )
764 return Type_None;
765
766 DWORD dwType;
767 m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED,
768 &dwType, NULL, NULL);
769 if ( m_dwLastError != ERROR_SUCCESS ) {
770 wxLogSysError(m_dwLastError, _("Can't read value of key '%s'"),
771 GetName().c_str());
772 return Type_None;
773 }
774
775 return (ValueType)dwType;
776 #else //WIN16
777 return IsEmpty(szValue) ? Type_String : Type_None;
778 #endif //WIN16/32
779 }
780
781 #ifdef __WIN32__
782 bool wxRegKey::SetValue(const wxChar *szValue, long lValue)
783 {
784 #ifdef __TWIN32__
785 wxFAIL_MSG("RegSetValueEx not implemented by TWIN32");
786 return FALSE;
787 #else
788 if ( CONST_CAST Open() ) {
789 m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_DWORD,
790 (RegString)&lValue, sizeof(lValue));
791 if ( m_dwLastError == ERROR_SUCCESS )
792 return TRUE;
793 }
794
795 wxLogSysError(m_dwLastError, _("Can't set value of '%s'"),
796 GetFullName(this, szValue));
797 return FALSE;
798 #endif
799 }
800
801 bool wxRegKey::QueryValue(const wxChar *szValue, long *plValue) const
802 {
803 if ( CONST_CAST Open() ) {
804 DWORD dwType, dwSize = sizeof(DWORD);
805 RegString pBuf = (RegString)plValue;
806 m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED,
807 &dwType, pBuf, &dwSize);
808 if ( m_dwLastError != ERROR_SUCCESS ) {
809 wxLogSysError(m_dwLastError, _("Can't read value of key '%s'"),
810 GetName().c_str());
811 return FALSE;
812 }
813 else {
814 // check that we read the value of right type
815 wxASSERT_MSG( IsNumericValue(szValue),
816 wxT("Type mismatch in wxRegKey::QueryValue().") );
817
818 return TRUE;
819 }
820 }
821 else
822 return FALSE;
823 }
824
825 #endif //Win32
826
827 bool wxRegKey::QueryValue(const wxChar *szValue, wxString& strValue) const
828 {
829 if ( CONST_CAST Open() ) {
830 #ifdef __WIN32__
831 // first get the type and size of the data
832 DWORD dwType, dwSize;
833 m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED,
834 &dwType, NULL, &dwSize);
835 if ( m_dwLastError == ERROR_SUCCESS ) {
836 if ( !dwSize ) {
837 // must treat this case specially as GetWriteBuf() doesn't like
838 // being called with 0 size
839 strValue.Empty();
840 }
841 else {
842 RegString pBuf = (RegString)strValue.GetWriteBuf(dwSize);
843 m_dwLastError = RegQueryValueEx((HKEY) m_hKey,
844 WXSTRINGCAST szValue,
845 RESERVED,
846 &dwType,
847 pBuf,
848 &dwSize);
849 strValue.UngetWriteBuf();
850 }
851
852 if ( m_dwLastError == ERROR_SUCCESS ) {
853 // check that it was the right type
854 wxASSERT_MSG( !IsNumericValue(szValue),
855 wxT("Type mismatch in wxRegKey::QueryValue().") );
856
857 return TRUE;
858 }
859 }
860 #else //WIN16
861 // named registry values don't exist in Win16
862 wxASSERT( IsEmpty(szValue) );
863
864 m_dwLastError = RegQueryValue((HKEY) m_hKey, 0, strValue.GetWriteBuf(256), &l);
865 strValue.UngetWriteBuf();
866 if ( m_dwLastError == ERROR_SUCCESS )
867 return TRUE;
868 #endif //WIN16/32
869 }
870
871 wxLogSysError(m_dwLastError, _("Can't read value of '%s'"),
872 GetFullName(this, szValue));
873 return FALSE;
874 }
875
876 bool wxRegKey::SetValue(const wxChar *szValue, const wxString& strValue)
877 {
878 if ( CONST_CAST Open() ) {
879 #if defined( __WIN32__) && !defined(__TWIN32__)
880 m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_SZ,
881 (RegString)strValue.c_str(),
882 strValue.Len() + 1);
883 if ( m_dwLastError == ERROR_SUCCESS )
884 return TRUE;
885 #else //WIN16
886 // named registry values don't exist in Win16
887 wxASSERT( IsEmpty(szValue) );
888
889 m_dwLastError = RegSetValue((HKEY) m_hKey, NULL, REG_SZ, strValue, NULL);
890 if ( m_dwLastError == ERROR_SUCCESS )
891 return TRUE;
892 #endif //WIN16/32
893 }
894
895 wxLogSysError(m_dwLastError, _("Can't set value of '%s'"),
896 GetFullName(this, szValue));
897 return FALSE;
898 }
899
900 wxRegKey::operator wxString() const
901 {
902 wxString str;
903 QueryValue(NULL, str);
904 return str;
905 }
906
907 // ----------------------------------------------------------------------------
908 // enumeration
909 // NB: all these functions require an index variable which allows to have
910 // several concurrently running indexations on the same key
911 // ----------------------------------------------------------------------------
912
913 bool wxRegKey::GetFirstValue(wxString& strValueName, long& lIndex)
914 {
915 if ( !Open() )
916 return FALSE;
917
918 lIndex = 0;
919 return GetNextValue(strValueName, lIndex);
920 }
921
922 bool wxRegKey::GetNextValue(wxString& strValueName, long& lIndex) const
923 {
924 wxASSERT( IsOpened() );
925
926 // are we already at the end of enumeration?
927 if ( lIndex == -1 )
928 return FALSE;
929
930 #if defined( __WIN32__) && !defined(__TWIN32__)
931 wxChar szValueName[1024]; // @@ use RegQueryInfoKey...
932 DWORD dwValueLen = WXSIZEOF(szValueName);
933
934 m_dwLastError = RegEnumValue((HKEY) m_hKey, lIndex++,
935 szValueName, &dwValueLen,
936 RESERVED,
937 NULL, // [out] type
938 NULL, // [out] buffer for value
939 NULL); // [i/o] it's length
940
941 if ( m_dwLastError != ERROR_SUCCESS ) {
942 if ( m_dwLastError == ERROR_NO_MORE_ITEMS ) {
943 m_dwLastError = ERROR_SUCCESS;
944 lIndex = -1;
945 }
946 else {
947 wxLogSysError(m_dwLastError, _("Can't enumerate values of key '%s'"),
948 GetName().c_str());
949 }
950
951 return FALSE;
952 }
953
954 strValueName = szValueName;
955 #else //WIN16
956 // only one unnamed value
957 wxASSERT( lIndex == 0 );
958
959 lIndex = -1;
960 strValueName.Empty();
961 #endif
962
963 return TRUE;
964 }
965
966 bool wxRegKey::GetFirstKey(wxString& strKeyName, long& lIndex)
967 {
968 if ( !Open() )
969 return FALSE;
970
971 lIndex = 0;
972 return GetNextKey(strKeyName, lIndex);
973 }
974
975 bool wxRegKey::GetNextKey(wxString& strKeyName, long& lIndex) const
976 {
977 wxASSERT( IsOpened() );
978
979 // are we already at the end of enumeration?
980 if ( lIndex == -1 )
981 return FALSE;
982
983 wxChar szKeyName[_MAX_PATH + 1];
984 m_dwLastError = RegEnumKey((HKEY) m_hKey, lIndex++, szKeyName, WXSIZEOF(szKeyName));
985
986 if ( m_dwLastError != ERROR_SUCCESS ) {
987 if ( m_dwLastError == ERROR_NO_MORE_ITEMS ) {
988 m_dwLastError = ERROR_SUCCESS;
989 lIndex = -1;
990 }
991 else {
992 wxLogSysError(m_dwLastError, _("Can't enumerate subkeys of key '%s'"),
993 GetName().c_str());
994 }
995
996 return FALSE;
997 }
998
999 strKeyName = szKeyName;
1000 return TRUE;
1001 }
1002
1003 // returns TRUE if the value contains a number (else it's some string)
1004 bool wxRegKey::IsNumericValue(const wxChar *szValue) const
1005 {
1006 ValueType type = GetValueType(szValue);
1007 switch ( type ) {
1008 case Type_Dword:
1009 /* case Type_Dword_little_endian: == Type_Dword */
1010 case Type_Dword_big_endian:
1011 return TRUE;
1012
1013 default:
1014 return FALSE;
1015 }
1016 }
1017
1018 // ============================================================================
1019 // implementation of global private functions
1020 // ============================================================================
1021 bool KeyExists(WXHKEY hRootKey, const wxChar *szKey)
1022 {
1023 HKEY hkeyDummy;
1024 if ( RegOpenKey( (HKEY) hRootKey, szKey, &hkeyDummy) == ERROR_SUCCESS ) {
1025 RegCloseKey(hkeyDummy);
1026 return TRUE;
1027 }
1028 else
1029 return FALSE;
1030 }
1031
1032 const wxChar *GetFullName(const wxRegKey *pKey, const wxChar *szValue)
1033 {
1034 static wxString s_str;
1035 s_str = pKey->GetName();
1036 if ( !wxIsEmpty(szValue) )
1037 s_str << wxT("\\") << szValue;
1038
1039 return s_str.c_str();
1040 }
1041
1042 void RemoveTrailingSeparator(wxString& str)
1043 {
1044 if ( !str.IsEmpty() && str.Last() == REG_SEPARATOR )
1045 str.Truncate(str.Len() - 1);
1046 }
1047
1048 #endif
1049 // __WIN16__
1050