]> git.saurik.com Git - wxWidgets.git/blob - src/msw/registry.cpp
Documented help API extension and fixed gsocket compilation.
[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, _T("HKEY_CLASSES_ROOT"), _T("HKCR") },
71 #ifdef __WIN32__
72 { HKEY_CURRENT_USER, _T("HKEY_CURRENT_USER"), _T("HKCU") },
73 { HKEY_LOCAL_MACHINE, _T("HKEY_LOCAL_MACHINE"), _T("HKLM") },
74 { HKEY_USERS, _T("HKEY_USERS"), _T("HKU") }, // short name?
75 { HKEY_PERFORMANCE_DATA, _T("HKEY_PERFORMANCE_DATA"), _T("HKPD") },
76 #if WINVER >= 0x0400
77 { HKEY_CURRENT_CONFIG, _T("HKEY_CURRENT_CONFIG"), _T("HKCC") },
78 #ifndef __GNUWIN32__
79 { HKEY_DYN_DATA, _T("HKEY_DYN_DATA"), _T("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 _T('\\')
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, _T(""), _T("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, _T("") );
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(_T("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(_T("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 #ifdef __GNUWIN32__
306 bool wxRegKey::GetKeyInfo(size_t* pnSubKeys,
307 size_t* pnMaxKeyLen,
308 size_t* pnValues,
309 size_t* pnMaxValueLen) const
310 #else
311 bool wxRegKey::GetKeyInfo(ulong *pnSubKeys,
312 ulong *pnMaxKeyLen,
313 ulong *pnValues,
314 ulong *pnMaxValueLen) const
315 #endif
316 {
317 #if defined(__WIN32__) && !defined(__TWIN32__)
318 m_dwLastError = ::RegQueryInfoKey
319 (
320 (HKEY) m_hKey,
321 NULL, // class name
322 NULL, // (ptr to) size of class name buffer
323 RESERVED,
324 pnSubKeys, // [out] number of subkeys
325 pnMaxKeyLen, // [out] max length of a subkey name
326 NULL, // longest subkey class name
327 pnValues, // [out] number of values
328 pnMaxValueLen, // [out] max length of a value name
329 NULL, // longest value data
330 NULL, // security descriptor
331 NULL // time of last modification
332 );
333
334 if ( m_dwLastError != ERROR_SUCCESS ) {
335 wxLogSysError(m_dwLastError, _("can't get info about registry key '%s'"),
336 GetName().c_str());
337 return FALSE;
338 }
339 else
340 return TRUE;
341 #else // Win16
342 wxFAIL_MSG("GetKeyInfo() not implemented");
343
344 return FALSE;
345 #endif
346 }
347
348 // ----------------------------------------------------------------------------
349 // operations
350 // ----------------------------------------------------------------------------
351
352 // opens key (it's not an error to call Open() on an already opened key)
353 bool wxRegKey::Open()
354 {
355 if ( IsOpened() )
356 return TRUE;
357
358 HKEY tmpKey;
359 m_dwLastError = RegOpenKey((HKEY) m_hRootKey, m_strKey, &tmpKey);
360 if ( m_dwLastError != ERROR_SUCCESS ) {
361 wxLogSysError(m_dwLastError, _("can't open registry key '%s'"),
362 GetName().c_str());
363 return FALSE;
364 }
365 else
366 {
367 m_hKey = (WXHKEY) tmpKey;
368 return TRUE;
369 }
370 }
371
372 // creates key, failing if it exists and !bOkIfExists
373 bool wxRegKey::Create(bool bOkIfExists)
374 {
375 // check for existence only if asked (i.e. order is important!)
376 if ( !bOkIfExists && Exists() ) {
377 return FALSE;
378 }
379
380 if ( IsOpened() )
381 return TRUE;
382
383 HKEY tmpKey;
384 m_dwLastError = RegCreateKey((HKEY) m_hRootKey, m_strKey, &tmpKey);
385 if ( m_dwLastError != ERROR_SUCCESS ) {
386 wxLogSysError(m_dwLastError, _("can't create registry key '%s'"),
387 GetName().c_str());
388 return FALSE;
389 }
390 else
391 {
392 m_hKey = (WXHKEY) tmpKey;
393 return TRUE;
394 }
395 }
396
397 // close the key, it's not an error to call it when not opened
398 bool wxRegKey::Close()
399 {
400 if ( IsOpened() ) {
401 m_dwLastError = RegCloseKey((HKEY) m_hKey);
402 if ( m_dwLastError != ERROR_SUCCESS ) {
403 wxLogSysError(m_dwLastError, _("can't close registry key '%s'"),
404 GetName().c_str());
405
406 m_hKey = 0;
407 return FALSE;
408 }
409 else {
410 m_hKey = 0;
411 }
412 }
413
414 return TRUE;
415 }
416
417 // ----------------------------------------------------------------------------
418 // delete keys/values
419 // ----------------------------------------------------------------------------
420 bool wxRegKey::DeleteSelf()
421 {
422 {
423 wxLogNull nolog;
424 if ( !Open() ) {
425 // it already doesn't exist - ok!
426 return TRUE;
427 }
428 }
429
430 // prevent a buggy program from erasing one of the root registry keys or an
431 // immediate subkey (i.e. one which doesn't have '\\' inside) of any other
432 // key except HKCR (HKCR has some "deleteable" subkeys)
433 if ( m_strKey.IsEmpty() || (m_hRootKey != HKCR &&
434 m_strKey.Find(REG_SEPARATOR) == wxNOT_FOUND) ) {
435 wxLogError(_("Registry key '%s' is needed for normal system operation,\n"
436 "deleting it will leave your system in unusable state:\n"
437 "operation aborted."), GetFullName(this));
438
439 return FALSE;
440 }
441
442 // we can't delete keys while enumerating because it confuses GetNextKey, so
443 // we first save the key names and then delete them all
444 wxArrayString astrSubkeys;
445
446 wxString strKey;
447 long lIndex;
448 bool bCont = GetFirstKey(strKey, lIndex);
449 while ( bCont ) {
450 astrSubkeys.Add(strKey);
451
452 bCont = GetNextKey(strKey, lIndex);
453 }
454
455 size_t nKeyCount = astrSubkeys.Count();
456 for ( size_t nKey = 0; nKey < nKeyCount; nKey++ ) {
457 wxRegKey key(*this, astrSubkeys[nKey]);
458 if ( !key.DeleteSelf() )
459 return FALSE;
460 }
461
462 // now delete this key itself
463 Close();
464
465 m_dwLastError = RegDeleteKey((HKEY) m_hRootKey, m_strKey);
466 if ( m_dwLastError != ERROR_SUCCESS ) {
467 wxLogSysError(m_dwLastError, _("can't delete key '%s'"),
468 GetName().c_str());
469 return FALSE;
470 }
471
472 return TRUE;
473 }
474
475 bool wxRegKey::DeleteKey(const wxChar *szKey)
476 {
477 if ( !Open() )
478 return FALSE;
479
480 wxRegKey key(*this, szKey);
481 return key.DeleteSelf();
482 }
483
484 bool wxRegKey::DeleteValue(const wxChar *szValue)
485 {
486 if ( !Open() )
487 return FALSE;
488
489 #if defined(__WIN32__) && !defined(__TWIN32__)
490 m_dwLastError = RegDeleteValue((HKEY) m_hKey, WXSTRINGCAST szValue);
491 if ( m_dwLastError != ERROR_SUCCESS ) {
492 wxLogSysError(m_dwLastError, _("can't delete value '%s' from key '%s'"),
493 szValue, GetName().c_str());
494 return FALSE;
495 }
496 #else //WIN16
497 // named registry values don't exist in Win16 world
498 wxASSERT( IsEmpty(szValue) );
499
500 // just set the (default and unique) value of the key to ""
501 m_dwLastError = RegSetValue((HKEY) m_hKey, NULL, REG_SZ, "", RESERVED);
502 if ( m_dwLastError != ERROR_SUCCESS ) {
503 wxLogSysError(m_dwLastError, _("can't delete value of key '%s'"),
504 GetName().c_str());
505 return FALSE;
506 }
507 #endif //WIN16/32
508
509 return TRUE;
510 }
511
512 // ----------------------------------------------------------------------------
513 // access to values and subkeys
514 // ----------------------------------------------------------------------------
515
516 // return TRUE if value exists
517 bool wxRegKey::HasValue(const wxChar *szValue) const
518 {
519 // this function should be silent, so suppress possible messages from Open()
520 wxLogNull nolog;
521
522 #ifdef __WIN32__
523 if ( CONST_CAST Open() ) {
524 return RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED,
525 NULL, NULL, NULL) == ERROR_SUCCESS;
526 }
527 else
528 return FALSE;
529 #else // WIN16
530 // only unnamed value exists
531 return IsEmpty(szValue);
532 #endif // WIN16/32
533 }
534
535 // returns TRUE if this key has any values
536 bool wxRegKey::HasValues() const
537 {
538 // suppress possible messages from GetFirstValue()
539 wxLogNull nolog;
540
541 // just call GetFirstValue with dummy parameters
542 wxString str;
543 long l;
544 return CONST_CAST GetFirstValue(str, l);
545 }
546
547 // returns TRUE if this key has any subkeys
548 bool wxRegKey::HasSubkeys() const
549 {
550 // suppress possible messages from GetFirstKey()
551 wxLogNull nolog;
552
553 // just call GetFirstKey with dummy parameters
554 wxString str;
555 long l;
556 return CONST_CAST GetFirstKey(str, l);
557 }
558
559 // returns TRUE if given subkey exists
560 bool wxRegKey::HasSubKey(const wxChar *szKey) const
561 {
562 // this function should be silent, so suppress possible messages from Open()
563 wxLogNull nolog;
564
565 if ( CONST_CAST Open() )
566 return KeyExists(m_hKey, szKey);
567 else
568 return FALSE;
569 }
570
571 wxRegKey::ValueType wxRegKey::GetValueType(const wxChar *szValue) const
572 {
573 #ifdef __WIN32__
574 if ( ! CONST_CAST Open() )
575 return Type_None;
576
577 DWORD dwType;
578 m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED,
579 &dwType, NULL, NULL);
580 if ( m_dwLastError != ERROR_SUCCESS ) {
581 wxLogSysError(m_dwLastError, _("can't read value of key '%s'"),
582 GetName().c_str());
583 return Type_None;
584 }
585
586 return (ValueType)dwType;
587 #else //WIN16
588 return IsEmpty(szValue) ? Type_String : Type_None;
589 #endif //WIN16/32
590 }
591
592 #ifdef __WIN32__
593 bool wxRegKey::SetValue(const wxChar *szValue, long lValue)
594 {
595 #ifdef __TWIN32__
596 wxFAIL_MSG("RegSetValueEx not implemented by TWIN32");
597 return FALSE;
598 #else
599 if ( CONST_CAST Open() ) {
600 m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_DWORD,
601 (RegString)&lValue, sizeof(lValue));
602 if ( m_dwLastError == ERROR_SUCCESS )
603 return TRUE;
604 }
605
606 wxLogSysError(m_dwLastError, _("can't set value of '%s'"),
607 GetFullName(this, szValue));
608 return FALSE;
609 #endif
610 }
611
612 bool wxRegKey::QueryValue(const wxChar *szValue, long *plValue) const
613 {
614 if ( CONST_CAST Open() ) {
615 DWORD dwType, dwSize = sizeof(DWORD);
616 RegString pBuf = (RegString)plValue;
617 m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED,
618 &dwType, pBuf, &dwSize);
619 if ( m_dwLastError != ERROR_SUCCESS ) {
620 wxLogSysError(m_dwLastError, _("can't read value of key '%s'"),
621 GetName().c_str());
622 return FALSE;
623 }
624 else {
625 // check that we read the value of right type
626 wxASSERT_MSG( dwType == REG_DWORD,
627 _T("Type mismatch in wxRegKey::QueryValue().") );
628
629 return TRUE;
630 }
631 }
632 else
633 return FALSE;
634 }
635
636 #endif //Win32
637
638 bool wxRegKey::QueryValue(const wxChar *szValue, wxString& strValue) const
639 {
640 if ( CONST_CAST Open() ) {
641 #ifdef __WIN32__
642 // first get the type and size of the data
643 DWORD dwType, dwSize;
644 m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED,
645 &dwType, NULL, &dwSize);
646 if ( m_dwLastError == ERROR_SUCCESS ) {
647 RegString pBuf = (RegString)strValue.GetWriteBuf(dwSize);
648 m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED,
649 &dwType, pBuf, &dwSize);
650 strValue.UngetWriteBuf();
651 if ( m_dwLastError == ERROR_SUCCESS ) {
652 // check that it was the right type
653 wxASSERT_MSG( dwType == REG_SZ,
654 _T("Type mismatch in wxRegKey::QueryValue().") );
655
656 return TRUE;
657 }
658 }
659 #else //WIN16
660 // named registry values don't exist in Win16
661 wxASSERT( IsEmpty(szValue) );
662
663 m_dwLastError = RegQueryValue((HKEY) m_hKey, 0, strValue.GetWriteBuf(256), &l);
664 strValue.UngetWriteBuf();
665 if ( m_dwLastError == ERROR_SUCCESS )
666 return TRUE;
667 #endif //WIN16/32
668 }
669
670 wxLogSysError(m_dwLastError, _("can't read value of '%s'"),
671 GetFullName(this, szValue));
672 return FALSE;
673 }
674
675 bool wxRegKey::SetValue(const wxChar *szValue, const wxString& strValue)
676 {
677 if ( CONST_CAST Open() ) {
678 #if defined( __WIN32__) && !defined(__TWIN32__)
679 m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_SZ,
680 (RegString)strValue.c_str(),
681 strValue.Len() + 1);
682 if ( m_dwLastError == ERROR_SUCCESS )
683 return TRUE;
684 #else //WIN16
685 // named registry values don't exist in Win16
686 wxASSERT( IsEmpty(szValue) );
687
688 m_dwLastError = RegSetValue((HKEY) m_hKey, NULL, REG_SZ, strValue, NULL);
689 if ( m_dwLastError == ERROR_SUCCESS )
690 return TRUE;
691 #endif //WIN16/32
692 }
693
694 wxLogSysError(m_dwLastError, _("can't set value of '%s'"),
695 GetFullName(this, szValue));
696 return FALSE;
697 }
698
699 wxRegKey::operator wxString() const
700 {
701 wxString str;
702 QueryValue(NULL, str);
703 return str;
704 }
705
706 // ----------------------------------------------------------------------------
707 // enumeration
708 // NB: all these functions require an index variable which allows to have
709 // several concurrently running indexations on the same key
710 // ----------------------------------------------------------------------------
711
712 bool wxRegKey::GetFirstValue(wxString& strValueName, long& lIndex)
713 {
714 if ( !Open() )
715 return FALSE;
716
717 lIndex = 0;
718 return GetNextValue(strValueName, lIndex);
719 }
720
721 bool wxRegKey::GetNextValue(wxString& strValueName, long& lIndex) const
722 {
723 wxASSERT( IsOpened() );
724
725 // are we already at the end of enumeration?
726 if ( lIndex == -1 )
727 return FALSE;
728
729 #if defined( __WIN32__) && !defined(__TWIN32__)
730 wxChar szValueName[1024]; // @@ use RegQueryInfoKey...
731 DWORD dwValueLen = WXSIZEOF(szValueName);
732
733 m_dwLastError = RegEnumValue((HKEY) m_hKey, lIndex++,
734 szValueName, &dwValueLen,
735 RESERVED,
736 NULL, // [out] type
737 NULL, // [out] buffer for value
738 NULL); // [i/o] it's length
739
740 if ( m_dwLastError != ERROR_SUCCESS ) {
741 if ( m_dwLastError == ERROR_NO_MORE_ITEMS ) {
742 m_dwLastError = ERROR_SUCCESS;
743 lIndex = -1;
744 }
745 else {
746 wxLogSysError(m_dwLastError, _("can't enumerate values of key '%s'"),
747 GetName().c_str());
748 }
749
750 return FALSE;
751 }
752
753 strValueName = szValueName;
754 #else //WIN16
755 // only one unnamed value
756 wxASSERT( lIndex == 0 );
757
758 lIndex = -1;
759 strValueName.Empty();
760 #endif
761
762 return TRUE;
763 }
764
765 bool wxRegKey::GetFirstKey(wxString& strKeyName, long& lIndex)
766 {
767 if ( !Open() )
768 return FALSE;
769
770 lIndex = 0;
771 return GetNextKey(strKeyName, lIndex);
772 }
773
774 bool wxRegKey::GetNextKey(wxString& strKeyName, long& lIndex) const
775 {
776 wxASSERT( IsOpened() );
777
778 // are we already at the end of enumeration?
779 if ( lIndex == -1 )
780 return FALSE;
781
782 wxChar szKeyName[_MAX_PATH + 1];
783 m_dwLastError = RegEnumKey((HKEY) m_hKey, lIndex++, szKeyName, WXSIZEOF(szKeyName));
784
785 if ( m_dwLastError != ERROR_SUCCESS ) {
786 if ( m_dwLastError == ERROR_NO_MORE_ITEMS ) {
787 m_dwLastError = ERROR_SUCCESS;
788 lIndex = -1;
789 }
790 else {
791 wxLogSysError(m_dwLastError, _("can't enumerate subkeys of key '%s'"),
792 GetName().c_str());
793 }
794
795 return FALSE;
796 }
797
798 strKeyName = szKeyName;
799 return TRUE;
800 }
801
802 // returns TRUE if the value contains a number (else it's some string)
803 bool wxRegKey::IsNumericValue(const wxChar *szValue) const
804 {
805 ValueType type = GetValueType(szValue);
806 switch ( type ) {
807 case Type_Dword:
808 case Type_Dword_little_endian:
809 case Type_Dword_big_endian:
810 return TRUE;
811
812 default:
813 return FALSE;
814 }
815 }
816
817 // ============================================================================
818 // implementation of global private functions
819 // ============================================================================
820 bool KeyExists(WXHKEY hRootKey, const wxChar *szKey)
821 {
822 HKEY hkeyDummy;
823 if ( RegOpenKey( (HKEY) hRootKey, szKey, &hkeyDummy) == ERROR_SUCCESS ) {
824 RegCloseKey(hkeyDummy);
825 return TRUE;
826 }
827 else
828 return FALSE;
829 }
830
831 const wxChar *GetFullName(const wxRegKey *pKey, const wxChar *szValue)
832 {
833 static wxString s_str;
834 s_str = pKey->GetName();
835 if ( !wxIsEmpty(szValue) )
836 s_str << _T("\\") << szValue;
837
838 return s_str.c_str();
839 }
840
841 void RemoveTrailingSeparator(wxString& str)
842 {
843 if ( !str.IsEmpty() && str.Last() == REG_SEPARATOR )
844 str.Truncate(str.Len() - 1);
845 }
846
847 #endif
848 // __WIN16__
849