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