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() &&
218 (strKey
.IsEmpty() || strKey
[0] != REG_SEPARATOR
) ) {
219 m_strKey
+= REG_SEPARATOR
;
223 RemoveTrailingSeparator(m_strKey
);
225 m_hRootKey
= keyParent
.m_hRootKey
;
230 // dtor closes the key releasing system resource
231 wxRegKey::~wxRegKey()
236 // ----------------------------------------------------------------------------
237 // change the key name/hkey
238 // ----------------------------------------------------------------------------
240 // set the full key name
241 void wxRegKey::SetName(const wxString
& strKey
)
246 m_hRootKey
= aStdKeys
[ExtractKeyName(m_strKey
)].hkey
;
249 // the name is relative to the parent key
250 void wxRegKey::SetName(StdKey keyParent
, const wxString
& strKey
)
255 RemoveTrailingSeparator(m_strKey
);
256 m_hRootKey
= aStdKeys
[keyParent
].hkey
;
259 // the name is relative to the parent key
260 void wxRegKey::SetName(const wxRegKey
& keyParent
, const wxString
& strKey
)
264 // combine our name with parent's to get the full name
265 m_strKey
= keyParent
.m_strKey
;
266 if ( !strKey
.IsEmpty() && strKey
[0] != REG_SEPARATOR
)
267 m_strKey
+= REG_SEPARATOR
;
270 RemoveTrailingSeparator(m_strKey
);
272 m_hRootKey
= keyParent
.m_hRootKey
;
275 // hKey should be opened and will be closed in wxRegKey dtor
276 void wxRegKey::SetHkey(HKEY hKey
)
283 // ----------------------------------------------------------------------------
284 // info about the key
285 // ----------------------------------------------------------------------------
287 // returns TRUE if the key exists
288 bool wxRegKey::Exists() const
290 // opened key has to exist, try to open it if not done yet
291 return IsOpened() ? TRUE
: KeyExists(m_hRootKey
, m_strKey
);
294 // returns the full name of the key (prefix is abbreviated if bShortPrefix)
295 wxString
wxRegKey::GetName(bool bShortPrefix
) const
297 StdKey key
= GetStdKeyFromHkey(m_hRootKey
);
298 wxString str
= bShortPrefix
? aStdKeys
[key
].szShortName
299 : aStdKeys
[key
].szName
;
300 if ( !m_strKey
.IsEmpty() )
301 str
<< "\\" << m_strKey
;
307 bool wxRegKey::GetKeyInfo(size_t* pnSubKeys
,
310 size_t* pnMaxValueLen
) const
312 bool wxRegKey::GetKeyInfo(ulong
*pnSubKeys
,
315 ulong
*pnMaxValueLen
) const
319 m_dwLastError
= ::RegQueryInfoKey
323 NULL
, // (ptr to) size of class name buffer
325 pnSubKeys
, // [out] number of subkeys
326 pnMaxKeyLen
, // [out] max length of a subkey name
327 NULL
, // longest subkey class name
328 pnValues
, // [out] number of values
329 pnMaxValueLen
, // [out] max length of a value name
330 NULL
, // longest value data
331 NULL
, // security descriptor
332 NULL
// time of last modification
335 if ( m_dwLastError
!= ERROR_SUCCESS
) {
336 wxLogSysError(m_dwLastError
, _("can't get info about registry key '%s'"),
343 wxFAIL_MSG("GetKeyInfo() not implemented");
349 // ----------------------------------------------------------------------------
351 // ----------------------------------------------------------------------------
353 // opens key (it's not an error to call Open() on an already opened key)
354 bool wxRegKey::Open()
359 m_dwLastError
= RegOpenKey(m_hRootKey
, m_strKey
, &m_hKey
);
360 if ( m_dwLastError
!= ERROR_SUCCESS
) {
361 wxLogSysError(m_dwLastError
, _("can't open registry key '%s'"),
369 // creates key, failing if it exists and !bOkIfExists
370 bool wxRegKey::Create(bool bOkIfExists
)
372 // check for existence only if asked (i.e. order is important!)
373 if ( !bOkIfExists
&& Exists() ) {
380 m_dwLastError
= RegCreateKey(m_hRootKey
, m_strKey
, &m_hKey
);
381 if ( m_dwLastError
!= ERROR_SUCCESS
) {
382 wxLogSysError(m_dwLastError
, _("can't create registry key '%s'"),
390 // close the key, it's not an error to call it when not opened
391 bool wxRegKey::Close()
394 m_dwLastError
= RegCloseKey(m_hKey
);
395 if ( m_dwLastError
!= ERROR_SUCCESS
) {
396 wxLogSysError(m_dwLastError
, _("can't close registry key '%s'"),
410 // ----------------------------------------------------------------------------
411 // delete keys/values
412 // ----------------------------------------------------------------------------
413 bool wxRegKey::DeleteSelf()
418 // it already doesn't exist - ok!
423 // we can't delete keys while enumerating because it confuses GetNextKey, so
424 // we first save the key names and then delete them all
425 wxArrayString astrSubkeys
;
429 bool bCont
= GetFirstKey(strKey
, lIndex
);
431 astrSubkeys
.Add(strKey
);
433 bCont
= GetNextKey(strKey
, lIndex
);
436 size_t nKeyCount
= astrSubkeys
.Count();
437 for ( size_t nKey
= 0; nKey
< nKeyCount
; nKey
++ ) {
438 wxRegKey
key(*this, astrSubkeys
[nKey
]);
439 if ( !key
.DeleteSelf() )
443 // now delete this key itself
446 m_dwLastError
= RegDeleteKey(m_hRootKey
, m_strKey
);
447 if ( m_dwLastError
!= ERROR_SUCCESS
) {
448 wxLogSysError(m_dwLastError
, _("can't delete key '%s'"),
456 bool wxRegKey::DeleteKey(const char *szKey
)
461 wxRegKey
key(*this, szKey
);
462 return key
.DeleteSelf();
465 bool wxRegKey::DeleteValue(const char *szValue
)
471 m_dwLastError
= RegDeleteValue(m_hKey
, szValue
);
472 if ( m_dwLastError
!= ERROR_SUCCESS
) {
473 wxLogSysError(m_dwLastError
, _("can't delete value '%s' from key '%s'"),
474 szValue
, GetName().c_str());
478 // named registry values don't exist in Win16 world
479 wxASSERT( IsEmpty(szValue
) );
481 // just set the (default and unique) value of the key to ""
482 m_dwLastError
= RegSetValue(m_hKey
, NULL
, REG_SZ
, "", RESERVED
);
483 if ( m_dwLastError
!= ERROR_SUCCESS
) {
484 wxLogSysError(m_dwLastError
, _("can't delete value of key '%s'"),
493 // ----------------------------------------------------------------------------
494 // access to values and subkeys
495 // ----------------------------------------------------------------------------
497 // return TRUE if value exists
498 bool wxRegKey::HasValue(const char *szValue
) const
500 // this function should be silent, so suppress possible messages from Open()
504 if ( CONST_CAST
Open() ) {
505 return RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
506 NULL
, NULL
, NULL
) == ERROR_SUCCESS
;
511 // only unnamed value exists
512 return IsEmpty(szValue
);
516 // returns TRUE if this key has any subkeys
517 bool wxRegKey::HasSubkeys() const
519 // just call GetFirstKey with dummy parameters
522 return CONST_CAST
GetFirstKey(str
, l
);
525 // returns TRUE if given subkey exists
526 bool wxRegKey::HasSubKey(const char *szKey
) const
528 if ( CONST_CAST
Open() )
529 return KeyExists(m_hKey
, szKey
);
534 wxRegKey::ValueType
wxRegKey::GetValueType(const char *szValue
)
541 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
542 &dwType
, NULL
, NULL
);
543 if ( m_dwLastError
!= ERROR_SUCCESS
) {
544 wxLogSysError(m_dwLastError
, _("can't read value of key '%s'"),
549 return (ValueType
)dwType
;
551 return IsEmpty(szValue
) ? Type_String
: Type_None
;
556 bool wxRegKey::SetValue(const char *szValue
, long lValue
)
558 if ( CONST_CAST
Open() ) {
559 m_dwLastError
= RegSetValueEx(m_hKey
, szValue
, RESERVED
, REG_DWORD
,
560 (RegString
)&lValue
, sizeof(lValue
));
561 if ( m_dwLastError
== ERROR_SUCCESS
)
565 wxLogSysError(m_dwLastError
, _("can't set value of '%s'"),
566 GetFullName(this, szValue
));
570 bool wxRegKey::QueryValue(const char *szValue
, long *plValue
) const
572 if ( CONST_CAST
Open() ) {
573 DWORD dwType
, dwSize
= sizeof(DWORD
);
574 RegString pBuf
= (RegString
)plValue
;
575 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
576 &dwType
, pBuf
, &dwSize
);
577 if ( m_dwLastError
!= ERROR_SUCCESS
) {
578 wxLogSysError(m_dwLastError
, _("can't read value of key '%s'"),
583 // check that we read the value of right type
584 wxASSERT_MSG( dwType
== REG_DWORD
,
585 "Type mismatch in wxRegKey::QueryValue()." );
596 bool wxRegKey::QueryValue(const char *szValue
, wxString
& strValue
) const
598 if ( CONST_CAST
Open() ) {
600 // first get the type and size of the data
601 DWORD dwType
, dwSize
;
602 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
603 &dwType
, NULL
, &dwSize
);
604 if ( m_dwLastError
== ERROR_SUCCESS
) {
605 RegString pBuf
= (RegString
)strValue
.GetWriteBuf(dwSize
);
606 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
607 &dwType
, pBuf
, &dwSize
);
608 strValue
.UngetWriteBuf();
609 if ( m_dwLastError
== ERROR_SUCCESS
) {
610 // check that it was the right type
611 wxASSERT_MSG( dwType
== REG_SZ
,
612 "Type mismatch in wxRegKey::QueryValue()." );
618 // named registry values don't exist in Win16
619 wxASSERT( IsEmpty(szValue
) );
621 m_dwLastError
= RegQueryValue(m_hKey
, 0, strValue
.GetWriteBuf(256), &l
);
622 strValue
.UngetWriteBuf();
623 if ( m_dwLastError
== ERROR_SUCCESS
)
628 wxLogSysError(m_dwLastError
, _("can't read value of '%s'"),
629 GetFullName(this, szValue
));
633 bool wxRegKey::SetValue(const char *szValue
, const wxString
& strValue
)
635 if ( CONST_CAST
Open() ) {
637 m_dwLastError
= RegSetValueEx(m_hKey
, szValue
, RESERVED
, REG_SZ
,
638 (RegString
)strValue
.c_str(),
640 if ( m_dwLastError
== ERROR_SUCCESS
)
643 // named registry values don't exist in Win16
644 wxASSERT( IsEmpty(szValue
) );
646 m_dwLastError
= RegSetValue(m_hKey
, NULL
, REG_SZ
, strValue
, NULL
);
647 if ( m_dwLastError
== ERROR_SUCCESS
)
652 wxLogSysError(m_dwLastError
, _("can't set value of '%s'"),
653 GetFullName(this, szValue
));
657 wxRegKey::operator wxString() const
660 QueryValue(NULL
, str
);
664 // ----------------------------------------------------------------------------
666 // NB: all these functions require an index variable which allows to have
667 // several concurrently running indexations on the same key
668 // ----------------------------------------------------------------------------
670 bool wxRegKey::GetFirstValue(wxString
& strValueName
, long& lIndex
)
676 return GetNextValue(strValueName
, lIndex
);
679 bool wxRegKey::GetNextValue(wxString
& strValueName
, long& lIndex
) const
681 wxASSERT( IsOpened() );
683 // are we already at the end of enumeration?
688 char szValueName
[1024]; // @@ use RegQueryInfoKey...
689 DWORD dwValueLen
= WXSIZEOF(szValueName
);
692 m_dwLastError
= RegEnumValue(m_hKey
, lIndex
,
693 szValueName
, &dwValueLen
,
696 NULL
, // [out] buffer for value
697 NULL
); // [i/o] it's length
699 if ( m_dwLastError
!= ERROR_SUCCESS
) {
700 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
701 m_dwLastError
= ERROR_SUCCESS
;
705 wxLogSysError(m_dwLastError
, _("can't enumerate values of key '%s'"),
712 strValueName
= szValueName
;
714 // only one unnamed value
715 wxASSERT( lIndex
== 0 );
718 strValueName
.Empty();
724 bool wxRegKey::GetFirstKey(wxString
& strKeyName
, long& lIndex
)
730 return GetNextKey(strKeyName
, lIndex
);
733 bool wxRegKey::GetNextKey(wxString
& strKeyName
, long& lIndex
) const
735 wxASSERT( IsOpened() );
737 // are we already at the end of enumeration?
741 char szKeyName
[_MAX_PATH
+ 1];
742 m_dwLastError
= RegEnumKey(m_hKey
, lIndex
++, szKeyName
, WXSIZEOF(szKeyName
));
744 if ( m_dwLastError
!= ERROR_SUCCESS
) {
745 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
746 m_dwLastError
= ERROR_SUCCESS
;
750 wxLogSysError(m_dwLastError
, _("can't enumerate subkeys of key '%s'"),
757 strKeyName
= szKeyName
;
761 // ============================================================================
762 // implementation of global functions
763 // ============================================================================
764 bool KeyExists(HKEY hRootKey
, const char *szKey
)
767 if ( RegOpenKey(hRootKey
, szKey
, &hkeyDummy
) == ERROR_SUCCESS
) {
768 RegCloseKey(hkeyDummy
);
775 const char *GetFullName(const wxRegKey
*pKey
, const char *szValue
)
777 static wxString s_str
;
778 s_str
= pKey
->GetName();
779 if ( !IsEmpty(szValue
) )
780 s_str
<< "\\" << szValue
;
782 return s_str
.c_str();
785 void RemoveTrailingSeparator(wxString
& str
)
787 if ( !str
.IsEmpty() && str
.Last() == REG_SEPARATOR
)
788 str
.Truncate(str
.Len() - 1);