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 licence
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 "wx/msw/private.h"
48 #include <stdlib.h> // for _MAX_PATH
55 #define HKEY_DEFINED // already defined in windows.h
56 #include "wx/msw/registry.h"
58 // some registry functions don't like signed chars
59 typedef unsigned char *RegString
;
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 // the standard key names, short names and handles all bundled together for
71 const wxChar
*szShortName
;
75 { HKEY_CLASSES_ROOT
, wxT("HKEY_CLASSES_ROOT"), wxT("HKCR") },
77 { HKEY_CURRENT_USER
, wxT("HKEY_CURRENT_USER"), wxT("HKCU") },
78 { HKEY_LOCAL_MACHINE
, wxT("HKEY_LOCAL_MACHINE"), wxT("HKLM") },
79 { HKEY_USERS
, wxT("HKEY_USERS"), wxT("HKU") }, // short name?
81 { HKEY_PERFORMANCE_DATA
, wxT("HKEY_PERFORMANCE_DATA"), wxT("HKPD") },
83 #if WINVER >= 0x0400 && !defined(__WXWINCE__)
84 { HKEY_CURRENT_CONFIG
, wxT("HKEY_CURRENT_CONFIG"), wxT("HKCC") },
85 #if !defined(__GNUWIN32__) && !defined(__WXWINCE__)
86 { HKEY_DYN_DATA
, wxT("HKEY_DYN_DATA"), wxT("HKDD") }, // short name?
88 #endif //WINVER >= 4.0
92 // the registry name separator (perhaps one day MS will change it to '/' ;-)
93 #define REG_SEPARATOR wxT('\\')
95 // useful for Windows programmers: makes somewhat more clear all these zeroes
96 // being passed to Windows APIs
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 // const_cast<> is not yet supported by all compilers
104 #define CONST_CAST ((wxRegKey *)this)->
106 // and neither is mutable which m_dwLastError should be
107 #define m_dwLastError CONST_CAST m_dwLastError
109 // ----------------------------------------------------------------------------
110 // non member functions
111 // ----------------------------------------------------------------------------
113 // removes the trailing backslash from the string if it has one
114 static inline void RemoveTrailingSeparator(wxString
& str
);
116 // returns TRUE if given registry key exists
117 static bool KeyExists(WXHKEY hRootKey
, const wxChar
*szKey
);
119 // combines value and key name (uses static buffer!)
120 static const wxChar
*GetFullName(const wxRegKey
*pKey
,
121 const wxChar
*szValue
= NULL
);
123 // ============================================================================
124 // implementation of wxRegKey class
125 // ============================================================================
127 // ----------------------------------------------------------------------------
128 // static functions and variables
129 // ----------------------------------------------------------------------------
131 const size_t wxRegKey::nStdKeys
= WXSIZEOF(aStdKeys
);
133 // @@ should take a `StdKey key', but as it's often going to be used in loops
134 // it would require casts in user code.
135 const wxChar
*wxRegKey::GetStdKeyName(size_t key
)
137 // return empty string if key is invalid
138 wxCHECK_MSG( key
< nStdKeys
, wxEmptyString
, wxT("invalid key in wxRegKey::GetStdKeyName") );
140 return aStdKeys
[key
].szName
;
143 const wxChar
*wxRegKey::GetStdKeyShortName(size_t key
)
145 // return empty string if key is invalid
146 wxCHECK( key
< nStdKeys
, wxEmptyString
);
148 return aStdKeys
[key
].szShortName
;
151 wxRegKey::StdKey
wxRegKey::ExtractKeyName(wxString
& strKey
)
153 wxString strRoot
= strKey
.BeforeFirst(REG_SEPARATOR
);
157 for ( ui
= 0; ui
< nStdKeys
; ui
++ ) {
158 if ( strRoot
.CmpNoCase(aStdKeys
[ui
].szName
) == 0 ||
159 strRoot
.CmpNoCase(aStdKeys
[ui
].szShortName
) == 0 ) {
160 hRootKey
= aStdKeys
[ui
].hkey
;
165 if ( ui
== nStdKeys
) {
166 wxFAIL_MSG(wxT("invalid key prefix in wxRegKey::ExtractKeyName."));
168 hRootKey
= HKEY_CLASSES_ROOT
;
171 strKey
= strKey
.After(REG_SEPARATOR
);
172 if ( !strKey
.IsEmpty() && strKey
.Last() == REG_SEPARATOR
)
173 strKey
.Truncate(strKey
.Len() - 1);
176 return (wxRegKey::StdKey
)(int)hRootKey
;
179 wxRegKey::StdKey
wxRegKey::GetStdKeyFromHkey(WXHKEY hkey
)
181 for ( size_t ui
= 0; ui
< nStdKeys
; ui
++ ) {
182 if ( (int) aStdKeys
[ui
].hkey
== (int) hkey
)
186 wxFAIL_MSG(wxT("non root hkey passed to wxRegKey::GetStdKeyFromHkey."));
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
197 m_hRootKey
= (WXHKEY
) aStdKeys
[HKCR
].hkey
;
202 wxRegKey::wxRegKey(const wxString
& strKey
) : m_strKey(strKey
)
204 m_hRootKey
= (WXHKEY
) aStdKeys
[ExtractKeyName(m_strKey
)].hkey
;
209 // parent is a predefined (and preopened) key
210 wxRegKey::wxRegKey(StdKey keyParent
, const wxString
& strKey
) : m_strKey(strKey
)
212 RemoveTrailingSeparator(m_strKey
);
213 m_hRootKey
= (WXHKEY
) aStdKeys
[keyParent
].hkey
;
218 // parent is a normal regkey
219 wxRegKey::wxRegKey(const wxRegKey
& keyParent
, const wxString
& strKey
)
220 : m_strKey(keyParent
.m_strKey
)
222 // combine our name with parent's to get the full name
223 if ( !m_strKey
.IsEmpty() &&
224 (strKey
.IsEmpty() || strKey
[0] != REG_SEPARATOR
) ) {
225 m_strKey
+= REG_SEPARATOR
;
229 RemoveTrailingSeparator(m_strKey
);
231 m_hRootKey
= keyParent
.m_hRootKey
;
236 // dtor closes the key releasing system resource
237 wxRegKey::~wxRegKey()
242 // ----------------------------------------------------------------------------
243 // change the key name/hkey
244 // ----------------------------------------------------------------------------
246 // set the full key name
247 void wxRegKey::SetName(const wxString
& strKey
)
252 m_hRootKey
= (WXHKEY
) aStdKeys
[ExtractKeyName(m_strKey
)].hkey
;
255 // the name is relative to the parent key
256 void wxRegKey::SetName(StdKey keyParent
, const wxString
& strKey
)
261 RemoveTrailingSeparator(m_strKey
);
262 m_hRootKey
= (WXHKEY
) aStdKeys
[keyParent
].hkey
;
265 // the name is relative to the parent key
266 void wxRegKey::SetName(const wxRegKey
& keyParent
, const wxString
& strKey
)
270 // combine our name with parent's to get the full name
272 // NB: this method is called by wxRegConfig::SetPath() which is a performance
273 // critical function and so it preallocates space for our m_strKey to
274 // gain some speed - this is why we only use += here and not = which
275 // would just free the prealloc'd buffer and would have to realloc it the
278 m_strKey
+= keyParent
.m_strKey
;
279 if ( !strKey
.IsEmpty() && strKey
[0] != REG_SEPARATOR
)
280 m_strKey
+= REG_SEPARATOR
;
283 RemoveTrailingSeparator(m_strKey
);
285 m_hRootKey
= keyParent
.m_hRootKey
;
288 // hKey should be opened and will be closed in wxRegKey dtor
289 void wxRegKey::SetHkey(WXHKEY hKey
)
296 // ----------------------------------------------------------------------------
297 // info about the key
298 // ----------------------------------------------------------------------------
300 // returns TRUE if the key exists
301 bool wxRegKey::Exists() const
303 // opened key has to exist, try to open it if not done yet
304 return IsOpened() ? TRUE
: KeyExists(m_hRootKey
, m_strKey
);
307 // returns the full name of the key (prefix is abbreviated if bShortPrefix)
308 wxString
wxRegKey::GetName(bool bShortPrefix
) const
310 StdKey key
= GetStdKeyFromHkey((WXHKEY
) m_hRootKey
);
311 wxString str
= bShortPrefix
? aStdKeys
[key
].szShortName
312 : aStdKeys
[key
].szName
;
313 if ( !m_strKey
.IsEmpty() )
314 str
<< _T("\\") << m_strKey
;
319 bool wxRegKey::GetKeyInfo(size_t *pnSubKeys
,
322 size_t *pnMaxValueLen
) const
324 #if defined(__WIN32__)
326 // old gcc headers incorrectly prototype RegQueryInfoKey()
327 #if defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__)
328 #define REG_PARAM (size_t *)
330 #define REG_PARAM (LPDWORD)
333 // it might be unexpected to some that this function doesn't open the key
334 wxASSERT_MSG( IsOpened(), _T("key should be opened in GetKeyInfo") );
336 m_dwLastError
= ::RegQueryInfoKey
340 NULL
, // (ptr to) size of class name buffer
343 pnSubKeys
, // [out] number of subkeys
345 pnMaxKeyLen
, // [out] max length of a subkey name
346 NULL
, // longest subkey class name
348 pnValues
, // [out] number of values
350 pnMaxValueLen
, // [out] max length of a value name
351 NULL
, // longest value data
352 NULL
, // security descriptor
353 NULL
// time of last modification
358 if ( m_dwLastError
!= ERROR_SUCCESS
) {
359 wxLogSysError(m_dwLastError
, _("Can't get info about registry key '%s'"),
366 wxFAIL_MSG("GetKeyInfo() not implemented");
372 // ----------------------------------------------------------------------------
374 // ----------------------------------------------------------------------------
376 // opens key (it's not an error to call Open() on an already opened key)
377 bool wxRegKey::Open()
383 m_dwLastError
= ::RegOpenKeyEx
392 if ( m_dwLastError
!= ERROR_SUCCESS
)
394 wxLogSysError(m_dwLastError
, _("Can't open registry key '%s'"),
399 m_hKey
= (WXHKEY
) tmpKey
;
403 // creates key, failing if it exists and !bOkIfExists
404 bool wxRegKey::Create(bool bOkIfExists
)
406 // check for existence only if asked (i.e. order is important!)
407 if ( !bOkIfExists
&& Exists() )
416 m_dwLastError
= RegCreateKeyEx((HKEY
) m_hRootKey
, m_strKey
,
418 NULL
, // class string
425 m_dwLastError
= RegCreateKey((HKEY
) m_hRootKey
, m_strKey
, &tmpKey
);
427 if ( m_dwLastError
!= ERROR_SUCCESS
) {
428 wxLogSysError(m_dwLastError
, _("Can't create registry key '%s'"),
434 m_hKey
= (WXHKEY
) tmpKey
;
439 // close the key, it's not an error to call it when not opened
440 bool wxRegKey::Close()
443 m_dwLastError
= RegCloseKey((HKEY
) m_hKey
);
446 if ( m_dwLastError
!= ERROR_SUCCESS
) {
447 wxLogSysError(m_dwLastError
, _("Can't close registry key '%s'"),
457 bool wxRegKey::RenameValue(const wxChar
*szValueOld
, const wxChar
*szValueNew
)
460 if ( HasValue(szValueNew
) ) {
461 wxLogError(_("Registry value '%s' already exists."), szValueNew
);
467 !CopyValue(szValueOld
, *this, szValueNew
) ||
468 !DeleteValue(szValueOld
) ) {
469 wxLogError(_("Failed to rename registry value '%s' to '%s'."),
470 szValueOld
, szValueNew
);
478 bool wxRegKey::CopyValue(const wxChar
*szValue
,
480 const wxChar
*szValueNew
)
483 // by default, use the same name
484 szValueNew
= szValue
;
487 switch ( GetValueType(szValue
) ) {
491 return QueryValue(szValue
, strVal
) &&
492 keyDst
.SetValue(szValueNew
, strVal
);
496 /* case Type_Dword_little_endian: == Type_Dword */
499 return QueryValue(szValue
, &dwVal
) &&
500 keyDst
.SetValue(szValueNew
, dwVal
);
503 // these types are unsupported because I am not sure about how
504 // exactly they should be copied and because they shouldn't
505 // occur among the application keys (supposedly created with
509 case Type_Expand_String
:
511 case Type_Dword_big_endian
:
513 case Type_Multi_String
:
514 case Type_Resource_list
:
515 case Type_Full_resource_descriptor
:
516 case Type_Resource_requirements_list
:
519 wxLogError(_("Can't copy values of unsupported type %d."),
520 GetValueType(szValue
));
525 bool wxRegKey::Rename(const wxChar
*szNewName
)
527 wxCHECK_MSG( !!m_strKey
, FALSE
, _T("registry hives can't be renamed") );
530 wxLogError(_("Registry key '%s' does not exist, cannot rename it."),
536 // do we stay in the same hive?
537 bool inSameHive
= !wxStrchr(szNewName
, REG_SEPARATOR
);
539 // construct the full new name of the key
543 // rename the key to the new name under the same parent
544 wxString strKey
= m_strKey
.BeforeLast(REG_SEPARATOR
);
546 // don't add '\\' in the start if strFullNewName is empty
547 strKey
+= REG_SEPARATOR
;
552 keyDst
.SetName(GetStdKeyFromHkey(m_hRootKey
), strKey
);
555 // this is the full name already
556 keyDst
.SetName(szNewName
);
559 bool ok
= keyDst
.Create(FALSE
/* fail if alredy exists */);
561 wxLogError(_("Registry key '%s' already exists."),
562 GetFullName(&keyDst
));
565 ok
= Copy(keyDst
) && DeleteSelf();
569 wxLogError(_("Failed to rename the registry key '%s' to '%s'."),
570 GetFullName(this), GetFullName(&keyDst
));
573 m_hRootKey
= keyDst
.m_hRootKey
;
574 m_strKey
= keyDst
.m_strKey
;
580 bool wxRegKey::Copy(const wxChar
*szNewName
)
582 // create the new key first
583 wxRegKey
keyDst(szNewName
);
584 bool ok
= keyDst
.Create(FALSE
/* fail if alredy exists */);
588 // we created the dest key but copying to it failed - delete it
590 (void)keyDst
.DeleteSelf();
597 bool wxRegKey::Copy(wxRegKey
& keyDst
)
601 // copy all sub keys to the new location
604 bool bCont
= GetFirstKey(strKey
, lIndex
);
605 while ( ok
&& bCont
) {
606 wxRegKey
key(*this, strKey
);
608 keyName
<< GetFullName(&keyDst
) << REG_SEPARATOR
<< strKey
;
609 ok
= key
.Copy((const wxChar
*) keyName
);
612 bCont
= GetNextKey(strKey
, lIndex
);
617 bCont
= GetFirstValue(strVal
, lIndex
);
618 while ( ok
&& bCont
) {
619 ok
= CopyValue(strVal
, keyDst
);
622 wxLogSysError(m_dwLastError
,
623 _("Failed to copy registry value '%s'"),
627 bCont
= GetNextValue(strVal
, lIndex
);
632 wxLogError(_("Failed to copy the contents of registry key '%s' to '%s'."), GetFullName(this), GetFullName(&keyDst
));
638 // ----------------------------------------------------------------------------
639 // delete keys/values
640 // ----------------------------------------------------------------------------
641 bool wxRegKey::DeleteSelf()
646 // it already doesn't exist - ok!
651 // prevent a buggy program from erasing one of the root registry keys or an
652 // immediate subkey (i.e. one which doesn't have '\\' inside) of any other
653 // key except HKCR (HKCR has some "deleteable" subkeys)
654 if ( m_strKey
.IsEmpty() ||
655 ((m_hRootKey
!= (WXHKEY
) aStdKeys
[HKCR
].hkey
) &&
656 (m_strKey
.Find(REG_SEPARATOR
) == wxNOT_FOUND
)) ) {
657 wxLogError(_("Registry key '%s' is needed for normal system operation,\ndeleting it will leave your system in unusable state:\noperation aborted."), GetFullName(this));
662 // we can't delete keys while enumerating because it confuses GetNextKey, so
663 // we first save the key names and then delete them all
664 wxArrayString astrSubkeys
;
668 bool bCont
= GetFirstKey(strKey
, lIndex
);
670 astrSubkeys
.Add(strKey
);
672 bCont
= GetNextKey(strKey
, lIndex
);
675 size_t nKeyCount
= astrSubkeys
.Count();
676 for ( size_t nKey
= 0; nKey
< nKeyCount
; nKey
++ ) {
677 wxRegKey
key(*this, astrSubkeys
[nKey
]);
678 if ( !key
.DeleteSelf() )
682 // now delete this key itself
685 m_dwLastError
= RegDeleteKey((HKEY
) m_hRootKey
, m_strKey
);
686 if ( m_dwLastError
!= ERROR_SUCCESS
) {
687 wxLogSysError(m_dwLastError
, _("Can't delete key '%s'"),
695 bool wxRegKey::DeleteKey(const wxChar
*szKey
)
700 wxRegKey
key(*this, szKey
);
701 return key
.DeleteSelf();
704 bool wxRegKey::DeleteValue(const wxChar
*szValue
)
709 #if defined(__WIN32__)
710 m_dwLastError
= RegDeleteValue((HKEY
) m_hKey
, WXSTRINGCAST szValue
);
712 // deleting a value which doesn't exist is not considered an error
713 if ( (m_dwLastError
!= ERROR_SUCCESS
) &&
714 (m_dwLastError
!= ERROR_FILE_NOT_FOUND
) ) {
715 wxLogSysError(m_dwLastError
, _("Can't delete value '%s' from key '%s'"),
716 szValue
, GetName().c_str());
720 // named registry values don't exist in Win16 world
721 wxASSERT( IsEmpty(szValue
) );
723 // just set the (default and unique) value of the key to ""
724 m_dwLastError
= RegSetValue((HKEY
) m_hKey
, NULL
, REG_SZ
, "", RESERVED
);
725 if ( m_dwLastError
!= ERROR_SUCCESS
) {
726 wxLogSysError(m_dwLastError
, _("Can't delete value of key '%s'"),
735 // ----------------------------------------------------------------------------
736 // access to values and subkeys
737 // ----------------------------------------------------------------------------
739 // return TRUE if value exists
740 bool wxRegKey::HasValue(const wxChar
*szValue
) const
742 // this function should be silent, so suppress possible messages from Open()
746 if ( !CONST_CAST
Open() )
749 LONG dwRet
= ::RegQueryValueEx((HKEY
) m_hKey
,
750 WXSTRINGCAST szValue
,
753 return dwRet
== ERROR_SUCCESS
;
755 // only unnamed value exists
756 return IsEmpty(szValue
);
760 // returns TRUE if this key has any values
761 bool wxRegKey::HasValues() const
763 // suppress possible messages from GetFirstValue()
766 // just call GetFirstValue with dummy parameters
769 return CONST_CAST
GetFirstValue(str
, l
);
772 // returns TRUE if this key has any subkeys
773 bool wxRegKey::HasSubkeys() const
775 // suppress possible messages from GetFirstKey()
778 // just call GetFirstKey with dummy parameters
781 return CONST_CAST
GetFirstKey(str
, l
);
784 // returns TRUE if given subkey exists
785 bool wxRegKey::HasSubKey(const wxChar
*szKey
) const
787 // this function should be silent, so suppress possible messages from Open()
790 if ( !CONST_CAST
Open() )
793 return KeyExists(m_hKey
, szKey
);
796 wxRegKey::ValueType
wxRegKey::GetValueType(const wxChar
*szValue
) const
799 if ( ! CONST_CAST
Open() )
803 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
804 &dwType
, NULL
, NULL
);
805 if ( m_dwLastError
!= ERROR_SUCCESS
) {
806 wxLogSysError(m_dwLastError
, _("Can't read value of key '%s'"),
811 return (ValueType
)dwType
;
813 return IsEmpty(szValue
) ? Type_String
: Type_None
;
818 bool wxRegKey::SetValue(const wxChar
*szValue
, long lValue
)
820 if ( CONST_CAST
Open() ) {
821 m_dwLastError
= RegSetValueEx((HKEY
) m_hKey
, szValue
, (DWORD
) RESERVED
, REG_DWORD
,
822 (RegString
)&lValue
, sizeof(lValue
));
823 if ( m_dwLastError
== ERROR_SUCCESS
)
827 wxLogSysError(m_dwLastError
, _("Can't set value of '%s'"),
828 GetFullName(this, szValue
));
832 bool wxRegKey::QueryValue(const wxChar
*szValue
, long *plValue
) const
834 if ( CONST_CAST
Open() ) {
835 DWORD dwType
, dwSize
= sizeof(DWORD
);
836 RegString pBuf
= (RegString
)plValue
;
837 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
838 &dwType
, pBuf
, &dwSize
);
839 if ( m_dwLastError
!= ERROR_SUCCESS
) {
840 wxLogSysError(m_dwLastError
, _("Can't read value of key '%s'"),
845 // check that we read the value of right type
846 wxASSERT_MSG( IsNumericValue(szValue
),
847 wxT("Type mismatch in wxRegKey::QueryValue().") );
858 bool wxRegKey::QueryValue(const wxChar
*szValue
,
862 if ( CONST_CAST
Open() ) {
864 // first get the type and size of the data
865 DWORD dwType
, dwSize
;
866 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
867 &dwType
, NULL
, &dwSize
);
868 if ( m_dwLastError
== ERROR_SUCCESS
) {
870 // must treat this case specially as GetWriteBuf() doesn't like
871 // being called with 0 size
875 RegString pBuf
= (RegString
)strValue
.GetWriteBuf(dwSize
);
876 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
,
877 WXSTRINGCAST szValue
,
882 strValue
.UngetWriteBuf();
884 // expand the var expansions in the string unless disabled
886 if ( (dwType
== REG_EXPAND_SZ
) && !raw
)
888 DWORD dwExpSize
= ::ExpandEnvironmentStrings(strValue
, NULL
, 0);
889 bool ok
= dwExpSize
!= 0;
892 wxString strExpValue
;
893 ok
= ::ExpandEnvironmentStrings
896 strExpValue
.GetWriteBuf(dwExpSize
),
899 strExpValue
.UngetWriteBuf();
900 strValue
= strExpValue
;
905 wxLogLastError(_T("ExpandEnvironmentStrings"));
912 if ( m_dwLastError
== ERROR_SUCCESS
) {
913 // check that it was the right type
914 wxASSERT_MSG( !IsNumericValue(szValue
),
915 wxT("Type mismatch in wxRegKey::QueryValue().") );
921 // named registry values don't exist in Win16
922 wxASSERT( IsEmpty(szValue
) );
924 m_dwLastError
= RegQueryValue((HKEY
) m_hKey
, 0, strValue
.GetWriteBuf(256), &l
);
925 strValue
.UngetWriteBuf();
926 if ( m_dwLastError
== ERROR_SUCCESS
)
931 wxLogSysError(m_dwLastError
, _("Can't read value of '%s'"),
932 GetFullName(this, szValue
));
936 bool wxRegKey::SetValue(const wxChar
*szValue
, const wxString
& strValue
)
938 if ( CONST_CAST
Open() ) {
939 #if defined( __WIN32__)
940 m_dwLastError
= RegSetValueEx((HKEY
) m_hKey
, szValue
, (DWORD
) RESERVED
, REG_SZ
,
941 (RegString
)strValue
.c_str(),
942 (strValue
.Len() + 1)*sizeof(wxChar
));
943 if ( m_dwLastError
== ERROR_SUCCESS
)
946 // named registry values don't exist in Win16
947 wxASSERT( IsEmpty(szValue
) );
949 m_dwLastError
= RegSetValue((HKEY
) m_hKey
, NULL
, REG_SZ
, strValue
, NULL
);
950 if ( m_dwLastError
== ERROR_SUCCESS
)
955 wxLogSysError(m_dwLastError
, _("Can't set value of '%s'"),
956 GetFullName(this, szValue
));
960 wxString
wxRegKey::QueryDefaultValue() const
963 QueryValue(NULL
, str
);
967 // ----------------------------------------------------------------------------
969 // NB: all these functions require an index variable which allows to have
970 // several concurrently running indexations on the same key
971 // ----------------------------------------------------------------------------
973 bool wxRegKey::GetFirstValue(wxString
& strValueName
, long& lIndex
)
979 return GetNextValue(strValueName
, lIndex
);
982 bool wxRegKey::GetNextValue(wxString
& strValueName
, long& lIndex
) const
984 wxASSERT( IsOpened() );
986 // are we already at the end of enumeration?
990 #if defined( __WIN32__)
991 wxChar szValueName
[1024]; // @@ use RegQueryInfoKey...
992 DWORD dwValueLen
= WXSIZEOF(szValueName
);
994 m_dwLastError
= RegEnumValue((HKEY
) m_hKey
, lIndex
++,
995 szValueName
, &dwValueLen
,
998 NULL
, // [out] buffer for value
999 NULL
); // [i/o] it's length
1001 if ( m_dwLastError
!= ERROR_SUCCESS
) {
1002 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
1003 m_dwLastError
= ERROR_SUCCESS
;
1007 wxLogSysError(m_dwLastError
, _("Can't enumerate values of key '%s'"),
1014 strValueName
= szValueName
;
1016 // only one unnamed value
1017 wxASSERT( lIndex
== 0 );
1020 strValueName
.Empty();
1026 bool wxRegKey::GetFirstKey(wxString
& strKeyName
, long& lIndex
)
1032 return GetNextKey(strKeyName
, lIndex
);
1035 bool wxRegKey::GetNextKey(wxString
& strKeyName
, long& lIndex
) const
1037 wxASSERT( IsOpened() );
1039 // are we already at the end of enumeration?
1043 wxChar szKeyName
[_MAX_PATH
+ 1];
1046 DWORD sizeName
= WXSIZEOF(szKeyName
);
1047 m_dwLastError
= RegEnumKeyEx((HKEY
) m_hKey
, lIndex
++, szKeyName
, & sizeName
,
1048 0, NULL
, NULL
, NULL
);
1050 m_dwLastError
= RegEnumKey((HKEY
) m_hKey
, lIndex
++, szKeyName
, WXSIZEOF(szKeyName
));
1053 if ( m_dwLastError
!= ERROR_SUCCESS
) {
1054 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
1055 m_dwLastError
= ERROR_SUCCESS
;
1059 wxLogSysError(m_dwLastError
, _("Can't enumerate subkeys of key '%s'"),
1066 strKeyName
= szKeyName
;
1070 // returns TRUE if the value contains a number (else it's some string)
1071 bool wxRegKey::IsNumericValue(const wxChar
*szValue
) const
1073 ValueType type
= GetValueType(szValue
);
1076 /* case Type_Dword_little_endian: == Type_Dword */
1077 case Type_Dword_big_endian
:
1085 // ============================================================================
1086 // implementation of global private functions
1087 // ============================================================================
1089 bool KeyExists(WXHKEY hRootKey
, const wxChar
*szKey
)
1091 // don't close this key itself for the case of empty szKey!
1092 if ( wxIsEmpty(szKey
) )
1103 ) == ERROR_SUCCESS
)
1105 ::RegCloseKey(hkeyDummy
);
1113 const wxChar
*GetFullName(const wxRegKey
*pKey
, const wxChar
*szValue
)
1115 static wxString s_str
;
1116 s_str
= pKey
->GetName();
1117 if ( !wxIsEmpty(szValue
) )
1118 s_str
<< wxT("\\") << szValue
;
1120 return s_str
.c_str();
1123 void RemoveTrailingSeparator(wxString
& str
)
1125 if ( !str
.IsEmpty() && str
.Last() == REG_SEPARATOR
)
1126 str
.Truncate(str
.Len() - 1);