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 ///////////////////////////////////////////////////////////////////////////////
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma implementation "registry.h"
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 // other wxWidgets headers
27 #include "wx/string.h"
34 #define WIN32_LEAN_AND_MEAN
37 #include "wx/msw/wrapwin.h"
40 #include "wx/msw/private.h"
46 #include <stdlib.h> // for _MAX_PATH
53 #define HKEY_DEFINED // already defined in windows.h
54 #include "wx/msw/registry.h"
56 // some registry functions don't like signed chars
57 typedef unsigned char *RegString
;
58 typedef BYTE
* RegBinary
;
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 // the standard key names, short names and handles all bundled together for
70 const wxChar
*szShortName
;
74 { HKEY_CLASSES_ROOT
, wxT("HKEY_CLASSES_ROOT"), wxT("HKCR") },
76 { HKEY_CURRENT_USER
, wxT("HKEY_CURRENT_USER"), wxT("HKCU") },
77 { HKEY_LOCAL_MACHINE
, wxT("HKEY_LOCAL_MACHINE"), wxT("HKLM") },
78 { HKEY_USERS
, wxT("HKEY_USERS"), wxT("HKU") }, // short name?
80 { HKEY_PERFORMANCE_DATA
, wxT("HKEY_PERFORMANCE_DATA"), wxT("HKPD") },
82 #if WINVER >= 0x0400 && !defined(__WXWINCE__)
83 { HKEY_CURRENT_CONFIG
, wxT("HKEY_CURRENT_CONFIG"), wxT("HKCC") },
84 #if !defined(__GNUWIN32__) && !defined(__WXWINCE__)
85 { HKEY_DYN_DATA
, wxT("HKEY_DYN_DATA"), wxT("HKDD") }, // short name?
87 #endif //WINVER >= 4.0
91 // the registry name separator (perhaps one day MS will change it to '/' ;-)
92 #define REG_SEPARATOR wxT('\\')
94 // useful for Windows programmers: makes somewhat more clear all these zeroes
95 // being passed to Windows APIs
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 // const_cast<> is not yet supported by all compilers
103 #define CONST_CAST ((wxRegKey *)this)->
105 // and neither is mutable which m_dwLastError should be
106 #define m_dwLastError CONST_CAST m_dwLastError
108 // ----------------------------------------------------------------------------
109 // non member functions
110 // ----------------------------------------------------------------------------
112 // removes the trailing backslash from the string if it has one
113 static inline void RemoveTrailingSeparator(wxString
& str
);
115 // returns TRUE if given registry key exists
116 static bool KeyExists(WXHKEY hRootKey
, const wxChar
*szKey
);
118 // combines value and key name (uses static buffer!)
119 static const wxChar
*GetFullName(const wxRegKey
*pKey
,
120 const wxChar
*szValue
= NULL
);
122 // ============================================================================
123 // implementation of wxRegKey class
124 // ============================================================================
126 // ----------------------------------------------------------------------------
127 // static functions and variables
128 // ----------------------------------------------------------------------------
130 const size_t wxRegKey::nStdKeys
= WXSIZEOF(aStdKeys
);
132 // @@ should take a `StdKey key', but as it's often going to be used in loops
133 // it would require casts in user code.
134 const wxChar
*wxRegKey::GetStdKeyName(size_t key
)
136 // return empty string if key is invalid
137 wxCHECK_MSG( key
< nStdKeys
, wxEmptyString
, wxT("invalid key in wxRegKey::GetStdKeyName") );
139 return aStdKeys
[key
].szName
;
142 const wxChar
*wxRegKey::GetStdKeyShortName(size_t key
)
144 // return empty string if key is invalid
145 wxCHECK( key
< nStdKeys
, wxEmptyString
);
147 return aStdKeys
[key
].szShortName
;
150 wxRegKey::StdKey
wxRegKey::ExtractKeyName(wxString
& strKey
)
152 wxString strRoot
= strKey
.BeforeFirst(REG_SEPARATOR
);
156 for ( ui
= 0; ui
< nStdKeys
; ui
++ ) {
157 if ( strRoot
.CmpNoCase(aStdKeys
[ui
].szName
) == 0 ||
158 strRoot
.CmpNoCase(aStdKeys
[ui
].szShortName
) == 0 ) {
159 hRootKey
= aStdKeys
[ui
].hkey
;
164 if ( ui
== nStdKeys
) {
165 wxFAIL_MSG(wxT("invalid key prefix in wxRegKey::ExtractKeyName."));
167 hRootKey
= HKEY_CLASSES_ROOT
;
170 strKey
= strKey
.After(REG_SEPARATOR
);
171 if ( !strKey
.IsEmpty() && strKey
.Last() == REG_SEPARATOR
)
172 strKey
.Truncate(strKey
.Len() - 1);
175 return (wxRegKey::StdKey
)(int)hRootKey
;
178 wxRegKey::StdKey
wxRegKey::GetStdKeyFromHkey(WXHKEY hkey
)
180 for ( size_t ui
= 0; ui
< nStdKeys
; ui
++ ) {
181 if ( (int) aStdKeys
[ui
].hkey
== (int) hkey
)
185 wxFAIL_MSG(wxT("non root hkey passed to wxRegKey::GetStdKeyFromHkey."));
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
196 m_hRootKey
= (WXHKEY
) aStdKeys
[HKCR
].hkey
;
201 wxRegKey::wxRegKey(const wxString
& strKey
) : m_strKey(strKey
)
203 m_hRootKey
= (WXHKEY
) aStdKeys
[ExtractKeyName(m_strKey
)].hkey
;
208 // parent is a predefined (and preopened) key
209 wxRegKey::wxRegKey(StdKey keyParent
, const wxString
& strKey
) : m_strKey(strKey
)
211 RemoveTrailingSeparator(m_strKey
);
212 m_hRootKey
= (WXHKEY
) aStdKeys
[keyParent
].hkey
;
217 // parent is a normal regkey
218 wxRegKey::wxRegKey(const wxRegKey
& keyParent
, const wxString
& strKey
)
219 : m_strKey(keyParent
.m_strKey
)
221 // combine our name with parent's to get the full name
222 if ( !m_strKey
.IsEmpty() &&
223 (strKey
.IsEmpty() || strKey
[0] != REG_SEPARATOR
) ) {
224 m_strKey
+= REG_SEPARATOR
;
228 RemoveTrailingSeparator(m_strKey
);
230 m_hRootKey
= keyParent
.m_hRootKey
;
235 // dtor closes the key releasing system resource
236 wxRegKey::~wxRegKey()
241 // ----------------------------------------------------------------------------
242 // change the key name/hkey
243 // ----------------------------------------------------------------------------
245 // set the full key name
246 void wxRegKey::SetName(const wxString
& strKey
)
251 m_hRootKey
= (WXHKEY
) aStdKeys
[ExtractKeyName(m_strKey
)].hkey
;
254 // the name is relative to the parent key
255 void wxRegKey::SetName(StdKey keyParent
, const wxString
& strKey
)
260 RemoveTrailingSeparator(m_strKey
);
261 m_hRootKey
= (WXHKEY
) aStdKeys
[keyParent
].hkey
;
264 // the name is relative to the parent key
265 void wxRegKey::SetName(const wxRegKey
& keyParent
, const wxString
& strKey
)
269 // combine our name with parent's to get the full name
271 // NB: this method is called by wxRegConfig::SetPath() which is a performance
272 // critical function and so it preallocates space for our m_strKey to
273 // gain some speed - this is why we only use += here and not = which
274 // would just free the prealloc'd buffer and would have to realloc it the
277 m_strKey
+= keyParent
.m_strKey
;
278 if ( !strKey
.IsEmpty() && strKey
[0] != REG_SEPARATOR
)
279 m_strKey
+= REG_SEPARATOR
;
282 RemoveTrailingSeparator(m_strKey
);
284 m_hRootKey
= keyParent
.m_hRootKey
;
287 // hKey should be opened and will be closed in wxRegKey dtor
288 void wxRegKey::SetHkey(WXHKEY hKey
)
295 // ----------------------------------------------------------------------------
296 // info about the key
297 // ----------------------------------------------------------------------------
299 // returns TRUE if the key exists
300 bool wxRegKey::Exists() const
302 // opened key has to exist, try to open it if not done yet
303 return IsOpened() ? TRUE
: KeyExists(m_hRootKey
, m_strKey
);
306 // returns the full name of the key (prefix is abbreviated if bShortPrefix)
307 wxString
wxRegKey::GetName(bool bShortPrefix
) const
309 StdKey key
= GetStdKeyFromHkey((WXHKEY
) m_hRootKey
);
310 wxString str
= bShortPrefix
? aStdKeys
[key
].szShortName
311 : aStdKeys
[key
].szName
;
312 if ( !m_strKey
.IsEmpty() )
313 str
<< _T("\\") << m_strKey
;
318 bool wxRegKey::GetKeyInfo(size_t *pnSubKeys
,
321 size_t *pnMaxValueLen
) const
323 // old gcc headers incorrectly prototype RegQueryInfoKey()
324 #if defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__)
325 #define REG_PARAM (size_t *)
327 #define REG_PARAM (LPDWORD)
330 // it might be unexpected to some that this function doesn't open the key
331 wxASSERT_MSG( IsOpened(), _T("key should be opened in GetKeyInfo") );
333 m_dwLastError
= ::RegQueryInfoKey
337 NULL
, // (ptr to) size of class name buffer
340 pnSubKeys
, // [out] number of subkeys
342 pnMaxKeyLen
, // [out] max length of a subkey name
343 NULL
, // longest subkey class name
345 pnValues
, // [out] number of values
347 pnMaxValueLen
, // [out] max length of a value name
348 NULL
, // longest value data
349 NULL
, // security descriptor
350 NULL
// time of last modification
355 if ( m_dwLastError
!= ERROR_SUCCESS
) {
356 wxLogSysError(m_dwLastError
, _("Can't get info about registry key '%s'"),
364 // ----------------------------------------------------------------------------
366 // ----------------------------------------------------------------------------
368 // opens key (it's not an error to call Open() on an already opened key)
369 bool wxRegKey::Open(AccessMode mode
)
375 m_dwLastError
= ::RegOpenKeyEx
380 mode
== Read
? KEY_READ
: KEY_ALL_ACCESS
,
384 if ( m_dwLastError
!= ERROR_SUCCESS
)
386 wxLogSysError(m_dwLastError
, _("Can't open registry key '%s'"),
391 m_hKey
= (WXHKEY
) tmpKey
;
395 // creates key, failing if it exists and !bOkIfExists
396 bool wxRegKey::Create(bool bOkIfExists
)
398 // check for existence only if asked (i.e. order is important!)
399 if ( !bOkIfExists
&& Exists() )
408 m_dwLastError
= RegCreateKeyEx((HKEY
) m_hRootKey
, m_strKey
,
410 NULL
, // class string
417 m_dwLastError
= RegCreateKey((HKEY
) m_hRootKey
, m_strKey
, &tmpKey
);
419 if ( m_dwLastError
!= ERROR_SUCCESS
) {
420 wxLogSysError(m_dwLastError
, _("Can't create registry key '%s'"),
426 m_hKey
= (WXHKEY
) tmpKey
;
431 // close the key, it's not an error to call it when not opened
432 bool wxRegKey::Close()
435 m_dwLastError
= RegCloseKey((HKEY
) m_hKey
);
438 if ( m_dwLastError
!= ERROR_SUCCESS
) {
439 wxLogSysError(m_dwLastError
, _("Can't close registry key '%s'"),
449 bool wxRegKey::RenameValue(const wxChar
*szValueOld
, const wxChar
*szValueNew
)
452 if ( HasValue(szValueNew
) ) {
453 wxLogError(_("Registry value '%s' already exists."), szValueNew
);
459 !CopyValue(szValueOld
, *this, szValueNew
) ||
460 !DeleteValue(szValueOld
) ) {
461 wxLogError(_("Failed to rename registry value '%s' to '%s'."),
462 szValueOld
, szValueNew
);
470 bool wxRegKey::CopyValue(const wxChar
*szValue
,
472 const wxChar
*szValueNew
)
475 // by default, use the same name
476 szValueNew
= szValue
;
479 switch ( GetValueType(szValue
) ) {
483 return QueryValue(szValue
, strVal
) &&
484 keyDst
.SetValue(szValueNew
, strVal
);
488 /* case Type_Dword_little_endian: == Type_Dword */
491 return QueryValue(szValue
, &dwVal
) &&
492 keyDst
.SetValue(szValueNew
, dwVal
);
499 return QueryValue(szValue
,buf
) &&
500 keyDst
.SetValue(szValueNew
,buf
);
502 // these types are unsupported because I am not sure about how
503 // exactly they should be copied and because they shouldn't
504 // occur among the application keys (supposedly created with
507 case Type_Expand_String
:
508 case Type_Dword_big_endian
:
510 case Type_Multi_String
:
511 case Type_Resource_list
:
512 case Type_Full_resource_descriptor
:
513 case Type_Resource_requirements_list
:
516 wxLogError(_("Can't copy values of unsupported type %d."),
517 GetValueType(szValue
));
522 bool wxRegKey::Rename(const wxChar
*szNewName
)
524 wxCHECK_MSG( !!m_strKey
, FALSE
, _T("registry hives can't be renamed") );
527 wxLogError(_("Registry key '%s' does not exist, cannot rename it."),
533 // do we stay in the same hive?
534 bool inSameHive
= !wxStrchr(szNewName
, REG_SEPARATOR
);
536 // construct the full new name of the key
540 // rename the key to the new name under the same parent
541 wxString strKey
= m_strKey
.BeforeLast(REG_SEPARATOR
);
543 // don't add '\\' in the start if strFullNewName is empty
544 strKey
+= REG_SEPARATOR
;
549 keyDst
.SetName(GetStdKeyFromHkey(m_hRootKey
), strKey
);
552 // this is the full name already
553 keyDst
.SetName(szNewName
);
556 bool ok
= keyDst
.Create(FALSE
/* fail if alredy exists */);
558 wxLogError(_("Registry key '%s' already exists."),
559 GetFullName(&keyDst
));
562 ok
= Copy(keyDst
) && DeleteSelf();
566 wxLogError(_("Failed to rename the registry key '%s' to '%s'."),
567 GetFullName(this), GetFullName(&keyDst
));
570 m_hRootKey
= keyDst
.m_hRootKey
;
571 m_strKey
= keyDst
.m_strKey
;
577 bool wxRegKey::Copy(const wxChar
*szNewName
)
579 // create the new key first
580 wxRegKey
keyDst(szNewName
);
581 bool ok
= keyDst
.Create(FALSE
/* fail if alredy exists */);
585 // we created the dest key but copying to it failed - delete it
587 (void)keyDst
.DeleteSelf();
594 bool wxRegKey::Copy(wxRegKey
& keyDst
)
598 // copy all sub keys to the new location
601 bool bCont
= GetFirstKey(strKey
, lIndex
);
602 while ( ok
&& bCont
) {
603 wxRegKey
key(*this, strKey
);
605 keyName
<< GetFullName(&keyDst
) << REG_SEPARATOR
<< strKey
;
606 ok
= key
.Copy((const wxChar
*) keyName
);
609 bCont
= GetNextKey(strKey
, lIndex
);
611 wxLogError(_("Failed to copy the registry subkey '%s' to '%s'."), GetFullName(&key
), keyName
.mb_str());
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 // deleting a key 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 key '%s'"),
697 bool wxRegKey::DeleteKey(const wxChar
*szKey
)
702 wxRegKey
key(*this, szKey
);
703 return key
.DeleteSelf();
706 bool wxRegKey::DeleteValue(const wxChar
*szValue
)
711 m_dwLastError
= RegDeleteValue((HKEY
) m_hKey
, WXSTRINGCAST szValue
);
713 // deleting a value which doesn't exist is not considered an error
714 if ( (m_dwLastError
!= ERROR_SUCCESS
) &&
715 (m_dwLastError
!= ERROR_FILE_NOT_FOUND
) ) {
716 wxLogSysError(m_dwLastError
, _("Can't delete value '%s' from key '%s'"),
717 szValue
, GetName().c_str());
724 // ----------------------------------------------------------------------------
725 // access to values and subkeys
726 // ----------------------------------------------------------------------------
728 // return TRUE if value exists
729 bool wxRegKey::HasValue(const wxChar
*szValue
) const
731 // this function should be silent, so suppress possible messages from Open()
734 if ( !CONST_CAST
Open(Read
) )
737 LONG dwRet
= ::RegQueryValueEx((HKEY
) m_hKey
,
738 WXSTRINGCAST szValue
,
741 return dwRet
== ERROR_SUCCESS
;
744 // returns TRUE if this key has any values
745 bool wxRegKey::HasValues() const
747 // suppress possible messages from GetFirstValue()
750 // just call GetFirstValue with dummy parameters
753 return CONST_CAST
GetFirstValue(str
, l
);
756 // returns TRUE if this key has any subkeys
757 bool wxRegKey::HasSubkeys() const
759 // suppress possible messages from GetFirstKey()
762 // just call GetFirstKey with dummy parameters
765 return CONST_CAST
GetFirstKey(str
, l
);
768 // returns TRUE if given subkey exists
769 bool wxRegKey::HasSubKey(const wxChar
*szKey
) const
771 // this function should be silent, so suppress possible messages from Open()
774 if ( !CONST_CAST
Open(Read
) )
777 return KeyExists(m_hKey
, szKey
);
780 wxRegKey::ValueType
wxRegKey::GetValueType(const wxChar
*szValue
) const
782 if ( ! CONST_CAST
Open(Read
) )
786 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
787 &dwType
, NULL
, NULL
);
788 if ( m_dwLastError
!= ERROR_SUCCESS
) {
789 wxLogSysError(m_dwLastError
, _("Can't read value of key '%s'"),
794 return (ValueType
)dwType
;
798 bool wxRegKey::SetValue(const wxChar
*szValue
, long lValue
)
800 if ( CONST_CAST
Open() ) {
801 m_dwLastError
= RegSetValueEx((HKEY
) m_hKey
, szValue
, (DWORD
) RESERVED
, REG_DWORD
,
802 (RegString
)&lValue
, sizeof(lValue
));
803 if ( m_dwLastError
== ERROR_SUCCESS
)
807 wxLogSysError(m_dwLastError
, _("Can't set value of '%s'"),
808 GetFullName(this, szValue
));
812 bool wxRegKey::QueryValue(const wxChar
*szValue
, long *plValue
) const
814 if ( CONST_CAST
Open(Read
) ) {
815 DWORD dwType
, dwSize
= sizeof(DWORD
);
816 RegString pBuf
= (RegString
)plValue
;
817 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
818 &dwType
, pBuf
, &dwSize
);
819 if ( m_dwLastError
!= ERROR_SUCCESS
) {
820 wxLogSysError(m_dwLastError
, _("Can't read value of key '%s'"),
825 // check that we read the value of right type
826 wxASSERT_MSG( IsNumericValue(szValue
),
827 wxT("Type mismatch in wxRegKey::QueryValue().") );
836 bool wxRegKey::SetValue(const wxChar
*szValue
,const wxMemoryBuffer
& buffer
)
839 wxFAIL_MSG("RegSetValueEx not implemented by TWIN32");
842 if ( CONST_CAST
Open() ) {
843 m_dwLastError
= RegSetValueEx((HKEY
) m_hKey
, szValue
, (DWORD
) RESERVED
, REG_BINARY
,
844 (RegBinary
)buffer
.GetData(),buffer
.GetDataLen());
845 if ( m_dwLastError
== ERROR_SUCCESS
)
849 wxLogSysError(m_dwLastError
, _("Can't set value of '%s'"),
850 GetFullName(this, szValue
));
855 bool wxRegKey::QueryValue(const wxChar
*szValue
, wxMemoryBuffer
& buffer
) const
857 if ( CONST_CAST
Open() ) {
858 // first get the type and size of the data
859 DWORD dwType
, dwSize
;
860 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
861 &dwType
, NULL
, &dwSize
);
863 if ( m_dwLastError
== ERROR_SUCCESS
) {
865 const RegBinary pBuf
= (RegBinary
)buffer
.GetWriteBuf(dwSize
);
866 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
,
867 WXSTRINGCAST szValue
,
872 buffer
.UngetWriteBuf(dwSize
);
874 buffer
.SetDataLen(0);
879 if ( m_dwLastError
!= ERROR_SUCCESS
) {
880 wxLogSysError(m_dwLastError
, _("Can't read value of key '%s'"),
893 bool wxRegKey::QueryValue(const wxChar
*szValue
,
897 if ( CONST_CAST
Open(Read
) ) {
899 // first get the type and size of the data
900 DWORD dwType
, dwSize
;
901 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
902 &dwType
, NULL
, &dwSize
);
903 if ( m_dwLastError
== ERROR_SUCCESS
) {
905 // must treat this case specially as GetWriteBuf() doesn't like
906 // being called with 0 size
910 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
,
911 WXSTRINGCAST szValue
,
914 (RegString
)(wxChar
*)wxStringBuffer(strValue
, dwSize
),
917 // expand the var expansions in the string unless disabled
919 if ( (dwType
== REG_EXPAND_SZ
) && !raw
)
921 DWORD dwExpSize
= ::ExpandEnvironmentStrings(strValue
, NULL
, 0);
922 bool ok
= dwExpSize
!= 0;
925 wxString strExpValue
;
926 ok
= ::ExpandEnvironmentStrings
929 wxStringBuffer(strExpValue
, dwExpSize
),
932 strValue
= strExpValue
;
937 wxLogLastError(_T("ExpandEnvironmentStrings"));
946 if ( m_dwLastError
== ERROR_SUCCESS
) {
947 // check that it was the right type
948 wxASSERT_MSG( !IsNumericValue(szValue
),
949 wxT("Type mismatch in wxRegKey::QueryValue().") );
956 wxLogSysError(m_dwLastError
, _("Can't read value of '%s'"),
957 GetFullName(this, szValue
));
961 bool wxRegKey::SetValue(const wxChar
*szValue
, const wxString
& strValue
)
963 if ( CONST_CAST
Open() ) {
964 m_dwLastError
= RegSetValueEx((HKEY
) m_hKey
, szValue
, (DWORD
) RESERVED
, REG_SZ
,
965 (RegString
)strValue
.c_str(),
966 (strValue
.Len() + 1)*sizeof(wxChar
));
967 if ( m_dwLastError
== ERROR_SUCCESS
)
971 wxLogSysError(m_dwLastError
, _("Can't set value of '%s'"),
972 GetFullName(this, szValue
));
976 wxString
wxRegKey::QueryDefaultValue() const
979 QueryValue(NULL
, str
);
983 // ----------------------------------------------------------------------------
985 // NB: all these functions require an index variable which allows to have
986 // several concurrently running indexations on the same key
987 // ----------------------------------------------------------------------------
989 bool wxRegKey::GetFirstValue(wxString
& strValueName
, long& lIndex
)
995 return GetNextValue(strValueName
, lIndex
);
998 bool wxRegKey::GetNextValue(wxString
& strValueName
, long& lIndex
) const
1000 wxASSERT( IsOpened() );
1002 // are we already at the end of enumeration?
1006 wxChar szValueName
[1024]; // @@ use RegQueryInfoKey...
1007 DWORD dwValueLen
= WXSIZEOF(szValueName
);
1009 m_dwLastError
= RegEnumValue((HKEY
) m_hKey
, lIndex
++,
1010 szValueName
, &dwValueLen
,
1013 NULL
, // [out] buffer for value
1014 NULL
); // [i/o] it's length
1016 if ( m_dwLastError
!= ERROR_SUCCESS
) {
1017 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
1018 m_dwLastError
= ERROR_SUCCESS
;
1022 wxLogSysError(m_dwLastError
, _("Can't enumerate values of key '%s'"),
1029 strValueName
= szValueName
;
1034 bool wxRegKey::GetFirstKey(wxString
& strKeyName
, long& lIndex
)
1040 return GetNextKey(strKeyName
, lIndex
);
1043 bool wxRegKey::GetNextKey(wxString
& strKeyName
, long& lIndex
) const
1045 wxASSERT( IsOpened() );
1047 // are we already at the end of enumeration?
1051 wxChar szKeyName
[_MAX_PATH
+ 1];
1054 DWORD sizeName
= WXSIZEOF(szKeyName
);
1055 m_dwLastError
= RegEnumKeyEx((HKEY
) m_hKey
, lIndex
++, szKeyName
, & sizeName
,
1056 0, NULL
, NULL
, NULL
);
1058 m_dwLastError
= RegEnumKey((HKEY
) m_hKey
, lIndex
++, szKeyName
, WXSIZEOF(szKeyName
));
1061 if ( m_dwLastError
!= ERROR_SUCCESS
) {
1062 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
1063 m_dwLastError
= ERROR_SUCCESS
;
1067 wxLogSysError(m_dwLastError
, _("Can't enumerate subkeys of key '%s'"),
1074 strKeyName
= szKeyName
;
1078 // returns TRUE if the value contains a number (else it's some string)
1079 bool wxRegKey::IsNumericValue(const wxChar
*szValue
) const
1081 ValueType type
= GetValueType(szValue
);
1084 /* case Type_Dword_little_endian: == Type_Dword */
1085 case Type_Dword_big_endian
:
1093 // ============================================================================
1094 // implementation of global private functions
1095 // ============================================================================
1097 bool KeyExists(WXHKEY hRootKey
, const wxChar
*szKey
)
1099 // don't close this key itself for the case of empty szKey!
1100 if ( wxIsEmpty(szKey
) )
1109 KEY_READ
, // we might not have enough rights for rw access
1111 ) == ERROR_SUCCESS
)
1113 ::RegCloseKey(hkeyDummy
);
1121 const wxChar
*GetFullName(const wxRegKey
*pKey
, const wxChar
*szValue
)
1123 static wxString s_str
;
1124 s_str
= pKey
->GetName();
1125 if ( !wxIsEmpty(szValue
) )
1126 s_str
<< wxT("\\") << szValue
;
1128 return s_str
.c_str();
1131 void RemoveTrailingSeparator(wxString
& str
)
1133 if ( !str
.IsEmpty() && str
.Last() == REG_SEPARATOR
)
1134 str
.Truncate(str
.Len() - 1);