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
;
300 bool wxRegKey::GetKeyInfo(ulong
*pnSubKeys
,
303 ulong
*pnMaxValueLen
) const
306 m_dwLastError
= ::RegQueryInfoKey
310 NULL
, // (ptr to) size of class name buffer
312 pnSubKeys
, // [out] number of subkeys
313 pnMaxKeyLen
, // [out] max length of a subkey name
314 NULL
, // longest subkey class name
315 pnValues
, // [out] number of values
316 pnMaxValueLen
, // [out] max length of a value name
317 NULL
, // longest value data
318 NULL
, // security descriptor
319 NULL
// time of last modification
322 if ( m_dwLastError
!= ERROR_SUCCESS
) {
323 wxLogSysError(m_dwLastError
, _("can't get info about registry key '%s'"),
330 wxFAIL_MSG("GetKeyInfo() not implemented");
336 // ----------------------------------------------------------------------------
338 // ----------------------------------------------------------------------------
340 // opens key (it's not an error to call Open() on an already opened key)
341 bool wxRegKey::Open()
346 m_dwLastError
= RegOpenKey(m_hRootKey
, m_strKey
, &m_hKey
);
347 if ( m_dwLastError
!= ERROR_SUCCESS
) {
348 wxLogSysError(m_dwLastError
, _("can't open registry key '%s'"),
356 // creates key, failing if it exists and !bOkIfExists
357 bool wxRegKey::Create(bool bOkIfExists
)
359 // check for existence only if asked (i.e. order is important!)
360 if ( !bOkIfExists
&& Exists() ) {
367 m_dwLastError
= RegCreateKey(m_hRootKey
, m_strKey
, &m_hKey
);
368 if ( m_dwLastError
!= ERROR_SUCCESS
) {
369 wxLogSysError(m_dwLastError
, _("can't create registry key '%s'"),
377 // close the key, it's not an error to call it when not opened
378 bool wxRegKey::Close()
381 m_dwLastError
= RegCloseKey(m_hKey
);
382 if ( m_dwLastError
!= ERROR_SUCCESS
) {
383 wxLogSysError(m_dwLastError
, _("can't close registry key '%s'"),
397 // ----------------------------------------------------------------------------
398 // delete keys/values
399 // ----------------------------------------------------------------------------
400 bool wxRegKey::DeleteSelf()
405 // it already doesn't exist - ok!
410 // we can't delete keys while enumerating because it confuses GetNextKey, so
411 // we first save the key names and then delete them all
412 wxArrayString astrSubkeys
;
416 bool bCont
= GetFirstKey(strKey
, lIndex
);
418 astrSubkeys
.Add(strKey
);
420 bCont
= GetNextKey(strKey
, lIndex
);
423 uint nKeyCount
= astrSubkeys
.Count();
424 for ( uint nKey
= 0; nKey
< nKeyCount
; nKey
++ ) {
425 wxRegKey
key(*this, astrSubkeys
[nKey
]);
426 if ( !key
.DeleteSelf() )
430 // now delete this key itself
433 m_dwLastError
= RegDeleteKey(m_hRootKey
, m_strKey
);
434 if ( m_dwLastError
!= ERROR_SUCCESS
) {
435 wxLogSysError(m_dwLastError
, _("can't delete key '%s'"),
443 bool wxRegKey::DeleteKey(const char *szKey
)
448 wxRegKey
key(*this, szKey
);
449 return key
.DeleteSelf();
452 bool wxRegKey::DeleteValue(const char *szValue
)
458 m_dwLastError
= RegDeleteValue(m_hKey
, szValue
);
459 if ( m_dwLastError
!= ERROR_SUCCESS
) {
460 wxLogSysError(m_dwLastError
, _("can't delete value '%s' from key '%s'"),
461 szValue
, GetName().c_str());
465 // named registry values don't exist in Win16 world
466 wxASSERT( IsEmpty(szValue
) );
468 // just set the (default and unique) value of the key to ""
469 m_dwLastError
= RegSetValue(m_hKey
, NULL
, REG_SZ
, "", RESERVED
);
470 if ( m_dwLastError
!= ERROR_SUCCESS
) {
471 wxLogSysError(m_dwLastError
, _("can't delete value of key '%s'"),
480 // ----------------------------------------------------------------------------
481 // access to values and subkeys
482 // ----------------------------------------------------------------------------
484 // return TRUE if value exists
485 bool wxRegKey::HasValue(const char *szValue
) const
488 if ( CONST_CAST
Open() ) {
489 return RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
490 NULL
, NULL
, NULL
) == ERROR_SUCCESS
;
495 // only unnamed value exists
496 return IsEmpty(szValue
);
500 // returns TRUE if this key has any subkeys
501 bool wxRegKey::HasSubkeys() const
503 // just call GetFirstKey with dummy parameters
506 return CONST_CAST
GetFirstKey(str
, l
);
509 // returns TRUE if given subkey exists
510 bool wxRegKey::HasSubKey(const char *szKey
) const
512 if ( CONST_CAST
Open() )
513 return KeyExists(m_hKey
, szKey
);
518 wxRegKey::ValueType
wxRegKey::GetValueType(const char *szValue
)
525 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
526 &dwType
, NULL
, NULL
);
527 if ( m_dwLastError
!= ERROR_SUCCESS
) {
528 wxLogSysError(m_dwLastError
, _("can't read value of key '%s'"),
533 return (ValueType
)dwType
;
535 return IsEmpty(szValue
) ? Type_String
: Type_None
;
540 bool wxRegKey::SetValue(const char *szValue
, long lValue
)
542 if ( CONST_CAST
Open() ) {
543 m_dwLastError
= RegSetValueEx(m_hKey
, szValue
, RESERVED
, REG_DWORD
,
544 (RegString
)&lValue
, sizeof(lValue
));
545 if ( m_dwLastError
== ERROR_SUCCESS
)
549 wxLogSysError(m_dwLastError
, _("can't set value of '%s'"),
550 GetFullName(this, szValue
));
554 bool wxRegKey::QueryValue(const char *szValue
, long *plValue
) const
556 if ( CONST_CAST
Open() ) {
557 DWORD dwType
, dwSize
= sizeof(DWORD
);
558 RegString pBuf
= (RegString
)plValue
;
559 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
560 &dwType
, pBuf
, &dwSize
);
561 if ( m_dwLastError
!= ERROR_SUCCESS
) {
562 wxLogSysError(m_dwLastError
, _("can't read value of key '%s'"),
567 // check that we read the value of right type
568 wxASSERT_MSG( dwType
== REG_DWORD
,
569 "Type mismatch in wxRegKey::QueryValue()." );
580 bool wxRegKey::QueryValue(const char *szValue
, wxString
& strValue
) const
582 if ( CONST_CAST
Open() ) {
584 // first get the type and size of the data
585 DWORD dwType
, dwSize
;
586 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
587 &dwType
, NULL
, &dwSize
);
588 if ( m_dwLastError
== ERROR_SUCCESS
) {
589 RegString pBuf
= (RegString
)strValue
.GetWriteBuf(dwSize
);
590 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
591 &dwType
, pBuf
, &dwSize
);
592 strValue
.UngetWriteBuf();
593 if ( m_dwLastError
== ERROR_SUCCESS
) {
594 // check that it was the right type
595 wxASSERT_MSG( dwType
== REG_SZ
,
596 "Type mismatch in wxRegKey::QueryValue()." );
602 // named registry values don't exist in Win16
603 wxASSERT( IsEmpty(szValue
) );
605 m_dwLastError
= RegQueryValue(m_hKey
, 0, strValue
.GetWriteBuf(256), &l
);
606 strValue
.UngetWriteBuf();
607 if ( m_dwLastError
== ERROR_SUCCESS
)
612 wxLogSysError(m_dwLastError
, _("can't read value of '%s'"),
613 GetFullName(this, szValue
));
617 bool wxRegKey::SetValue(const char *szValue
, const wxString
& strValue
)
619 if ( CONST_CAST
Open() ) {
621 m_dwLastError
= RegSetValueEx(m_hKey
, szValue
, RESERVED
, REG_SZ
,
622 (RegString
)strValue
.c_str(),
624 if ( m_dwLastError
== ERROR_SUCCESS
)
627 // named registry values don't exist in Win16
628 wxASSERT( IsEmpty(szValue
) );
630 m_dwLastError
= RegSetValue(m_hKey
, NULL
, REG_SZ
, strValue
, NULL
);
631 if ( m_dwLastError
== ERROR_SUCCESS
)
636 wxLogSysError(m_dwLastError
, _("can't set value of '%s'"),
637 GetFullName(this, szValue
));
641 wxRegKey::operator wxString() const
644 QueryValue(NULL
, str
);
648 // ----------------------------------------------------------------------------
650 // NB: all these functions require an index variable which allows to have
651 // several concurrently running indexations on the same key
652 // ----------------------------------------------------------------------------
654 bool wxRegKey::GetFirstValue(wxString
& strValueName
, long& lIndex
)
660 return GetNextValue(strValueName
, lIndex
);
663 bool wxRegKey::GetNextValue(wxString
& strValueName
, long& lIndex
) const
665 wxASSERT( IsOpened() );
667 // are we already at the end of enumeration?
672 char szValueName
[1024]; // @@ use RegQueryInfoKey...
673 DWORD dwValueLen
= WXSIZEOF(szValueName
);
676 m_dwLastError
= RegEnumValue(m_hKey
, lIndex
,
677 szValueName
, &dwValueLen
,
680 NULL
, // [out] buffer for value
681 NULL
); // [i/o] it's length
683 if ( m_dwLastError
!= ERROR_SUCCESS
) {
684 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
685 m_dwLastError
= ERROR_SUCCESS
;
689 wxLogSysError(m_dwLastError
, _("can't enumerate values of key '%s'"),
696 strValueName
= szValueName
;
698 // only one unnamed value
699 wxASSERT( lIndex
== 0 );
702 strValueName
.Empty();
708 bool wxRegKey::GetFirstKey(wxString
& strKeyName
, long& lIndex
)
714 return GetNextKey(strKeyName
, lIndex
);
717 bool wxRegKey::GetNextKey(wxString
& strKeyName
, long& lIndex
) const
719 wxASSERT( IsOpened() );
721 // are we already at the end of enumeration?
725 char szKeyName
[_MAX_PATH
+ 1];
726 m_dwLastError
= RegEnumKey(m_hKey
, lIndex
++, szKeyName
, WXSIZEOF(szKeyName
));
728 if ( m_dwLastError
!= ERROR_SUCCESS
) {
729 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
730 m_dwLastError
= ERROR_SUCCESS
;
734 wxLogSysError(m_dwLastError
, _("can't enumerate subkeys of key '%s'"),
741 strKeyName
= szKeyName
;
745 // ============================================================================
746 // implementation of global functions
747 // ============================================================================
748 bool KeyExists(HKEY hRootKey
, const char *szKey
)
751 if ( RegOpenKey(hRootKey
, szKey
, &hkeyDummy
) == ERROR_SUCCESS
) {
752 RegCloseKey(hkeyDummy
);
759 const char *GetFullName(const wxRegKey
*pKey
, const char *szValue
)
761 static wxString s_str
;
762 s_str
= pKey
->GetName();
763 if ( !IsEmpty(szValue
) )
764 s_str
<< "\\" << szValue
;
766 return s_str
.c_str();
769 void RemoveTrailingSeparator(wxString
& str
)
771 if ( !str
.IsEmpty() && str
.Last() == REG_SEPARATOR
)
772 str
.Truncate(str
.Len() - 1);