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 ///////////////////////////////////////////////////////////////////////////////
15 // ============================================================================
17 // ============================================================================
19 // ----------------------------------------------------------------------------
21 // ----------------------------------------------------------------------------
23 // for compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
30 // other wxWindows headers
31 #include "wx/string.h"
38 #define WIN32_LEAN_AND_MEAN
44 #include <stdlib.h> // for _MAX_PATH
51 #define HKEY_DEFINED // already defined in windows.h
52 #include "wx/msw/registry.h"
54 // some registry functions don't like signed chars
55 typedef unsigned char *RegString
;
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // the standard key names, short names and handles all bundled together for
67 const char *szShortName
;
71 { HKEY_CLASSES_ROOT
, "HKEY_CLASSES_ROOT", "HKCR" },
73 { HKEY_CURRENT_USER
, "HKEY_CURRENT_USER", "HKCU" },
74 { HKEY_LOCAL_MACHINE
, "HKEY_LOCAL_MACHINE", "HKLM" },
75 { HKEY_USERS
, "HKEY_USERS", "HKU" }, // short name?
76 { HKEY_PERFORMANCE_DATA
, "HKEY_PERFORMANCE_DATA", "HKPD" },
78 { HKEY_CURRENT_CONFIG
, "HKEY_CURRENT_CONFIG", "HKCC" },
80 { HKEY_DYN_DATA
, "HKEY_DYN_DATA", "HKDD" }, // short name?
82 #endif //WINVER >= 4.0
86 // the registry name separator (perhaps one day MS will change it to '/' ;-)
87 #define REG_SEPARATOR '\\'
89 // useful for Windows programmers: makes somewhat more clear all these zeroes
90 // being passed to Windows APIs
91 #define RESERVED (NULL)
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
96 // @ const_cast<> is not yet supported by all compilers
97 #define CONST_CAST ((wxRegKey *)this)->
100 #define m_dwLastError CONST_CAST m_dwLastError
103 // ----------------------------------------------------------------------------
104 // non member functions
105 // ----------------------------------------------------------------------------
107 // removes the trailing backslash from the string if it has one
108 static inline void RemoveTrailingSeparator(wxString
& str
);
110 // returns TRUE if given registry key exists
111 static bool KeyExists(HKEY hRootKey
, const char *szKey
);
113 // combines value and key name (uses static buffer!)
114 static const char *GetFullName(const wxRegKey
*pKey
,
115 const char *szValue
= NULL
);
117 // ============================================================================
118 // implementation of wxRegKey class
119 // ============================================================================
121 // ----------------------------------------------------------------------------
122 // static functions and variables
123 // ----------------------------------------------------------------------------
125 const size_t wxRegKey::nStdKeys
= WXSIZEOF(aStdKeys
);
127 // @@ should take a `StdKey key', but as it's often going to be used in loops
128 // it would require casts in user code.
129 const char *wxRegKey::GetStdKeyName(size_t key
)
131 // return empty string if key is invalid
132 wxCHECK_MSG( key
< nStdKeys
, "", "invalid key in wxRegKey::GetStdKeyName" );
134 return aStdKeys
[key
].szName
;
137 const char *wxRegKey::GetStdKeyShortName(size_t key
)
139 // return empty string if key is invalid
140 wxCHECK( key
< nStdKeys
, "" );
142 return aStdKeys
[key
].szShortName
;
145 wxRegKey::StdKey
wxRegKey::ExtractKeyName(wxString
& strKey
)
147 wxString strRoot
= strKey
.Left(REG_SEPARATOR
);
151 for ( ui
= 0; ui
< nStdKeys
; ui
++ ) {
152 if ( strRoot
.CmpNoCase(aStdKeys
[ui
].szName
) == 0 ||
153 strRoot
.CmpNoCase(aStdKeys
[ui
].szShortName
) == 0 ) {
154 hRootKey
= aStdKeys
[ui
].hkey
;
159 if ( ui
== nStdKeys
) {
160 wxFAIL_MSG("invalid key prefix in wxRegKey::ExtractKeyName.");
162 hRootKey
= HKEY_CLASSES_ROOT
;
165 strKey
= strKey
.After(REG_SEPARATOR
);
166 if ( !strKey
.IsEmpty() && strKey
.Last() == REG_SEPARATOR
)
167 strKey
.Truncate(strKey
.Len() - 1);
170 return (wxRegKey::StdKey
)(int)hRootKey
;
173 wxRegKey::StdKey
wxRegKey::GetStdKeyFromHkey(HKEY hkey
)
175 for ( size_t ui
= 0; ui
< nStdKeys
; ui
++ ) {
176 if ( aStdKeys
[ui
].hkey
== hkey
)
180 wxFAIL_MSG("non root hkey passed to wxRegKey::GetStdKeyFromHkey.");
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
192 m_hRootKey
= aStdKeys
[HKCR
].hkey
;
196 wxRegKey::wxRegKey(const wxString
& strKey
) : m_strKey(strKey
)
198 m_hRootKey
= aStdKeys
[ExtractKeyName(m_strKey
)].hkey
;
203 // parent is a predefined (and preopened) key
204 wxRegKey::wxRegKey(StdKey keyParent
, const wxString
& strKey
) : m_strKey(strKey
)
206 RemoveTrailingSeparator(m_strKey
);
207 m_hRootKey
= aStdKeys
[keyParent
].hkey
;
212 // parent is a normal regkey
213 wxRegKey::wxRegKey(const wxRegKey
& keyParent
, const wxString
& strKey
)
214 : m_strKey(keyParent
.m_strKey
)
216 // combine our name with parent's to get the full name
217 if ( !m_strKey
.IsEmpty() && strKey
[0] != REG_SEPARATOR
)
218 m_strKey
+= REG_SEPARATOR
;
221 RemoveTrailingSeparator(m_strKey
);
223 m_hRootKey
= keyParent
.m_hRootKey
;
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
= 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
= 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(HKEY 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(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()
357 m_dwLastError
= RegOpenKey(m_hRootKey
, m_strKey
, &m_hKey
);
358 if ( m_dwLastError
!= ERROR_SUCCESS
) {
359 wxLogSysError(m_dwLastError
, _("can't open registry key '%s'"),
367 // creates key, failing if it exists and !bOkIfExists
368 bool wxRegKey::Create(bool bOkIfExists
)
370 // check for existence only if asked (i.e. order is important!)
371 if ( !bOkIfExists
&& Exists() ) {
378 m_dwLastError
= RegCreateKey(m_hRootKey
, m_strKey
, &m_hKey
);
379 if ( m_dwLastError
!= ERROR_SUCCESS
) {
380 wxLogSysError(m_dwLastError
, _("can't create registry key '%s'"),
388 // close the key, it's not an error to call it when not opened
389 bool wxRegKey::Close()
392 m_dwLastError
= RegCloseKey(m_hKey
);
393 if ( m_dwLastError
!= ERROR_SUCCESS
) {
394 wxLogSysError(m_dwLastError
, _("can't close registry key '%s'"),
408 // ----------------------------------------------------------------------------
409 // delete keys/values
410 // ----------------------------------------------------------------------------
411 bool wxRegKey::DeleteSelf()
416 // it already doesn't exist - ok!
421 // we can't delete keys while enumerating because it confuses GetNextKey, so
422 // we first save the key names and then delete them all
423 wxArrayString astrSubkeys
;
427 bool bCont
= GetFirstKey(strKey
, lIndex
);
429 astrSubkeys
.Add(strKey
);
431 bCont
= GetNextKey(strKey
, lIndex
);
434 size_t nKeyCount
= astrSubkeys
.Count();
435 for ( size_t nKey
= 0; nKey
< nKeyCount
; nKey
++ ) {
436 wxRegKey
key(*this, astrSubkeys
[nKey
]);
437 if ( !key
.DeleteSelf() )
441 // now delete this key itself
444 m_dwLastError
= RegDeleteKey(m_hRootKey
, m_strKey
);
445 if ( m_dwLastError
!= ERROR_SUCCESS
) {
446 wxLogSysError(m_dwLastError
, _("can't delete key '%s'"),
454 bool wxRegKey::DeleteKey(const char *szKey
)
459 wxRegKey
key(*this, szKey
);
460 return key
.DeleteSelf();
463 bool wxRegKey::DeleteValue(const char *szValue
)
469 m_dwLastError
= RegDeleteValue(m_hKey
, szValue
);
470 if ( m_dwLastError
!= ERROR_SUCCESS
) {
471 wxLogSysError(m_dwLastError
, _("can't delete value '%s' from key '%s'"),
472 szValue
, GetName().c_str());
476 // named registry values don't exist in Win16 world
477 wxASSERT( IsEmpty(szValue
) );
479 // just set the (default and unique) value of the key to ""
480 m_dwLastError
= RegSetValue(m_hKey
, NULL
, REG_SZ
, "", RESERVED
);
481 if ( m_dwLastError
!= ERROR_SUCCESS
) {
482 wxLogSysError(m_dwLastError
, _("can't delete value of key '%s'"),
491 // ----------------------------------------------------------------------------
492 // access to values and subkeys
493 // ----------------------------------------------------------------------------
495 // return TRUE if value exists
496 bool wxRegKey::HasValue(const char *szValue
) const
499 if ( CONST_CAST
Open() ) {
500 return RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
501 NULL
, NULL
, NULL
) == ERROR_SUCCESS
;
506 // only unnamed value exists
507 return IsEmpty(szValue
);
511 // returns TRUE if this key has any subkeys
512 bool wxRegKey::HasSubkeys() const
514 // just call GetFirstKey with dummy parameters
517 return CONST_CAST
GetFirstKey(str
, l
);
520 // returns TRUE if given subkey exists
521 bool wxRegKey::HasSubKey(const char *szKey
) const
523 if ( CONST_CAST
Open() )
524 return KeyExists(m_hKey
, szKey
);
529 wxRegKey::ValueType
wxRegKey::GetValueType(const char *szValue
)
536 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
537 &dwType
, NULL
, NULL
);
538 if ( m_dwLastError
!= ERROR_SUCCESS
) {
539 wxLogSysError(m_dwLastError
, _("can't read value of key '%s'"),
544 return (ValueType
)dwType
;
546 return IsEmpty(szValue
) ? Type_String
: Type_None
;
551 bool wxRegKey::SetValue(const char *szValue
, long lValue
)
553 if ( CONST_CAST
Open() ) {
554 m_dwLastError
= RegSetValueEx(m_hKey
, szValue
, RESERVED
, REG_DWORD
,
555 (RegString
)&lValue
, sizeof(lValue
));
556 if ( m_dwLastError
== ERROR_SUCCESS
)
560 wxLogSysError(m_dwLastError
, _("can't set value of '%s'"),
561 GetFullName(this, szValue
));
565 bool wxRegKey::QueryValue(const char *szValue
, long *plValue
) const
567 if ( CONST_CAST
Open() ) {
568 DWORD dwType
, dwSize
= sizeof(DWORD
);
569 RegString pBuf
= (RegString
)plValue
;
570 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
571 &dwType
, pBuf
, &dwSize
);
572 if ( m_dwLastError
!= ERROR_SUCCESS
) {
573 wxLogSysError(m_dwLastError
, _("can't read value of key '%s'"),
578 // check that we read the value of right type
579 wxASSERT_MSG( dwType
== REG_DWORD
,
580 "Type mismatch in wxRegKey::QueryValue()." );
591 bool wxRegKey::QueryValue(const char *szValue
, wxString
& strValue
) const
593 if ( CONST_CAST
Open() ) {
595 // first get the type and size of the data
596 DWORD dwType
, dwSize
;
597 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
598 &dwType
, NULL
, &dwSize
);
599 if ( m_dwLastError
== ERROR_SUCCESS
) {
600 RegString pBuf
= (RegString
)strValue
.GetWriteBuf(dwSize
);
601 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
602 &dwType
, pBuf
, &dwSize
);
603 strValue
.UngetWriteBuf();
604 if ( m_dwLastError
== ERROR_SUCCESS
) {
605 // check that it was the right type
606 wxASSERT_MSG( dwType
== REG_SZ
,
607 "Type mismatch in wxRegKey::QueryValue()." );
613 // named registry values don't exist in Win16
614 wxASSERT( IsEmpty(szValue
) );
616 m_dwLastError
= RegQueryValue(m_hKey
, 0, strValue
.GetWriteBuf(256), &l
);
617 strValue
.UngetWriteBuf();
618 if ( m_dwLastError
== ERROR_SUCCESS
)
623 wxLogSysError(m_dwLastError
, _("can't read value of '%s'"),
624 GetFullName(this, szValue
));
628 bool wxRegKey::SetValue(const char *szValue
, const wxString
& strValue
)
630 if ( CONST_CAST
Open() ) {
632 m_dwLastError
= RegSetValueEx(m_hKey
, szValue
, RESERVED
, REG_SZ
,
633 (RegString
)strValue
.c_str(),
635 if ( m_dwLastError
== ERROR_SUCCESS
)
638 // named registry values don't exist in Win16
639 wxASSERT( IsEmpty(szValue
) );
641 m_dwLastError
= RegSetValue(m_hKey
, NULL
, REG_SZ
, strValue
, NULL
);
642 if ( m_dwLastError
== ERROR_SUCCESS
)
647 wxLogSysError(m_dwLastError
, _("can't set value of '%s'"),
648 GetFullName(this, szValue
));
652 wxRegKey::operator wxString() const
655 QueryValue(NULL
, str
);
659 // ----------------------------------------------------------------------------
661 // NB: all these functions require an index variable which allows to have
662 // several concurrently running indexations on the same key
663 // ----------------------------------------------------------------------------
665 bool wxRegKey::GetFirstValue(wxString
& strValueName
, long& lIndex
)
671 return GetNextValue(strValueName
, lIndex
);
674 bool wxRegKey::GetNextValue(wxString
& strValueName
, long& lIndex
) const
676 wxASSERT( IsOpened() );
678 // are we already at the end of enumeration?
683 char szValueName
[1024]; // @@ use RegQueryInfoKey...
684 DWORD dwValueLen
= WXSIZEOF(szValueName
);
687 m_dwLastError
= RegEnumValue(m_hKey
, lIndex
,
688 szValueName
, &dwValueLen
,
691 NULL
, // [out] buffer for value
692 NULL
); // [i/o] it's length
694 if ( m_dwLastError
!= ERROR_SUCCESS
) {
695 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
696 m_dwLastError
= ERROR_SUCCESS
;
700 wxLogSysError(m_dwLastError
, _("can't enumerate values of key '%s'"),
707 strValueName
= szValueName
;
709 // only one unnamed value
710 wxASSERT( lIndex
== 0 );
713 strValueName
.Empty();
719 bool wxRegKey::GetFirstKey(wxString
& strKeyName
, long& lIndex
)
725 return GetNextKey(strKeyName
, lIndex
);
728 bool wxRegKey::GetNextKey(wxString
& strKeyName
, long& lIndex
) const
730 wxASSERT( IsOpened() );
732 // are we already at the end of enumeration?
736 char szKeyName
[_MAX_PATH
+ 1];
737 m_dwLastError
= RegEnumKey(m_hKey
, lIndex
++, szKeyName
, WXSIZEOF(szKeyName
));
739 if ( m_dwLastError
!= ERROR_SUCCESS
) {
740 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
741 m_dwLastError
= ERROR_SUCCESS
;
745 wxLogSysError(m_dwLastError
, _("can't enumerate subkeys of key '%s'"),
752 strKeyName
= szKeyName
;
756 // ============================================================================
757 // implementation of global functions
758 // ============================================================================
759 bool KeyExists(HKEY hRootKey
, const char *szKey
)
762 if ( RegOpenKey(hRootKey
, szKey
, &hkeyDummy
) == ERROR_SUCCESS
) {
763 RegCloseKey(hkeyDummy
);
770 const char *GetFullName(const wxRegKey
*pKey
, const char *szValue
)
772 static wxString s_str
;
773 s_str
= pKey
->GetName();
774 if ( !IsEmpty(szValue
) )
775 s_str
<< "\\" << szValue
;
777 return s_str
.c_str();
780 void RemoveTrailingSeparator(wxString
& str
)
782 if ( !str
.IsEmpty() && str
.Last() == REG_SEPARATOR
)
783 str
.Truncate(str
.Len() - 1);