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 // for compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
22 // other wxWidgets headers
23 #include "wx/string.h"
27 #include "wx/wfstream.h"
30 #include "wx/msw/wrapwin.h"
33 #include "wx/msw/private.h"
39 #include <stdlib.h> // for _MAX_PATH
46 #define HKEY_DEFINED // already defined in windows.h
47 #include "wx/msw/registry.h"
49 // some registry functions don't like signed chars
50 typedef unsigned char *RegString
;
51 typedef BYTE
* RegBinary
;
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 // the standard key names, short names and handles all bundled together for
63 const wxChar
*szShortName
;
67 { HKEY_CLASSES_ROOT
, wxT("HKEY_CLASSES_ROOT"), wxT("HKCR") },
68 { HKEY_CURRENT_USER
, wxT("HKEY_CURRENT_USER"), wxT("HKCU") },
69 { HKEY_LOCAL_MACHINE
, wxT("HKEY_LOCAL_MACHINE"), wxT("HKLM") },
70 { HKEY_USERS
, wxT("HKEY_USERS"), wxT("HKU") }, // short name?
72 { HKEY_PERFORMANCE_DATA
, wxT("HKEY_PERFORMANCE_DATA"), wxT("HKPD") },
74 #ifdef HKEY_CURRENT_CONFIG
75 { HKEY_CURRENT_CONFIG
, wxT("HKEY_CURRENT_CONFIG"), wxT("HKCC") },
78 { HKEY_DYN_DATA
, wxT("HKEY_DYN_DATA"), wxT("HKDD") }, // short name?
82 // the registry name separator (perhaps one day MS will change it to '/' ;-)
83 #define REG_SEPARATOR wxT('\\')
85 // useful for Windows programmers: makes somewhat more clear all these zeroes
86 // being passed to Windows APIs
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 // const_cast<> is not yet supported by all compilers
94 #define CONST_CAST ((wxRegKey *)this)->
96 // and neither is mutable which m_dwLastError should be
97 #define m_dwLastError CONST_CAST m_dwLastError
99 // ----------------------------------------------------------------------------
100 // non member functions
101 // ----------------------------------------------------------------------------
103 // removes the trailing backslash from the string if it has one
104 static inline void RemoveTrailingSeparator(wxString
& str
);
106 // returns true if given registry key exists
107 static bool KeyExists(WXHKEY hRootKey
, const wxChar
*szKey
);
109 // combines value and key name (uses static buffer!)
110 static const wxChar
*GetFullName(const wxRegKey
*pKey
,
111 const wxChar
*szValue
= NULL
);
113 // ============================================================================
114 // implementation of wxRegKey class
115 // ============================================================================
117 // ----------------------------------------------------------------------------
118 // static functions and variables
119 // ----------------------------------------------------------------------------
121 const size_t wxRegKey::nStdKeys
= WXSIZEOF(aStdKeys
);
123 // @@ should take a `StdKey key', but as it's often going to be used in loops
124 // it would require casts in user code.
125 const wxChar
*wxRegKey::GetStdKeyName(size_t key
)
127 // return empty string if key is invalid
128 wxCHECK_MSG( key
< nStdKeys
, wxEmptyString
, wxT("invalid key in wxRegKey::GetStdKeyName") );
130 return aStdKeys
[key
].szName
;
133 const wxChar
*wxRegKey::GetStdKeyShortName(size_t key
)
135 // return empty string if key is invalid
136 wxCHECK( key
< nStdKeys
, wxEmptyString
);
138 return aStdKeys
[key
].szShortName
;
141 wxRegKey::StdKey
wxRegKey::ExtractKeyName(wxString
& strKey
)
143 wxString strRoot
= strKey
.BeforeFirst(REG_SEPARATOR
);
147 for ( ui
= 0; ui
< nStdKeys
; ui
++ ) {
148 if ( strRoot
.CmpNoCase(aStdKeys
[ui
].szName
) == 0 ||
149 strRoot
.CmpNoCase(aStdKeys
[ui
].szShortName
) == 0 ) {
150 hRootKey
= aStdKeys
[ui
].hkey
;
155 if ( ui
== nStdKeys
) {
156 wxFAIL_MSG(wxT("invalid key prefix in wxRegKey::ExtractKeyName."));
158 hRootKey
= HKEY_CLASSES_ROOT
;
161 strKey
= strKey
.After(REG_SEPARATOR
);
162 if ( !strKey
.empty() && strKey
.Last() == REG_SEPARATOR
)
163 strKey
.Truncate(strKey
.Len() - 1);
166 return (wxRegKey::StdKey
)(int)hRootKey
;
169 wxRegKey::StdKey
wxRegKey::GetStdKeyFromHkey(WXHKEY hkey
)
171 for ( size_t ui
= 0; ui
< nStdKeys
; ui
++ ) {
172 if ( (int) aStdKeys
[ui
].hkey
== (int) hkey
)
176 wxFAIL_MSG(wxT("non root hkey passed to wxRegKey::GetStdKeyFromHkey."));
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
187 m_hRootKey
= (WXHKEY
) aStdKeys
[HKCR
].hkey
;
192 wxRegKey::wxRegKey(const wxString
& strKey
) : m_strKey(strKey
)
194 m_hRootKey
= (WXHKEY
) aStdKeys
[ExtractKeyName(m_strKey
)].hkey
;
199 // parent is a predefined (and preopened) key
200 wxRegKey::wxRegKey(StdKey keyParent
, const wxString
& strKey
) : m_strKey(strKey
)
202 RemoveTrailingSeparator(m_strKey
);
203 m_hRootKey
= (WXHKEY
) aStdKeys
[keyParent
].hkey
;
208 // parent is a normal regkey
209 wxRegKey::wxRegKey(const wxRegKey
& keyParent
, const wxString
& strKey
)
210 : m_strKey(keyParent
.m_strKey
)
212 // combine our name with parent's to get the full name
213 if ( !m_strKey
.empty() &&
214 (strKey
.empty() || strKey
[0] != REG_SEPARATOR
) ) {
215 m_strKey
+= REG_SEPARATOR
;
219 RemoveTrailingSeparator(m_strKey
);
221 m_hRootKey
= keyParent
.m_hRootKey
;
226 // dtor closes the key releasing system resource
227 wxRegKey::~wxRegKey()
232 // ----------------------------------------------------------------------------
233 // change the key name/hkey
234 // ----------------------------------------------------------------------------
236 // set the full key name
237 void wxRegKey::SetName(const wxString
& strKey
)
242 m_hRootKey
= (WXHKEY
) aStdKeys
[ExtractKeyName(m_strKey
)].hkey
;
245 // the name is relative to the parent key
246 void wxRegKey::SetName(StdKey keyParent
, const wxString
& strKey
)
251 RemoveTrailingSeparator(m_strKey
);
252 m_hRootKey
= (WXHKEY
) aStdKeys
[keyParent
].hkey
;
255 // the name is relative to the parent key
256 void wxRegKey::SetName(const wxRegKey
& keyParent
, const wxString
& strKey
)
260 // combine our name with parent's to get the full name
262 // NB: this method is called by wxRegConfig::SetPath() which is a performance
263 // critical function and so it preallocates space for our m_strKey to
264 // gain some speed - this is why we only use += here and not = which
265 // would just free the prealloc'd buffer and would have to realloc it the
268 m_strKey
+= keyParent
.m_strKey
;
269 if ( !strKey
.empty() && strKey
[0] != REG_SEPARATOR
)
270 m_strKey
+= REG_SEPARATOR
;
273 RemoveTrailingSeparator(m_strKey
);
275 m_hRootKey
= keyParent
.m_hRootKey
;
278 // hKey should be opened and will be closed in wxRegKey dtor
279 void wxRegKey::SetHkey(WXHKEY hKey
)
286 // ----------------------------------------------------------------------------
287 // info about the key
288 // ----------------------------------------------------------------------------
290 // returns true if the key exists
291 bool wxRegKey::Exists() const
293 // opened key has to exist, try to open it if not done yet
294 return IsOpened() ? true : KeyExists(m_hRootKey
, m_strKey
);
297 // returns the full name of the key (prefix is abbreviated if bShortPrefix)
298 wxString
wxRegKey::GetName(bool bShortPrefix
) const
300 StdKey key
= GetStdKeyFromHkey((WXHKEY
) m_hRootKey
);
301 wxString str
= bShortPrefix
? aStdKeys
[key
].szShortName
302 : aStdKeys
[key
].szName
;
303 if ( !m_strKey
.empty() )
304 str
<< _T("\\") << m_strKey
;
309 bool wxRegKey::GetKeyInfo(size_t *pnSubKeys
,
312 size_t *pnMaxValueLen
) const
314 // old gcc headers incorrectly prototype RegQueryInfoKey()
315 #if defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__)
316 #define REG_PARAM (size_t *)
318 #define REG_PARAM (LPDWORD)
321 // it might be unexpected to some that this function doesn't open the key
322 wxASSERT_MSG( IsOpened(), _T("key should be opened in GetKeyInfo") );
324 m_dwLastError
= ::RegQueryInfoKey
328 NULL
, // (ptr to) size of class name buffer
331 pnSubKeys
, // [out] number of subkeys
333 pnMaxKeyLen
, // [out] max length of a subkey name
334 NULL
, // longest subkey class name
336 pnValues
, // [out] number of values
338 pnMaxValueLen
, // [out] max length of a value name
339 NULL
, // longest value data
340 NULL
, // security descriptor
341 NULL
// time of last modification
346 if ( m_dwLastError
!= ERROR_SUCCESS
) {
347 wxLogSysError(m_dwLastError
, _("Can't get info about registry key '%s'"),
355 // ----------------------------------------------------------------------------
357 // ----------------------------------------------------------------------------
359 // opens key (it's not an error to call Open() on an already opened key)
360 bool wxRegKey::Open(AccessMode mode
)
364 if ( mode
<= m_mode
)
367 // we had been opened in read mode but now must be reopened in write
372 m_dwLastError
= ::RegOpenKeyEx
377 mode
== Read
? KEY_READ
: KEY_ALL_ACCESS
,
381 if ( m_dwLastError
!= ERROR_SUCCESS
)
383 wxLogSysError(m_dwLastError
, _("Can't open registry key '%s'"),
388 m_hKey
= (WXHKEY
) tmpKey
;
394 // creates key, failing if it exists and !bOkIfExists
395 bool wxRegKey::Create(bool bOkIfExists
)
397 // check for existence only if asked (i.e. order is important!)
398 if ( !bOkIfExists
&& Exists() )
407 m_dwLastError
= RegCreateKeyEx((HKEY
) m_hRootKey
, m_strKey
,
409 NULL
, // class string
416 m_dwLastError
= RegCreateKey((HKEY
) m_hRootKey
, m_strKey
, &tmpKey
);
418 if ( m_dwLastError
!= ERROR_SUCCESS
) {
419 wxLogSysError(m_dwLastError
, _("Can't create registry key '%s'"),
425 m_hKey
= (WXHKEY
) tmpKey
;
430 // close the key, it's not an error to call it when not opened
431 bool wxRegKey::Close()
434 m_dwLastError
= RegCloseKey((HKEY
) m_hKey
);
437 if ( m_dwLastError
!= ERROR_SUCCESS
) {
438 wxLogSysError(m_dwLastError
, _("Can't close registry key '%s'"),
448 bool wxRegKey::RenameValue(const wxChar
*szValueOld
, const wxChar
*szValueNew
)
451 if ( HasValue(szValueNew
) ) {
452 wxLogError(_("Registry value '%s' already exists."), szValueNew
);
458 !CopyValue(szValueOld
, *this, szValueNew
) ||
459 !DeleteValue(szValueOld
) ) {
460 wxLogError(_("Failed to rename registry value '%s' to '%s'."),
461 szValueOld
, szValueNew
);
469 bool wxRegKey::CopyValue(const wxChar
*szValue
,
471 const wxChar
*szValueNew
)
474 // by default, use the same name
475 szValueNew
= szValue
;
478 switch ( GetValueType(szValue
) ) {
482 return QueryValue(szValue
, strVal
) &&
483 keyDst
.SetValue(szValueNew
, strVal
);
487 /* case Type_Dword_little_endian: == Type_Dword */
490 return QueryValue(szValue
, &dwVal
) &&
491 keyDst
.SetValue(szValueNew
, dwVal
);
497 return QueryValue(szValue
,buf
) &&
498 keyDst
.SetValue(szValueNew
,buf
);
501 // these types are unsupported because I am not sure about how
502 // exactly they should be copied and because they shouldn't
503 // occur among the application keys (supposedly created with
506 case Type_Expand_String
:
507 case Type_Dword_big_endian
:
509 case Type_Multi_String
:
510 case Type_Resource_list
:
511 case Type_Full_resource_descriptor
:
512 case Type_Resource_requirements_list
:
514 wxLogError(_("Can't copy values of unsupported type %d."),
515 GetValueType(szValue
));
520 bool wxRegKey::Rename(const wxChar
*szNewName
)
522 wxCHECK_MSG( !m_strKey
.empty(), false, _T("registry hives can't be renamed") );
525 wxLogError(_("Registry key '%s' does not exist, cannot rename it."),
531 // do we stay in the same hive?
532 bool inSameHive
= !wxStrchr(szNewName
, REG_SEPARATOR
);
534 // construct the full new name of the key
538 // rename the key to the new name under the same parent
539 wxString strKey
= m_strKey
.BeforeLast(REG_SEPARATOR
);
540 if ( !strKey
.empty() ) {
541 // don't add '\\' in the start if strFullNewName is empty
542 strKey
+= REG_SEPARATOR
;
547 keyDst
.SetName(GetStdKeyFromHkey(m_hRootKey
), strKey
);
550 // this is the full name already
551 keyDst
.SetName(szNewName
);
554 bool ok
= keyDst
.Create(false /* fail if alredy exists */);
556 wxLogError(_("Registry key '%s' already exists."),
557 GetFullName(&keyDst
));
560 ok
= Copy(keyDst
) && DeleteSelf();
564 wxLogError(_("Failed to rename the registry key '%s' to '%s'."),
565 GetFullName(this), GetFullName(&keyDst
));
568 m_hRootKey
= keyDst
.m_hRootKey
;
569 m_strKey
= keyDst
.m_strKey
;
575 bool wxRegKey::Copy(const wxChar
*szNewName
)
577 // create the new key first
578 wxRegKey
keyDst(szNewName
);
579 bool ok
= keyDst
.Create(false /* fail if alredy exists */);
583 // we created the dest key but copying to it failed - delete it
585 (void)keyDst
.DeleteSelf();
592 bool wxRegKey::Copy(wxRegKey
& keyDst
)
596 // copy all sub keys to the new location
599 bool bCont
= GetFirstKey(strKey
, lIndex
);
600 while ( ok
&& bCont
) {
601 wxRegKey
key(*this, strKey
);
603 keyName
<< GetFullName(&keyDst
) << REG_SEPARATOR
<< strKey
;
604 ok
= key
.Copy((const wxChar
*) keyName
);
607 bCont
= GetNextKey(strKey
, lIndex
);
609 wxLogError(_("Failed to copy the registry subkey '%s' to '%s'."),
610 GetFullName(&key
), keyName
.c_str());
616 bCont
= GetFirstValue(strVal
, lIndex
);
617 while ( ok
&& bCont
) {
618 ok
= CopyValue(strVal
, keyDst
);
621 wxLogSysError(m_dwLastError
,
622 _("Failed to copy registry value '%s'"),
626 bCont
= GetNextValue(strVal
, lIndex
);
631 wxLogError(_("Failed to copy the contents of registry key '%s' to '%s'."),
632 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
.empty() ||
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."),
663 // we can't delete keys while enumerating because it confuses GetNextKey, so
664 // we first save the key names and then delete them all
665 wxArrayString astrSubkeys
;
669 bool bCont
= GetFirstKey(strKey
, lIndex
);
671 astrSubkeys
.Add(strKey
);
673 bCont
= GetNextKey(strKey
, lIndex
);
676 size_t nKeyCount
= astrSubkeys
.Count();
677 for ( size_t nKey
= 0; nKey
< nKeyCount
; nKey
++ ) {
678 wxRegKey
key(*this, astrSubkeys
[nKey
]);
679 if ( !key
.DeleteSelf() )
683 // now delete this key itself
686 m_dwLastError
= RegDeleteKey((HKEY
) m_hRootKey
, m_strKey
);
687 // deleting a key which doesn't exist is not considered an error
688 if ( m_dwLastError
!= ERROR_SUCCESS
&&
689 m_dwLastError
!= ERROR_FILE_NOT_FOUND
) {
690 wxLogSysError(m_dwLastError
, _("Can't delete key '%s'"),
698 bool wxRegKey::DeleteKey(const wxChar
*szKey
)
703 wxRegKey
key(*this, szKey
);
704 return key
.DeleteSelf();
707 bool wxRegKey::DeleteValue(const wxChar
*szValue
)
712 m_dwLastError
= RegDeleteValue((HKEY
) m_hKey
, WXSTRINGCAST szValue
);
714 // deleting a value which doesn't exist is not considered an error
715 if ( (m_dwLastError
!= ERROR_SUCCESS
) &&
716 (m_dwLastError
!= ERROR_FILE_NOT_FOUND
) )
718 wxLogSysError(m_dwLastError
, _("Can't delete value '%s' from key '%s'"),
719 szValue
, GetName().c_str());
726 // ----------------------------------------------------------------------------
727 // access to values and subkeys
728 // ----------------------------------------------------------------------------
730 // return true if value exists
731 bool wxRegKey::HasValue(const wxChar
*szValue
) const
733 // this function should be silent, so suppress possible messages from Open()
736 if ( !CONST_CAST
Open(Read
) )
739 LONG dwRet
= ::RegQueryValueEx((HKEY
) m_hKey
,
740 WXSTRINGCAST szValue
,
743 return dwRet
== ERROR_SUCCESS
;
746 // returns true if this key has any values
747 bool wxRegKey::HasValues() const
749 // suppress possible messages from GetFirstValue()
752 // just call GetFirstValue with dummy parameters
755 return CONST_CAST
GetFirstValue(str
, l
);
758 // returns true if this key has any subkeys
759 bool wxRegKey::HasSubkeys() const
761 // suppress possible messages from GetFirstKey()
764 // just call GetFirstKey with dummy parameters
767 return CONST_CAST
GetFirstKey(str
, l
);
770 // returns true if given subkey exists
771 bool wxRegKey::HasSubKey(const wxChar
*szKey
) const
773 // this function should be silent, so suppress possible messages from Open()
776 if ( !CONST_CAST
Open(Read
) )
779 return KeyExists(m_hKey
, szKey
);
782 wxRegKey::ValueType
wxRegKey::GetValueType(const wxChar
*szValue
) const
784 if ( ! CONST_CAST
Open(Read
) )
788 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
789 &dwType
, NULL
, NULL
);
790 if ( m_dwLastError
!= ERROR_SUCCESS
) {
791 wxLogSysError(m_dwLastError
, _("Can't read value of key '%s'"),
796 return (ValueType
)dwType
;
799 bool wxRegKey::SetValue(const wxChar
*szValue
, long lValue
)
801 if ( CONST_CAST
Open() ) {
802 m_dwLastError
= RegSetValueEx((HKEY
) m_hKey
, szValue
, (DWORD
) RESERVED
, REG_DWORD
,
803 (RegString
)&lValue
, sizeof(lValue
));
804 if ( m_dwLastError
== ERROR_SUCCESS
)
808 wxLogSysError(m_dwLastError
, _("Can't set value of '%s'"),
809 GetFullName(this, szValue
));
813 bool wxRegKey::QueryValue(const wxChar
*szValue
, long *plValue
) const
815 if ( CONST_CAST
Open(Read
) ) {
816 DWORD dwType
, dwSize
= sizeof(DWORD
);
817 RegString pBuf
= (RegString
)plValue
;
818 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
819 &dwType
, pBuf
, &dwSize
);
820 if ( m_dwLastError
!= ERROR_SUCCESS
) {
821 wxLogSysError(m_dwLastError
, _("Can't read value of key '%s'"),
826 // check that we read the value of right type
827 wxASSERT_MSG( IsNumericValue(szValue
),
828 wxT("Type mismatch in wxRegKey::QueryValue().") );
837 bool wxRegKey::SetValue(const wxChar
*szValue
,const wxMemoryBuffer
& buffer
)
840 wxFAIL_MSG("RegSetValueEx not implemented by TWIN32");
843 if ( CONST_CAST
Open() ) {
844 m_dwLastError
= RegSetValueEx((HKEY
) m_hKey
, szValue
, (DWORD
) RESERVED
, REG_BINARY
,
845 (RegBinary
)buffer
.GetData(),buffer
.GetDataLen());
846 if ( m_dwLastError
== ERROR_SUCCESS
)
850 wxLogSysError(m_dwLastError
, _("Can't set value of '%s'"),
851 GetFullName(this, szValue
));
856 bool wxRegKey::QueryValue(const wxChar
*szValue
, wxMemoryBuffer
& buffer
) const
858 if ( CONST_CAST
Open(Read
) ) {
859 // first get the type and size of the data
860 DWORD dwType
, dwSize
;
861 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
862 &dwType
, NULL
, &dwSize
);
864 if ( m_dwLastError
== ERROR_SUCCESS
) {
866 const RegBinary pBuf
= (RegBinary
)buffer
.GetWriteBuf(dwSize
);
867 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
,
868 WXSTRINGCAST szValue
,
873 buffer
.UngetWriteBuf(dwSize
);
875 buffer
.SetDataLen(0);
880 if ( m_dwLastError
!= ERROR_SUCCESS
) {
881 wxLogSysError(m_dwLastError
, _("Can't read value of key '%s'"),
892 bool wxRegKey::QueryValue(const wxChar
*szValue
,
894 bool WXUNUSED_IN_WINCE(raw
)) const
896 if ( CONST_CAST
Open(Read
) )
899 // first get the type and size of the data
900 DWORD dwType
=REG_NONE
, dwSize
=0;
901 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
902 &dwType
, NULL
, &dwSize
);
903 if ( m_dwLastError
== ERROR_SUCCESS
)
907 // must treat this case specially as GetWriteBuf() doesn't like
908 // 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(strValue
,
930 wxStringBuffer(strExpValue
, dwExpSize
),
933 strValue
= strExpValue
;
938 wxLogLastError(_T("ExpandEnvironmentStrings"));
945 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 // exporting registry keys to file
1095 // ----------------------------------------------------------------------------
1099 // helper functions for writing ASCII strings (even in Unicode build)
1100 static inline bool WriteAsciiChar(wxOutputStream
& ostr
, char ch
)
1106 static inline bool WriteAsciiEOL(wxOutputStream
& ostr
)
1108 // as we open the file in text mode, it is enough to write LF without CR
1109 return WriteAsciiChar(ostr
, '\n');
1112 static inline bool WriteAsciiString(wxOutputStream
& ostr
, const char *p
)
1114 return ostr
.Write(p
, strlen(p
)).IsOk();
1117 static inline bool WriteAsciiString(wxOutputStream
& ostr
, const wxString
& s
)
1120 wxCharBuffer
name(s
.mb_str());
1121 ostr
.Write(name
, strlen(name
));
1123 ostr
.Write(s
, s
.length());
1129 #endif // wxUSE_STREAMS
1131 bool wxRegKey::Export(const wxString
& filename
) const
1133 #if wxUSE_FFILE && wxUSE_STREAMS
1134 if ( wxFile::Exists(filename
) )
1136 wxLogError(_("Exporting registry key: file \"%s\" already exists and won't be overwritten."),
1141 wxFFileOutputStream
ostr(filename
, _T("w"));
1143 return ostr
.Ok() && Export(ostr
);
1145 wxUnusedVar(filename
);
1151 bool wxRegKey::Export(wxOutputStream
& ostr
) const
1153 // write out the header
1154 if ( !WriteAsciiString(ostr
, "REGEDIT4\n\n") )
1157 return DoExport(ostr
);
1159 #endif // wxUSE_STREAMS
1163 FormatAsHex(const void *data
,
1165 wxRegKey::ValueType type
= wxRegKey::Type_Binary
)
1167 wxString
value(_T("hex"));
1169 // binary values use just "hex:" prefix while the other ones must indicate
1171 if ( type
!= wxRegKey::Type_Binary
)
1172 value
<< _T('(') << type
<< _T(')');
1175 // write all the rest as comma-separated bytes
1176 value
.reserve(3*size
+ 10);
1177 const char * const p
= wx_static_cast(const char *, data
);
1178 for ( size_t n
= 0; n
< size
; n
++ )
1180 // TODO: line wrapping: although not required by regedit, this makes
1181 // the generated files easier to read and compare with the files
1182 // produced by regedit
1186 value
<< wxString::Format(_T("%02x"), (unsigned char)p
[n
]);
1193 wxString
FormatAsHex(const wxString
& value
, wxRegKey::ValueType type
)
1195 return FormatAsHex(value
.c_str(), value
.length() + 1, type
);
1198 wxString
wxRegKey::FormatValue(const wxString
& name
) const
1201 const ValueType type
= GetValueType(name
);
1207 if ( !QueryValue(name
, value
) )
1210 // quotes and backslashes must be quoted, linefeeds are not
1211 // allowed in string values
1212 rhs
.reserve(value
.length() + 2);
1215 // there can be no NULs here
1216 bool useHex
= false;
1217 for ( const wxChar
*p
= value
.c_str(); *p
&& !useHex
; p
++ )
1222 // we can only represent this string in hex
1228 // escape special symbol
1238 rhs
= FormatAsHex(value
, Type_String
);
1245 /* case Type_Dword_little_endian: == Type_Dword */
1248 if ( !QueryValue(name
, &value
) )
1251 rhs
.Printf(_T("dword:%08x"), (unsigned int)value
);
1255 case Type_Expand_String
:
1256 case Type_Multi_String
:
1259 if ( !QueryRawValue(name
, value
) )
1262 rhs
= FormatAsHex(value
, type
);
1269 if ( !QueryValue(name
, buf
) )
1272 rhs
= FormatAsHex(buf
.GetData(), buf
.GetDataLen());
1276 // no idea how those appear in REGEDIT4 files
1278 case Type_Dword_big_endian
:
1280 case Type_Resource_list
:
1281 case Type_Full_resource_descriptor
:
1282 case Type_Resource_requirements_list
:
1284 wxLogWarning(_("Can't export value of unsupported type %d."), type
);
1292 bool wxRegKey::DoExportValue(wxOutputStream
& ostr
, const wxString
& name
) const
1294 // first examine the value type: if it's unsupported, simply skip it
1295 // instead of aborting the entire export process because we failed to
1296 // export a single value
1297 wxString value
= FormatValue(name
);
1298 if ( value
.empty() )
1300 wxLogWarning(_("Ignoring value \"%s\" of the key \"%s\"."),
1301 name
.c_str(), GetName().c_str());
1305 // we do have the text representation of the value, now write everything
1308 // special case: unnamed/default value is represented as just "@"
1311 if ( !WriteAsciiChar(ostr
, '@') )
1314 else // normal, named, value
1316 if ( !WriteAsciiChar(ostr
, '"') ||
1317 !WriteAsciiString(ostr
, name
) ||
1318 !WriteAsciiChar(ostr
, '"') )
1322 if ( !WriteAsciiChar(ostr
, '=') )
1325 return WriteAsciiString(ostr
, value
) && WriteAsciiEOL(ostr
);
1328 bool wxRegKey::DoExport(wxOutputStream
& ostr
) const
1330 // write out this key name
1331 if ( !WriteAsciiChar(ostr
, '[') )
1334 if ( !WriteAsciiString(ostr
, GetName(false /* no short prefix */)) )
1337 if ( !WriteAsciiChar(ostr
, ']') || !WriteAsciiEOL(ostr
) )
1340 // dump all our values
1343 wxRegKey
& self
= wx_const_cast(wxRegKey
&, *this);
1344 bool cont
= self
.GetFirstValue(name
, dummy
);
1347 if ( !DoExportValue(ostr
, name
) )
1350 cont
= GetNextValue(name
, dummy
);
1353 // always terminate values by blank line, even if there were no values
1354 if ( !WriteAsciiEOL(ostr
) )
1357 // recurse to subkeys
1358 cont
= self
.GetFirstKey(name
, dummy
);
1361 wxRegKey
subkey(*this, name
);
1362 if ( !subkey
.DoExport(ostr
) )
1365 cont
= GetNextKey(name
, dummy
);
1371 #endif // wxUSE_STREAMS
1373 // ============================================================================
1374 // implementation of global private functions
1375 // ============================================================================
1377 bool KeyExists(WXHKEY hRootKey
, const wxChar
*szKey
)
1379 // don't close this key itself for the case of empty szKey!
1380 if ( wxIsEmpty(szKey
) )
1389 KEY_READ
, // we might not have enough rights for rw access
1391 ) == ERROR_SUCCESS
)
1393 ::RegCloseKey(hkeyDummy
);
1401 const wxChar
*GetFullName(const wxRegKey
*pKey
, const wxChar
*szValue
)
1403 static wxString s_str
;
1404 s_str
= pKey
->GetName();
1405 if ( !wxIsEmpty(szValue
) )
1406 s_str
<< wxT("\\") << szValue
;
1408 return s_str
.c_str();
1411 void RemoveTrailingSeparator(wxString
& str
)
1413 if ( !str
.empty() && str
.Last() == REG_SEPARATOR
)
1414 str
.Truncate(str
.Len() - 1);