]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/registry.cpp
wxMenu and wxMenuBar modifications: now works much better with owner-drawn
[wxWidgets.git] / src / msw / registry.cpp
... / ...
CommitLineData
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
54typedef 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
62static struct
63{
64 HKEY hkey;
65 const char *szName;
66 const char *szShortName;
67}
68aStdKeys[] =
69{
70 { HKEY_CLASSES_ROOT, "HKEY_CLASSES_ROOT", "HKCR" },
71#ifdef __WIN32__
72 { HKEY_CURRENT_USER, "HKEY_CURRENT_USER", "HKCU" },
73 { HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE", "HKLM" },
74 { HKEY_USERS, "HKEY_USERS", "HKU" }, // short name?
75 { HKEY_PERFORMANCE_DATA, "HKEY_PERFORMANCE_DATA", "HKPD" },
76#if WINVER >= 0x0400
77 { HKEY_CURRENT_CONFIG, "HKEY_CURRENT_CONFIG", "HKCC" },
78#ifndef __GNUWIN32__
79 { HKEY_DYN_DATA, "HKEY_DYN_DATA", "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 '\\'
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
107static inline void RemoveTrailingSeparator(wxString& str);
108
109// returns TRUE if given registry key exists
110static bool KeyExists(WXHKEY hRootKey, const char *szKey);
111
112// combines value and key name (uses static buffer!)
113static const char *GetFullName(const wxRegKey *pKey,
114 const char *szValue = NULL);
115
116// ============================================================================
117// implementation of wxRegKey class
118// ============================================================================
119
120// ----------------------------------------------------------------------------
121// static functions and variables
122// ----------------------------------------------------------------------------
123
124const 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.
128const char *wxRegKey::GetStdKeyName(size_t key)
129{
130 // return empty string if key is invalid
131 wxCHECK_MSG( key < nStdKeys, "", "invalid key in wxRegKey::GetStdKeyName" );
132
133 return aStdKeys[key].szName;
134}
135
136const char *wxRegKey::GetStdKeyShortName(size_t key)
137{
138 // return empty string if key is invalid
139 wxCHECK( key < nStdKeys, "" );
140
141 return aStdKeys[key].szShortName;
142}
143
144wxRegKey::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("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
172wxRegKey::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("non root hkey passed to wxRegKey::GetStdKeyFromHkey.");
180
181 return HKCR;
182}
183
184// ----------------------------------------------------------------------------
185// ctors and dtor
186// ----------------------------------------------------------------------------
187
188wxRegKey::wxRegKey()
189{
190 m_hKey = 0;
191 m_hRootKey = (WXHKEY) aStdKeys[HKCR].hkey;
192 m_dwLastError = 0;
193}
194
195wxRegKey::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
203wxRegKey::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
212wxRegKey::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
230wxRegKey::~wxRegKey()
231{
232 Close();
233}
234
235// ----------------------------------------------------------------------------
236// change the key name/hkey
237// ----------------------------------------------------------------------------
238
239// set the full key name
240void 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
249void 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
259void 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
275void 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
287bool 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)
294wxString 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__
306bool wxRegKey::GetKeyInfo(size_t* pnSubKeys,
307 size_t* pnMaxKeyLen,
308 size_t* pnValues,
309 size_t* pnMaxValueLen) const
310#else
311bool 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)
353bool 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
373bool 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
398bool 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// ----------------------------------------------------------------------------
420bool 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
475bool wxRegKey::DeleteKey(const char *szKey)
476{
477 if ( !Open() )
478 return FALSE;
479
480 wxRegKey key(*this, szKey);
481 return key.DeleteSelf();
482}
483
484bool wxRegKey::DeleteValue(const char *szValue)
485{
486 if ( !Open() )
487 return FALSE;
488
489#if defined(__WIN32__) && !defined(__TWIN32__)
490 m_dwLastError = RegDeleteValue((HKEY) m_hKey, (char*) (const char*) 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
517bool wxRegKey::HasValue(const char *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, (char*) (const char*) 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 subkeys
536bool wxRegKey::HasSubkeys() const
537{
538 // suppress possible messages from GetFirstKey()
539 wxLogNull nolog;
540
541 // just call GetFirstKey with dummy parameters
542 wxString str;
543 long l;
544 return CONST_CAST GetFirstKey(str, l);
545}
546
547// returns TRUE if given subkey exists
548bool wxRegKey::HasSubKey(const char *szKey) const
549{
550 // this function should be silent, so suppress possible messages from Open()
551 wxLogNull nolog;
552
553 if ( CONST_CAST Open() )
554 return KeyExists(m_hKey, szKey);
555 else
556 return FALSE;
557}
558
559wxRegKey::ValueType wxRegKey::GetValueType(const char *szValue) const
560{
561 #ifdef __WIN32__
562 if ( ! CONST_CAST Open() )
563 return Type_None;
564
565 DWORD dwType;
566 m_dwLastError = RegQueryValueEx((HKEY) m_hKey, (char*) (const char*) szValue, RESERVED,
567 &dwType, NULL, NULL);
568 if ( m_dwLastError != ERROR_SUCCESS ) {
569 wxLogSysError(m_dwLastError, _("can't read value of key '%s'"),
570 GetName().c_str());
571 return Type_None;
572 }
573
574 return (ValueType)dwType;
575 #else //WIN16
576 return IsEmpty(szValue) ? Type_String : Type_None;
577 #endif //WIN16/32
578}
579
580#ifdef __WIN32__
581bool wxRegKey::SetValue(const char *szValue, long lValue)
582{
583#ifdef __TWIN32__
584 wxFAIL_MSG("RegSetValueEx not implemented by TWIN32");
585 return FALSE;
586#else
587 if ( CONST_CAST Open() ) {
588 m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_DWORD,
589 (RegString)&lValue, sizeof(lValue));
590 if ( m_dwLastError == ERROR_SUCCESS )
591 return TRUE;
592 }
593
594 wxLogSysError(m_dwLastError, _("can't set value of '%s'"),
595 GetFullName(this, szValue));
596 return FALSE;
597#endif
598}
599
600bool wxRegKey::QueryValue(const char *szValue, long *plValue) const
601{
602 if ( CONST_CAST Open() ) {
603 DWORD dwType, dwSize = sizeof(DWORD);
604 RegString pBuf = (RegString)plValue;
605 m_dwLastError = RegQueryValueEx((HKEY) m_hKey, (char*) (const char*) szValue, RESERVED,
606 &dwType, pBuf, &dwSize);
607 if ( m_dwLastError != ERROR_SUCCESS ) {
608 wxLogSysError(m_dwLastError, _("can't read value of key '%s'"),
609 GetName().c_str());
610 return FALSE;
611 }
612 else {
613 // check that we read the value of right type
614 wxASSERT_MSG( dwType == REG_DWORD,
615 "Type mismatch in wxRegKey::QueryValue()." );
616
617 return TRUE;
618 }
619 }
620 else
621 return FALSE;
622}
623
624#endif //Win32
625
626bool wxRegKey::QueryValue(const char *szValue, wxString& strValue) const
627{
628 if ( CONST_CAST Open() ) {
629 #ifdef __WIN32__
630 // first get the type and size of the data
631 DWORD dwType, dwSize;
632 m_dwLastError = RegQueryValueEx((HKEY) m_hKey, (char*) (const char*) szValue, RESERVED,
633 &dwType, NULL, &dwSize);
634 if ( m_dwLastError == ERROR_SUCCESS ) {
635 RegString pBuf = (RegString)strValue.GetWriteBuf(dwSize);
636 m_dwLastError = RegQueryValueEx((HKEY) m_hKey, (char*) (const char*) szValue, RESERVED,
637 &dwType, pBuf, &dwSize);
638 strValue.UngetWriteBuf();
639 if ( m_dwLastError == ERROR_SUCCESS ) {
640 // check that it was the right type
641 wxASSERT_MSG( dwType == REG_SZ,
642 "Type mismatch in wxRegKey::QueryValue()." );
643
644 return TRUE;
645 }
646 }
647 #else //WIN16
648 // named registry values don't exist in Win16
649 wxASSERT( IsEmpty(szValue) );
650
651 m_dwLastError = RegQueryValue((HKEY) m_hKey, 0, strValue.GetWriteBuf(256), &l);
652 strValue.UngetWriteBuf();
653 if ( m_dwLastError == ERROR_SUCCESS )
654 return TRUE;
655 #endif //WIN16/32
656 }
657
658 wxLogSysError(m_dwLastError, _("can't read value of '%s'"),
659 GetFullName(this, szValue));
660 return FALSE;
661}
662
663bool wxRegKey::SetValue(const char *szValue, const wxString& strValue)
664{
665 if ( CONST_CAST Open() ) {
666#if defined( __WIN32__) && !defined(__TWIN32__)
667 m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_SZ,
668 (RegString)strValue.c_str(),
669 strValue.Len() + 1);
670 if ( m_dwLastError == ERROR_SUCCESS )
671 return TRUE;
672 #else //WIN16
673 // named registry values don't exist in Win16
674 wxASSERT( IsEmpty(szValue) );
675
676 m_dwLastError = RegSetValue((HKEY) m_hKey, NULL, REG_SZ, strValue, NULL);
677 if ( m_dwLastError == ERROR_SUCCESS )
678 return TRUE;
679 #endif //WIN16/32
680 }
681
682 wxLogSysError(m_dwLastError, _("can't set value of '%s'"),
683 GetFullName(this, szValue));
684 return FALSE;
685}
686
687wxRegKey::operator wxString() const
688{
689 wxString str;
690 QueryValue(NULL, str);
691 return str;
692}
693
694// ----------------------------------------------------------------------------
695// enumeration
696// NB: all these functions require an index variable which allows to have
697// several concurrently running indexations on the same key
698// ----------------------------------------------------------------------------
699
700bool wxRegKey::GetFirstValue(wxString& strValueName, long& lIndex)
701{
702 if ( !Open() )
703 return FALSE;
704
705 lIndex = 0;
706 return GetNextValue(strValueName, lIndex);
707}
708
709bool wxRegKey::GetNextValue(wxString& strValueName, long& lIndex) const
710{
711 wxASSERT( IsOpened() );
712
713 // are we already at the end of enumeration?
714 if ( lIndex == -1 )
715 return FALSE;
716
717#if defined( __WIN32__) && !defined(__TWIN32__)
718 char szValueName[1024]; // @@ use RegQueryInfoKey...
719 DWORD dwValueLen = WXSIZEOF(szValueName);
720
721 lIndex++;
722 m_dwLastError = RegEnumValue((HKEY) m_hKey, lIndex,
723 szValueName, &dwValueLen,
724 RESERVED,
725 NULL, // [out] type
726 NULL, // [out] buffer for value
727 NULL); // [i/o] it's length
728
729 if ( m_dwLastError != ERROR_SUCCESS ) {
730 if ( m_dwLastError == ERROR_NO_MORE_ITEMS ) {
731 m_dwLastError = ERROR_SUCCESS;
732 lIndex = -1;
733 }
734 else {
735 wxLogSysError(m_dwLastError, _("can't enumerate values of key '%s'"),
736 GetName().c_str());
737 }
738
739 return FALSE;
740 }
741
742 strValueName = szValueName;
743 #else //WIN16
744 // only one unnamed value
745 wxASSERT( lIndex == 0 );
746
747 lIndex = -1;
748 strValueName.Empty();
749 #endif
750
751 return TRUE;
752}
753
754bool wxRegKey::GetFirstKey(wxString& strKeyName, long& lIndex)
755{
756 if ( !Open() )
757 return FALSE;
758
759 lIndex = 0;
760 return GetNextKey(strKeyName, lIndex);
761}
762
763bool wxRegKey::GetNextKey(wxString& strKeyName, long& lIndex) const
764{
765 wxASSERT( IsOpened() );
766
767 // are we already at the end of enumeration?
768 if ( lIndex == -1 )
769 return FALSE;
770
771 char szKeyName[_MAX_PATH + 1];
772 m_dwLastError = RegEnumKey((HKEY) m_hKey, lIndex++, szKeyName, WXSIZEOF(szKeyName));
773
774 if ( m_dwLastError != ERROR_SUCCESS ) {
775 if ( m_dwLastError == ERROR_NO_MORE_ITEMS ) {
776 m_dwLastError = ERROR_SUCCESS;
777 lIndex = -1;
778 }
779 else {
780 wxLogSysError(m_dwLastError, _("can't enumerate subkeys of key '%s'"),
781 GetName().c_str());
782 }
783
784 return FALSE;
785 }
786
787 strKeyName = szKeyName;
788 return TRUE;
789}
790
791// returns TRUE if the value contains a number (else it's some string)
792bool wxRegKey::IsNumericValue(const char *szValue) const
793 {
794 ValueType type = GetValueType(szValue);
795 switch ( type ) {
796 case Type_Dword:
797 case Type_Dword_little_endian:
798 case Type_Dword_big_endian:
799 return TRUE;
800
801 default:
802 return FALSE;
803 }
804 }
805
806// ============================================================================
807// implementation of global private functions
808// ============================================================================
809bool KeyExists(WXHKEY hRootKey, const char *szKey)
810{
811 HKEY hkeyDummy;
812 if ( RegOpenKey( (HKEY) hRootKey, szKey, &hkeyDummy) == ERROR_SUCCESS ) {
813 RegCloseKey(hkeyDummy);
814 return TRUE;
815 }
816 else
817 return FALSE;
818}
819
820const char *GetFullName(const wxRegKey *pKey, const char *szValue)
821{
822 static wxString s_str;
823 s_str = pKey->GetName();
824 if ( !IsEmpty(szValue) )
825 s_str << "\\" << szValue;
826
827 return s_str.c_str();
828}
829
830void RemoveTrailingSeparator(wxString& str)
831{
832 if ( !str.IsEmpty() && str.Last() == REG_SEPARATOR )
833 str.Truncate(str.Len() - 1);
834}
835
836#endif
837 // __WIN16__
838