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"
37 #define WIN32_LEAN_AND_MEAN
41 #include <stdlib.h> // for _MAX_PATH
48 #define HKEY_DEFINED // already defined in windows.h
49 #include "wx/msw/registry.h"
51 // some registry functions don't like signed chars
52 typedef unsigned char *RegString
;
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 // the standard key names, short names and handles all bundled together for
64 const char *szShortName
;
68 { HKEY_CLASSES_ROOT
, "HKEY_CLASSES_ROOT", "HKCR" },
70 { HKEY_CURRENT_USER
, "HKEY_CURRENT_USER", "HKCU" },
71 { HKEY_LOCAL_MACHINE
, "HKEY_LOCAL_MACHINE", "HKLM" },
72 { HKEY_USERS
, "HKEY_USERS", "HKU" }, // short name?
73 { HKEY_PERFORMANCE_DATA
, "HKEY_PERFORMANCE_DATA", "HKPD" },
75 { HKEY_CURRENT_CONFIG
, "HKEY_CURRENT_CONFIG", "HKCC" },
77 { HKEY_DYN_DATA
, "HKEY_DYN_DATA", "HKDD" }, // short name?
79 #endif //WINVER >= 4.0
83 // the registry name separator (perhaps one day MS will change it to '/' ;-)
84 #define REG_SEPARATOR '\\'
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
89 // @ const_cast<> is not yet supported by all compilers
90 #define CONST_CAST ((wxRegKey *)this)->
93 #define m_dwLastError CONST_CAST m_dwLastError
96 // ----------------------------------------------------------------------------
97 // non member functions
98 // ----------------------------------------------------------------------------
100 // removes the trailing backslash from the string if it has one
101 static inline void RemoveTrailingSeparator(wxString
& str
);
103 // returns TRUE if given registry key exists
104 static bool KeyExists(HKEY hRootKey
, const char *szKey
);
106 // combines value and key name (uses static buffer!)
107 static const char *GetFullName(const wxRegKey
*pKey
,
108 const char *szValue
= NULL
);
110 // ============================================================================
111 // implementation of wxRegKey class
112 // ============================================================================
114 // ----------------------------------------------------------------------------
115 // static functions and variables
116 // ----------------------------------------------------------------------------
118 const size_t wxRegKey::nStdKeys
= WXSIZEOF(aStdKeys
);
120 // @@ should take a `StdKey key', but as it's often going to be used in loops
121 // it would require casts in user code.
122 const char *wxRegKey::GetStdKeyName(uint key
)
124 // return empty string if key is invalid
125 wxCHECK_MSG( key
< nStdKeys
, "", "invalid key in wxRegKey::GetStdKeyName" );
127 return aStdKeys
[key
].szName
;
130 const char *wxRegKey::GetStdKeyShortName(uint key
)
132 // return empty string if key is invalid
133 wxCHECK( key
< nStdKeys
, "" );
135 return aStdKeys
[key
].szShortName
;
138 wxRegKey::StdKey
wxRegKey::ExtractKeyName(wxString
& strKey
)
140 wxString strRoot
= strKey
.Left(REG_SEPARATOR
);
144 for ( ui
= 0; ui
< nStdKeys
; ui
++ ) {
145 if ( strRoot
.CmpNoCase(aStdKeys
[ui
].szName
) == 0 ||
146 strRoot
.CmpNoCase(aStdKeys
[ui
].szShortName
) == 0 ) {
147 hRootKey
= aStdKeys
[ui
].hkey
;
152 if ( ui
== nStdKeys
) {
153 wxFAIL_MSG("invalid key prefix in wxRegKey::ExtractKeyName.");
155 hRootKey
= HKEY_CLASSES_ROOT
;
158 strKey
= strKey
.After(REG_SEPARATOR
);
159 if ( !strKey
.IsEmpty() && strKey
.Last() == REG_SEPARATOR
)
160 strKey
.Truncate(strKey
.Len() - 1);
163 return (wxRegKey::StdKey
)(int)hRootKey
;
166 wxRegKey::StdKey
wxRegKey::GetStdKeyFromHkey(HKEY hkey
)
168 for ( uint ui
= 0; ui
< nStdKeys
; ui
++ ) {
169 if ( aStdKeys
[ui
].hkey
== hkey
)
173 wxFAIL_MSG("non root hkey passed to wxRegKey::GetStdKeyFromHkey.");
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
185 m_hRootKey
= aStdKeys
[HKCR
].hkey
;
189 wxRegKey::wxRegKey(const wxString
& strKey
) : m_strKey(strKey
)
191 m_hRootKey
= aStdKeys
[ExtractKeyName(m_strKey
)].hkey
;
196 // parent is a predefined (and preopened) key
197 wxRegKey::wxRegKey(StdKey keyParent
, const wxString
& strKey
) : m_strKey(strKey
)
199 RemoveTrailingSeparator(m_strKey
);
200 m_hRootKey
= aStdKeys
[keyParent
].hkey
;
205 // parent is a normal regkey
206 wxRegKey::wxRegKey(const wxRegKey
& keyParent
, const wxString
& strKey
)
207 : m_strKey(keyParent
.m_strKey
)
209 // combine our name with parent's to get the full name
210 if ( !strKey
.IsEmpty() && strKey
[0] != REG_SEPARATOR
)
211 m_strKey
+= REG_SEPARATOR
;
214 RemoveTrailingSeparator(m_strKey
);
216 m_hRootKey
= keyParent
.m_hRootKey
;
221 // dtor closes the key releasing system resource
222 wxRegKey::~wxRegKey()
227 // ----------------------------------------------------------------------------
228 // change the key name/hkey
229 // ----------------------------------------------------------------------------
231 // set the full key name
232 void wxRegKey::SetName(const wxString
& strKey
)
237 m_hRootKey
= aStdKeys
[ExtractKeyName(m_strKey
)].hkey
;
240 // the name is relative to the parent key
241 void wxRegKey::SetName(StdKey keyParent
, const wxString
& strKey
)
246 RemoveTrailingSeparator(m_strKey
);
247 m_hRootKey
= aStdKeys
[keyParent
].hkey
;
250 // the name is relative to the parent key
251 void wxRegKey::SetName(const wxRegKey
& keyParent
, const wxString
& strKey
)
255 // combine our name with parent's to get the full name
257 if ( !strKey
.IsEmpty() && strKey
[0] != REG_SEPARATOR
)
258 m_strKey
+= REG_SEPARATOR
;
260 RemoveTrailingSeparator(m_strKey
);
262 m_hRootKey
= keyParent
.m_hRootKey
;
265 // hKey should be opened and will be closed in wxRegKey dtor
266 void wxRegKey::SetHkey(HKEY hKey
)
273 // ----------------------------------------------------------------------------
274 // info about the key
275 // ----------------------------------------------------------------------------
277 // returns TRUE if the key exists
278 bool wxRegKey::Exists() const
280 // opened key has to exist, try to open it if not done yet
281 return IsOpened() ? TRUE
: KeyExists(m_hRootKey
, m_strKey
);
284 // returns the full name of the key (prefix is abbreviated if bShortPrefix)
285 wxString
wxRegKey::GetName(bool bShortPrefix
) const
287 StdKey key
= GetStdKeyFromHkey(m_hRootKey
);
288 wxString str
= bShortPrefix
? aStdKeys
[key
].szShortName
289 : aStdKeys
[key
].szName
;
290 if ( !m_strKey
.IsEmpty() )
291 str
<< "\\" << m_strKey
;
296 // ----------------------------------------------------------------------------
298 // ----------------------------------------------------------------------------
300 // opens key (it's not an error to call Open() on an already opened key)
301 bool wxRegKey::Open()
306 m_dwLastError
= RegOpenKey(m_hRootKey
, m_strKey
, &m_hKey
);
307 if ( m_dwLastError
!= ERROR_SUCCESS
) {
308 wxLogSysError(m_dwLastError
, "can't open registry key '%s'",
316 // creates key, failing if it exists and !bOkIfExists
317 bool wxRegKey::Create(bool bOkIfExists
)
319 // check for existence only if asked (i.e. order is important!)
320 if ( !bOkIfExists
&& Exists() ) {
327 m_dwLastError
= RegCreateKey(m_hRootKey
, m_strKey
, &m_hKey
);
328 if ( m_dwLastError
!= ERROR_SUCCESS
) {
329 wxLogSysError(m_dwLastError
, "can't create registry key '%s'",
337 // close the key, it's not an error to call it when not opened
338 bool wxRegKey::Close()
341 m_dwLastError
= RegCloseKey(m_hKey
);
342 if ( m_dwLastError
!= ERROR_SUCCESS
) {
343 wxLogSysError(m_dwLastError
, "can't close registry key '%s'",
357 // ----------------------------------------------------------------------------
358 // delete keys/values
359 // ----------------------------------------------------------------------------
360 bool wxRegKey::DeleteSelf()
365 // it already doesn't exist - ok!
370 // we can't delete keys while enumerating because it confuses GetNextKey, so
371 // we first save the key names and then delete them all
372 wxArrayString astrSubkeys
;
376 bool bCont
= GetFirstKey(strKey
, lIndex
);
378 astrSubkeys
.Add(strKey
);
380 bCont
= GetNextKey(strKey
, lIndex
);
383 uint nKeyCount
= astrSubkeys
.Count();
384 for ( uint nKey
= 0; nKey
< nKeyCount
; nKey
++ ) {
385 wxRegKey
key(*this, astrSubkeys
[nKey
]);
386 if ( !key
.DeleteSelf() )
390 // now delete this key itself
393 m_dwLastError
= RegDeleteKey(m_hRootKey
, m_strKey
);
394 if ( m_dwLastError
!= ERROR_SUCCESS
) {
395 wxLogSysError(m_dwLastError
, "can't delete key '%s'", GetName().c_str());
402 bool wxRegKey::DeleteKey(const char *szKey
)
407 wxRegKey
key(*this, szKey
);
408 return key
.DeleteSelf();
411 bool wxRegKey::DeleteValue(const char *szValue
)
417 m_dwLastError
= RegDeleteValue(m_hKey
, szValue
);
418 if ( m_dwLastError
!= ERROR_SUCCESS
) {
419 wxLogSysError(m_dwLastError
, "can't delete value '%s' from key '%s'",
420 szValue
, GetName().c_str());
424 // named registry values don't exist in Win16 world
425 wxASSERT( IsEmpty(szValue
) );
427 // just set the (default and unique) value of the key to ""
428 m_dwLastError
= RegSetValue(m_hKey
, NULL
, REG_SZ
, "", RESERVED
);
429 if ( m_dwLastError
!= ERROR_SUCCESS
) {
430 wxLogSysError(m_dwLastError
, "can't delete value of key '%s'",
439 // ----------------------------------------------------------------------------
440 // access to values and subkeys
441 // ----------------------------------------------------------------------------
443 // return TRUE if value exists
444 bool wxRegKey::HasValue(const char *szValue
) const
447 if ( CONST_CAST
Open() ) {
448 return RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
449 NULL
, NULL
, NULL
) == ERROR_SUCCESS
;
454 // only unnamed value exists
455 return IsEmpty(szValue
);
459 // returns TRUE if this key has any subkeys
460 bool wxRegKey::HasSubkeys() const
462 // just call GetFirstKey with dummy parameters
465 return CONST_CAST
GetFirstKey(str
, l
);
468 // returns TRUE if given subkey exists
469 bool wxRegKey::HasSubKey(const char *szKey
) const
471 if ( CONST_CAST
Open() )
472 return KeyExists(m_hKey
, szKey
);
477 wxRegKey::ValueType
wxRegKey::GetValueType(const char *szValue
)
484 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
485 &dwType
, NULL
, NULL
);
486 if ( m_dwLastError
!= ERROR_SUCCESS
) {
487 wxLogSysError(m_dwLastError
, "can't read value of key '%s'",
492 return (ValueType
)dwType
;
494 return IsEmpty(szValue
) ? Type_String
: Type_None
;
499 bool wxRegKey::SetValue(const char *szValue
, long lValue
)
501 if ( CONST_CAST
Open() ) {
502 m_dwLastError
= RegSetValueEx(m_hKey
, szValue
, RESERVED
, REG_DWORD
,
503 (RegString
)&lValue
, sizeof(lValue
));
504 if ( m_dwLastError
== ERROR_SUCCESS
)
508 wxLogSysError(m_dwLastError
, "can't set value of '%s'",
509 GetFullName(this, szValue
));
513 bool wxRegKey::QueryValue(const char *szValue
, long *plValue
) const
515 if ( CONST_CAST
Open() ) {
516 DWORD dwType
, dwSize
= sizeof(DWORD
);
517 RegString pBuf
= (RegString
)plValue
;
518 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
519 &dwType
, pBuf
, &dwSize
);
520 if ( m_dwLastError
!= ERROR_SUCCESS
) {
521 wxLogSysError(m_dwLastError
, "can't read value of key '%s'",
526 // check that we read the value of right type
527 wxASSERT_MSG( dwType
== REG_DWORD
,
528 "Type mismatch in wxRegKey::QueryValue()." );
539 bool wxRegKey::QueryValue(const char *szValue
, wxString
& strValue
) const
541 if ( CONST_CAST
Open() ) {
543 // first get the type and size of the data
544 DWORD dwType
, dwSize
;
545 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
546 &dwType
, NULL
, &dwSize
);
547 if ( m_dwLastError
== ERROR_SUCCESS
) {
548 RegString pBuf
= (RegString
)strValue
.GetWriteBuf(dwSize
);
549 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
550 &dwType
, pBuf
, &dwSize
);
551 if ( m_dwLastError
== ERROR_SUCCESS
) {
552 // check that it was the right type
553 wxASSERT_MSG( dwType
== REG_SZ
,
554 "Type mismatch in wxRegKey::QueryValue()." );
560 // named registry values don't exist in Win16
561 wxASSERT( IsEmpty(szValue
) );
563 m_dwLastError
= RegQueryValue(m_hKey
, 0, strValue
.GetWriteBuf(256), &l
);
564 if ( m_dwLastError
== ERROR_SUCCESS
)
569 wxLogSysError(m_dwLastError
, "can't read value of '%s'",
570 GetFullName(this, szValue
));
574 bool wxRegKey::SetValue(const char *szValue
, const wxString
& strValue
)
576 if ( CONST_CAST
Open() ) {
578 m_dwLastError
= RegSetValueEx(m_hKey
, szValue
, RESERVED
, REG_SZ
,
579 (RegString
)strValue
.c_str(),
581 if ( m_dwLastError
== ERROR_SUCCESS
)
584 // named registry values don't exist in Win16
585 wxASSERT( IsEmpty(szValue
) );
587 m_dwLastError
= RegSetValue(m_hKey
, NULL
, REG_SZ
, strValue
, NULL
);
588 if ( m_dwLastError
== ERROR_SUCCESS
)
593 wxLogSysError(m_dwLastError
, "can't set value of '%s'",
594 GetFullName(this, szValue
));
598 wxRegKey::operator wxString() const
601 QueryValue(NULL
, str
);
605 // ----------------------------------------------------------------------------
607 // NB: all these functions require an index variable which allows to have
608 // several concurrently running indexations on the same key
609 // ----------------------------------------------------------------------------
611 bool wxRegKey::GetFirstValue(wxString
& strValueName
, long& lIndex
)
617 return GetNextValue(strValueName
, lIndex
);
620 bool wxRegKey::GetNextValue(wxString
& strValueName
, long& lIndex
) const
622 wxASSERT( IsOpened() );
624 // are we already at the end of enumeration?
629 char szValueName
[1024]; // @@ use RegQueryInfoKey...
630 DWORD dwValueLen
= WXSIZEOF(szValueName
);
633 m_dwLastError
= RegEnumValue(m_hKey
, lIndex
,
634 szValueName
, &dwValueLen
,
637 NULL
, // [out] buffer for value
638 NULL
); // [i/o] it's length
640 if ( m_dwLastError
!= ERROR_SUCCESS
) {
641 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
642 m_dwLastError
= ERROR_SUCCESS
;
646 wxLogSysError(m_dwLastError
, "can't enumerate values of key '%s'",
653 strValueName
= szValueName
;
655 // only one unnamed value
656 wxASSERT( lIndex
== 0 );
659 strValueName
.Empty();
665 bool wxRegKey::GetFirstKey(wxString
& strKeyName
, long& lIndex
)
671 return GetNextKey(strKeyName
, lIndex
);
674 bool wxRegKey::GetNextKey(wxString
& strKeyName
, long& lIndex
) const
676 wxASSERT( IsOpened() );
678 // are we already at the end of enumeration?
682 char szKeyName
[_MAX_PATH
+ 1];
683 m_dwLastError
= RegEnumKey(m_hKey
, lIndex
++, szKeyName
, WXSIZEOF(szKeyName
));
685 if ( m_dwLastError
!= ERROR_SUCCESS
) {
686 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
687 m_dwLastError
= ERROR_SUCCESS
;
691 wxLogSysError(m_dwLastError
, "can't enumerate subkeys of key '%s'",
698 strKeyName
= szKeyName
;
702 // ============================================================================
703 // implementation of global functions
704 // ============================================================================
705 bool KeyExists(HKEY hRootKey
, const char *szKey
)
708 if ( RegOpenKey(hRootKey
, szKey
, &hkeyDummy
) == ERROR_SUCCESS
) {
709 RegCloseKey(hkeyDummy
);
716 const char *GetFullName(const wxRegKey
*pKey
, const char *szValue
)
718 static wxString s_str
;
719 s_str
= pKey
->GetName();
720 if ( !IsEmpty(szValue
) )
721 s_str
<< "\\" << szValue
;
723 return s_str
.c_str();
726 void RemoveTrailingSeparator(wxString
& str
)
728 if ( !str
.IsEmpty() && str
.Last() == REG_SEPARATOR
)
729 str
.Truncate(str
.Len() - 1);