]> git.saurik.com Git - wxWidgets.git/blob - src/msw/registry.cpp
SCROLLWIN instead of SCROLL again
[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.Left(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 || !CopyValue(szValueOld, *this, szValueNew) ) {
434 wxLogError(_("Failed to rename registry value '%s' to '%s'."),
435 szValueOld, szValueNew);
436
437 return FALSE;
438 }
439
440 return TRUE;
441 }
442
443 bool wxRegKey::CopyValue(const wxChar *szValue,
444 wxRegKey& keyDst,
445 const wxChar *szValueNew)
446 {
447 switch ( GetValueType(szValue) ) {
448 case Type_String:
449 {
450 wxString strVal;
451 return QueryValue(szValue, strVal) &&
452 keyDst.SetValue(szValueNew, strVal);
453 }
454
455 case Type_Dword:
456 /* case Type_Dword_little_endian: == Type_Dword */
457 {
458 long dwVal;
459 return QueryValue(szValue, &dwVal) &&
460 keyDst.SetValue(szValueNew, dwVal);
461 }
462
463 // these types are unsupported because I am not sure about how
464 // exactly they should be copied and because they shouldn't
465 // occur among the application keys (supposedly created with
466 // this class)
467 #ifdef __WIN32__
468 case Type_None:
469 case Type_Expand_String:
470 case Type_Binary:
471 case Type_Dword_big_endian:
472 case Type_Link:
473 case Type_Multi_String:
474 case Type_Resource_list:
475 case Type_Full_resource_descriptor:
476 case Type_Resource_requirements_list:
477 #endif // Win32
478 default:
479 wxLogError(_("Can't copy values of unsupported type %d."),
480 GetValueType(szValue));
481 return FALSE;
482 }
483 }
484
485 bool wxRegKey::Copy(const wxString& strNewName)
486 {
487 // create the new key first
488 wxRegKey keyDst(strNewName);
489 bool ok = keyDst.Create(FALSE /* fail if alredy exists */);
490 if ( ok ) {
491 ok = Copy(keyDst);
492
493 // we created the dest key but copying to it failed - delete it
494 if ( !ok ) {
495 (void)keyDst.DeleteSelf();
496 }
497 }
498
499 return ok;
500 }
501
502 bool wxRegKey::Copy(wxRegKey& keyDst)
503 {
504 bool ok = TRUE;
505
506 // copy all sub keys to the new location
507 wxString strKey;
508 long lIndex;
509 bool bCont = GetFirstKey(strKey, lIndex);
510 while ( ok && bCont ) {
511 wxRegKey key(*this, strKey);
512 wxString keyName;
513 keyName << GetFullName(&keyDst) << REG_SEPARATOR << strKey;
514 ok = key.Copy(keyName);
515
516 if ( ok )
517 bCont = GetNextKey(strKey, lIndex);
518 }
519
520 // copy all values
521 wxString strVal;
522 bCont = GetFirstValue(strVal, lIndex);
523 while ( ok && bCont ) {
524 ok = CopyValue(strVal, keyDst);
525
526 if ( !ok ) {
527 wxLogSysError(m_dwLastError,
528 _("Failed to copy registry value '%s'"),
529 strVal.c_str());
530 }
531 else {
532 bCont = GetNextValue(strVal, lIndex);
533 }
534 }
535
536 if ( !ok ) {
537 wxLogError(_("Failed to copy the contents of registry key '%s' to "
538 "'%s'."), GetFullName(this), GetFullName(&keyDst));
539 }
540
541 return ok;
542 }
543
544 // ----------------------------------------------------------------------------
545 // delete keys/values
546 // ----------------------------------------------------------------------------
547 bool wxRegKey::DeleteSelf()
548 {
549 {
550 wxLogNull nolog;
551 if ( !Open() ) {
552 // it already doesn't exist - ok!
553 return TRUE;
554 }
555 }
556
557 // prevent a buggy program from erasing one of the root registry keys or an
558 // immediate subkey (i.e. one which doesn't have '\\' inside) of any other
559 // key except HKCR (HKCR has some "deleteable" subkeys)
560 if ( m_strKey.IsEmpty() || (m_hRootKey != HKCR &&
561 m_strKey.Find(REG_SEPARATOR) == wxNOT_FOUND) ) {
562 wxLogError(_("Registry key '%s' is needed for normal system operation,\n"
563 "deleting it will leave your system in unusable state:\n"
564 "operation aborted."), GetFullName(this));
565
566 return FALSE;
567 }
568
569 // we can't delete keys while enumerating because it confuses GetNextKey, so
570 // we first save the key names and then delete them all
571 wxArrayString astrSubkeys;
572
573 wxString strKey;
574 long lIndex;
575 bool bCont = GetFirstKey(strKey, lIndex);
576 while ( bCont ) {
577 astrSubkeys.Add(strKey);
578
579 bCont = GetNextKey(strKey, lIndex);
580 }
581
582 size_t nKeyCount = astrSubkeys.Count();
583 for ( size_t nKey = 0; nKey < nKeyCount; nKey++ ) {
584 wxRegKey key(*this, astrSubkeys[nKey]);
585 if ( !key.DeleteSelf() )
586 return FALSE;
587 }
588
589 // now delete this key itself
590 Close();
591
592 m_dwLastError = RegDeleteKey((HKEY) m_hRootKey, m_strKey);
593 if ( m_dwLastError != ERROR_SUCCESS ) {
594 wxLogSysError(m_dwLastError, _("Can't delete key '%s'"),
595 GetName().c_str());
596 return FALSE;
597 }
598
599 return TRUE;
600 }
601
602 bool wxRegKey::DeleteKey(const wxChar *szKey)
603 {
604 if ( !Open() )
605 return FALSE;
606
607 wxRegKey key(*this, szKey);
608 return key.DeleteSelf();
609 }
610
611 bool wxRegKey::DeleteValue(const wxChar *szValue)
612 {
613 if ( !Open() )
614 return FALSE;
615
616 #if defined(__WIN32__) && !defined(__TWIN32__)
617 m_dwLastError = RegDeleteValue((HKEY) m_hKey, WXSTRINGCAST szValue);
618 if ( m_dwLastError != ERROR_SUCCESS ) {
619 wxLogSysError(m_dwLastError, _("Can't delete value '%s' from key '%s'"),
620 szValue, GetName().c_str());
621 return FALSE;
622 }
623 #else //WIN16
624 // named registry values don't exist in Win16 world
625 wxASSERT( IsEmpty(szValue) );
626
627 // just set the (default and unique) value of the key to ""
628 m_dwLastError = RegSetValue((HKEY) m_hKey, NULL, REG_SZ, "", RESERVED);
629 if ( m_dwLastError != ERROR_SUCCESS ) {
630 wxLogSysError(m_dwLastError, _("Can't delete value of key '%s'"),
631 GetName().c_str());
632 return FALSE;
633 }
634 #endif //WIN16/32
635
636 return TRUE;
637 }
638
639 // ----------------------------------------------------------------------------
640 // access to values and subkeys
641 // ----------------------------------------------------------------------------
642
643 // return TRUE if value exists
644 bool wxRegKey::HasValue(const wxChar *szValue) const
645 {
646 // this function should be silent, so suppress possible messages from Open()
647 wxLogNull nolog;
648
649 #ifdef __WIN32__
650 if ( CONST_CAST Open() ) {
651 return RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED,
652 NULL, NULL, NULL) == ERROR_SUCCESS;
653 }
654 else
655 return FALSE;
656 #else // WIN16
657 // only unnamed value exists
658 return IsEmpty(szValue);
659 #endif // WIN16/32
660 }
661
662 // returns TRUE if this key has any values
663 bool wxRegKey::HasValues() const
664 {
665 // suppress possible messages from GetFirstValue()
666 wxLogNull nolog;
667
668 // just call GetFirstValue with dummy parameters
669 wxString str;
670 long l;
671 return CONST_CAST GetFirstValue(str, l);
672 }
673
674 // returns TRUE if this key has any subkeys
675 bool wxRegKey::HasSubkeys() const
676 {
677 // suppress possible messages from GetFirstKey()
678 wxLogNull nolog;
679
680 // just call GetFirstKey with dummy parameters
681 wxString str;
682 long l;
683 return CONST_CAST GetFirstKey(str, l);
684 }
685
686 // returns TRUE if given subkey exists
687 bool wxRegKey::HasSubKey(const wxChar *szKey) const
688 {
689 // this function should be silent, so suppress possible messages from Open()
690 wxLogNull nolog;
691
692 if ( CONST_CAST Open() )
693 return KeyExists(m_hKey, szKey);
694 else
695 return FALSE;
696 }
697
698 wxRegKey::ValueType wxRegKey::GetValueType(const wxChar *szValue) const
699 {
700 #ifdef __WIN32__
701 if ( ! CONST_CAST Open() )
702 return Type_None;
703
704 DWORD dwType;
705 m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED,
706 &dwType, NULL, NULL);
707 if ( m_dwLastError != ERROR_SUCCESS ) {
708 wxLogSysError(m_dwLastError, _("Can't read value of key '%s'"),
709 GetName().c_str());
710 return Type_None;
711 }
712
713 return (ValueType)dwType;
714 #else //WIN16
715 return IsEmpty(szValue) ? Type_String : Type_None;
716 #endif //WIN16/32
717 }
718
719 #ifdef __WIN32__
720 bool wxRegKey::SetValue(const wxChar *szValue, long lValue)
721 {
722 #ifdef __TWIN32__
723 wxFAIL_MSG("RegSetValueEx not implemented by TWIN32");
724 return FALSE;
725 #else
726 if ( CONST_CAST Open() ) {
727 m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_DWORD,
728 (RegString)&lValue, sizeof(lValue));
729 if ( m_dwLastError == ERROR_SUCCESS )
730 return TRUE;
731 }
732
733 wxLogSysError(m_dwLastError, _("Can't set value of '%s'"),
734 GetFullName(this, szValue));
735 return FALSE;
736 #endif
737 }
738
739 bool wxRegKey::QueryValue(const wxChar *szValue, long *plValue) const
740 {
741 if ( CONST_CAST Open() ) {
742 DWORD dwType, dwSize = sizeof(DWORD);
743 RegString pBuf = (RegString)plValue;
744 m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED,
745 &dwType, pBuf, &dwSize);
746 if ( m_dwLastError != ERROR_SUCCESS ) {
747 wxLogSysError(m_dwLastError, _("Can't read value of key '%s'"),
748 GetName().c_str());
749 return FALSE;
750 }
751 else {
752 // check that we read the value of right type
753 wxASSERT_MSG( IsNumericValue(szValue),
754 wxT("Type mismatch in wxRegKey::QueryValue().") );
755
756 return TRUE;
757 }
758 }
759 else
760 return FALSE;
761 }
762
763 #endif //Win32
764
765 bool wxRegKey::QueryValue(const wxChar *szValue, wxString& strValue) const
766 {
767 if ( CONST_CAST Open() ) {
768 #ifdef __WIN32__
769 // first get the type and size of the data
770 DWORD dwType, dwSize;
771 m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED,
772 &dwType, NULL, &dwSize);
773 if ( m_dwLastError == ERROR_SUCCESS ) {
774 if ( !dwSize ) {
775 // must treat this case specially as GetWriteBuf() doesn't like
776 // being called with 0 size
777 strValue.Empty();
778 }
779 else {
780 RegString pBuf = (RegString)strValue.GetWriteBuf(dwSize);
781 m_dwLastError = RegQueryValueEx((HKEY) m_hKey,
782 WXSTRINGCAST szValue,
783 RESERVED,
784 &dwType,
785 pBuf,
786 &dwSize);
787 strValue.UngetWriteBuf();
788 }
789
790 if ( m_dwLastError == ERROR_SUCCESS ) {
791 // check that it was the right type
792 wxASSERT_MSG( !IsNumericValue(szValue),
793 wxT("Type mismatch in wxRegKey::QueryValue().") );
794
795 return TRUE;
796 }
797 }
798 #else //WIN16
799 // named registry values don't exist in Win16
800 wxASSERT( IsEmpty(szValue) );
801
802 m_dwLastError = RegQueryValue((HKEY) m_hKey, 0, strValue.GetWriteBuf(256), &l);
803 strValue.UngetWriteBuf();
804 if ( m_dwLastError == ERROR_SUCCESS )
805 return TRUE;
806 #endif //WIN16/32
807 }
808
809 wxLogSysError(m_dwLastError, _("Can't read value of '%s'"),
810 GetFullName(this, szValue));
811 return FALSE;
812 }
813
814 bool wxRegKey::SetValue(const wxChar *szValue, const wxString& strValue)
815 {
816 if ( CONST_CAST Open() ) {
817 #if defined( __WIN32__) && !defined(__TWIN32__)
818 m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_SZ,
819 (RegString)strValue.c_str(),
820 strValue.Len() + 1);
821 if ( m_dwLastError == ERROR_SUCCESS )
822 return TRUE;
823 #else //WIN16
824 // named registry values don't exist in Win16
825 wxASSERT( IsEmpty(szValue) );
826
827 m_dwLastError = RegSetValue((HKEY) m_hKey, NULL, REG_SZ, strValue, NULL);
828 if ( m_dwLastError == ERROR_SUCCESS )
829 return TRUE;
830 #endif //WIN16/32
831 }
832
833 wxLogSysError(m_dwLastError, _("Can't set value of '%s'"),
834 GetFullName(this, szValue));
835 return FALSE;
836 }
837
838 wxRegKey::operator wxString() const
839 {
840 wxString str;
841 QueryValue(NULL, str);
842 return str;
843 }
844
845 // ----------------------------------------------------------------------------
846 // enumeration
847 // NB: all these functions require an index variable which allows to have
848 // several concurrently running indexations on the same key
849 // ----------------------------------------------------------------------------
850
851 bool wxRegKey::GetFirstValue(wxString& strValueName, long& lIndex)
852 {
853 if ( !Open() )
854 return FALSE;
855
856 lIndex = 0;
857 return GetNextValue(strValueName, lIndex);
858 }
859
860 bool wxRegKey::GetNextValue(wxString& strValueName, long& lIndex) const
861 {
862 wxASSERT( IsOpened() );
863
864 // are we already at the end of enumeration?
865 if ( lIndex == -1 )
866 return FALSE;
867
868 #if defined( __WIN32__) && !defined(__TWIN32__)
869 wxChar szValueName[1024]; // @@ use RegQueryInfoKey...
870 DWORD dwValueLen = WXSIZEOF(szValueName);
871
872 m_dwLastError = RegEnumValue((HKEY) m_hKey, lIndex++,
873 szValueName, &dwValueLen,
874 RESERVED,
875 NULL, // [out] type
876 NULL, // [out] buffer for value
877 NULL); // [i/o] it's length
878
879 if ( m_dwLastError != ERROR_SUCCESS ) {
880 if ( m_dwLastError == ERROR_NO_MORE_ITEMS ) {
881 m_dwLastError = ERROR_SUCCESS;
882 lIndex = -1;
883 }
884 else {
885 wxLogSysError(m_dwLastError, _("Can't enumerate values of key '%s'"),
886 GetName().c_str());
887 }
888
889 return FALSE;
890 }
891
892 strValueName = szValueName;
893 #else //WIN16
894 // only one unnamed value
895 wxASSERT( lIndex == 0 );
896
897 lIndex = -1;
898 strValueName.Empty();
899 #endif
900
901 return TRUE;
902 }
903
904 bool wxRegKey::GetFirstKey(wxString& strKeyName, long& lIndex)
905 {
906 if ( !Open() )
907 return FALSE;
908
909 lIndex = 0;
910 return GetNextKey(strKeyName, lIndex);
911 }
912
913 bool wxRegKey::GetNextKey(wxString& strKeyName, long& lIndex) const
914 {
915 wxASSERT( IsOpened() );
916
917 // are we already at the end of enumeration?
918 if ( lIndex == -1 )
919 return FALSE;
920
921 wxChar szKeyName[_MAX_PATH + 1];
922 m_dwLastError = RegEnumKey((HKEY) m_hKey, lIndex++, szKeyName, WXSIZEOF(szKeyName));
923
924 if ( m_dwLastError != ERROR_SUCCESS ) {
925 if ( m_dwLastError == ERROR_NO_MORE_ITEMS ) {
926 m_dwLastError = ERROR_SUCCESS;
927 lIndex = -1;
928 }
929 else {
930 wxLogSysError(m_dwLastError, _("Can't enumerate subkeys of key '%s'"),
931 GetName().c_str());
932 }
933
934 return FALSE;
935 }
936
937 strKeyName = szKeyName;
938 return TRUE;
939 }
940
941 // returns TRUE if the value contains a number (else it's some string)
942 bool wxRegKey::IsNumericValue(const wxChar *szValue) const
943 {
944 ValueType type = GetValueType(szValue);
945 switch ( type ) {
946 case Type_Dword:
947 /* case Type_Dword_little_endian: == Type_Dword */
948 case Type_Dword_big_endian:
949 return TRUE;
950
951 default:
952 return FALSE;
953 }
954 }
955
956 // ============================================================================
957 // implementation of global private functions
958 // ============================================================================
959 bool KeyExists(WXHKEY hRootKey, const wxChar *szKey)
960 {
961 HKEY hkeyDummy;
962 if ( RegOpenKey( (HKEY) hRootKey, szKey, &hkeyDummy) == ERROR_SUCCESS ) {
963 RegCloseKey(hkeyDummy);
964 return TRUE;
965 }
966 else
967 return FALSE;
968 }
969
970 const wxChar *GetFullName(const wxRegKey *pKey, const wxChar *szValue)
971 {
972 static wxString s_str;
973 s_str = pKey->GetName();
974 if ( !wxIsEmpty(szValue) )
975 s_str << wxT("\\") << szValue;
976
977 return s_str.c_str();
978 }
979
980 void RemoveTrailingSeparator(wxString& str)
981 {
982 if ( !str.IsEmpty() && str.Last() == REG_SEPARATOR )
983 str.Truncate(str.Len() - 1);
984 }
985
986 #endif
987 // __WIN16__
988