1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: palmos/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 // This really doesn't apply to the Palm OS platform. It would be better to
27 // support the Palm OS preference database instead.
30 // other wxWidgets headers
31 #include "wx/string.h"
35 #include "wx/palmos/wrapwin.h"
38 #include <stdlib.h> // for _MAX_PATH
45 #define HKEY_DEFINED // already defined in windows.h
46 #include "wx/palmos/registry.h"
48 // some registry functions don't like signed chars
49 typedef unsigned char *RegString
;
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // the registry name separator (perhaps one day MS will change it to '/' ;-)
56 #define REG_SEPARATOR wxT('\\')
58 // useful for Windows programmers: makes somewhat more clear all these zeroes
59 // being passed to Windows APIs
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // const_cast<> is not yet supported by all compilers
67 #define CONST_CAST ((wxRegKey *)this)->
69 // and neither is mutable which m_dwLastError should be
70 #define m_dwLastError CONST_CAST m_dwLastError
72 // ----------------------------------------------------------------------------
73 // non member functions
74 // ----------------------------------------------------------------------------
76 // ============================================================================
77 // implementation of wxRegKey class
78 // ============================================================================
80 // ----------------------------------------------------------------------------
81 // static functions and variables
82 // ----------------------------------------------------------------------------
84 const size_t wxRegKey::nStdKeys
= WXSIZEOF(aStdKeys
);
86 // @@ should take a `StdKey key', but as it's often going to be used in loops
87 // it would require casts in user code.
88 const wxChar
*wxRegKey::GetStdKeyName(size_t key
)
90 // return empty string if key is invalid
91 wxCHECK_MSG( key
< nStdKeys
, wxEmptyString
, wxT("invalid key in wxRegKey::GetStdKeyName") );
93 return aStdKeys
[key
].szName
;
96 const wxChar
*wxRegKey::GetStdKeyShortName(size_t key
)
98 // return empty string if key is invalid
99 wxCHECK( key
< nStdKeys
, wxEmptyString
);
101 return aStdKeys
[key
].szShortName
;
104 wxRegKey::StdKey
wxRegKey::ExtractKeyName(wxString
& strKey
)
106 wxString strRoot
= strKey
.BeforeFirst(REG_SEPARATOR
);
110 for ( ui
= 0; ui
< nStdKeys
; ui
++ ) {
111 if ( strRoot
.CmpNoCase(aStdKeys
[ui
].szName
) == 0 ||
112 strRoot
.CmpNoCase(aStdKeys
[ui
].szShortName
) == 0 ) {
113 hRootKey
= aStdKeys
[ui
].hkey
;
118 if ( ui
== nStdKeys
) {
119 wxFAIL_MSG(wxT("invalid key prefix in wxRegKey::ExtractKeyName."));
121 hRootKey
= HKEY_CLASSES_ROOT
;
124 strKey
= strKey
.After(REG_SEPARATOR
);
125 if ( !strKey
.empty() && strKey
.Last() == REG_SEPARATOR
)
126 strKey
.Truncate(strKey
.Len() - 1);
129 return (wxRegKey::StdKey
)(int)hRootKey
;
132 wxRegKey::StdKey
wxRegKey::GetStdKeyFromHkey(WXHKEY hkey
)
134 for ( size_t ui
= 0; ui
< nStdKeys
; ui
++ ) {
135 if ( (int) aStdKeys
[ui
].hkey
== (int) hkey
)
139 wxFAIL_MSG(wxT("non root hkey passed to wxRegKey::GetStdKeyFromHkey."));
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
150 m_hRootKey
= (WXHKEY
) aStdKeys
[HKCR
].hkey
;
155 wxRegKey::wxRegKey(const wxString
& strKey
) : m_strKey(strKey
)
157 m_hRootKey
= (WXHKEY
) aStdKeys
[ExtractKeyName(m_strKey
)].hkey
;
162 // parent is a predefined (and preopened) key
163 wxRegKey::wxRegKey(StdKey keyParent
, const wxString
& strKey
) : m_strKey(strKey
)
165 RemoveTrailingSeparator(m_strKey
);
166 m_hRootKey
= (WXHKEY
) aStdKeys
[keyParent
].hkey
;
171 // parent is a normal regkey
172 wxRegKey::wxRegKey(const wxRegKey
& keyParent
, const wxString
& strKey
)
173 : m_strKey(keyParent
.m_strKey
)
175 // combine our name with parent's to get the full name
176 if ( !m_strKey
.empty() &&
177 (strKey
.empty() || strKey
[0] != REG_SEPARATOR
) ) {
178 m_strKey
+= REG_SEPARATOR
;
182 RemoveTrailingSeparator(m_strKey
);
184 m_hRootKey
= keyParent
.m_hRootKey
;
189 // dtor closes the key releasing system resource
190 wxRegKey::~wxRegKey()
195 // ----------------------------------------------------------------------------
196 // change the key name/hkey
197 // ----------------------------------------------------------------------------
199 // set the full key name
200 void wxRegKey::SetName(const wxString
& strKey
)
205 m_hRootKey
= (WXHKEY
) aStdKeys
[ExtractKeyName(m_strKey
)].hkey
;
208 // the name is relative to the parent key
209 void wxRegKey::SetName(StdKey keyParent
, const wxString
& strKey
)
214 RemoveTrailingSeparator(m_strKey
);
215 m_hRootKey
= (WXHKEY
) aStdKeys
[keyParent
].hkey
;
218 // the name is relative to the parent key
219 void wxRegKey::SetName(const wxRegKey
& keyParent
, const wxString
& strKey
)
223 // combine our name with parent's to get the full name
225 // NB: this method is called by wxRegConfig::SetPath() which is a performance
226 // critical function and so it preallocates space for our m_strKey to
227 // gain some speed - this is why we only use += here and not = which
228 // would just free the prealloc'd buffer and would have to realloc it the
231 m_strKey
+= keyParent
.m_strKey
;
232 if ( !strKey
.empty() && strKey
[0] != REG_SEPARATOR
)
233 m_strKey
+= REG_SEPARATOR
;
236 RemoveTrailingSeparator(m_strKey
);
238 m_hRootKey
= keyParent
.m_hRootKey
;
241 // hKey should be opened and will be closed in wxRegKey dtor
242 void wxRegKey::SetHkey(WXHKEY hKey
)
249 // ----------------------------------------------------------------------------
250 // info about the key
251 // ----------------------------------------------------------------------------
253 // returns true if the key exists
254 bool wxRegKey::Exists() const
256 // opened key has to exist, try to open it if not done yet
257 return IsOpened() ? true : KeyExists(m_hRootKey
, m_strKey
);
260 // returns the full name of the key (prefix is abbreviated if bShortPrefix)
261 wxString
wxRegKey::GetName(bool bShortPrefix
) const
263 StdKey key
= GetStdKeyFromHkey((WXHKEY
) m_hRootKey
);
264 wxString str
= bShortPrefix
? aStdKeys
[key
].szShortName
265 : aStdKeys
[key
].szName
;
266 if ( !m_strKey
.empty() )
267 str
<< _T("\\") << m_strKey
;
272 bool wxRegKey::GetKeyInfo(size_t *pnSubKeys
,
275 size_t *pnMaxValueLen
) const
277 // old gcc headers incorrectly prototype RegQueryInfoKey()
278 #if defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__)
279 #define REG_PARAM (size_t *)
281 #define REG_PARAM (LPDWORD)
284 // it might be unexpected to some that this function doesn't open the key
285 wxASSERT_MSG( IsOpened(), _T("key should be opened in GetKeyInfo") );
287 m_dwLastError
= ::RegQueryInfoKey
291 NULL
, // (ptr to) size of class name buffer
294 pnSubKeys
, // [out] number of subkeys
296 pnMaxKeyLen
, // [out] max length of a subkey name
297 NULL
, // longest subkey class name
299 pnValues
, // [out] number of values
301 pnMaxValueLen
, // [out] max length of a value name
302 NULL
, // longest value data
303 NULL
, // security descriptor
304 NULL
// time of last modification
309 if ( m_dwLastError
!= ERROR_SUCCESS
) {
310 wxLogSysError(m_dwLastError
, _("Can't get info about registry key '%s'"),
318 // ----------------------------------------------------------------------------
320 // ----------------------------------------------------------------------------
322 // opens key (it's not an error to call Open() on an already opened key)
323 bool wxRegKey::Open(AccessMode mode
)
329 m_dwLastError
= ::RegOpenKeyEx
334 mode
== Read
? KEY_READ
: KEY_ALL_ACCESS
,
338 if ( m_dwLastError
!= ERROR_SUCCESS
)
340 wxLogSysError(m_dwLastError
, _("Can't open registry key '%s'"),
345 m_hKey
= (WXHKEY
) tmpKey
;
349 // creates key, failing if it exists and !bOkIfExists
350 bool wxRegKey::Create(bool bOkIfExists
)
352 // check for existence only if asked (i.e. order is important!)
353 if ( !bOkIfExists
&& Exists() )
362 m_dwLastError
= RegCreateKeyEx((HKEY
) m_hRootKey
, m_strKey
,
364 NULL
, // class string
371 m_dwLastError
= RegCreateKey((HKEY
) m_hRootKey
, m_strKey
, &tmpKey
);
373 if ( m_dwLastError
!= ERROR_SUCCESS
) {
374 wxLogSysError(m_dwLastError
, _("Can't create registry key '%s'"),
380 m_hKey
= (WXHKEY
) tmpKey
;
385 // close the key, it's not an error to call it when not opened
386 bool wxRegKey::Close()
389 m_dwLastError
= RegCloseKey((HKEY
) m_hKey
);
392 if ( m_dwLastError
!= ERROR_SUCCESS
) {
393 wxLogSysError(m_dwLastError
, _("Can't close registry key '%s'"),
403 bool wxRegKey::RenameValue(const wxChar
*szValueOld
, const wxChar
*szValueNew
)
406 if ( HasValue(szValueNew
) ) {
407 wxLogError(_("Registry value '%s' already exists."), szValueNew
);
413 !CopyValue(szValueOld
, *this, szValueNew
) ||
414 !DeleteValue(szValueOld
) ) {
415 wxLogError(_("Failed to rename registry value '%s' to '%s'."),
416 szValueOld
, szValueNew
);
424 bool wxRegKey::CopyValue(const wxChar
*szValue
,
426 const wxChar
*szValueNew
)
429 // by default, use the same name
430 szValueNew
= szValue
;
433 switch ( GetValueType(szValue
) ) {
437 return QueryValue(szValue
, strVal
) &&
438 keyDst
.SetValue(szValueNew
, strVal
);
442 /* case Type_Dword_little_endian: == Type_Dword */
445 return QueryValue(szValue
, &dwVal
) &&
446 keyDst
.SetValue(szValueNew
, dwVal
);
449 // these types are unsupported because I am not sure about how
450 // exactly they should be copied and because they shouldn't
451 // occur among the application keys (supposedly created with
454 wxLogError(_("Can't copy values of unsupported type %d."),
455 GetValueType(szValue
));
460 bool wxRegKey::Rename(const wxChar
*szNewName
)
462 wxCHECK_MSG( !!m_strKey
, false, _T("registry hives can't be renamed") );
465 wxLogError(_("Registry key '%s' does not exist, cannot rename it."),
471 // do we stay in the same hive?
472 bool inSameHive
= !wxStrchr(szNewName
, REG_SEPARATOR
);
474 // construct the full new name of the key
478 // rename the key to the new name under the same parent
479 wxString strKey
= m_strKey
.BeforeLast(REG_SEPARATOR
);
481 // don't add '\\' in the start if strFullNewName is empty
482 strKey
+= REG_SEPARATOR
;
487 keyDst
.SetName(GetStdKeyFromHkey(m_hRootKey
), strKey
);
490 // this is the full name already
491 keyDst
.SetName(szNewName
);
494 bool ok
= keyDst
.Create(false /* fail if alredy exists */);
496 wxLogError(_("Registry key '%s' already exists."),
497 GetFullName(&keyDst
));
500 ok
= Copy(keyDst
) && DeleteSelf();
504 wxLogError(_("Failed to rename the registry key '%s' to '%s'."),
505 GetFullName(this), GetFullName(&keyDst
));
508 m_hRootKey
= keyDst
.m_hRootKey
;
509 m_strKey
= keyDst
.m_strKey
;
515 bool wxRegKey::Copy(const wxChar
*szNewName
)
517 // create the new key first
518 wxRegKey
keyDst(szNewName
);
519 bool ok
= keyDst
.Create(false /* fail if alredy exists */);
523 // we created the dest key but copying to it failed - delete it
525 (void)keyDst
.DeleteSelf();
532 bool wxRegKey::Copy(wxRegKey
& keyDst
)
536 // copy all sub keys to the new location
539 bool bCont
= GetFirstKey(strKey
, lIndex
);
540 while ( ok
&& bCont
) {
541 wxRegKey
key(*this, strKey
);
543 keyName
<< GetFullName(&keyDst
) << REG_SEPARATOR
<< strKey
;
544 ok
= key
.Copy((const wxChar
*) keyName
);
547 bCont
= GetNextKey(strKey
, lIndex
);
552 bCont
= GetFirstValue(strVal
, lIndex
);
553 while ( ok
&& bCont
) {
554 ok
= CopyValue(strVal
, keyDst
);
557 wxLogSysError(m_dwLastError
,
558 _("Failed to copy registry value '%s'"),
562 bCont
= GetNextValue(strVal
, lIndex
);
567 wxLogError(_("Failed to copy the contents of registry key '%s' to '%s'."), GetFullName(this), GetFullName(&keyDst
));
573 // ----------------------------------------------------------------------------
574 // delete keys/values
575 // ----------------------------------------------------------------------------
576 bool wxRegKey::DeleteSelf()
581 // it already doesn't exist - ok!
586 // prevent a buggy program from erasing one of the root registry keys or an
587 // immediate subkey (i.e. one which doesn't have '\\' inside) of any other
588 // key except HKCR (HKCR has some "deleteable" subkeys)
589 if ( m_strKey
.empty() ||
590 ((m_hRootKey
!= (WXHKEY
) aStdKeys
[HKCR
].hkey
) &&
591 (m_strKey
.Find(REG_SEPARATOR
) == wxNOT_FOUND
)) ) {
592 wxLogError(_("Registry key '%s' is needed for normal system operation,\ndeleting it will leave your system in unusable state:\noperation aborted."), GetFullName(this));
597 // we can't delete keys while enumerating because it confuses GetNextKey, so
598 // we first save the key names and then delete them all
599 wxArrayString astrSubkeys
;
603 bool bCont
= GetFirstKey(strKey
, lIndex
);
605 astrSubkeys
.Add(strKey
);
607 bCont
= GetNextKey(strKey
, lIndex
);
610 size_t nKeyCount
= astrSubkeys
.Count();
611 for ( size_t nKey
= 0; nKey
< nKeyCount
; nKey
++ ) {
612 wxRegKey
key(*this, astrSubkeys
[nKey
]);
613 if ( !key
.DeleteSelf() )
617 // now delete this key itself
620 m_dwLastError
= RegDeleteKey((HKEY
) m_hRootKey
, m_strKey
);
621 // deleting a key which doesn't exist is not considered an error
622 if ( m_dwLastError
!= ERROR_SUCCESS
&&
623 m_dwLastError
!= ERROR_FILE_NOT_FOUND
) {
624 wxLogSysError(m_dwLastError
, _("Can't delete key '%s'"),
632 bool wxRegKey::DeleteKey(const wxChar
*szKey
)
637 wxRegKey
key(*this, szKey
);
638 return key
.DeleteSelf();
641 bool wxRegKey::DeleteValue(const wxChar
*szValue
)
646 m_dwLastError
= RegDeleteValue((HKEY
) m_hKey
, WXSTRINGCAST szValue
);
648 // deleting a value which doesn't exist is not considered an error
649 if ( (m_dwLastError
!= ERROR_SUCCESS
) &&
650 (m_dwLastError
!= ERROR_FILE_NOT_FOUND
) ) {
651 wxLogSysError(m_dwLastError
, _("Can't delete value '%s' from key '%s'"),
652 szValue
, GetName().c_str());
659 // ----------------------------------------------------------------------------
660 // access to values and subkeys
661 // ----------------------------------------------------------------------------
663 // return true if value exists
664 bool wxRegKey::HasValue(const wxChar
*szValue
) const
666 // this function should be silent, so suppress possible messages from Open()
669 if ( !CONST_CAST
Open() )
672 LONG dwRet
= ::RegQueryValueEx((HKEY
) m_hKey
,
673 WXSTRINGCAST szValue
,
676 return dwRet
== ERROR_SUCCESS
;
679 // returns true if this key has any values
680 bool wxRegKey::HasValues() const
682 // suppress possible messages from GetFirstValue()
685 // just call GetFirstValue with dummy parameters
688 return CONST_CAST
GetFirstValue(str
, l
);
691 // returns true if this key has any subkeys
692 bool wxRegKey::HasSubkeys() const
694 // suppress possible messages from GetFirstKey()
697 // just call GetFirstKey with dummy parameters
700 return CONST_CAST
GetFirstKey(str
, l
);
703 // returns true if given subkey exists
704 bool wxRegKey::HasSubKey(const wxChar
*szKey
) const
706 // this function should be silent, so suppress possible messages from Open()
709 if ( !CONST_CAST
Open() )
712 return KeyExists(m_hKey
, szKey
);
715 wxRegKey::ValueType
wxRegKey::GetValueType(const wxChar
*szValue
) const
717 if ( ! CONST_CAST
Open() )
721 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
722 &dwType
, NULL
, NULL
);
723 if ( m_dwLastError
!= ERROR_SUCCESS
) {
724 wxLogSysError(m_dwLastError
, _("Can't read value of key '%s'"),
729 return (ValueType
)dwType
;
732 bool wxRegKey::QueryValue(const wxChar
*szValue
,
736 if ( CONST_CAST
Open() ) {
738 // first get the type and size of the data
739 DWORD dwType
, dwSize
;
740 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, WXSTRINGCAST szValue
, RESERVED
,
741 &dwType
, NULL
, &dwSize
);
742 if ( m_dwLastError
== ERROR_SUCCESS
) {
744 // must treat this case specially as GetWriteBuf() doesn't like
745 // being called with 0 size
749 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
,
750 WXSTRINGCAST szValue
,
753 (RegString
)(wxChar
*)wxStringBuffer(strValue
, dwSize
),
756 // expand the var expansions in the string unless disabled
758 if ( (dwType
== REG_EXPAND_SZ
) && !raw
)
760 DWORD dwExpSize
= ::ExpandEnvironmentStrings(strValue
, NULL
, 0);
761 bool ok
= dwExpSize
!= 0;
764 wxString strExpValue
;
765 ok
= ::ExpandEnvironmentStrings
768 wxStringBuffer(strExpValue
, dwExpSize
),
771 strValue
= strExpValue
;
776 wxLogLastError(_T("ExpandEnvironmentStrings"));
783 if ( m_dwLastError
== ERROR_SUCCESS
) {
784 // check that it was the right type
785 wxASSERT_MSG( !IsNumericValue(szValue
),
786 wxT("Type mismatch in wxRegKey::QueryValue().") );
793 wxLogSysError(m_dwLastError
, _("Can't read value of '%s'"),
794 GetFullName(this, szValue
));
798 bool wxRegKey::SetValue(const wxChar
*szValue
, const wxString
& strValue
)
800 if ( CONST_CAST
Open() ) {
801 m_dwLastError
= RegSetValueEx((HKEY
) m_hKey
, szValue
, (DWORD
) RESERVED
, REG_SZ
,
802 (RegString
)strValue
.c_str(),
803 (strValue
.Len() + 1)*sizeof(wxChar
));
804 if ( m_dwLastError
== ERROR_SUCCESS
)
808 wxLogSysError(m_dwLastError
, _("Can't set value of '%s'"),
809 GetFullName(this, szValue
));
813 wxString
wxRegKey::QueryDefaultValue() const
816 QueryValue(NULL
, str
);
820 // ----------------------------------------------------------------------------
822 // NB: all these functions require an index variable which allows to have
823 // several concurrently running indexations on the same key
824 // ----------------------------------------------------------------------------
826 bool wxRegKey::GetFirstValue(wxString
& strValueName
, long& lIndex
)
832 return GetNextValue(strValueName
, lIndex
);
835 bool wxRegKey::GetNextValue(wxString
& strValueName
, long& lIndex
) const
837 wxASSERT( IsOpened() );
839 // are we already at the end of enumeration?
843 wxChar szValueName
[1024]; // @@ use RegQueryInfoKey...
844 DWORD dwValueLen
= WXSIZEOF(szValueName
);
846 m_dwLastError
= RegEnumValue((HKEY
) m_hKey
, lIndex
++,
847 szValueName
, &dwValueLen
,
850 NULL
, // [out] buffer for value
851 NULL
); // [i/o] it's length
853 if ( m_dwLastError
!= ERROR_SUCCESS
) {
854 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
855 m_dwLastError
= ERROR_SUCCESS
;
859 wxLogSysError(m_dwLastError
, _("Can't enumerate values of key '%s'"),
866 strValueName
= szValueName
;
871 bool wxRegKey::GetFirstKey(wxString
& strKeyName
, long& lIndex
)
877 return GetNextKey(strKeyName
, lIndex
);
880 bool wxRegKey::GetNextKey(wxString
& strKeyName
, long& lIndex
) const
882 wxASSERT( IsOpened() );
884 // are we already at the end of enumeration?
888 wxChar szKeyName
[_MAX_PATH
+ 1];
891 DWORD sizeName
= WXSIZEOF(szKeyName
);
892 m_dwLastError
= RegEnumKeyEx((HKEY
) m_hKey
, lIndex
++, szKeyName
, & sizeName
,
893 0, NULL
, NULL
, NULL
);
895 m_dwLastError
= RegEnumKey((HKEY
) m_hKey
, lIndex
++, szKeyName
, WXSIZEOF(szKeyName
));
898 if ( m_dwLastError
!= ERROR_SUCCESS
) {
899 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
900 m_dwLastError
= ERROR_SUCCESS
;
904 wxLogSysError(m_dwLastError
, _("Can't enumerate subkeys of key '%s'"),
911 strKeyName
= szKeyName
;
915 // returns true if the value contains a number (else it's some string)
916 bool wxRegKey::IsNumericValue(const wxChar
*szValue
) const
918 ValueType type
= GetValueType(szValue
);
921 /* case Type_Dword_little_endian: == Type_Dword */
922 case Type_Dword_big_endian
:
930 // ============================================================================
931 // implementation of global private functions
932 // ============================================================================
934 bool KeyExists(WXHKEY hRootKey
, const wxChar
*szKey
)
936 // don't close this key itself for the case of empty szKey!
937 if ( wxIsEmpty(szKey
) )
946 KEY_READ
, // we might not have enough rights for rw access
950 ::RegCloseKey(hkeyDummy
);
958 const wxChar
*GetFullName(const wxRegKey
*pKey
, const wxChar
*szValue
)
960 static wxString s_str
;
961 s_str
= pKey
->GetName();
962 if ( !wxIsEmpty(szValue
) )
963 s_str
<< wxT("\\") << szValue
;
965 return s_str
.c_str();
968 void RemoveTrailingSeparator(wxString
& str
)
970 if ( !str
.empty() && str
.Last() == REG_SEPARATOR
)
971 str
.Truncate(str
.Len() - 1);