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
260 if ( !strKey
.IsEmpty() && strKey
[0] != REG_SEPARATOR
)
261 m_strKey
+= REG_SEPARATOR
;
263 RemoveTrailingSeparator(m_strKey
);
265 m_hRootKey
= keyParent
.m_hRootKey
;
268 // hKey should be opened and will be closed in wxRegKey dtor
269 void wxRegKey::SetHkey(HKEY hKey
)
276 // ----------------------------------------------------------------------------
277 // info about the key
278 // ----------------------------------------------------------------------------
280 // returns TRUE if the key exists
281 bool wxRegKey::Exists() const
283 // opened key has to exist, try to open it if not done yet
284 return IsOpened() ? TRUE
: KeyExists(m_hRootKey
, m_strKey
);
287 // returns the full name of the key (prefix is abbreviated if bShortPrefix)
288 wxString
wxRegKey::GetName(bool bShortPrefix
) const
290 StdKey key
= GetStdKeyFromHkey(m_hRootKey
);
291 wxString str
= bShortPrefix
? aStdKeys
[key
].szShortName
292 : aStdKeys
[key
].szName
;
293 if ( !m_strKey
.IsEmpty() )
294 str
<< "\\" << m_strKey
;
299 // ----------------------------------------------------------------------------
301 // ----------------------------------------------------------------------------
303 // opens key (it's not an error to call Open() on an already opened key)
304 bool wxRegKey::Open()
309 m_dwLastError
= RegOpenKey(m_hRootKey
, m_strKey
, &m_hKey
);
310 if ( m_dwLastError
!= ERROR_SUCCESS
) {
311 wxLogSysError(m_dwLastError
, "can't open registry key '%s'",
319 // creates key, failing if it exists and !bOkIfExists
320 bool wxRegKey::Create(bool bOkIfExists
)
322 // check for existence only if asked (i.e. order is important!)
323 if ( !bOkIfExists
&& Exists() ) {
330 m_dwLastError
= RegCreateKey(m_hRootKey
, m_strKey
, &m_hKey
);
331 if ( m_dwLastError
!= ERROR_SUCCESS
) {
332 wxLogSysError(m_dwLastError
, "can't create registry key '%s'",
340 // close the key, it's not an error to call it when not opened
341 bool wxRegKey::Close()
344 m_dwLastError
= RegCloseKey(m_hKey
);
345 if ( m_dwLastError
!= ERROR_SUCCESS
) {
346 wxLogSysError(m_dwLastError
, "can't close registry key '%s'",
360 // ----------------------------------------------------------------------------
361 // delete keys/values
362 // ----------------------------------------------------------------------------
363 bool wxRegKey::DeleteSelf()
368 // it already doesn't exist - ok!
373 // we can't delete keys while enumerating because it confuses GetNextKey, so
374 // we first save the key names and then delete them all
375 wxArrayString astrSubkeys
;
379 bool bCont
= GetFirstKey(strKey
, lIndex
);
381 astrSubkeys
.Add(strKey
);
383 bCont
= GetNextKey(strKey
, lIndex
);
386 uint nKeyCount
= astrSubkeys
.Count();
387 for ( uint nKey
= 0; nKey
< nKeyCount
; nKey
++ ) {
388 wxRegKey
key(*this, astrSubkeys
[nKey
]);
389 if ( !key
.DeleteSelf() )
393 // now delete this key itself
396 m_dwLastError
= RegDeleteKey(m_hRootKey
, m_strKey
);
397 if ( m_dwLastError
!= ERROR_SUCCESS
) {
398 wxLogSysError(m_dwLastError
, "can't delete key '%s'", GetName().c_str());
405 bool wxRegKey::DeleteKey(const char *szKey
)
410 wxRegKey
key(*this, szKey
);
411 return key
.DeleteSelf();
414 bool wxRegKey::DeleteValue(const char *szValue
)
420 m_dwLastError
= RegDeleteValue(m_hKey
, szValue
);
421 if ( m_dwLastError
!= ERROR_SUCCESS
) {
422 wxLogSysError(m_dwLastError
, "can't delete value '%s' from key '%s'",
423 szValue
, GetName().c_str());
427 // named registry values don't exist in Win16 world
428 wxASSERT( IsEmpty(szValue
) );
430 // just set the (default and unique) value of the key to ""
431 m_dwLastError
= RegSetValue(m_hKey
, NULL
, REG_SZ
, "", RESERVED
);
432 if ( m_dwLastError
!= ERROR_SUCCESS
) {
433 wxLogSysError(m_dwLastError
, "can't delete value of key '%s'",
442 // ----------------------------------------------------------------------------
443 // access to values and subkeys
444 // ----------------------------------------------------------------------------
446 // return TRUE if value exists
447 bool wxRegKey::HasValue(const char *szValue
) const
450 if ( CONST_CAST
Open() ) {
451 return RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
452 NULL
, NULL
, NULL
) == ERROR_SUCCESS
;
457 // only unnamed value exists
458 return IsEmpty(szValue
);
462 // returns TRUE if this key has any subkeys
463 bool wxRegKey::HasSubkeys() const
465 // just call GetFirstKey with dummy parameters
468 return CONST_CAST
GetFirstKey(str
, l
);
471 // returns TRUE if given subkey exists
472 bool wxRegKey::HasSubKey(const char *szKey
) const
474 if ( CONST_CAST
Open() )
475 return KeyExists(m_hKey
, szKey
);
480 wxRegKey::ValueType
wxRegKey::GetValueType(const char *szValue
)
487 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
488 &dwType
, NULL
, NULL
);
489 if ( m_dwLastError
!= ERROR_SUCCESS
) {
490 wxLogSysError(m_dwLastError
, "can't read value of key '%s'",
495 return (ValueType
)dwType
;
497 return IsEmpty(szValue
) ? Type_String
: Type_None
;
502 bool wxRegKey::SetValue(const char *szValue
, long lValue
)
504 if ( CONST_CAST
Open() ) {
505 m_dwLastError
= RegSetValueEx(m_hKey
, szValue
, RESERVED
, REG_DWORD
,
506 (RegString
)&lValue
, sizeof(lValue
));
507 if ( m_dwLastError
== ERROR_SUCCESS
)
511 wxLogSysError(m_dwLastError
, "can't set value of '%s'",
512 GetFullName(this, szValue
));
516 bool wxRegKey::QueryValue(const char *szValue
, long *plValue
) const
518 if ( CONST_CAST
Open() ) {
519 DWORD dwType
, dwSize
= sizeof(DWORD
);
520 RegString pBuf
= (RegString
)plValue
;
521 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
522 &dwType
, pBuf
, &dwSize
);
523 if ( m_dwLastError
!= ERROR_SUCCESS
) {
524 wxLogSysError(m_dwLastError
, "can't read value of key '%s'",
529 // check that we read the value of right type
530 wxASSERT_MSG( dwType
== REG_DWORD
,
531 "Type mismatch in wxRegKey::QueryValue()." );
542 bool wxRegKey::QueryValue(const char *szValue
, wxString
& strValue
) const
544 if ( CONST_CAST
Open() ) {
546 // first get the type and size of the data
547 DWORD dwType
, dwSize
;
548 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
549 &dwType
, NULL
, &dwSize
);
550 if ( m_dwLastError
== ERROR_SUCCESS
) {
551 RegString pBuf
= (RegString
)strValue
.GetWriteBuf(dwSize
);
552 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
553 &dwType
, pBuf
, &dwSize
);
554 if ( m_dwLastError
== ERROR_SUCCESS
) {
555 // check that it was the right type
556 wxASSERT_MSG( dwType
== REG_SZ
,
557 "Type mismatch in wxRegKey::QueryValue()." );
563 // named registry values don't exist in Win16
564 wxASSERT( IsEmpty(szValue
) );
566 m_dwLastError
= RegQueryValue(m_hKey
, 0, strValue
.GetWriteBuf(256), &l
);
567 if ( m_dwLastError
== ERROR_SUCCESS
)
572 wxLogSysError(m_dwLastError
, "can't read value of '%s'",
573 GetFullName(this, szValue
));
577 bool wxRegKey::SetValue(const char *szValue
, const wxString
& strValue
)
579 if ( CONST_CAST
Open() ) {
581 m_dwLastError
= RegSetValueEx(m_hKey
, szValue
, RESERVED
, REG_SZ
,
582 (RegString
)strValue
.c_str(),
584 if ( m_dwLastError
== ERROR_SUCCESS
)
587 // named registry values don't exist in Win16
588 wxASSERT( IsEmpty(szValue
) );
590 m_dwLastError
= RegSetValue(m_hKey
, NULL
, REG_SZ
, strValue
, NULL
);
591 if ( m_dwLastError
== ERROR_SUCCESS
)
596 wxLogSysError(m_dwLastError
, "can't set value of '%s'",
597 GetFullName(this, szValue
));
601 wxRegKey::operator wxString() const
604 QueryValue(NULL
, str
);
608 // ----------------------------------------------------------------------------
610 // NB: all these functions require an index variable which allows to have
611 // several concurrently running indexations on the same key
612 // ----------------------------------------------------------------------------
614 bool wxRegKey::GetFirstValue(wxString
& strValueName
, long& lIndex
)
620 return GetNextValue(strValueName
, lIndex
);
623 bool wxRegKey::GetNextValue(wxString
& strValueName
, long& lIndex
) const
625 wxASSERT( IsOpened() );
627 // are we already at the end of enumeration?
632 char szValueName
[1024]; // @@ use RegQueryInfoKey...
633 DWORD dwValueLen
= WXSIZEOF(szValueName
);
636 m_dwLastError
= RegEnumValue(m_hKey
, lIndex
,
637 szValueName
, &dwValueLen
,
640 NULL
, // [out] buffer for value
641 NULL
); // [i/o] it's length
643 if ( m_dwLastError
!= ERROR_SUCCESS
) {
644 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
645 m_dwLastError
= ERROR_SUCCESS
;
649 wxLogSysError(m_dwLastError
, "can't enumerate values of key '%s'",
656 strValueName
= szValueName
;
658 // only one unnamed value
659 wxASSERT( lIndex
== 0 );
662 strValueName
.Empty();
668 bool wxRegKey::GetFirstKey(wxString
& strKeyName
, long& lIndex
)
674 return GetNextKey(strKeyName
, lIndex
);
677 bool wxRegKey::GetNextKey(wxString
& strKeyName
, long& lIndex
) const
679 wxASSERT( IsOpened() );
681 // are we already at the end of enumeration?
685 char szKeyName
[_MAX_PATH
+ 1];
686 m_dwLastError
= RegEnumKey(m_hKey
, lIndex
++, szKeyName
, WXSIZEOF(szKeyName
));
688 if ( m_dwLastError
!= ERROR_SUCCESS
) {
689 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
690 m_dwLastError
= ERROR_SUCCESS
;
694 wxLogSysError(m_dwLastError
, "can't enumerate subkeys of key '%s'",
701 strKeyName
= szKeyName
;
705 // ============================================================================
706 // implementation of global functions
707 // ============================================================================
708 bool KeyExists(HKEY hRootKey
, const char *szKey
)
711 if ( RegOpenKey(hRootKey
, szKey
, &hkeyDummy
) == ERROR_SUCCESS
) {
712 RegCloseKey(hkeyDummy
);
719 const char *GetFullName(const wxRegKey
*pKey
, const char *szValue
)
721 static wxString s_str
;
722 s_str
= pKey
->GetName();
723 if ( !IsEmpty(szValue
) )
724 s_str
<< "\\" << szValue
;
726 return s_str
.c_str();
729 void RemoveTrailingSeparator(wxString
& str
)
731 if ( !str
.IsEmpty() && str
.Last() == REG_SEPARATOR
)
732 str
.Truncate(str
.Len() - 1);