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'."),
612 GetFullName(&key
), keyName
.c_str());
618 bCont
= GetFirstValue(strVal
, lIndex
);
619 while ( ok
&& bCont
) {
620 ok
= CopyValue(strVal
, keyDst
);
623 wxLogSysError(m_dwLastError
,
624 _("Failed to copy registry value '%s'"),
628 bCont
= GetNextValue(strVal
, lIndex
);
633 wxLogError(_("Failed to copy the contents of registry key '%s' to '%s'."),
634 GetFullName(this), GetFullName(&keyDst
));
640 // ----------------------------------------------------------------------------
641 // delete keys/values
642 // ----------------------------------------------------------------------------
643 bool wxRegKey::DeleteSelf()
648 // it already doesn't exist - ok!
653 // prevent a buggy program from erasing one of the root registry keys or an
654 // immediate subkey (i.e. one which doesn't have '\\' inside) of any other
655 // key except HKCR (HKCR has some "deleteable" subkeys)
656 if ( m_strKey
.IsEmpty() ||
657 ((m_hRootKey
!= (WXHKEY
) aStdKeys
[HKCR
].hkey
) &&
658 (m_strKey
.Find(REG_SEPARATOR
) == wxNOT_FOUND
)) ) {
659 wxLogError(_("Registry key '%s' is needed for normal system operation,\ndeleting it will leave your system in unusable state:\noperation aborted."),
665 // we can't delete keys while enumerating because it confuses GetNextKey, so
666 // we first save the key names and then delete them all
667 wxArrayString astrSubkeys
;
671 bool bCont
= GetFirstKey(strKey
, lIndex
);
673 astrSubkeys
.Add(strKey
);
675 bCont
= GetNextKey(strKey
, lIndex
);
678 size_t nKeyCount
= astrSubkeys
.Count();
679 for ( size_t nKey
= 0; nKey
< nKeyCount
; nKey
++ ) {
680 wxRegKey
key(*this, astrSubkeys
[nKey
]);
681 if ( !key
.DeleteSelf() )
685 // now delete this key itself
688 m_dwLastError
= RegDeleteKey((HKEY
) m_hRootKey
, m_strKey
);
689 // deleting a key which doesn't exist is not considered an error
690 if ( m_dwLastError
!= ERROR_SUCCESS
&&
691 m_dwLastError
!= ERROR_FILE_NOT_FOUND
) {
692 wxLogSysError(m_dwLastError
, _("Can't delete key '%s'"),
700 bool wxRegKey::DeleteKey(const wxChar
*szKey
)
705 wxRegKey
key(*this, szKey
);
706 return key
.DeleteSelf();
709 bool wxRegKey::DeleteValue(const wxChar
*szValue
)
714 m_dwLastError
= RegDeleteValue((HKEY
) m_hKey
, WXSTRINGCAST szValue
);
716 // deleting a value which doesn't exist is not considered an error
717 if ( (m_dwLastError
!= ERROR_SUCCESS
) &&
718 (m_dwLastError
!= ERROR_FILE_NOT_FOUND
) ) {
719 wxLogSysError(m_dwLastError
, _("Can't delete value '%s' from key '%s'"),
720 szValue
, GetName().c_str());
727 // ----------------------------------------------------------------------------
728 // access to values and subkeys
729 // ----------------------------------------------------------------------------
731 // return TRUE if value exists
732 bool wxRegKey::HasValue(const wxChar
*szValue
) const
734 // this function should be silent, so suppress possible messages from Open()
737 if ( !CONST_CAST
Open(Read
) )
740 LONG dwRet
= ::RegQueryValueEx((HKEY
) m_hKey
,
741 WXSTRINGCAST szValue
,
744 return dwRet
== ERROR_SUCCESS
;
747 // returns TRUE if this key has any values
748 bool wxRegKey::HasValues() const
750 // suppress possible messages from GetFirstValue()
753 // just call GetFirstValue with dummy parameters
756 return CONST_CAST
GetFirstValue(str
, l
);
759 // returns TRUE if this key has any subkeys
760 bool wxRegKey::HasSubkeys() const
762 // suppress possible messages from GetFirstKey()
765 // just call GetFirstKey with dummy parameters
768 return CONST_CAST
GetFirstKey(str
, l
);
771 // returns TRUE if given subkey exists
772 bool wxRegKey::HasSubKey(const wxChar
*szKey
) const
774 // this function should be silent, so suppress possible messages from Open()
777 if ( !CONST_CAST
Open(Read
) )
780 return KeyExists(m_hKey
, szKey
);
783 wxRegKey::ValueType
wxRegKey::GetValueType(const wxChar
*szValue
) const
785 if ( ! CONST_CAST
Open(Read
) )
789 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
790 &dwType
, NULL
, NULL
);
791 if ( m_dwLastError
!= ERROR_SUCCESS
) {
792 wxLogSysError(m_dwLastError
, _("Can't read value of key '%s'"),
797 return (ValueType
)dwType
;
801 bool wxRegKey::SetValue(const wxChar
*szValue
, long lValue
)
803 if ( CONST_CAST
Open() ) {
804 m_dwLastError
= RegSetValueEx((HKEY
) m_hKey
, szValue
, (DWORD
) RESERVED
, REG_DWORD
,
805 (RegString
)&lValue
, sizeof(lValue
));
806 if ( m_dwLastError
== ERROR_SUCCESS
)
810 wxLogSysError(m_dwLastError
, _("Can't set value of '%s'"),
811 GetFullName(this, szValue
));
815 bool wxRegKey::QueryValue(const wxChar
*szValue
, long *plValue
) const
817 if ( CONST_CAST
Open(Read
) ) {
818 DWORD dwType
, dwSize
= sizeof(DWORD
);
819 RegString pBuf
= (RegString
)plValue
;
820 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
821 &dwType
, pBuf
, &dwSize
);
822 if ( m_dwLastError
!= ERROR_SUCCESS
) {
823 wxLogSysError(m_dwLastError
, _("Can't read value of key '%s'"),
828 // check that we read the value of right type
829 wxASSERT_MSG( IsNumericValue(szValue
),
830 wxT("Type mismatch in wxRegKey::QueryValue().") );
839 bool wxRegKey::SetValue(const wxChar
*szValue
,const wxMemoryBuffer
& buffer
)
842 wxFAIL_MSG("RegSetValueEx not implemented by TWIN32");
845 if ( CONST_CAST
Open() ) {
846 m_dwLastError
= RegSetValueEx((HKEY
) m_hKey
, szValue
, (DWORD
) RESERVED
, REG_BINARY
,
847 (RegBinary
)buffer
.GetData(),buffer
.GetDataLen());
848 if ( m_dwLastError
== ERROR_SUCCESS
)
852 wxLogSysError(m_dwLastError
, _("Can't set value of '%s'"),
853 GetFullName(this, szValue
));
858 bool wxRegKey::QueryValue(const wxChar
*szValue
, wxMemoryBuffer
& buffer
) const
860 if ( CONST_CAST
Open() ) {
861 // first get the type and size of the data
862 DWORD dwType
, dwSize
;
863 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
864 &dwType
, NULL
, &dwSize
);
866 if ( m_dwLastError
== ERROR_SUCCESS
) {
868 const RegBinary pBuf
= (RegBinary
)buffer
.GetWriteBuf(dwSize
);
869 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
,
870 WXSTRINGCAST szValue
,
875 buffer
.UngetWriteBuf(dwSize
);
877 buffer
.SetDataLen(0);
882 if ( m_dwLastError
!= ERROR_SUCCESS
) {
883 wxLogSysError(m_dwLastError
, _("Can't read value of key '%s'"),
896 bool wxRegKey::QueryValue(const wxChar
*szValue
,
900 if ( CONST_CAST
Open(Read
) ) {
902 // first get the type and size of the data
903 DWORD dwType
, dwSize
;
904 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
905 &dwType
, NULL
, &dwSize
);
906 if ( m_dwLastError
== ERROR_SUCCESS
) {
908 // must treat this case specially as GetWriteBuf() doesn't like
909 // being called with 0 size
913 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
,
914 WXSTRINGCAST szValue
,
917 (RegString
)(wxChar
*)wxStringBuffer(strValue
, dwSize
),
920 // expand the var expansions in the string unless disabled
922 if ( (dwType
== REG_EXPAND_SZ
) && !raw
)
924 DWORD dwExpSize
= ::ExpandEnvironmentStrings(strValue
, NULL
, 0);
925 bool ok
= dwExpSize
!= 0;
928 wxString strExpValue
;
929 ok
= ::ExpandEnvironmentStrings
932 wxStringBuffer(strExpValue
, dwExpSize
),
935 strValue
= strExpValue
;
940 wxLogLastError(_T("ExpandEnvironmentStrings"));
949 if ( m_dwLastError
== ERROR_SUCCESS
) {
950 // check that it was the right type
951 wxASSERT_MSG( !IsNumericValue(szValue
),
952 wxT("Type mismatch in wxRegKey::QueryValue().") );
959 wxLogSysError(m_dwLastError
, _("Can't read value of '%s'"),
960 GetFullName(this, szValue
));
964 bool wxRegKey::SetValue(const wxChar
*szValue
, const wxString
& strValue
)
966 if ( CONST_CAST
Open() ) {
967 m_dwLastError
= RegSetValueEx((HKEY
) m_hKey
, szValue
, (DWORD
) RESERVED
, REG_SZ
,
968 (RegString
)strValue
.c_str(),
969 (strValue
.Len() + 1)*sizeof(wxChar
));
970 if ( m_dwLastError
== ERROR_SUCCESS
)
974 wxLogSysError(m_dwLastError
, _("Can't set value of '%s'"),
975 GetFullName(this, szValue
));
979 wxString
wxRegKey::QueryDefaultValue() const
982 QueryValue(NULL
, str
);
986 // ----------------------------------------------------------------------------
988 // NB: all these functions require an index variable which allows to have
989 // several concurrently running indexations on the same key
990 // ----------------------------------------------------------------------------
992 bool wxRegKey::GetFirstValue(wxString
& strValueName
, long& lIndex
)
998 return GetNextValue(strValueName
, lIndex
);
1001 bool wxRegKey::GetNextValue(wxString
& strValueName
, long& lIndex
) const
1003 wxASSERT( IsOpened() );
1005 // are we already at the end of enumeration?
1009 wxChar szValueName
[1024]; // @@ use RegQueryInfoKey...
1010 DWORD dwValueLen
= WXSIZEOF(szValueName
);
1012 m_dwLastError
= RegEnumValue((HKEY
) m_hKey
, lIndex
++,
1013 szValueName
, &dwValueLen
,
1016 NULL
, // [out] buffer for value
1017 NULL
); // [i/o] it's length
1019 if ( m_dwLastError
!= ERROR_SUCCESS
) {
1020 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
1021 m_dwLastError
= ERROR_SUCCESS
;
1025 wxLogSysError(m_dwLastError
, _("Can't enumerate values of key '%s'"),
1032 strValueName
= szValueName
;
1037 bool wxRegKey::GetFirstKey(wxString
& strKeyName
, long& lIndex
)
1043 return GetNextKey(strKeyName
, lIndex
);
1046 bool wxRegKey::GetNextKey(wxString
& strKeyName
, long& lIndex
) const
1048 wxASSERT( IsOpened() );
1050 // are we already at the end of enumeration?
1054 wxChar szKeyName
[_MAX_PATH
+ 1];
1057 DWORD sizeName
= WXSIZEOF(szKeyName
);
1058 m_dwLastError
= RegEnumKeyEx((HKEY
) m_hKey
, lIndex
++, szKeyName
, & sizeName
,
1059 0, NULL
, NULL
, NULL
);
1061 m_dwLastError
= RegEnumKey((HKEY
) m_hKey
, lIndex
++, szKeyName
, WXSIZEOF(szKeyName
));
1064 if ( m_dwLastError
!= ERROR_SUCCESS
) {
1065 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
1066 m_dwLastError
= ERROR_SUCCESS
;
1070 wxLogSysError(m_dwLastError
, _("Can't enumerate subkeys of key '%s'"),
1077 strKeyName
= szKeyName
;
1081 // returns TRUE if the value contains a number (else it's some string)
1082 bool wxRegKey::IsNumericValue(const wxChar
*szValue
) const
1084 ValueType type
= GetValueType(szValue
);
1087 /* case Type_Dword_little_endian: == Type_Dword */
1088 case Type_Dword_big_endian
:
1096 // ============================================================================
1097 // implementation of global private functions
1098 // ============================================================================
1100 bool KeyExists(WXHKEY hRootKey
, const wxChar
*szKey
)
1102 // don't close this key itself for the case of empty szKey!
1103 if ( wxIsEmpty(szKey
) )
1112 KEY_READ
, // we might not have enough rights for rw access
1114 ) == ERROR_SUCCESS
)
1116 ::RegCloseKey(hkeyDummy
);
1124 const wxChar
*GetFullName(const wxRegKey
*pKey
, const wxChar
*szValue
)
1126 static wxString s_str
;
1127 s_str
= pKey
->GetName();
1128 if ( !wxIsEmpty(szValue
) )
1129 s_str
<< wxT("\\") << szValue
;
1131 return s_str
.c_str();
1134 void RemoveTrailingSeparator(wxString
& str
)
1136 if ( !str
.IsEmpty() && str
.Last() == REG_SEPARATOR
)
1137 str
.Truncate(str
.Len() - 1);