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 license
10 // TODO: - parsing of registry key names
11 // - support of other (than REG_SZ/REG_DWORD) registry types
12 // - add high level functions (RegisterOleServer, ...)
13 ///////////////////////////////////////////////////////////////////////////////
16 #pragma implementation "registry.h"
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 // other wxWindows headers
27 #include "wx/string.h"
31 #include "wx/config.h" // for wxExpandEnvVars
36 #define WIN32_LEAN_AND_MEAN
42 #include <stdlib.h> // for _MAX_PATH
49 #define HKEY_DEFINED // already defined in windows.h
50 #include "wx/msw/registry.h"
52 // some registry functions don't like signed chars
53 typedef unsigned char *RegString
;
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // the standard key names, short names and handles all bundled together for
65 const char *szShortName
;
69 { HKEY_CLASSES_ROOT
, "HKEY_CLASSES_ROOT", "HKCR" },
71 { HKEY_CURRENT_USER
, "HKEY_CURRENT_USER", "HKCU" },
72 { HKEY_LOCAL_MACHINE
, "HKEY_LOCAL_MACHINE", "HKLM" },
73 { HKEY_USERS
, "HKEY_USERS", "HKU" }, // short name?
74 { HKEY_PERFORMANCE_DATA
, "HKEY_PERFORMANCE_DATA", "HKPD" },
76 { HKEY_CURRENT_CONFIG
, "HKEY_CURRENT_CONFIG", "HKCC" },
78 { HKEY_DYN_DATA
, "HKEY_DYN_DATA", "HKDD" }, // short name?
80 #endif //WINVER >= 4.0
84 // the registry name separator (perhaps one day MS will change it to '/' ;-)
85 #define REG_SEPARATOR '\\'
87 // useful for Windows programmers: makes somewhat more clear all these zeroes
88 // being passed to Windows APIs
89 #define RESERVED (NULL)
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
94 // @ const_cast<> is not yet supported by all compilers
95 #define CONST_CAST ((wxRegKey *)this)->
98 #define m_dwLastError CONST_CAST m_dwLastError
101 // ----------------------------------------------------------------------------
102 // non member functions
103 // ----------------------------------------------------------------------------
105 // removes the trailing backslash from the string if it has one
106 static inline void RemoveTrailingSeparator(wxString
& str
);
108 // returns TRUE if given registry key exists
109 static bool KeyExists(WXHKEY hRootKey
, const char *szKey
);
111 // combines value and key name (uses static buffer!)
112 static const char *GetFullName(const wxRegKey
*pKey
,
113 const char *szValue
= NULL
);
115 // ============================================================================
116 // implementation of wxRegKey class
117 // ============================================================================
119 // ----------------------------------------------------------------------------
120 // static functions and variables
121 // ----------------------------------------------------------------------------
123 const size_t wxRegKey::nStdKeys
= WXSIZEOF(aStdKeys
);
125 // @@ should take a `StdKey key', but as it's often going to be used in loops
126 // it would require casts in user code.
127 const char *wxRegKey::GetStdKeyName(size_t key
)
129 // return empty string if key is invalid
130 wxCHECK_MSG( key
< nStdKeys
, "", "invalid key in wxRegKey::GetStdKeyName" );
132 return aStdKeys
[key
].szName
;
135 const char *wxRegKey::GetStdKeyShortName(size_t key
)
137 // return empty string if key is invalid
138 wxCHECK( key
< nStdKeys
, "" );
140 return aStdKeys
[key
].szShortName
;
143 wxRegKey::StdKey
wxRegKey::ExtractKeyName(wxString
& strKey
)
145 wxString strRoot
= strKey
.Left(REG_SEPARATOR
);
149 for ( ui
= 0; ui
< nStdKeys
; ui
++ ) {
150 if ( strRoot
.CmpNoCase(aStdKeys
[ui
].szName
) == 0 ||
151 strRoot
.CmpNoCase(aStdKeys
[ui
].szShortName
) == 0 ) {
152 hRootKey
= aStdKeys
[ui
].hkey
;
157 if ( ui
== nStdKeys
) {
158 wxFAIL_MSG("invalid key prefix in wxRegKey::ExtractKeyName.");
160 hRootKey
= HKEY_CLASSES_ROOT
;
163 strKey
= strKey
.After(REG_SEPARATOR
);
164 if ( !strKey
.IsEmpty() && strKey
.Last() == REG_SEPARATOR
)
165 strKey
.Truncate(strKey
.Len() - 1);
168 return (wxRegKey::StdKey
)(int)hRootKey
;
171 wxRegKey::StdKey
wxRegKey::GetStdKeyFromHkey(WXHKEY hkey
)
173 for ( size_t ui
= 0; ui
< nStdKeys
; ui
++ ) {
174 if ( (int) aStdKeys
[ui
].hkey
== (int) hkey
)
178 wxFAIL_MSG("non root hkey passed to wxRegKey::GetStdKeyFromHkey.");
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
190 m_hRootKey
= (WXHKEY
) aStdKeys
[HKCR
].hkey
;
194 wxRegKey::wxRegKey(const wxString
& strKey
) : m_strKey(strKey
)
196 m_hRootKey
= (WXHKEY
) aStdKeys
[ExtractKeyName(m_strKey
)].hkey
;
197 m_hKey
= (WXHKEY
) NULL
;
201 // parent is a predefined (and preopened) key
202 wxRegKey::wxRegKey(StdKey keyParent
, const wxString
& strKey
) : m_strKey(strKey
)
204 RemoveTrailingSeparator(m_strKey
);
205 m_hRootKey
= (WXHKEY
) aStdKeys
[keyParent
].hkey
;
206 m_hKey
= (WXHKEY
) NULL
;
210 // parent is a normal regkey
211 wxRegKey::wxRegKey(const wxRegKey
& keyParent
, const wxString
& strKey
)
212 : m_strKey(keyParent
.m_strKey
)
214 // combine our name with parent's to get the full name
215 if ( !m_strKey
.IsEmpty() &&
216 (strKey
.IsEmpty() || strKey
[0] != REG_SEPARATOR
) ) {
217 m_strKey
+= REG_SEPARATOR
;
221 RemoveTrailingSeparator(m_strKey
);
223 m_hRootKey
= keyParent
.m_hRootKey
;
224 m_hKey
= (WXHKEY
) NULL
;
228 // dtor closes the key releasing system resource
229 wxRegKey::~wxRegKey()
234 // ----------------------------------------------------------------------------
235 // change the key name/hkey
236 // ----------------------------------------------------------------------------
238 // set the full key name
239 void wxRegKey::SetName(const wxString
& strKey
)
244 m_hRootKey
= (WXHKEY
) aStdKeys
[ExtractKeyName(m_strKey
)].hkey
;
247 // the name is relative to the parent key
248 void wxRegKey::SetName(StdKey keyParent
, const wxString
& strKey
)
253 RemoveTrailingSeparator(m_strKey
);
254 m_hRootKey
= (WXHKEY
) aStdKeys
[keyParent
].hkey
;
257 // the name is relative to the parent key
258 void wxRegKey::SetName(const wxRegKey
& keyParent
, const wxString
& strKey
)
262 // combine our name with parent's to get the full name
263 m_strKey
= keyParent
.m_strKey
;
264 if ( !strKey
.IsEmpty() && strKey
[0] != REG_SEPARATOR
)
265 m_strKey
+= REG_SEPARATOR
;
268 RemoveTrailingSeparator(m_strKey
);
270 m_hRootKey
= keyParent
.m_hRootKey
;
273 // hKey should be opened and will be closed in wxRegKey dtor
274 void wxRegKey::SetHkey(WXHKEY hKey
)
281 // ----------------------------------------------------------------------------
282 // info about the key
283 // ----------------------------------------------------------------------------
285 // returns TRUE if the key exists
286 bool wxRegKey::Exists() const
288 // opened key has to exist, try to open it if not done yet
289 return IsOpened() ? TRUE
: KeyExists(m_hRootKey
, m_strKey
);
292 // returns the full name of the key (prefix is abbreviated if bShortPrefix)
293 wxString
wxRegKey::GetName(bool bShortPrefix
) const
295 StdKey key
= GetStdKeyFromHkey((StdKey
) m_hRootKey
);
296 wxString str
= bShortPrefix
? aStdKeys
[key
].szShortName
297 : aStdKeys
[key
].szName
;
298 if ( !m_strKey
.IsEmpty() )
299 str
<< "\\" << m_strKey
;
305 bool wxRegKey::GetKeyInfo(size_t* pnSubKeys
,
308 size_t* pnMaxValueLen
) const
310 bool wxRegKey::GetKeyInfo(ulong
*pnSubKeys
,
313 ulong
*pnMaxValueLen
) const
317 m_dwLastError
= ::RegQueryInfoKey
321 NULL
, // (ptr to) size of class name buffer
323 pnSubKeys
, // [out] number of subkeys
324 pnMaxKeyLen
, // [out] max length of a subkey name
325 NULL
, // longest subkey class name
326 pnValues
, // [out] number of values
327 pnMaxValueLen
, // [out] max length of a value name
328 NULL
, // longest value data
329 NULL
, // security descriptor
330 NULL
// time of last modification
333 if ( m_dwLastError
!= ERROR_SUCCESS
) {
334 wxLogSysError(m_dwLastError
, _("can't get info about registry key '%s'"),
341 wxFAIL_MSG("GetKeyInfo() not implemented");
347 // ----------------------------------------------------------------------------
349 // ----------------------------------------------------------------------------
351 // opens key (it's not an error to call Open() on an already opened key)
352 bool wxRegKey::Open()
358 m_dwLastError
= RegOpenKey((HKEY
) m_hRootKey
, m_strKey
, &tmpKey
);
359 if ( m_dwLastError
!= ERROR_SUCCESS
) {
360 wxLogSysError(m_dwLastError
, _("can't open registry key '%s'"),
366 m_hKey
= (WXHKEY
) tmpKey
;
371 // creates key, failing if it exists and !bOkIfExists
372 bool wxRegKey::Create(bool bOkIfExists
)
374 // check for existence only if asked (i.e. order is important!)
375 if ( !bOkIfExists
&& Exists() ) {
383 m_dwLastError
= RegCreateKey((HKEY
) m_hRootKey
, m_strKey
, &tmpKey
);
384 if ( m_dwLastError
!= ERROR_SUCCESS
) {
385 wxLogSysError(m_dwLastError
, _("can't create registry key '%s'"),
391 m_hKey
= (WXHKEY
) tmpKey
;
396 // close the key, it's not an error to call it when not opened
397 bool wxRegKey::Close()
400 m_dwLastError
= RegCloseKey((HKEY
) m_hKey
);
401 if ( m_dwLastError
!= ERROR_SUCCESS
) {
402 wxLogSysError(m_dwLastError
, _("can't close registry key '%s'"),
416 // ----------------------------------------------------------------------------
417 // delete keys/values
418 // ----------------------------------------------------------------------------
419 bool wxRegKey::DeleteSelf()
424 // it already doesn't exist - ok!
429 // we can't delete keys while enumerating because it confuses GetNextKey, so
430 // we first save the key names and then delete them all
431 wxArrayString astrSubkeys
;
435 bool bCont
= GetFirstKey(strKey
, lIndex
);
437 astrSubkeys
.Add(strKey
);
439 bCont
= GetNextKey(strKey
, lIndex
);
442 size_t nKeyCount
= astrSubkeys
.Count();
443 for ( size_t nKey
= 0; nKey
< nKeyCount
; nKey
++ ) {
444 wxRegKey
key(*this, astrSubkeys
[nKey
]);
445 if ( !key
.DeleteSelf() )
449 // now delete this key itself
452 m_dwLastError
= RegDeleteKey((HKEY
) m_hRootKey
, m_strKey
);
453 if ( m_dwLastError
!= ERROR_SUCCESS
) {
454 wxLogSysError(m_dwLastError
, _("can't delete key '%s'"),
462 bool wxRegKey::DeleteKey(const char *szKey
)
467 wxRegKey
key(*this, szKey
);
468 return key
.DeleteSelf();
471 bool wxRegKey::DeleteValue(const char *szValue
)
477 m_dwLastError
= RegDeleteValue((HKEY
) m_hKey
, szValue
);
478 if ( m_dwLastError
!= ERROR_SUCCESS
) {
479 wxLogSysError(m_dwLastError
, _("can't delete value '%s' from key '%s'"),
480 szValue
, GetName().c_str());
484 // named registry values don't exist in Win16 world
485 wxASSERT( IsEmpty(szValue
) );
487 // just set the (default and unique) value of the key to ""
488 m_dwLastError
= RegSetValue((HKEY
) m_hKey
, NULL
, REG_SZ
, "", RESERVED
);
489 if ( m_dwLastError
!= ERROR_SUCCESS
) {
490 wxLogSysError(m_dwLastError
, _("can't delete value of key '%s'"),
499 // ----------------------------------------------------------------------------
500 // access to values and subkeys
501 // ----------------------------------------------------------------------------
503 // return TRUE if value exists
504 bool wxRegKey::HasValue(const char *szValue
) const
506 // this function should be silent, so suppress possible messages from Open()
510 if ( CONST_CAST
Open() ) {
511 return RegQueryValueEx((HKEY
) m_hKey
, szValue
, RESERVED
,
512 NULL
, NULL
, NULL
) == ERROR_SUCCESS
;
517 // only unnamed value exists
518 return IsEmpty(szValue
);
522 // returns TRUE if this key has any subkeys
523 bool wxRegKey::HasSubkeys() const
525 // just call GetFirstKey with dummy parameters
528 return CONST_CAST
GetFirstKey(str
, l
);
531 // returns TRUE if given subkey exists
532 bool wxRegKey::HasSubKey(const char *szKey
) const
534 if ( CONST_CAST
Open() )
535 return KeyExists(m_hKey
, szKey
);
540 wxRegKey::ValueType
wxRegKey::GetValueType(const char *szValue
)
547 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, szValue
, RESERVED
,
548 &dwType
, NULL
, NULL
);
549 if ( m_dwLastError
!= ERROR_SUCCESS
) {
550 wxLogSysError(m_dwLastError
, _("can't read value of key '%s'"),
555 return (ValueType
)dwType
;
557 return IsEmpty(szValue
) ? Type_String
: Type_None
;
562 bool wxRegKey::SetValue(const char *szValue
, long lValue
)
564 if ( CONST_CAST
Open() ) {
565 m_dwLastError
= RegSetValueEx((HKEY
) m_hKey
, szValue
, (DWORD
) RESERVED
, REG_DWORD
,
566 (RegString
)&lValue
, sizeof(lValue
));
567 if ( m_dwLastError
== ERROR_SUCCESS
)
571 wxLogSysError(m_dwLastError
, _("can't set value of '%s'"),
572 GetFullName(this, szValue
));
576 bool wxRegKey::QueryValue(const char *szValue
, long *plValue
) const
578 if ( CONST_CAST
Open() ) {
579 DWORD dwType
, dwSize
= sizeof(DWORD
);
580 RegString pBuf
= (RegString
)plValue
;
581 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, szValue
, RESERVED
,
582 &dwType
, pBuf
, &dwSize
);
583 if ( m_dwLastError
!= ERROR_SUCCESS
) {
584 wxLogSysError(m_dwLastError
, _("can't read value of key '%s'"),
589 // check that we read the value of right type
590 wxASSERT_MSG( dwType
== REG_DWORD
,
591 "Type mismatch in wxRegKey::QueryValue()." );
602 bool wxRegKey::QueryValue(const char *szValue
, wxString
& strValue
) const
604 if ( CONST_CAST
Open() ) {
606 // first get the type and size of the data
607 DWORD dwType
, dwSize
;
608 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, szValue
, RESERVED
,
609 &dwType
, NULL
, &dwSize
);
610 if ( m_dwLastError
== ERROR_SUCCESS
) {
611 RegString pBuf
= (RegString
)strValue
.GetWriteBuf(dwSize
);
612 m_dwLastError
= RegQueryValueEx((HKEY
) m_hKey
, szValue
, RESERVED
,
613 &dwType
, pBuf
, &dwSize
);
614 strValue
.UngetWriteBuf();
615 if ( m_dwLastError
== ERROR_SUCCESS
) {
616 // check that it was the right type
617 wxASSERT_MSG( dwType
== REG_SZ
,
618 "Type mismatch in wxRegKey::QueryValue()." );
624 // named registry values don't exist in Win16
625 wxASSERT( IsEmpty(szValue
) );
627 m_dwLastError
= RegQueryValue((HKEY
) m_hKey
, 0, strValue
.GetWriteBuf(256), &l
);
628 strValue
.UngetWriteBuf();
629 if ( m_dwLastError
== ERROR_SUCCESS
)
634 wxLogSysError(m_dwLastError
, _("can't read value of '%s'"),
635 GetFullName(this, szValue
));
639 bool wxRegKey::SetValue(const char *szValue
, const wxString
& strValue
)
641 if ( CONST_CAST
Open() ) {
643 m_dwLastError
= RegSetValueEx((HKEY
) m_hKey
, szValue
, (DWORD
) RESERVED
, REG_SZ
,
644 (RegString
)strValue
.c_str(),
646 if ( m_dwLastError
== ERROR_SUCCESS
)
649 // named registry values don't exist in Win16
650 wxASSERT( IsEmpty(szValue
) );
652 m_dwLastError
= RegSetValue((HKEY
) m_hKey
, NULL
, REG_SZ
, strValue
, NULL
);
653 if ( m_dwLastError
== ERROR_SUCCESS
)
658 wxLogSysError(m_dwLastError
, _("can't set value of '%s'"),
659 GetFullName(this, szValue
));
663 wxRegKey::operator wxString() const
666 QueryValue(NULL
, str
);
670 // ----------------------------------------------------------------------------
672 // NB: all these functions require an index variable which allows to have
673 // several concurrently running indexations on the same key
674 // ----------------------------------------------------------------------------
676 bool wxRegKey::GetFirstValue(wxString
& strValueName
, long& lIndex
)
682 return GetNextValue(strValueName
, lIndex
);
685 bool wxRegKey::GetNextValue(wxString
& strValueName
, long& lIndex
) const
687 wxASSERT( IsOpened() );
689 // are we already at the end of enumeration?
694 char szValueName
[1024]; // @@ use RegQueryInfoKey...
695 DWORD dwValueLen
= WXSIZEOF(szValueName
);
698 m_dwLastError
= RegEnumValue((HKEY
) m_hKey
, lIndex
,
699 szValueName
, &dwValueLen
,
702 NULL
, // [out] buffer for value
703 NULL
); // [i/o] it's length
705 if ( m_dwLastError
!= ERROR_SUCCESS
) {
706 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
707 m_dwLastError
= ERROR_SUCCESS
;
711 wxLogSysError(m_dwLastError
, _("can't enumerate values of key '%s'"),
718 strValueName
= szValueName
;
720 // only one unnamed value
721 wxASSERT( lIndex
== 0 );
724 strValueName
.Empty();
730 bool wxRegKey::GetFirstKey(wxString
& strKeyName
, long& lIndex
)
736 return GetNextKey(strKeyName
, lIndex
);
739 bool wxRegKey::GetNextKey(wxString
& strKeyName
, long& lIndex
) const
741 wxASSERT( IsOpened() );
743 // are we already at the end of enumeration?
747 char szKeyName
[_MAX_PATH
+ 1];
748 m_dwLastError
= RegEnumKey((HKEY
) m_hKey
, lIndex
++, szKeyName
, WXSIZEOF(szKeyName
));
750 if ( m_dwLastError
!= ERROR_SUCCESS
) {
751 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
752 m_dwLastError
= ERROR_SUCCESS
;
756 wxLogSysError(m_dwLastError
, _("can't enumerate subkeys of key '%s'"),
763 strKeyName
= szKeyName
;
767 // ============================================================================
768 // implementation of global private functions
769 // ============================================================================
770 bool KeyExists(WXHKEY hRootKey
, const char *szKey
)
773 if ( RegOpenKey( (HKEY
) hRootKey
, szKey
, &hkeyDummy
) == ERROR_SUCCESS
) {
774 RegCloseKey(hkeyDummy
);
781 const char *GetFullName(const wxRegKey
*pKey
, const char *szValue
)
783 static wxString s_str
;
784 s_str
= pKey
->GetName();
785 if ( !IsEmpty(szValue
) )
786 s_str
<< "\\" << szValue
;
788 return s_str
.c_str();
791 void RemoveTrailingSeparator(wxString
& str
)
793 if ( !str
.IsEmpty() && str
.Last() == REG_SEPARATOR
)
794 str
.Truncate(str
.Len() - 1);