1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/registry.cpp
3 // Purpose: implementation of registry classes and functions
4 // Author: Vadim Zeitlin
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 ///////////////////////////////////////////////////////////////////////////////
16 #pragma implementation "registry.h"
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 // other wxWindows headers
27 #include "wx/string.h"
30 #include "wx/config.h" // for wxExpandEnvVars
37 #define WIN32_LEAN_AND_MEAN
43 #include <stdlib.h> // for _MAX_PATH
50 #define HKEY_DEFINED // already defined in windows.h
51 #include "wx/msw/registry.h"
53 // some registry functions don't like signed chars
54 typedef unsigned char *RegString
;
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // the standard key names, short names and handles all bundled together for
66 const wxChar
*szShortName
;
70 { HKEY_CLASSES_ROOT
, wxT("HKEY_CLASSES_ROOT"), wxT("HKCR") },
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") },
77 { HKEY_CURRENT_CONFIG
, wxT("HKEY_CURRENT_CONFIG"), wxT("HKCC") },
79 { HKEY_DYN_DATA
, wxT("HKEY_DYN_DATA"), wxT("HKDD") }, // short name?
81 #endif //WINVER >= 4.0
85 // the registry name separator (perhaps one day MS will change it to '/' ;-)
86 #define REG_SEPARATOR wxT('\\')
88 // useful for Windows programmers: makes somewhat more clear all these zeroes
89 // being passed to Windows APIs
90 #define RESERVED (NULL)
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
95 // @ const_cast<> is not yet supported by all compilers
96 #define CONST_CAST ((wxRegKey *)this)->
99 #define m_dwLastError CONST_CAST m_dwLastError
102 // ----------------------------------------------------------------------------
103 // non member functions
104 // ----------------------------------------------------------------------------
106 // removes the trailing backslash from the string if it has one
107 static inline void RemoveTrailingSeparator(wxString
& str
);
109 // returns TRUE if given registry key exists
110 static bool KeyExists(WXHKEY hRootKey
, const wxChar
*szKey
);
112 // combines value and key name (uses static buffer!)
113 static const wxChar
*GetFullName(const wxRegKey
*pKey
,
114 const wxChar
*szValue
= NULL
);
116 // ============================================================================
117 // implementation of wxRegKey class
118 // ============================================================================
120 // ----------------------------------------------------------------------------
121 // static functions and variables
122 // ----------------------------------------------------------------------------
124 const size_t wxRegKey::nStdKeys
= WXSIZEOF(aStdKeys
);
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
)
130 // return empty string if key is invalid
131 wxCHECK_MSG( key
< nStdKeys
, wxT(""), wxT("invalid key in wxRegKey::GetStdKeyName") );
133 return aStdKeys
[key
].szName
;
136 const wxChar
*wxRegKey::GetStdKeyShortName(size_t key
)
138 // return empty string if key is invalid
139 wxCHECK( key
< nStdKeys
, wxT("") );
141 return aStdKeys
[key
].szShortName
;
144 wxRegKey::StdKey
wxRegKey::ExtractKeyName(wxString
& strKey
)
146 wxString strRoot
= strKey
.BeforeFirst(REG_SEPARATOR
);
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
;
158 if ( ui
== nStdKeys
) {
159 wxFAIL_MSG(wxT("invalid key prefix in wxRegKey::ExtractKeyName."));
161 hRootKey
= HKEY_CLASSES_ROOT
;
164 strKey
= strKey
.After(REG_SEPARATOR
);
165 if ( !strKey
.IsEmpty() && strKey
.Last() == REG_SEPARATOR
)
166 strKey
.Truncate(strKey
.Len() - 1);
169 return (wxRegKey::StdKey
)(int)hRootKey
;
172 wxRegKey::StdKey
wxRegKey::GetStdKeyFromHkey(WXHKEY hkey
)
174 for ( size_t ui
= 0; ui
< nStdKeys
; ui
++ ) {
175 if ( (int) aStdKeys
[ui
].hkey
== (int) hkey
)
179 wxFAIL_MSG(wxT("non root hkey passed to wxRegKey::GetStdKeyFromHkey."));
184 // ----------------------------------------------------------------------------
186 // ----------------------------------------------------------------------------
191 m_hRootKey
= (WXHKEY
) aStdKeys
[HKCR
].hkey
;
195 wxRegKey::wxRegKey(const wxString
& strKey
) : m_strKey(strKey
)
197 m_hRootKey
= (WXHKEY
) aStdKeys
[ExtractKeyName(m_strKey
)].hkey
;
198 m_hKey
= (WXHKEY
) NULL
;
202 // parent is a predefined (and preopened) key
203 wxRegKey::wxRegKey(StdKey keyParent
, const wxString
& strKey
) : m_strKey(strKey
)
205 RemoveTrailingSeparator(m_strKey
);
206 m_hRootKey
= (WXHKEY
) aStdKeys
[keyParent
].hkey
;
207 m_hKey
= (WXHKEY
) NULL
;
211 // parent is a normal regkey
212 wxRegKey::wxRegKey(const wxRegKey
& keyParent
, const wxString
& strKey
)
213 : m_strKey(keyParent
.m_strKey
)
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
;
222 RemoveTrailingSeparator(m_strKey
);
224 m_hRootKey
= keyParent
.m_hRootKey
;
225 m_hKey
= (WXHKEY
) NULL
;
229 // dtor closes the key releasing system resource
230 wxRegKey::~wxRegKey()
235 // ----------------------------------------------------------------------------
236 // change the key name/hkey
237 // ----------------------------------------------------------------------------
239 // set the full key name
240 void wxRegKey::SetName(const wxString
& strKey
)
245 m_hRootKey
= (WXHKEY
) aStdKeys
[ExtractKeyName(m_strKey
)].hkey
;
248 // the name is relative to the parent key
249 void wxRegKey::SetName(StdKey keyParent
, const wxString
& strKey
)
254 RemoveTrailingSeparator(m_strKey
);
255 m_hRootKey
= (WXHKEY
) aStdKeys
[keyParent
].hkey
;
258 // the name is relative to the parent key
259 void wxRegKey::SetName(const wxRegKey
& keyParent
, const wxString
& strKey
)
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
;
269 RemoveTrailingSeparator(m_strKey
);
271 m_hRootKey
= keyParent
.m_hRootKey
;
274 // hKey should be opened and will be closed in wxRegKey dtor
275 void wxRegKey::SetHkey(WXHKEY hKey
)
282 // ----------------------------------------------------------------------------
283 // info about the key
284 // ----------------------------------------------------------------------------
286 // returns TRUE if the key exists
287 bool wxRegKey::Exists() const
289 // opened key has to exist, try to open it if not done yet
290 return IsOpened() ? TRUE
: KeyExists(m_hRootKey
, m_strKey
);
293 // returns the full name of the key (prefix is abbreviated if bShortPrefix)
294 wxString
wxRegKey::GetName(bool bShortPrefix
) const
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
;
305 bool wxRegKey::GetKeyInfo(size_t *pnSubKeys
,
308 size_t *pnMaxValueLen
) const
310 #if defined(__WIN32__) && !defined(__TWIN32__)
312 // old gcc headers incorrectly prototype RegQueryInfoKey()
313 #ifdef __GNUWIN32_OLD__
314 #define REG_PARAM (size_t *)
316 #define REG_PARAM (LPDWORD)
319 m_dwLastError
= ::RegQueryInfoKey
323 NULL
, // (ptr to) size of class name buffer
326 pnSubKeys
, // [out] number of subkeys
328 pnMaxKeyLen
, // [out] max length of a subkey name
329 NULL
, // longest subkey class name
331 pnValues
, // [out] number of values
333 pnMaxValueLen
, // [out] max length of a value name
334 NULL
, // longest value data
335 NULL
, // security descriptor
336 NULL
// time of last modification
341 if ( m_dwLastError
!= ERROR_SUCCESS
) {
342 wxLogSysError(m_dwLastError
, _("Can't get info about registry key '%s'"),
349 wxFAIL_MSG("GetKeyInfo() not implemented");
355 // ----------------------------------------------------------------------------
357 // ----------------------------------------------------------------------------
359 // opens key (it's not an error to call Open() on an already opened key)
360 bool wxRegKey::Open()
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'"),
374 m_hKey
= (WXHKEY
) tmpKey
;
379 // creates key, failing if it exists and !bOkIfExists
380 bool wxRegKey::Create(bool bOkIfExists
)
382 // check for existence only if asked (i.e. order is important!)
383 if ( !bOkIfExists
&& Exists() ) {
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'"),
399 m_hKey
= (WXHKEY
) tmpKey
;
404 // close the key, it's not an error to call it when not opened
405 bool wxRegKey::Close()
408 m_dwLastError
= RegCloseKey((HKEY
) m_hKey
);
409 if ( m_dwLastError
!= ERROR_SUCCESS
) {
410 wxLogSysError(m_dwLastError
, _("Can't close registry key '%s'"),
424 bool wxRegKey::RenameValue(const wxChar
*szValueOld
, const wxChar
*szValueNew
)
427 if ( HasValue(szValueNew
) ) {
428 wxLogError(_("Registry value '%s' already exists."), szValueNew
);
434 !CopyValue(szValueOld
, *this, szValueNew
) ||
435 !DeleteValue(szValueOld
) ) {
436 wxLogError(_("Failed to rename registry value '%s' to '%s'."),
437 szValueOld
, szValueNew
);
445 bool wxRegKey::CopyValue(const wxChar
*szValue
,
447 const wxChar
*szValueNew
)
450 // by default, use the same name
451 szValueNew
= szValue
;
454 switch ( GetValueType(szValue
) ) {
458 return QueryValue(szValue
, strVal
) &&
459 keyDst
.SetValue(szValueNew
, strVal
);
463 /* case Type_Dword_little_endian: == Type_Dword */
466 return QueryValue(szValue
, &dwVal
) &&
467 keyDst
.SetValue(szValueNew
, dwVal
);
470 // these types are unsupported because I am not sure about how
471 // exactly they should be copied and because they shouldn't
472 // occur among the application keys (supposedly created with
476 case Type_Expand_String
:
478 case Type_Dword_big_endian
:
480 case Type_Multi_String
:
481 case Type_Resource_list
:
482 case Type_Full_resource_descriptor
:
483 case Type_Resource_requirements_list
:
486 wxLogError(_("Can't copy values of unsupported type %d."),
487 GetValueType(szValue
));
492 bool wxRegKey::Rename(const wxChar
*szNewName
)
494 wxCHECK_MSG( !!m_strKey
, FALSE
, _T("registry hives can't be renamed") );
497 wxLogError(_("Registry key '%s' does not exist, cannot rename it."),
503 // do we stay in the same hive?
504 bool inSameHive
= !wxStrchr(szNewName
, REG_SEPARATOR
);
506 // construct the full new name of the key
510 // rename the key to the new name under the same parent
511 wxString strKey
= m_strKey
.BeforeLast(REG_SEPARATOR
);
513 // don't add '\\' in the start if strFullNewName is empty
514 strKey
+= REG_SEPARATOR
;
519 keyDst
.SetName(GetStdKeyFromHkey(m_hRootKey
), strKey
);
522 // this is the full name already
523 keyDst
.SetName(szNewName
);
526 bool ok
= keyDst
.Create(FALSE
/* fail if alredy exists */);
528 wxLogError(_("Registry key '%s' already exists."),
529 GetFullName(&keyDst
));
532 ok
= Copy(keyDst
) && DeleteSelf();
536 wxLogError(_("Failed to rename the registry key '%s' to '%s'."),
537 GetFullName(this), GetFullName(&keyDst
));
540 m_hRootKey
= keyDst
.m_hRootKey
;
541 m_strKey
= keyDst
.m_strKey
;
547 bool wxRegKey::Copy(const wxChar
*szNewName
)
549 // create the new key first
550 wxRegKey
keyDst(szNewName
);
551 bool ok
= keyDst
.Create(FALSE
/* fail if alredy exists */);
555 // we created the dest key but copying to it failed - delete it
557 (void)keyDst
.DeleteSelf();
564 bool wxRegKey::Copy(wxRegKey
& keyDst
)
568 // copy all sub keys to the new location
571 bool bCont
= GetFirstKey(strKey
, lIndex
);
572 while ( ok
&& bCont
) {
573 wxRegKey
key(*this, strKey
);
575 keyName
<< GetFullName(&keyDst
) << REG_SEPARATOR
<< strKey
;
576 ok
= key
.Copy((const wxChar
*) keyName
);
579 bCont
= GetNextKey(strKey
, lIndex
);
584 bCont
= GetFirstValue(strVal
, lIndex
);
585 while ( ok
&& bCont
) {
586 ok
= CopyValue(strVal
, keyDst
);
589 wxLogSysError(m_dwLastError
,
590 _("Failed to copy registry value '%s'"),
594 bCont
= GetNextValue(strVal
, lIndex
);
599 wxLogError(_("Failed to copy the contents of registry key '%s' to '%s'."), GetFullName(this), GetFullName(&keyDst
));
605 // ----------------------------------------------------------------------------
606 // delete keys/values
607 // ----------------------------------------------------------------------------
608 bool wxRegKey::DeleteSelf()
613 // it already doesn't exist - ok!
618 // prevent a buggy program from erasing one of the root registry keys or an
619 // immediate subkey (i.e. one which doesn't have '\\' inside) of any other
620 // key except HKCR (HKCR has some "deleteable" subkeys)
621 if ( m_strKey
.IsEmpty() || (m_hRootKey
!= HKCR
&&
622 m_strKey
.Find(REG_SEPARATOR
) == wxNOT_FOUND
) ) {
623 wxLogError(_("Registry key '%s' is needed for normal system operation,\ndeleting it will leave your system in unusable state:\noperation aborted."), GetFullName(this));
628 // we can't delete keys while enumerating because it confuses GetNextKey, so
629 // we first save the key names and then delete them all
630 wxArrayString astrSubkeys
;
634 bool bCont
= GetFirstKey(strKey
, lIndex
);
636 astrSubkeys
.Add(strKey
);
638 bCont
= GetNextKey(strKey
, lIndex
);
641 size_t nKeyCount
= astrSubkeys
.Count();
642 for ( size_t nKey
= 0; nKey
< nKeyCount
; nKey
++ ) {
643 wxRegKey
key(*this, astrSubkeys
[nKey
]);
644 if ( !key
.DeleteSelf() )
648 // now delete this key itself
651 m_dwLastError
= RegDeleteKey((HKEY
) m_hRootKey
, m_strKey
);
652 if ( m_dwLastError
!= ERROR_SUCCESS
) {
653 wxLogSysError(m_dwLastError
, _("Can't delete key '%s'"),
661 bool wxRegKey::DeleteKey(const wxChar
*szKey
)
666 wxRegKey
key(*this, szKey
);
667 return key
.DeleteSelf();
670 bool wxRegKey::DeleteValue(const wxChar
*szValue
)
675 #if defined(__WIN32__) && !defined(__TWIN32__)
676 m_dwLastError
= RegDeleteValue((HKEY
) m_hKey
, WXSTRINGCAST szValue
);
677 if ( m_dwLastError
!= ERROR_SUCCESS
) {
678 wxLogSysError(m_dwLastError
, _("Can't delete value '%s' from key '%s'"),
679 szValue
, GetName().c_str());
683 // named registry values don't exist in Win16 world
684 wxASSERT( IsEmpty(szValue
) );
686 // just set the (default and unique) value of the key to ""
687 m_dwLastError
= RegSetValue((HKEY
) m_hKey
, NULL
, REG_SZ
, "", RESERVED
);
688 if ( m_dwLastError
!= ERROR_SUCCESS
) {
689 wxLogSysError(m_dwLastError
, _("Can't delete value of key '%s'"),
698 // ----------------------------------------------------------------------------
699 // access to values and subkeys
700 // ----------------------------------------------------------------------------
702 // return TRUE if value exists
703 bool wxRegKey::HasValue(const wxChar
*szValue
) const
705 // this function should be silent, so suppress possible messages from Open()
709 if ( !CONST_CAST
Open() )
712 LONG dwRet
= ::RegQueryValueEx((HKEY
) m_hKey
,
713 WXSTRINGCAST szValue
,
716 return dwRet
== ERROR_SUCCESS
;
718 // only unnamed value exists
719 return IsEmpty(szValue
);
723 // returns TRUE if this key has any values
724 bool wxRegKey::HasValues() const
726 // suppress possible messages from GetFirstValue()
729 // just call GetFirstValue with dummy parameters
732 return CONST_CAST
GetFirstValue(str
, l
);
735 // returns TRUE if this key has any subkeys
736 bool wxRegKey::HasSubkeys() const
738 // suppress possible messages from GetFirstKey()
741 // just call GetFirstKey with dummy parameters
744 return CONST_CAST
GetFirstKey(str
, l
);
747 // returns TRUE if given subkey exists
748 bool wxRegKey::HasSubKey(const wxChar
*szKey
) const
750 // this function should be silent, so suppress possible messages from Open()
753 if ( !CONST_CAST
Open() )
756 return KeyExists(m_hKey
, szKey
);
759 wxRegKey::ValueType
wxRegKey::GetValueType(const wxChar
*szValue
) const
762 if ( ! CONST_CAST
Open() )
766 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
767 &dwType
, NULL
, NULL
);
768 if ( m_dwLastError
!= ERROR_SUCCESS
) {
769 wxLogSysError(m_dwLastError
, _("Can't read value of key '%s'"),
774 return (ValueType
)dwType
;
776 return IsEmpty(szValue
) ? Type_String
: Type_None
;
781 bool wxRegKey::SetValue(const wxChar
*szValue
, long lValue
)
784 wxFAIL_MSG("RegSetValueEx not implemented by TWIN32");
787 if ( CONST_CAST
Open() ) {
788 m_dwLastError
= RegSetValueEx((HKEY
) m_hKey
, szValue
, (DWORD
) RESERVED
, REG_DWORD
,
789 (RegString
)&lValue
, sizeof(lValue
));
790 if ( m_dwLastError
== ERROR_SUCCESS
)
794 wxLogSysError(m_dwLastError
, _("Can't set value of '%s'"),
795 GetFullName(this, szValue
));
800 bool wxRegKey::QueryValue(const wxChar
*szValue
, long *plValue
) const
802 if ( CONST_CAST
Open() ) {
803 DWORD dwType
, dwSize
= sizeof(DWORD
);
804 RegString pBuf
= (RegString
)plValue
;
805 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
806 &dwType
, pBuf
, &dwSize
);
807 if ( m_dwLastError
!= ERROR_SUCCESS
) {
808 wxLogSysError(m_dwLastError
, _("Can't read value of key '%s'"),
813 // check that we read the value of right type
814 wxASSERT_MSG( IsNumericValue(szValue
),
815 wxT("Type mismatch in wxRegKey::QueryValue().") );
826 bool wxRegKey::QueryValue(const wxChar
*szValue
, wxString
& strValue
) const
828 if ( CONST_CAST
Open() ) {
830 // first get the type and size of the data
831 DWORD dwType
, dwSize
;
832 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
833 &dwType
, NULL
, &dwSize
);
834 if ( m_dwLastError
== ERROR_SUCCESS
) {
836 // must treat this case specially as GetWriteBuf() doesn't like
837 // being called with 0 size
841 RegString pBuf
= (RegString
)strValue
.GetWriteBuf(dwSize
);
842 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
,
843 WXSTRINGCAST szValue
,
848 strValue
.UngetWriteBuf();
851 if ( m_dwLastError
== ERROR_SUCCESS
) {
852 // check that it was the right type
853 wxASSERT_MSG( !IsNumericValue(szValue
),
854 wxT("Type mismatch in wxRegKey::QueryValue().") );
860 // named registry values don't exist in Win16
861 wxASSERT( IsEmpty(szValue
) );
863 m_dwLastError
= RegQueryValue((HKEY
) m_hKey
, 0, strValue
.GetWriteBuf(256), &l
);
864 strValue
.UngetWriteBuf();
865 if ( m_dwLastError
== ERROR_SUCCESS
)
870 wxLogSysError(m_dwLastError
, _("Can't read value of '%s'"),
871 GetFullName(this, szValue
));
875 bool wxRegKey::SetValue(const wxChar
*szValue
, const wxString
& strValue
)
877 if ( CONST_CAST
Open() ) {
878 #if defined( __WIN32__) && !defined(__TWIN32__)
879 m_dwLastError
= RegSetValueEx((HKEY
) m_hKey
, szValue
, (DWORD
) RESERVED
, REG_SZ
,
880 (RegString
)strValue
.c_str(),
881 (strValue
.Len() + 1)*sizeof(wxChar
));
882 if ( m_dwLastError
== ERROR_SUCCESS
)
885 // named registry values don't exist in Win16
886 wxASSERT( IsEmpty(szValue
) );
888 m_dwLastError
= RegSetValue((HKEY
) m_hKey
, NULL
, REG_SZ
, strValue
, NULL
);
889 if ( m_dwLastError
== ERROR_SUCCESS
)
894 wxLogSysError(m_dwLastError
, _("Can't set value of '%s'"),
895 GetFullName(this, szValue
));
899 wxRegKey::operator wxString() const
902 QueryValue(NULL
, str
);
906 // ----------------------------------------------------------------------------
908 // NB: all these functions require an index variable which allows to have
909 // several concurrently running indexations on the same key
910 // ----------------------------------------------------------------------------
912 bool wxRegKey::GetFirstValue(wxString
& strValueName
, long& lIndex
)
918 return GetNextValue(strValueName
, lIndex
);
921 bool wxRegKey::GetNextValue(wxString
& strValueName
, long& lIndex
) const
923 wxASSERT( IsOpened() );
925 // are we already at the end of enumeration?
929 #if defined( __WIN32__) && !defined(__TWIN32__)
930 wxChar szValueName
[1024]; // @@ use RegQueryInfoKey...
931 DWORD dwValueLen
= WXSIZEOF(szValueName
);
933 m_dwLastError
= RegEnumValue((HKEY
) m_hKey
, lIndex
++,
934 szValueName
, &dwValueLen
,
937 NULL
, // [out] buffer for value
938 NULL
); // [i/o] it's length
940 if ( m_dwLastError
!= ERROR_SUCCESS
) {
941 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
942 m_dwLastError
= ERROR_SUCCESS
;
946 wxLogSysError(m_dwLastError
, _("Can't enumerate values of key '%s'"),
953 strValueName
= szValueName
;
955 // only one unnamed value
956 wxASSERT( lIndex
== 0 );
959 strValueName
.Empty();
965 bool wxRegKey::GetFirstKey(wxString
& strKeyName
, long& lIndex
)
971 return GetNextKey(strKeyName
, lIndex
);
974 bool wxRegKey::GetNextKey(wxString
& strKeyName
, long& lIndex
) const
976 wxASSERT( IsOpened() );
978 // are we already at the end of enumeration?
982 wxChar szKeyName
[_MAX_PATH
+ 1];
983 m_dwLastError
= RegEnumKey((HKEY
) m_hKey
, lIndex
++, szKeyName
, WXSIZEOF(szKeyName
));
985 if ( m_dwLastError
!= ERROR_SUCCESS
) {
986 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
987 m_dwLastError
= ERROR_SUCCESS
;
991 wxLogSysError(m_dwLastError
, _("Can't enumerate subkeys of key '%s'"),
998 strKeyName
= szKeyName
;
1002 // returns TRUE if the value contains a number (else it's some string)
1003 bool wxRegKey::IsNumericValue(const wxChar
*szValue
) const
1005 ValueType type
= GetValueType(szValue
);
1008 /* case Type_Dword_little_endian: == Type_Dword */
1009 case Type_Dword_big_endian
:
1017 // ============================================================================
1018 // implementation of global private functions
1019 // ============================================================================
1021 bool KeyExists(WXHKEY hRootKey
, const wxChar
*szKey
)
1023 // don't close this key itself for the case of empty szKey!
1024 if ( wxIsEmpty(szKey
) )
1028 if ( RegOpenKey( (HKEY
) hRootKey
, szKey
, &hkeyDummy
) == ERROR_SUCCESS
) {
1029 RegCloseKey(hkeyDummy
);
1036 const wxChar
*GetFullName(const wxRegKey
*pKey
, const wxChar
*szValue
)
1038 static wxString s_str
;
1039 s_str
= pKey
->GetName();
1040 if ( !wxIsEmpty(szValue
) )
1041 s_str
<< wxT("\\") << szValue
;
1043 return s_str
.c_str();
1046 void RemoveTrailingSeparator(wxString
& str
)
1048 if ( !str
.IsEmpty() && str
.Last() == REG_SEPARATOR
)
1049 str
.Truncate(str
.Len() - 1);