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"
36 #define WIN32_LEAN_AND_MEAN
42 #include <stdlib.h> // for _MAX_PATH
49 #define HKEY_DEFINED // already defined in windows.h
50 #include "wx/msw/registry.h"
52 // some registry functions don't like signed chars
53 typedef unsigned char *RegString
;
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // the standard key names, short names and handles all bundled together for
65 const wxChar
*szShortName
;
69 { HKEY_CLASSES_ROOT
, wxT("HKEY_CLASSES_ROOT"), wxT("HKCR") },
71 { HKEY_CURRENT_USER
, wxT("HKEY_CURRENT_USER"), wxT("HKCU") },
72 { HKEY_LOCAL_MACHINE
, wxT("HKEY_LOCAL_MACHINE"), wxT("HKLM") },
73 { HKEY_USERS
, wxT("HKEY_USERS"), wxT("HKU") }, // short name?
74 { HKEY_PERFORMANCE_DATA
, wxT("HKEY_PERFORMANCE_DATA"), wxT("HKPD") },
76 { HKEY_CURRENT_CONFIG
, wxT("HKEY_CURRENT_CONFIG"), wxT("HKCC") },
78 { HKEY_DYN_DATA
, wxT("HKEY_DYN_DATA"), wxT("HKDD") }, // short name?
80 #endif //WINVER >= 4.0
84 // the registry name separator (perhaps one day MS will change it to '/' ;-)
85 #define REG_SEPARATOR wxT('\\')
87 // useful for Windows programmers: makes somewhat more clear all these zeroes
88 // being passed to Windows APIs
89 #define RESERVED (NULL)
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
94 // @ const_cast<> is not yet supported by all compilers
95 #define CONST_CAST ((wxRegKey *)this)->
98 #define m_dwLastError CONST_CAST m_dwLastError
101 // ----------------------------------------------------------------------------
102 // non member functions
103 // ----------------------------------------------------------------------------
105 // removes the trailing backslash from the string if it has one
106 static inline void RemoveTrailingSeparator(wxString
& str
);
108 // returns TRUE if given registry key exists
109 static bool KeyExists(WXHKEY hRootKey
, const wxChar
*szKey
);
111 // combines value and key name (uses static buffer!)
112 static const wxChar
*GetFullName(const wxRegKey
*pKey
,
113 const wxChar
*szValue
= NULL
);
115 // ============================================================================
116 // implementation of wxRegKey class
117 // ============================================================================
119 // ----------------------------------------------------------------------------
120 // static functions and variables
121 // ----------------------------------------------------------------------------
123 const size_t wxRegKey
::nStdKeys
= WXSIZEOF(aStdKeys
);
125 // @@ should take a `StdKey key', but as it's often going to be used in loops
126 // it would require casts in user code.
127 const wxChar
*wxRegKey
::GetStdKeyName(size_t key
)
129 // return empty string if key is invalid
130 wxCHECK_MSG( key
< nStdKeys
, wxT(""), wxT("invalid key in wxRegKey::GetStdKeyName") );
132 return aStdKeys
[key
].szName
;
135 const wxChar
*wxRegKey
::GetStdKeyShortName(size_t key
)
137 // return empty string if key is invalid
138 wxCHECK( key
< nStdKeys
, wxT("") );
140 return aStdKeys
[key
].szShortName
;
143 wxRegKey
::StdKey wxRegKey
::ExtractKeyName(wxString
& strKey
)
145 wxString strRoot
= strKey
.BeforeFirst(REG_SEPARATOR
);
149 for ( ui
= 0; ui
< nStdKeys
; ui
++ ) {
150 if ( strRoot
.CmpNoCase(aStdKeys
[ui
].szName
) == 0 ||
151 strRoot
.CmpNoCase(aStdKeys
[ui
].szShortName
) == 0 ) {
152 hRootKey
= aStdKeys
[ui
].hkey
;
157 if ( ui
== nStdKeys
) {
158 wxFAIL_MSG(wxT("invalid key prefix in wxRegKey::ExtractKeyName."));
160 hRootKey
= HKEY_CLASSES_ROOT
;
163 strKey
= strKey
.After(REG_SEPARATOR
);
164 if ( !strKey
.IsEmpty() && strKey
.Last() == REG_SEPARATOR
)
165 strKey
.Truncate(strKey
.Len() - 1);
168 return (wxRegKey
::StdKey
)(int)hRootKey
;
171 wxRegKey
::StdKey wxRegKey
::GetStdKeyFromHkey(WXHKEY hkey
)
173 for ( size_t ui
= 0; ui
< nStdKeys
; ui
++ ) {
174 if ( (int) aStdKeys
[ui
].hkey
== (int) hkey
)
178 wxFAIL_MSG(wxT("non root hkey passed to wxRegKey::GetStdKeyFromHkey."));
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
190 m_hRootKey
= (WXHKEY
) aStdKeys
[HKCR
].hkey
;
194 wxRegKey
::wxRegKey(const wxString
& strKey
) : m_strKey(strKey
)
196 m_hRootKey
= (WXHKEY
) aStdKeys
[ExtractKeyName(m_strKey
)].hkey
;
197 m_hKey
= (WXHKEY
) NULL
;
201 // parent is a predefined (and preopened) key
202 wxRegKey
::wxRegKey(StdKey keyParent
, const wxString
& strKey
) : m_strKey(strKey
)
204 RemoveTrailingSeparator(m_strKey
);
205 m_hRootKey
= (WXHKEY
) aStdKeys
[keyParent
].hkey
;
206 m_hKey
= (WXHKEY
) NULL
;
210 // parent is a normal regkey
211 wxRegKey
::wxRegKey(const wxRegKey
& keyParent
, const wxString
& strKey
)
212 : m_strKey(keyParent
.m_strKey
)
214 // combine our name with parent's to get the full name
215 if ( !m_strKey
.IsEmpty() &&
216 (strKey
.IsEmpty() || strKey
[0] != REG_SEPARATOR
) ) {
217 m_strKey
+= REG_SEPARATOR
;
221 RemoveTrailingSeparator(m_strKey
);
223 m_hRootKey
= keyParent
.m_hRootKey
;
224 m_hKey
= (WXHKEY
) NULL
;
228 // dtor closes the key releasing system resource
229 wxRegKey
::~wxRegKey()
234 // ----------------------------------------------------------------------------
235 // change the key name/hkey
236 // ----------------------------------------------------------------------------
238 // set the full key name
239 void wxRegKey
::SetName(const wxString
& strKey
)
244 m_hRootKey
= (WXHKEY
) aStdKeys
[ExtractKeyName(m_strKey
)].hkey
;
247 // the name is relative to the parent key
248 void wxRegKey
::SetName(StdKey keyParent
, const wxString
& strKey
)
253 RemoveTrailingSeparator(m_strKey
);
254 m_hRootKey
= (WXHKEY
) aStdKeys
[keyParent
].hkey
;
257 // the name is relative to the parent key
258 void wxRegKey
::SetName(const wxRegKey
& keyParent
, const wxString
& strKey
)
262 // combine our name with parent's to get the full name
263 m_strKey
= keyParent
.m_strKey
;
264 if ( !strKey
.IsEmpty() && strKey
[0] != REG_SEPARATOR
)
265 m_strKey
+= REG_SEPARATOR
;
268 RemoveTrailingSeparator(m_strKey
);
270 m_hRootKey
= keyParent
.m_hRootKey
;
273 // hKey should be opened and will be closed in wxRegKey dtor
274 void wxRegKey
::SetHkey(WXHKEY hKey
)
281 // ----------------------------------------------------------------------------
282 // info about the key
283 // ----------------------------------------------------------------------------
285 // returns TRUE if the key exists
286 bool wxRegKey
::Exists() const
288 // opened key has to exist, try to open it if not done yet
289 return IsOpened() ? TRUE
: KeyExists(m_hRootKey
, m_strKey
);
292 // returns the full name of the key (prefix is abbreviated if bShortPrefix)
293 wxString wxRegKey
::GetName(bool bShortPrefix
) const
295 StdKey key
= GetStdKeyFromHkey((StdKey
) m_hRootKey
);
296 wxString str
= bShortPrefix ? aStdKeys
[key
].szShortName
297 : aStdKeys
[key
].szName
;
298 if ( !m_strKey
.IsEmpty() )
299 str
<< "\\" << m_strKey
;
304 bool wxRegKey
::GetKeyInfo(size_t *pnSubKeys
,
307 size_t *pnMaxValueLen
) const
309 #if defined(__WIN32__) && !defined(__TWIN32__)
311 // old gcc headers incorrectly prototype RegQueryInfoKey()
312 #ifdef __GNUWIN32_OLD__
313 #define REG_PARAM (size_t *)
315 #define REG_PARAM (LPDWORD)
318 // it might be unexpected to some that this function doesn't open the key
319 wxASSERT_MSG( IsOpened(), _T("key should be opened in GetKeyInfo") );
321 m_dwLastError
= ::RegQueryInfoKey
325 NULL
, // (ptr to) size of class name buffer
328 pnSubKeys
, // [out] number of subkeys
330 pnMaxKeyLen
, // [out] max length of a subkey name
331 NULL
, // longest subkey class name
333 pnValues
, // [out] number of values
335 pnMaxValueLen
, // [out] max length of a value name
336 NULL
, // longest value data
337 NULL
, // security descriptor
338 NULL
// time of last modification
343 if ( m_dwLastError
!= ERROR_SUCCESS
) {
344 wxLogSysError(m_dwLastError
, _("Can't get info about registry key '%s'"),
351 wxFAIL_MSG("GetKeyInfo() not implemented");
357 // ----------------------------------------------------------------------------
359 // ----------------------------------------------------------------------------
361 // opens key (it's not an error to call Open() on an already opened key)
362 bool wxRegKey
::Open()
368 m_dwLastError
= RegOpenKey((HKEY
) m_hRootKey
, m_strKey
, &tmpKey
);
369 if ( m_dwLastError
!= ERROR_SUCCESS
) {
370 wxLogSysError(m_dwLastError
, _("Can't open registry key '%s'"),
376 m_hKey
= (WXHKEY
) tmpKey
;
381 // creates key, failing if it exists and !bOkIfExists
382 bool wxRegKey
::Create(bool bOkIfExists
)
384 // check for existence only if asked (i.e. order is important!)
385 if ( !bOkIfExists
&& Exists() ) {
393 m_dwLastError
= RegCreateKey((HKEY
) m_hRootKey
, m_strKey
, &tmpKey
);
394 if ( m_dwLastError
!= ERROR_SUCCESS
) {
395 wxLogSysError(m_dwLastError
, _("Can't create registry key '%s'"),
401 m_hKey
= (WXHKEY
) tmpKey
;
406 // close the key, it's not an error to call it when not opened
407 bool wxRegKey
::Close()
410 m_dwLastError
= RegCloseKey((HKEY
) m_hKey
);
411 if ( m_dwLastError
!= ERROR_SUCCESS
) {
412 wxLogSysError(m_dwLastError
, _("Can't close registry key '%s'"),
426 bool wxRegKey
::RenameValue(const wxChar
*szValueOld
, const wxChar
*szValueNew
)
429 if ( HasValue(szValueNew
) ) {
430 wxLogError(_("Registry value '%s' already exists."), szValueNew
);
436 !CopyValue(szValueOld
, *this, szValueNew
) ||
437 !DeleteValue(szValueOld
) ) {
438 wxLogError(_("Failed to rename registry value '%s' to '%s'."),
439 szValueOld
, szValueNew
);
447 bool wxRegKey
::CopyValue(const wxChar
*szValue
,
449 const wxChar
*szValueNew
)
452 // by default, use the same name
453 szValueNew
= szValue
;
456 switch ( GetValueType(szValue
) ) {
460 return QueryValue(szValue
, strVal
) &&
461 keyDst
.SetValue(szValueNew
, strVal
);
465 /* case Type_Dword_little_endian: == Type_Dword */
468 return QueryValue(szValue
, &dwVal
) &&
469 keyDst
.SetValue(szValueNew
, dwVal
);
472 // these types are unsupported because I am not sure about how
473 // exactly they should be copied and because they shouldn't
474 // occur among the application keys (supposedly created with
478 case Type_Expand_String
:
480 case Type_Dword_big_endian
:
482 case Type_Multi_String
:
483 case Type_Resource_list
:
484 case Type_Full_resource_descriptor
:
485 case Type_Resource_requirements_list
:
488 wxLogError(_("Can't copy values of unsupported type %d."),
489 GetValueType(szValue
));
494 bool wxRegKey
::Rename(const wxChar
*szNewName
)
496 wxCHECK_MSG( !!m_strKey
, FALSE
, _T("registry hives can't be renamed") );
499 wxLogError(_("Registry key '%s' does not exist, cannot rename it."),
505 // do we stay in the same hive?
506 bool inSameHive
= !wxStrchr(szNewName
, REG_SEPARATOR
);
508 // construct the full new name of the key
512 // rename the key to the new name under the same parent
513 wxString strKey
= m_strKey
.BeforeLast(REG_SEPARATOR
);
515 // don't add '\\' in the start if strFullNewName is empty
516 strKey
+= REG_SEPARATOR
;
521 keyDst
.SetName(GetStdKeyFromHkey(m_hRootKey
), strKey
);
524 // this is the full name already
525 keyDst
.SetName(szNewName
);
528 bool ok
= keyDst
.Create(FALSE
/* fail if alredy exists */);
530 wxLogError(_("Registry key '%s' already exists."),
531 GetFullName(&keyDst
));
534 ok
= Copy(keyDst
) && DeleteSelf();
538 wxLogError(_("Failed to rename the registry key '%s' to '%s'."),
539 GetFullName(this), GetFullName(&keyDst
));
542 m_hRootKey
= keyDst
.m_hRootKey
;
543 m_strKey
= keyDst
.m_strKey
;
549 bool wxRegKey
::Copy(const wxChar
*szNewName
)
551 // create the new key first
552 wxRegKey
keyDst(szNewName
);
553 bool ok
= keyDst
.Create(FALSE
/* fail if alredy exists */);
557 // we created the dest key but copying to it failed - delete it
559 (void)keyDst
.DeleteSelf();
566 bool wxRegKey
::Copy(wxRegKey
& keyDst
)
570 // copy all sub keys to the new location
573 bool bCont
= GetFirstKey(strKey
, lIndex
);
574 while ( ok
&& bCont
) {
575 wxRegKey
key(*this, strKey
);
577 keyName
<< GetFullName(&keyDst
) << REG_SEPARATOR
<< strKey
;
578 ok
= key
.Copy((const wxChar
*) keyName
);
581 bCont
= GetNextKey(strKey
, lIndex
);
586 bCont
= GetFirstValue(strVal
, lIndex
);
587 while ( ok
&& bCont
) {
588 ok
= CopyValue(strVal
, keyDst
);
591 wxLogSysError(m_dwLastError
,
592 _("Failed to copy registry value '%s'"),
596 bCont
= GetNextValue(strVal
, lIndex
);
601 wxLogError(_("Failed to copy the contents of registry key '%s' to '%s'."), GetFullName(this), GetFullName(&keyDst
));
607 // ----------------------------------------------------------------------------
608 // delete keys/values
609 // ----------------------------------------------------------------------------
610 bool wxRegKey
::DeleteSelf()
615 // it already doesn't exist - ok!
620 // prevent a buggy program from erasing one of the root registry keys or an
621 // immediate subkey (i.e. one which doesn't have '\\' inside) of any other
622 // key except HKCR (HKCR has some "deleteable" subkeys)
623 if ( m_strKey
.IsEmpty() || (m_hRootKey
!= HKCR
&&
624 m_strKey
.Find(REG_SEPARATOR
) == wxNOT_FOUND
) ) {
625 wxLogError(_("Registry key '%s' is needed for normal system operation,\ndeleting it will leave your system in unusable state:\noperation aborted."), GetFullName(this));
630 // we can't delete keys while enumerating because it confuses GetNextKey, so
631 // we first save the key names and then delete them all
632 wxArrayString astrSubkeys
;
636 bool bCont
= GetFirstKey(strKey
, lIndex
);
638 astrSubkeys
.Add(strKey
);
640 bCont
= GetNextKey(strKey
, lIndex
);
643 size_t nKeyCount
= astrSubkeys
.Count();
644 for ( size_t nKey
= 0; nKey
< nKeyCount
; nKey
++ ) {
645 wxRegKey
key(*this, astrSubkeys
[nKey
]);
646 if ( !key
.DeleteSelf() )
650 // now delete this key itself
653 m_dwLastError
= RegDeleteKey((HKEY
) m_hRootKey
, m_strKey
);
654 if ( m_dwLastError
!= ERROR_SUCCESS
) {
655 wxLogSysError(m_dwLastError
, _("Can't delete key '%s'"),
663 bool wxRegKey
::DeleteKey(const wxChar
*szKey
)
668 wxRegKey
key(*this, szKey
);
669 return key
.DeleteSelf();
672 bool wxRegKey
::DeleteValue(const wxChar
*szValue
)
677 #if defined(__WIN32__) && !defined(__TWIN32__)
678 m_dwLastError
= RegDeleteValue((HKEY
) m_hKey
, WXSTRINGCAST szValue
);
679 if ( m_dwLastError
!= ERROR_SUCCESS
) {
680 wxLogSysError(m_dwLastError
, _("Can't delete value '%s' from key '%s'"),
681 szValue
, GetName().c_str());
685 // named registry values don't exist in Win16 world
686 wxASSERT( IsEmpty(szValue
) );
688 // just set the (default and unique) value of the key to ""
689 m_dwLastError
= RegSetValue((HKEY
) m_hKey
, NULL
, REG_SZ
, "", RESERVED
);
690 if ( m_dwLastError
!= ERROR_SUCCESS
) {
691 wxLogSysError(m_dwLastError
, _("Can't delete value of key '%s'"),
700 // ----------------------------------------------------------------------------
701 // access to values and subkeys
702 // ----------------------------------------------------------------------------
704 // return TRUE if value exists
705 bool wxRegKey
::HasValue(const wxChar
*szValue
) const
707 // this function should be silent, so suppress possible messages from Open()
711 if ( !CONST_CAST
Open() )
714 LONG dwRet
= ::RegQueryValueEx((HKEY
) m_hKey
,
715 WXSTRINGCAST szValue
,
718 return dwRet
== ERROR_SUCCESS
;
720 // only unnamed value exists
721 return IsEmpty(szValue
);
725 // returns TRUE if this key has any values
726 bool wxRegKey
::HasValues() const
728 // suppress possible messages from GetFirstValue()
731 // just call GetFirstValue with dummy parameters
734 return CONST_CAST
GetFirstValue(str
, l
);
737 // returns TRUE if this key has any subkeys
738 bool wxRegKey
::HasSubkeys() const
740 // suppress possible messages from GetFirstKey()
743 // just call GetFirstKey with dummy parameters
746 return CONST_CAST
GetFirstKey(str
, l
);
749 // returns TRUE if given subkey exists
750 bool wxRegKey
::HasSubKey(const wxChar
*szKey
) const
752 // this function should be silent, so suppress possible messages from Open()
755 if ( !CONST_CAST
Open() )
758 return KeyExists(m_hKey
, szKey
);
761 wxRegKey
::ValueType wxRegKey
::GetValueType(const wxChar
*szValue
) const
764 if ( ! CONST_CAST
Open() )
768 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
769 &dwType
, NULL
, NULL
);
770 if ( m_dwLastError
!= ERROR_SUCCESS
) {
771 wxLogSysError(m_dwLastError
, _("Can't read value of key '%s'"),
776 return (ValueType
)dwType
;
778 return IsEmpty(szValue
) ? Type_String
: Type_None
;
783 bool wxRegKey
::SetValue(const wxChar
*szValue
, long lValue
)
786 wxFAIL_MSG("RegSetValueEx not implemented by TWIN32");
789 if ( CONST_CAST
Open() ) {
790 m_dwLastError
= RegSetValueEx((HKEY
) m_hKey
, szValue
, (DWORD
) RESERVED
, REG_DWORD
,
791 (RegString
)&lValue
, sizeof(lValue
));
792 if ( m_dwLastError
== ERROR_SUCCESS
)
796 wxLogSysError(m_dwLastError
, _("Can't set value of '%s'"),
797 GetFullName(this, szValue
));
802 bool wxRegKey
::QueryValue(const wxChar
*szValue
, long *plValue
) const
804 if ( CONST_CAST
Open() ) {
805 DWORD dwType
, dwSize
= sizeof(DWORD
);
806 RegString pBuf
= (RegString
)plValue
;
807 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
808 &dwType
, pBuf
, &dwSize
);
809 if ( m_dwLastError
!= ERROR_SUCCESS
) {
810 wxLogSysError(m_dwLastError
, _("Can't read value of key '%s'"),
815 // check that we read the value of right type
816 wxASSERT_MSG( IsNumericValue(szValue
),
817 wxT("Type mismatch in wxRegKey::QueryValue().") );
828 bool wxRegKey
::QueryValue(const wxChar
*szValue
,
832 if ( CONST_CAST
Open() ) {
834 // first get the type and size of the data
835 DWORD dwType
, dwSize
;
836 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
837 &dwType
, NULL
, &dwSize
);
838 if ( m_dwLastError
== ERROR_SUCCESS
) {
840 // must treat this case specially as GetWriteBuf() doesn't like
841 // being called with 0 size
845 RegString pBuf
= (RegString
)strValue
.GetWriteBuf(dwSize
);
846 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
,
847 WXSTRINGCAST szValue
,
852 strValue
.UngetWriteBuf();
854 // expand the var expansions in the string unless disabled
855 if ( (dwType
== REG_EXPAND_SZ
) && !raw
)
857 DWORD dwExpSize
= ::ExpandEnvironmentStrings(strValue
, NULL
, 0);
858 bool ok
= dwExpSize
!= 0;
861 wxString strExpValue
;
862 ok
= ::ExpandEnvironmentStrings
865 strExpValue
.GetWriteBuf(dwExpSize
),
868 strExpValue
.UngetWriteBuf();
869 strValue
= strExpValue
;
874 wxLogLastError(_T("ExpandEnvironmentStrings"));
879 if ( m_dwLastError
== ERROR_SUCCESS
) {
880 // check that it was the right type
881 wxASSERT_MSG( !IsNumericValue(szValue
),
882 wxT("Type mismatch in wxRegKey::QueryValue().") );
888 // named registry values don't exist in Win16
889 wxASSERT( IsEmpty(szValue
) );
891 m_dwLastError
= RegQueryValue((HKEY
) m_hKey
, 0, strValue
.GetWriteBuf(256), &l
);
892 strValue
.UngetWriteBuf();
893 if ( m_dwLastError
== ERROR_SUCCESS
)
898 wxLogSysError(m_dwLastError
, _("Can't read value of '%s'"),
899 GetFullName(this, szValue
));
903 bool wxRegKey
::SetValue(const wxChar
*szValue
, const wxString
& strValue
)
905 if ( CONST_CAST
Open() ) {
906 #if defined( __WIN32__) && !defined(__TWIN32__)
907 m_dwLastError
= RegSetValueEx((HKEY
) m_hKey
, szValue
, (DWORD
) RESERVED
, REG_SZ
,
908 (RegString
)strValue
.c_str(),
909 (strValue
.Len() + 1)*sizeof(wxChar
));
910 if ( m_dwLastError
== ERROR_SUCCESS
)
913 // named registry values don't exist in Win16
914 wxASSERT( IsEmpty(szValue
) );
916 m_dwLastError
= RegSetValue((HKEY
) m_hKey
, NULL
, REG_SZ
, strValue
, NULL
);
917 if ( m_dwLastError
== ERROR_SUCCESS
)
922 wxLogSysError(m_dwLastError
, _("Can't set value of '%s'"),
923 GetFullName(this, szValue
));
927 wxRegKey
::operator wxString() const
930 QueryValue(NULL
, str
);
934 // ----------------------------------------------------------------------------
936 // NB: all these functions require an index variable which allows to have
937 // several concurrently running indexations on the same key
938 // ----------------------------------------------------------------------------
940 bool wxRegKey
::GetFirstValue(wxString
& strValueName
, long& lIndex
)
946 return GetNextValue(strValueName
, lIndex
);
949 bool wxRegKey
::GetNextValue(wxString
& strValueName
, long& lIndex
) const
951 wxASSERT( IsOpened() );
953 // are we already at the end of enumeration?
957 #if defined( __WIN32__) && !defined(__TWIN32__)
958 wxChar szValueName
[1024]; // @@ use RegQueryInfoKey...
959 DWORD dwValueLen
= WXSIZEOF(szValueName
);
961 m_dwLastError
= RegEnumValue((HKEY
) m_hKey
, lIndex
++,
962 szValueName
, &dwValueLen
,
965 NULL
, // [out] buffer for value
966 NULL
); // [i/o] it's length
968 if ( m_dwLastError
!= ERROR_SUCCESS
) {
969 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
970 m_dwLastError
= ERROR_SUCCESS
;
974 wxLogSysError(m_dwLastError
, _("Can't enumerate values of key '%s'"),
981 strValueName
= szValueName
;
983 // only one unnamed value
984 wxASSERT( lIndex
== 0 );
987 strValueName
.Empty();
993 bool wxRegKey
::GetFirstKey(wxString
& strKeyName
, long& lIndex
)
999 return GetNextKey(strKeyName
, lIndex
);
1002 bool wxRegKey
::GetNextKey(wxString
& strKeyName
, long& lIndex
) const
1004 wxASSERT( IsOpened() );
1006 // are we already at the end of enumeration?
1010 wxChar szKeyName
[_MAX_PATH
+ 1];
1011 m_dwLastError
= RegEnumKey((HKEY
) m_hKey
, lIndex
++, szKeyName
, WXSIZEOF(szKeyName
));
1013 if ( m_dwLastError
!= ERROR_SUCCESS
) {
1014 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
1015 m_dwLastError
= ERROR_SUCCESS
;
1019 wxLogSysError(m_dwLastError
, _("Can't enumerate subkeys of key '%s'"),
1026 strKeyName
= szKeyName
;
1030 // returns TRUE if the value contains a number (else it's some string)
1031 bool wxRegKey
::IsNumericValue(const wxChar
*szValue
) const
1033 ValueType type
= GetValueType(szValue
);
1036 /* case Type_Dword_little_endian: == Type_Dword */
1037 case Type_Dword_big_endian
:
1045 // ============================================================================
1046 // implementation of global private functions
1047 // ============================================================================
1049 bool KeyExists(WXHKEY hRootKey
, const wxChar
*szKey
)
1051 // don't close this key itself for the case of empty szKey!
1052 if ( wxIsEmpty(szKey
) )
1056 if ( RegOpenKey( (HKEY
) hRootKey
, szKey
, &hkeyDummy
) == ERROR_SUCCESS
) {
1057 RegCloseKey(hkeyDummy
);
1064 const wxChar
*GetFullName(const wxRegKey
*pKey
, const wxChar
*szValue
)
1066 static wxString s_str
;
1067 s_str
= pKey
->GetName();
1068 if ( !wxIsEmpty(szValue
) )
1069 s_str
<< wxT("\\") << szValue
;
1071 return s_str
.c_str();
1074 void RemoveTrailingSeparator(wxString
& str
)
1076 if ( !str
.IsEmpty() && str
.Last() == REG_SEPARATOR
)
1077 str
.Truncate(str
.Len() - 1);