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 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
92 // @ const_cast<> is not yet supported by all compilers
93 #define CONST_CAST ((wxRegKey *)this)->
96 #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(HKEY hRootKey
, const char *szKey
);
109 // combines value and key name (uses static buffer!)
110 static const char *GetFullName(const wxRegKey
*pKey
,
111 const char *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 char *wxRegKey::GetStdKeyName(uint key
)
127 // return empty string if key is invalid
128 wxCHECK_MSG( key
< nStdKeys
, "", "invalid key in wxRegKey::GetStdKeyName" );
130 return aStdKeys
[key
].szName
;
133 const char *wxRegKey::GetStdKeyShortName(uint key
)
135 // return empty string if key is invalid
136 wxCHECK( key
< nStdKeys
, "" );
138 return aStdKeys
[key
].szShortName
;
141 wxRegKey::StdKey
wxRegKey::ExtractKeyName(wxString
& strKey
)
143 wxString strRoot
= strKey
.Left(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("invalid key prefix in wxRegKey::ExtractKeyName.");
158 hRootKey
= HKEY_CLASSES_ROOT
;
161 strKey
= strKey
.After(REG_SEPARATOR
);
162 if ( !strKey
.IsEmpty() && strKey
.Last() == REG_SEPARATOR
)
163 strKey
.Truncate(strKey
.Len() - 1);
166 return (wxRegKey::StdKey
)(int)hRootKey
;
169 wxRegKey::StdKey
wxRegKey::GetStdKeyFromHkey(HKEY hkey
)
171 for ( uint ui
= 0; ui
< nStdKeys
; ui
++ ) {
172 if ( aStdKeys
[ui
].hkey
== hkey
)
176 wxFAIL_MSG("non root hkey passed to wxRegKey::GetStdKeyFromHkey.");
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
188 m_hRootKey
= aStdKeys
[HKCR
].hkey
;
192 wxRegKey::wxRegKey(const wxString
& strKey
) : m_strKey(strKey
)
194 m_hRootKey
= 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
= 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 ( !strKey
.IsEmpty() && strKey
[0] != REG_SEPARATOR
)
214 m_strKey
+= REG_SEPARATOR
;
217 RemoveTrailingSeparator(m_strKey
);
219 m_hRootKey
= keyParent
.m_hRootKey
;
224 // dtor closes the key releasing system resource
225 wxRegKey::~wxRegKey()
230 // ----------------------------------------------------------------------------
231 // change the key name/hkey
232 // ----------------------------------------------------------------------------
234 // set the full key name
235 void wxRegKey::SetName(const wxString
& strKey
)
240 m_hRootKey
= aStdKeys
[ExtractKeyName(m_strKey
)].hkey
;
243 // the name is relative to the parent key
244 void wxRegKey::SetName(StdKey keyParent
, const wxString
& strKey
)
249 RemoveTrailingSeparator(m_strKey
);
250 m_hRootKey
= aStdKeys
[keyParent
].hkey
;
253 // the name is relative to the parent key
254 void wxRegKey::SetName(const wxRegKey
& keyParent
, const wxString
& strKey
)
258 // combine our name with parent's to get the full name
259 m_strKey
= keyParent
.m_strKey
;
260 if ( !strKey
.IsEmpty() && strKey
[0] != REG_SEPARATOR
)
261 m_strKey
+= REG_SEPARATOR
;
264 RemoveTrailingSeparator(m_strKey
);
266 m_hRootKey
= keyParent
.m_hRootKey
;
269 // hKey should be opened and will be closed in wxRegKey dtor
270 void wxRegKey::SetHkey(HKEY hKey
)
277 // ----------------------------------------------------------------------------
278 // info about the key
279 // ----------------------------------------------------------------------------
281 // returns TRUE if the key exists
282 bool wxRegKey::Exists() const
284 // opened key has to exist, try to open it if not done yet
285 return IsOpened() ? TRUE
: KeyExists(m_hRootKey
, m_strKey
);
288 // returns the full name of the key (prefix is abbreviated if bShortPrefix)
289 wxString
wxRegKey::GetName(bool bShortPrefix
) const
291 StdKey key
= GetStdKeyFromHkey(m_hRootKey
);
292 wxString str
= bShortPrefix
? aStdKeys
[key
].szShortName
293 : aStdKeys
[key
].szName
;
294 if ( !m_strKey
.IsEmpty() )
295 str
<< "\\" << m_strKey
;
301 bool wxRegKey::GetKeyInfo(uint
* pnSubKeys
,
304 uint
* pnMaxValueLen
) const
306 bool wxRegKey::GetKeyInfo(ulong
*pnSubKeys
,
309 ulong
*pnMaxValueLen
) const
313 m_dwLastError
= ::RegQueryInfoKey
317 NULL
, // (ptr to) size of class name buffer
319 pnSubKeys
, // [out] number of subkeys
320 pnMaxKeyLen
, // [out] max length of a subkey name
321 NULL
, // longest subkey class name
322 pnValues
, // [out] number of values
323 pnMaxValueLen
, // [out] max length of a value name
324 NULL
, // longest value data
325 NULL
, // security descriptor
326 NULL
// time of last modification
329 if ( m_dwLastError
!= ERROR_SUCCESS
) {
330 wxLogSysError(m_dwLastError
, _("can't get info about registry key '%s'"),
337 wxFAIL_MSG("GetKeyInfo() not implemented");
343 // ----------------------------------------------------------------------------
345 // ----------------------------------------------------------------------------
347 // opens key (it's not an error to call Open() on an already opened key)
348 bool wxRegKey::Open()
353 m_dwLastError
= RegOpenKey(m_hRootKey
, m_strKey
, &m_hKey
);
354 if ( m_dwLastError
!= ERROR_SUCCESS
) {
355 wxLogSysError(m_dwLastError
, _("can't open registry key '%s'"),
363 // creates key, failing if it exists and !bOkIfExists
364 bool wxRegKey::Create(bool bOkIfExists
)
366 // check for existence only if asked (i.e. order is important!)
367 if ( !bOkIfExists
&& Exists() ) {
374 m_dwLastError
= RegCreateKey(m_hRootKey
, m_strKey
, &m_hKey
);
375 if ( m_dwLastError
!= ERROR_SUCCESS
) {
376 wxLogSysError(m_dwLastError
, _("can't create registry key '%s'"),
384 // close the key, it's not an error to call it when not opened
385 bool wxRegKey::Close()
388 m_dwLastError
= RegCloseKey(m_hKey
);
389 if ( m_dwLastError
!= ERROR_SUCCESS
) {
390 wxLogSysError(m_dwLastError
, _("can't close registry key '%s'"),
404 // ----------------------------------------------------------------------------
405 // delete keys/values
406 // ----------------------------------------------------------------------------
407 bool wxRegKey::DeleteSelf()
412 // it already doesn't exist - ok!
417 // we can't delete keys while enumerating because it confuses GetNextKey, so
418 // we first save the key names and then delete them all
419 wxArrayString astrSubkeys
;
423 bool bCont
= GetFirstKey(strKey
, lIndex
);
425 astrSubkeys
.Add(strKey
);
427 bCont
= GetNextKey(strKey
, lIndex
);
430 uint nKeyCount
= astrSubkeys
.Count();
431 for ( uint nKey
= 0; nKey
< nKeyCount
; nKey
++ ) {
432 wxRegKey
key(*this, astrSubkeys
[nKey
]);
433 if ( !key
.DeleteSelf() )
437 // now delete this key itself
440 m_dwLastError
= RegDeleteKey(m_hRootKey
, m_strKey
);
441 if ( m_dwLastError
!= ERROR_SUCCESS
) {
442 wxLogSysError(m_dwLastError
, _("can't delete key '%s'"),
450 bool wxRegKey::DeleteKey(const char *szKey
)
455 wxRegKey
key(*this, szKey
);
456 return key
.DeleteSelf();
459 bool wxRegKey::DeleteValue(const char *szValue
)
465 m_dwLastError
= RegDeleteValue(m_hKey
, szValue
);
466 if ( m_dwLastError
!= ERROR_SUCCESS
) {
467 wxLogSysError(m_dwLastError
, _("can't delete value '%s' from key '%s'"),
468 szValue
, GetName().c_str());
472 // named registry values don't exist in Win16 world
473 wxASSERT( IsEmpty(szValue
) );
475 // just set the (default and unique) value of the key to ""
476 m_dwLastError
= RegSetValue(m_hKey
, NULL
, REG_SZ
, "", RESERVED
);
477 if ( m_dwLastError
!= ERROR_SUCCESS
) {
478 wxLogSysError(m_dwLastError
, _("can't delete value of key '%s'"),
487 // ----------------------------------------------------------------------------
488 // access to values and subkeys
489 // ----------------------------------------------------------------------------
491 // return TRUE if value exists
492 bool wxRegKey::HasValue(const char *szValue
) const
495 if ( CONST_CAST
Open() ) {
496 return RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
497 NULL
, NULL
, NULL
) == ERROR_SUCCESS
;
502 // only unnamed value exists
503 return IsEmpty(szValue
);
507 // returns TRUE if this key has any subkeys
508 bool wxRegKey::HasSubkeys() const
510 // just call GetFirstKey with dummy parameters
513 return CONST_CAST
GetFirstKey(str
, l
);
516 // returns TRUE if given subkey exists
517 bool wxRegKey::HasSubKey(const char *szKey
) const
519 if ( CONST_CAST
Open() )
520 return KeyExists(m_hKey
, szKey
);
525 wxRegKey::ValueType
wxRegKey::GetValueType(const char *szValue
)
532 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
533 &dwType
, NULL
, NULL
);
534 if ( m_dwLastError
!= ERROR_SUCCESS
) {
535 wxLogSysError(m_dwLastError
, _("can't read value of key '%s'"),
540 return (ValueType
)dwType
;
542 return IsEmpty(szValue
) ? Type_String
: Type_None
;
547 bool wxRegKey::SetValue(const char *szValue
, long lValue
)
549 if ( CONST_CAST
Open() ) {
550 m_dwLastError
= RegSetValueEx(m_hKey
, szValue
, RESERVED
, REG_DWORD
,
551 (RegString
)&lValue
, sizeof(lValue
));
552 if ( m_dwLastError
== ERROR_SUCCESS
)
556 wxLogSysError(m_dwLastError
, _("can't set value of '%s'"),
557 GetFullName(this, szValue
));
561 bool wxRegKey::QueryValue(const char *szValue
, long *plValue
) const
563 if ( CONST_CAST
Open() ) {
564 DWORD dwType
, dwSize
= sizeof(DWORD
);
565 RegString pBuf
= (RegString
)plValue
;
566 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
567 &dwType
, pBuf
, &dwSize
);
568 if ( m_dwLastError
!= ERROR_SUCCESS
) {
569 wxLogSysError(m_dwLastError
, _("can't read value of key '%s'"),
574 // check that we read the value of right type
575 wxASSERT_MSG( dwType
== REG_DWORD
,
576 "Type mismatch in wxRegKey::QueryValue()." );
587 bool wxRegKey::QueryValue(const char *szValue
, wxString
& strValue
) const
589 if ( CONST_CAST
Open() ) {
591 // first get the type and size of the data
592 DWORD dwType
, dwSize
;
593 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
594 &dwType
, NULL
, &dwSize
);
595 if ( m_dwLastError
== ERROR_SUCCESS
) {
596 RegString pBuf
= (RegString
)strValue
.GetWriteBuf(dwSize
);
597 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
598 &dwType
, pBuf
, &dwSize
);
599 strValue
.UngetWriteBuf();
600 if ( m_dwLastError
== ERROR_SUCCESS
) {
601 // check that it was the right type
602 wxASSERT_MSG( dwType
== REG_SZ
,
603 "Type mismatch in wxRegKey::QueryValue()." );
609 // named registry values don't exist in Win16
610 wxASSERT( IsEmpty(szValue
) );
612 m_dwLastError
= RegQueryValue(m_hKey
, 0, strValue
.GetWriteBuf(256), &l
);
613 strValue
.UngetWriteBuf();
614 if ( m_dwLastError
== ERROR_SUCCESS
)
619 wxLogSysError(m_dwLastError
, _("can't read value of '%s'"),
620 GetFullName(this, szValue
));
624 bool wxRegKey::SetValue(const char *szValue
, const wxString
& strValue
)
626 if ( CONST_CAST
Open() ) {
628 m_dwLastError
= RegSetValueEx(m_hKey
, szValue
, RESERVED
, REG_SZ
,
629 (RegString
)strValue
.c_str(),
631 if ( m_dwLastError
== ERROR_SUCCESS
)
634 // named registry values don't exist in Win16
635 wxASSERT( IsEmpty(szValue
) );
637 m_dwLastError
= RegSetValue(m_hKey
, NULL
, REG_SZ
, strValue
, NULL
);
638 if ( m_dwLastError
== ERROR_SUCCESS
)
643 wxLogSysError(m_dwLastError
, _("can't set value of '%s'"),
644 GetFullName(this, szValue
));
648 wxRegKey::operator wxString() const
651 QueryValue(NULL
, str
);
655 // ----------------------------------------------------------------------------
657 // NB: all these functions require an index variable which allows to have
658 // several concurrently running indexations on the same key
659 // ----------------------------------------------------------------------------
661 bool wxRegKey::GetFirstValue(wxString
& strValueName
, long& lIndex
)
667 return GetNextValue(strValueName
, lIndex
);
670 bool wxRegKey::GetNextValue(wxString
& strValueName
, long& lIndex
) const
672 wxASSERT( IsOpened() );
674 // are we already at the end of enumeration?
679 char szValueName
[1024]; // @@ use RegQueryInfoKey...
680 DWORD dwValueLen
= WXSIZEOF(szValueName
);
683 m_dwLastError
= RegEnumValue(m_hKey
, lIndex
,
684 szValueName
, &dwValueLen
,
687 NULL
, // [out] buffer for value
688 NULL
); // [i/o] it's length
690 if ( m_dwLastError
!= ERROR_SUCCESS
) {
691 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
692 m_dwLastError
= ERROR_SUCCESS
;
696 wxLogSysError(m_dwLastError
, _("can't enumerate values of key '%s'"),
703 strValueName
= szValueName
;
705 // only one unnamed value
706 wxASSERT( lIndex
== 0 );
709 strValueName
.Empty();
715 bool wxRegKey::GetFirstKey(wxString
& strKeyName
, long& lIndex
)
721 return GetNextKey(strKeyName
, lIndex
);
724 bool wxRegKey::GetNextKey(wxString
& strKeyName
, long& lIndex
) const
726 wxASSERT( IsOpened() );
728 // are we already at the end of enumeration?
732 char szKeyName
[_MAX_PATH
+ 1];
733 m_dwLastError
= RegEnumKey(m_hKey
, lIndex
++, szKeyName
, WXSIZEOF(szKeyName
));
735 if ( m_dwLastError
!= ERROR_SUCCESS
) {
736 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
737 m_dwLastError
= ERROR_SUCCESS
;
741 wxLogSysError(m_dwLastError
, _("can't enumerate subkeys of key '%s'"),
748 strKeyName
= szKeyName
;
752 // ============================================================================
753 // implementation of global functions
754 // ============================================================================
755 bool KeyExists(HKEY hRootKey
, const char *szKey
)
758 if ( RegOpenKey(hRootKey
, szKey
, &hkeyDummy
) == ERROR_SUCCESS
) {
759 RegCloseKey(hkeyDummy
);
766 const char *GetFullName(const wxRegKey
*pKey
, const char *szValue
)
768 static wxString s_str
;
769 s_str
= pKey
->GetName();
770 if ( !IsEmpty(szValue
) )
771 s_str
<< "\\" << szValue
;
773 return s_str
.c_str();
776 void RemoveTrailingSeparator(wxString
& str
)
778 if ( !str
.IsEmpty() && str
.Last() == REG_SEPARATOR
)
779 str
.Truncate(str
.Len() - 1);