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 <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 // ----------------------------------------------------------------------------
95 // const_cast<> is not yet supported by all compilers
96 #define CONST_CAST ((wxRegKey *)this)->
98 // and neither is mutable which m_dwLastError should be
99 #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 // ----------------------------------------------------------------------------
189 m_hRootKey
= (WXHKEY
) aStdKeys
[HKCR
].hkey
;
194 wxRegKey::wxRegKey(const wxString
& strKey
) : m_strKey(strKey
)
196 m_hRootKey
= (WXHKEY
) aStdKeys
[ExtractKeyName(m_strKey
)].hkey
;
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
;
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
;
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
264 // NB: this method is called by wxRegConfig::SetPath() which is a performance
265 // critical function and so it preallocates space for our m_strKey to
266 // gain some speed - this is why we only use += here and not = which
267 // would just free the prealloc'd buffer and would have to realloc it the
270 m_strKey
+= keyParent
.m_strKey
;
271 if ( !strKey
.IsEmpty() && strKey
[0] != REG_SEPARATOR
)
272 m_strKey
+= REG_SEPARATOR
;
275 RemoveTrailingSeparator(m_strKey
);
277 m_hRootKey
= keyParent
.m_hRootKey
;
280 // hKey should be opened and will be closed in wxRegKey dtor
281 void wxRegKey::SetHkey(WXHKEY hKey
)
288 // ----------------------------------------------------------------------------
289 // info about the key
290 // ----------------------------------------------------------------------------
292 // returns TRUE if the key exists
293 bool wxRegKey::Exists() const
295 // opened key has to exist, try to open it if not done yet
296 return IsOpened() ? TRUE
: KeyExists(m_hRootKey
, m_strKey
);
299 // returns the full name of the key (prefix is abbreviated if bShortPrefix)
300 wxString
wxRegKey::GetName(bool bShortPrefix
) const
302 StdKey key
= GetStdKeyFromHkey((WXHKEY
) m_hRootKey
);
303 wxString str
= bShortPrefix
? aStdKeys
[key
].szShortName
304 : aStdKeys
[key
].szName
;
305 if ( !m_strKey
.IsEmpty() )
306 str
<< _T("\\") << m_strKey
;
311 bool wxRegKey::GetKeyInfo(size_t *pnSubKeys
,
314 size_t *pnMaxValueLen
) const
316 #if defined(__WIN32__)
318 // old gcc headers incorrectly prototype RegQueryInfoKey()
319 #if defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__)
320 #define REG_PARAM (size_t *)
322 #define REG_PARAM (LPDWORD)
325 // it might be unexpected to some that this function doesn't open the key
326 wxASSERT_MSG( IsOpened(), _T("key should be opened in GetKeyInfo") );
328 m_dwLastError
= ::RegQueryInfoKey
332 NULL
, // (ptr to) size of class name buffer
335 pnSubKeys
, // [out] number of subkeys
337 pnMaxKeyLen
, // [out] max length of a subkey name
338 NULL
, // longest subkey class name
340 pnValues
, // [out] number of values
342 pnMaxValueLen
, // [out] max length of a value name
343 NULL
, // longest value data
344 NULL
, // security descriptor
345 NULL
// time of last modification
350 if ( m_dwLastError
!= ERROR_SUCCESS
) {
351 wxLogSysError(m_dwLastError
, _("Can't get info about registry key '%s'"),
358 wxFAIL_MSG("GetKeyInfo() not implemented");
364 // ----------------------------------------------------------------------------
366 // ----------------------------------------------------------------------------
368 // opens key (it's not an error to call Open() on an already opened key)
369 bool wxRegKey::Open()
375 m_dwLastError
= RegOpenKey((HKEY
) m_hRootKey
, m_strKey
, &tmpKey
);
376 if ( m_dwLastError
!= ERROR_SUCCESS
) {
377 wxLogSysError(m_dwLastError
, _("Can't open registry key '%s'"),
383 m_hKey
= (WXHKEY
) tmpKey
;
388 // creates key, failing if it exists and !bOkIfExists
389 bool wxRegKey::Create(bool bOkIfExists
)
391 // check for existence only if asked (i.e. order is important!)
392 if ( !bOkIfExists
&& Exists() ) {
400 m_dwLastError
= RegCreateKey((HKEY
) m_hRootKey
, m_strKey
, &tmpKey
);
401 if ( m_dwLastError
!= ERROR_SUCCESS
) {
402 wxLogSysError(m_dwLastError
, _("Can't create registry key '%s'"),
408 m_hKey
= (WXHKEY
) tmpKey
;
413 // close the key, it's not an error to call it when not opened
414 bool wxRegKey::Close()
417 m_dwLastError
= RegCloseKey((HKEY
) m_hKey
);
420 if ( m_dwLastError
!= ERROR_SUCCESS
) {
421 wxLogSysError(m_dwLastError
, _("Can't close registry key '%s'"),
431 bool wxRegKey::RenameValue(const wxChar
*szValueOld
, const wxChar
*szValueNew
)
434 if ( HasValue(szValueNew
) ) {
435 wxLogError(_("Registry value '%s' already exists."), szValueNew
);
441 !CopyValue(szValueOld
, *this, szValueNew
) ||
442 !DeleteValue(szValueOld
) ) {
443 wxLogError(_("Failed to rename registry value '%s' to '%s'."),
444 szValueOld
, szValueNew
);
452 bool wxRegKey::CopyValue(const wxChar
*szValue
,
454 const wxChar
*szValueNew
)
457 // by default, use the same name
458 szValueNew
= szValue
;
461 switch ( GetValueType(szValue
) ) {
465 return QueryValue(szValue
, strVal
) &&
466 keyDst
.SetValue(szValueNew
, strVal
);
470 /* case Type_Dword_little_endian: == Type_Dword */
473 return QueryValue(szValue
, &dwVal
) &&
474 keyDst
.SetValue(szValueNew
, dwVal
);
477 // these types are unsupported because I am not sure about how
478 // exactly they should be copied and because they shouldn't
479 // occur among the application keys (supposedly created with
483 case Type_Expand_String
:
485 case Type_Dword_big_endian
:
487 case Type_Multi_String
:
488 case Type_Resource_list
:
489 case Type_Full_resource_descriptor
:
490 case Type_Resource_requirements_list
:
493 wxLogError(_("Can't copy values of unsupported type %d."),
494 GetValueType(szValue
));
499 bool wxRegKey::Rename(const wxChar
*szNewName
)
501 wxCHECK_MSG( !!m_strKey
, FALSE
, _T("registry hives can't be renamed") );
504 wxLogError(_("Registry key '%s' does not exist, cannot rename it."),
510 // do we stay in the same hive?
511 bool inSameHive
= !wxStrchr(szNewName
, REG_SEPARATOR
);
513 // construct the full new name of the key
517 // rename the key to the new name under the same parent
518 wxString strKey
= m_strKey
.BeforeLast(REG_SEPARATOR
);
520 // don't add '\\' in the start if strFullNewName is empty
521 strKey
+= REG_SEPARATOR
;
526 keyDst
.SetName(GetStdKeyFromHkey(m_hRootKey
), strKey
);
529 // this is the full name already
530 keyDst
.SetName(szNewName
);
533 bool ok
= keyDst
.Create(FALSE
/* fail if alredy exists */);
535 wxLogError(_("Registry key '%s' already exists."),
536 GetFullName(&keyDst
));
539 ok
= Copy(keyDst
) && DeleteSelf();
543 wxLogError(_("Failed to rename the registry key '%s' to '%s'."),
544 GetFullName(this), GetFullName(&keyDst
));
547 m_hRootKey
= keyDst
.m_hRootKey
;
548 m_strKey
= keyDst
.m_strKey
;
554 bool wxRegKey::Copy(const wxChar
*szNewName
)
556 // create the new key first
557 wxRegKey
keyDst(szNewName
);
558 bool ok
= keyDst
.Create(FALSE
/* fail if alredy exists */);
562 // we created the dest key but copying to it failed - delete it
564 (void)keyDst
.DeleteSelf();
571 bool wxRegKey::Copy(wxRegKey
& keyDst
)
575 // copy all sub keys to the new location
578 bool bCont
= GetFirstKey(strKey
, lIndex
);
579 while ( ok
&& bCont
) {
580 wxRegKey
key(*this, strKey
);
582 keyName
<< GetFullName(&keyDst
) << REG_SEPARATOR
<< strKey
;
583 ok
= key
.Copy((const wxChar
*) keyName
);
586 bCont
= GetNextKey(strKey
, lIndex
);
591 bCont
= GetFirstValue(strVal
, lIndex
);
592 while ( ok
&& bCont
) {
593 ok
= CopyValue(strVal
, keyDst
);
596 wxLogSysError(m_dwLastError
,
597 _("Failed to copy registry value '%s'"),
601 bCont
= GetNextValue(strVal
, lIndex
);
606 wxLogError(_("Failed to copy the contents of registry key '%s' to '%s'."), GetFullName(this), GetFullName(&keyDst
));
612 // ----------------------------------------------------------------------------
613 // delete keys/values
614 // ----------------------------------------------------------------------------
615 bool wxRegKey::DeleteSelf()
620 // it already doesn't exist - ok!
625 // prevent a buggy program from erasing one of the root registry keys or an
626 // immediate subkey (i.e. one which doesn't have '\\' inside) of any other
627 // key except HKCR (HKCR has some "deleteable" subkeys)
628 if ( m_strKey
.IsEmpty() ||
629 ((m_hRootKey
!= (WXHKEY
) aStdKeys
[HKCR
].hkey
) &&
630 (m_strKey
.Find(REG_SEPARATOR
) == wxNOT_FOUND
)) ) {
631 wxLogError(_("Registry key '%s' is needed for normal system operation,\ndeleting it will leave your system in unusable state:\noperation aborted."), GetFullName(this));
636 // we can't delete keys while enumerating because it confuses GetNextKey, so
637 // we first save the key names and then delete them all
638 wxArrayString astrSubkeys
;
642 bool bCont
= GetFirstKey(strKey
, lIndex
);
644 astrSubkeys
.Add(strKey
);
646 bCont
= GetNextKey(strKey
, lIndex
);
649 size_t nKeyCount
= astrSubkeys
.Count();
650 for ( size_t nKey
= 0; nKey
< nKeyCount
; nKey
++ ) {
651 wxRegKey
key(*this, astrSubkeys
[nKey
]);
652 if ( !key
.DeleteSelf() )
656 // now delete this key itself
659 m_dwLastError
= RegDeleteKey((HKEY
) m_hRootKey
, m_strKey
);
660 if ( m_dwLastError
!= ERROR_SUCCESS
) {
661 wxLogSysError(m_dwLastError
, _("Can't delete key '%s'"),
669 bool wxRegKey::DeleteKey(const wxChar
*szKey
)
674 wxRegKey
key(*this, szKey
);
675 return key
.DeleteSelf();
678 bool wxRegKey::DeleteValue(const wxChar
*szValue
)
683 #if defined(__WIN32__)
684 m_dwLastError
= RegDeleteValue((HKEY
) m_hKey
, WXSTRINGCAST szValue
);
686 // deleting a value which doesn't exist is not considered an error
687 if ( (m_dwLastError
!= ERROR_SUCCESS
) &&
688 (m_dwLastError
!= ERROR_FILE_NOT_FOUND
) ) {
689 wxLogSysError(m_dwLastError
, _("Can't delete value '%s' from key '%s'"),
690 szValue
, GetName().c_str());
694 // named registry values don't exist in Win16 world
695 wxASSERT( IsEmpty(szValue
) );
697 // just set the (default and unique) value of the key to ""
698 m_dwLastError
= RegSetValue((HKEY
) m_hKey
, NULL
, REG_SZ
, "", RESERVED
);
699 if ( m_dwLastError
!= ERROR_SUCCESS
) {
700 wxLogSysError(m_dwLastError
, _("Can't delete value of key '%s'"),
709 // ----------------------------------------------------------------------------
710 // access to values and subkeys
711 // ----------------------------------------------------------------------------
713 // return TRUE if value exists
714 bool wxRegKey::HasValue(const wxChar
*szValue
) const
716 // this function should be silent, so suppress possible messages from Open()
720 if ( !CONST_CAST
Open() )
723 LONG dwRet
= ::RegQueryValueEx((HKEY
) m_hKey
,
724 WXSTRINGCAST szValue
,
727 return dwRet
== ERROR_SUCCESS
;
729 // only unnamed value exists
730 return IsEmpty(szValue
);
734 // returns TRUE if this key has any values
735 bool wxRegKey::HasValues() const
737 // suppress possible messages from GetFirstValue()
740 // just call GetFirstValue with dummy parameters
743 return CONST_CAST
GetFirstValue(str
, l
);
746 // returns TRUE if this key has any subkeys
747 bool wxRegKey::HasSubkeys() const
749 // suppress possible messages from GetFirstKey()
752 // just call GetFirstKey with dummy parameters
755 return CONST_CAST
GetFirstKey(str
, l
);
758 // returns TRUE if given subkey exists
759 bool wxRegKey::HasSubKey(const wxChar
*szKey
) const
761 // this function should be silent, so suppress possible messages from Open()
764 if ( !CONST_CAST
Open() )
767 return KeyExists(m_hKey
, szKey
);
770 wxRegKey::ValueType
wxRegKey::GetValueType(const wxChar
*szValue
) const
773 if ( ! CONST_CAST
Open() )
777 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
778 &dwType
, NULL
, NULL
);
779 if ( m_dwLastError
!= ERROR_SUCCESS
) {
780 wxLogSysError(m_dwLastError
, _("Can't read value of key '%s'"),
785 return (ValueType
)dwType
;
787 return IsEmpty(szValue
) ? Type_String
: Type_None
;
792 bool wxRegKey::SetValue(const wxChar
*szValue
, long lValue
)
794 if ( CONST_CAST
Open() ) {
795 m_dwLastError
= RegSetValueEx((HKEY
) m_hKey
, szValue
, (DWORD
) RESERVED
, REG_DWORD
,
796 (RegString
)&lValue
, sizeof(lValue
));
797 if ( m_dwLastError
== ERROR_SUCCESS
)
801 wxLogSysError(m_dwLastError
, _("Can't set value of '%s'"),
802 GetFullName(this, szValue
));
806 bool wxRegKey::QueryValue(const wxChar
*szValue
, long *plValue
) const
808 if ( CONST_CAST
Open() ) {
809 DWORD dwType
, dwSize
= sizeof(DWORD
);
810 RegString pBuf
= (RegString
)plValue
;
811 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
812 &dwType
, pBuf
, &dwSize
);
813 if ( m_dwLastError
!= ERROR_SUCCESS
) {
814 wxLogSysError(m_dwLastError
, _("Can't read value of key '%s'"),
819 // check that we read the value of right type
820 wxASSERT_MSG( IsNumericValue(szValue
),
821 wxT("Type mismatch in wxRegKey::QueryValue().") );
832 bool wxRegKey::QueryValue(const wxChar
*szValue
,
836 if ( CONST_CAST
Open() ) {
838 // first get the type and size of the data
839 DWORD dwType
, dwSize
;
840 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
841 &dwType
, NULL
, &dwSize
);
842 if ( m_dwLastError
== ERROR_SUCCESS
) {
844 // must treat this case specially as GetWriteBuf() doesn't like
845 // being called with 0 size
849 RegString pBuf
= (RegString
)strValue
.GetWriteBuf(dwSize
);
850 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
,
851 WXSTRINGCAST szValue
,
856 strValue
.UngetWriteBuf();
858 // expand the var expansions in the string unless disabled
859 if ( (dwType
== REG_EXPAND_SZ
) && !raw
)
861 DWORD dwExpSize
= ::ExpandEnvironmentStrings(strValue
, NULL
, 0);
862 bool ok
= dwExpSize
!= 0;
865 wxString strExpValue
;
866 ok
= ::ExpandEnvironmentStrings
869 strExpValue
.GetWriteBuf(dwExpSize
),
872 strExpValue
.UngetWriteBuf();
873 strValue
= strExpValue
;
878 wxLogLastError(_T("ExpandEnvironmentStrings"));
883 if ( m_dwLastError
== ERROR_SUCCESS
) {
884 // check that it was the right type
885 wxASSERT_MSG( !IsNumericValue(szValue
),
886 wxT("Type mismatch in wxRegKey::QueryValue().") );
892 // named registry values don't exist in Win16
893 wxASSERT( IsEmpty(szValue
) );
895 m_dwLastError
= RegQueryValue((HKEY
) m_hKey
, 0, strValue
.GetWriteBuf(256), &l
);
896 strValue
.UngetWriteBuf();
897 if ( m_dwLastError
== ERROR_SUCCESS
)
902 wxLogSysError(m_dwLastError
, _("Can't read value of '%s'"),
903 GetFullName(this, szValue
));
907 bool wxRegKey::SetValue(const wxChar
*szValue
, const wxString
& strValue
)
909 if ( CONST_CAST
Open() ) {
910 #if defined( __WIN32__)
911 m_dwLastError
= RegSetValueEx((HKEY
) m_hKey
, szValue
, (DWORD
) RESERVED
, REG_SZ
,
912 (RegString
)strValue
.c_str(),
913 (strValue
.Len() + 1)*sizeof(wxChar
));
914 if ( m_dwLastError
== ERROR_SUCCESS
)
917 // named registry values don't exist in Win16
918 wxASSERT( IsEmpty(szValue
) );
920 m_dwLastError
= RegSetValue((HKEY
) m_hKey
, NULL
, REG_SZ
, strValue
, NULL
);
921 if ( m_dwLastError
== ERROR_SUCCESS
)
926 wxLogSysError(m_dwLastError
, _("Can't set value of '%s'"),
927 GetFullName(this, szValue
));
931 wxString
wxRegKey::QueryDefaultValue() const
934 QueryValue(NULL
, str
);
938 // ----------------------------------------------------------------------------
940 // NB: all these functions require an index variable which allows to have
941 // several concurrently running indexations on the same key
942 // ----------------------------------------------------------------------------
944 bool wxRegKey::GetFirstValue(wxString
& strValueName
, long& lIndex
)
950 return GetNextValue(strValueName
, lIndex
);
953 bool wxRegKey::GetNextValue(wxString
& strValueName
, long& lIndex
) const
955 wxASSERT( IsOpened() );
957 // are we already at the end of enumeration?
961 #if defined( __WIN32__)
962 wxChar szValueName
[1024]; // @@ use RegQueryInfoKey...
963 DWORD dwValueLen
= WXSIZEOF(szValueName
);
965 m_dwLastError
= RegEnumValue((HKEY
) m_hKey
, lIndex
++,
966 szValueName
, &dwValueLen
,
969 NULL
, // [out] buffer for value
970 NULL
); // [i/o] it's length
972 if ( m_dwLastError
!= ERROR_SUCCESS
) {
973 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
974 m_dwLastError
= ERROR_SUCCESS
;
978 wxLogSysError(m_dwLastError
, _("Can't enumerate values of key '%s'"),
985 strValueName
= szValueName
;
987 // only one unnamed value
988 wxASSERT( lIndex
== 0 );
991 strValueName
.Empty();
997 bool wxRegKey::GetFirstKey(wxString
& strKeyName
, long& lIndex
)
1003 return GetNextKey(strKeyName
, lIndex
);
1006 bool wxRegKey::GetNextKey(wxString
& strKeyName
, long& lIndex
) const
1008 wxASSERT( IsOpened() );
1010 // are we already at the end of enumeration?
1014 wxChar szKeyName
[_MAX_PATH
+ 1];
1015 m_dwLastError
= RegEnumKey((HKEY
) m_hKey
, lIndex
++, szKeyName
, WXSIZEOF(szKeyName
));
1017 if ( m_dwLastError
!= ERROR_SUCCESS
) {
1018 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
1019 m_dwLastError
= ERROR_SUCCESS
;
1023 wxLogSysError(m_dwLastError
, _("Can't enumerate subkeys of key '%s'"),
1030 strKeyName
= szKeyName
;
1034 // returns TRUE if the value contains a number (else it's some string)
1035 bool wxRegKey::IsNumericValue(const wxChar
*szValue
) const
1037 ValueType type
= GetValueType(szValue
);
1040 /* case Type_Dword_little_endian: == Type_Dword */
1041 case Type_Dword_big_endian
:
1049 // ============================================================================
1050 // implementation of global private functions
1051 // ============================================================================
1053 bool KeyExists(WXHKEY hRootKey
, const wxChar
*szKey
)
1055 // don't close this key itself for the case of empty szKey!
1056 if ( wxIsEmpty(szKey
) )
1060 if ( RegOpenKey( (HKEY
) hRootKey
, szKey
, &hkeyDummy
) == ERROR_SUCCESS
) {
1061 RegCloseKey(hkeyDummy
);
1068 const wxChar
*GetFullName(const wxRegKey
*pKey
, const wxChar
*szValue
)
1070 static wxString s_str
;
1071 s_str
= pKey
->GetName();
1072 if ( !wxIsEmpty(szValue
) )
1073 s_str
<< wxT("\\") << szValue
;
1075 return s_str
.c_str();
1078 void RemoveTrailingSeparator(wxString
& str
)
1080 if ( !str
.IsEmpty() && str
.Last() == REG_SEPARATOR
)
1081 str
.Truncate(str
.Len() - 1);