]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/registry.cpp
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 // returns TRUE if given registry key exists
101 static bool KeyExists(HKEY hRootKey
, const char *szKey
);
103 // combines value and key name (uses static buffer!)
104 static const char *GetFullName(const wxRegKey
*pKey
,
105 const char *szValue
= NULL
);
107 // ============================================================================
108 // implementation of wxRegKey class
109 // ============================================================================
111 // ----------------------------------------------------------------------------
112 // static functions and variables
113 // ----------------------------------------------------------------------------
115 const size_t wxRegKey::nStdKeys
= WXSIZEOF(aStdKeys
);
117 // @@ should take a `StdKey key', but as it's often going to be used in loops
118 // it would require casts in user code.
119 const char *wxRegKey::GetStdKeyName(uint key
)
121 // return empty string if key is invalid
122 wxCHECK_RET( key
< nStdKeys
, "" );
124 return aStdKeys
[key
].szName
;
127 const char *wxRegKey::GetStdKeyShortName(uint key
)
129 // return empty string if key is invalid
130 wxCHECK_RET( key
< nStdKeys
, "" );
132 return aStdKeys
[key
].szShortName
;
135 wxRegKey::StdKey
wxRegKey::ExtractKeyName(wxString
& strKey
)
137 wxString strRoot
= strKey
.Left(REG_SEPARATOR
);
141 for ( ui
= 0; ui
< nStdKeys
; ui
++ ) {
142 if ( strRoot
.CmpNoCase(aStdKeys
[ui
].szName
) == 0 ||
143 strRoot
.CmpNoCase(aStdKeys
[ui
].szShortName
) == 0 ) {
144 hRootKey
= aStdKeys
[ui
].hkey
;
149 if ( ui
== nStdKeys
) {
150 wxFAIL_MSG("invalid key prefix in wxRegKey::ExtractKeyName.");
152 hRootKey
= HKEY_CLASSES_ROOT
;
155 strKey
= strKey
.After(REG_SEPARATOR
);
156 if ( !strKey
.IsEmpty() && strKey
.Last() == REG_SEPARATOR
)
157 strKey
.Truncate(strKey
.Len() - 1);
160 return (wxRegKey::StdKey
)(int)hRootKey
;
163 wxRegKey::StdKey
wxRegKey::GetStdKeyFromHkey(HKEY hkey
)
165 for ( uint ui
= 0; ui
< nStdKeys
; ui
++ ) {
166 if ( aStdKeys
[ui
].hkey
== hkey
)
170 wxFAIL_MSG("non root hkey passed to wxRegKey::GetStdKeyFromHkey.");
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
182 m_hRootKey
= aStdKeys
[HKCR
].hkey
;
186 wxRegKey::wxRegKey(const wxString
& strKey
) : m_strKey(strKey
)
188 m_hRootKey
= aStdKeys
[ExtractKeyName(m_strKey
)].hkey
;
193 // parent is a predefined (and preopened) key
194 wxRegKey::wxRegKey(StdKey keyParent
, const wxString
& strKey
) : m_strKey(strKey
)
196 if ( !m_strKey
.IsEmpty() && m_strKey
.Last() == REG_SEPARATOR
)
197 m_strKey
.Truncate(m_strKey
.Len() - 1);
199 m_hRootKey
= aStdKeys
[keyParent
].hkey
;
204 // parent is a normal regkey
205 wxRegKey::wxRegKey(const wxRegKey
& keyParent
, const wxString
& strKey
)
206 : m_strKey(keyParent
.m_strKey
)
208 // combine our name with parent's to get the full name
209 if ( !m_strKey
.IsEmpty() )
210 m_strKey
+= REG_SEPARATOR
;
213 if ( !m_strKey
.IsEmpty() && m_strKey
.Last() == REG_SEPARATOR
)
214 m_strKey
.Truncate(m_strKey
.Len() - 1);
216 m_hRootKey
= keyParent
.m_hRootKey
;
221 // dtor closes the key releasing system resource
222 wxRegKey::~wxRegKey()
227 // ----------------------------------------------------------------------------
228 // info about the key
229 // ----------------------------------------------------------------------------
231 // returns TRUE if the key exists
232 bool wxRegKey::Exists() const
234 // opened key has to exist, try to open it if not done yet
235 return IsOpened() ? TRUE
: KeyExists(m_hRootKey
, m_strKey
);
238 // returns the full name of the key (prefix is abbreviated if bShortPrefix)
239 wxString
wxRegKey::GetName(bool bShortPrefix
) const
241 StdKey key
= GetStdKeyFromHkey(m_hRootKey
);
242 wxString str
= bShortPrefix
? aStdKeys
[key
].szShortName
243 : aStdKeys
[key
].szName
;
244 if ( !m_strKey
.IsEmpty() )
245 str
<< "\\" << m_strKey
;
250 // ----------------------------------------------------------------------------
252 // ----------------------------------------------------------------------------
254 // opens key (it's not an error to call Open() on an already opened key)
255 bool wxRegKey::Open()
260 m_dwLastError
= RegOpenKey(m_hRootKey
, m_strKey
, &m_hKey
);
261 if ( m_dwLastError
!= ERROR_SUCCESS
) {
262 wxLogSysError(m_dwLastError
, "can't open registry key '%s'",
270 // creates key, failing if it exists and !bOkIfExists
271 bool wxRegKey::Create(bool bOkIfExists
)
273 // check for existence only if asked (i.e. order is important!)
274 if ( !bOkIfExists
&& Exists() ) {
281 m_dwLastError
= RegCreateKey(m_hRootKey
, m_strKey
, &m_hKey
);
282 if ( m_dwLastError
!= ERROR_SUCCESS
) {
283 wxLogSysError(m_dwLastError
, "can't create registry key '%s'",
291 bool wxRegKey::DeleteSelf()
298 bool bCont
= GetFirstKey(strKey
, lIndex
);
300 wxRegKey
key(*this, strKey
);
301 if ( !key
.DeleteSelf() )
304 bCont
= GetNextKey(strKey
, lIndex
);
309 m_dwLastError
= RegDeleteKey(m_hRootKey
, m_strKey
);
310 if ( m_dwLastError
!= ERROR_SUCCESS
) {
311 wxLogSysError(m_dwLastError
, "can't delete key '%s'", GetName().c_str());
318 bool wxRegKey::DeleteKey(const char *szKey
)
323 wxRegKey
key(*this, szKey
);
324 return key
.DeleteSelf();
327 bool wxRegKey::DeleteValue(const char *szValue
)
333 m_dwLastError
= RegDeleteValue(m_hKey
, szValue
);
334 if ( m_dwLastError
!= ERROR_SUCCESS
) {
335 wxLogSysError(m_dwLastError
, "can't delete value '%s' from key '%s'",
336 szValue
, GetName().c_str());
340 // named registry values don't exist in Win16 world
341 wxASSERT( IsEmpty(szValue
) );
343 // just set the (default and unique) value of the key to ""
344 m_dwLastError
= RegSetValue(m_hKey
, NULL
, REG_SZ
, "", RESERVED
);
345 if ( m_dwLastError
!= ERROR_SUCCESS
) {
346 wxLogSysError(m_dwLastError
, "can't delete value of key '%s'",
355 // close the key, it's not an error to call it when not opened
356 bool wxRegKey::Close()
359 m_dwLastError
= RegCloseKey(m_hKey
);
360 if ( m_dwLastError
!= ERROR_SUCCESS
) {
361 wxLogSysError(m_dwLastError
, "can't close registry key '%s'",
375 // ----------------------------------------------------------------------------
376 // access to values and subkeys
377 // ----------------------------------------------------------------------------
379 // returns TRUE if this key has any subkeys
380 bool wxRegKey::HasSubkeys() const
382 // just call GetFirstKey with dummy parameters
385 return CONST_CAST
GetFirstKey(str
, l
);
388 // returns TRUE if given subkey exists
389 bool wxRegKey::HasSubKey(const char *szKey
) const
391 if ( CONST_CAST
Open() )
392 return KeyExists(m_hKey
, szKey
);
397 wxRegKey::ValueType
wxRegKey::GetValueType(const char *szValue
)
404 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
405 &dwType
, NULL
, NULL
);
406 if ( m_dwLastError
!= ERROR_SUCCESS
) {
407 wxLogSysError(m_dwLastError
, "can't read value of key '%s'",
412 return (ValueType
)dwType
;
414 return IsEmpty(szValue
) ? Type_String
: Type_None
;
419 bool wxRegKey::SetValue(const char *szValue
, long lValue
)
421 if ( CONST_CAST
Open() ) {
422 m_dwLastError
= RegSetValueEx(m_hKey
, szValue
, RESERVED
, REG_DWORD
,
423 (RegString
)&lValue
, sizeof(lValue
));
424 if ( m_dwLastError
== ERROR_SUCCESS
)
428 wxLogSysError(m_dwLastError
, "can't set value of '%s'",
429 GetFullName(this, szValue
));
433 bool wxRegKey::QueryValue(const char *szValue
, long *plValue
) const
435 if ( CONST_CAST
Open() ) {
436 DWORD dwType
, dwSize
= sizeof(DWORD
);
437 RegString pBuf
= (RegString
)plValue
;
438 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
439 &dwType
, pBuf
, &dwSize
);
440 if ( m_dwLastError
!= ERROR_SUCCESS
) {
441 wxLogSysError(m_dwLastError
, "can't read value of key '%s'",
446 // check that we read the value of right type
447 wxASSERT_MSG( dwType
== REG_DWORD
,
448 "Type mismatch in wxRegKey::QueryValue()." );
459 bool wxRegKey::QueryValue(const char *szValue
, wxString
& strValue
) const
461 if ( CONST_CAST
Open() ) {
463 // first get the type and size of the data
464 DWORD dwType
, dwSize
;
465 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
466 &dwType
, NULL
, &dwSize
);
467 if ( m_dwLastError
== ERROR_SUCCESS
) {
468 RegString pBuf
= (RegString
)strValue
.GetWriteBuf(dwSize
);
469 m_dwLastError
= RegQueryValueEx(m_hKey
, szValue
, RESERVED
,
470 &dwType
, pBuf
, &dwSize
);
471 if ( m_dwLastError
== ERROR_SUCCESS
) {
472 // check that it was the right type
473 wxASSERT_MSG( dwType
== REG_SZ
,
474 "Type mismatch in wxRegKey::QueryValue()." );
480 // named registry values don't exist in Win16
481 wxASSERT( IsEmpty(szValue
) );
483 m_dwLastError
= RegQueryValue(m_hKey
, 0, strValue
.GetWriteBuf(256), &l
);
484 if ( m_dwLastError
== ERROR_SUCCESS
)
489 wxLogSysError(m_dwLastError
, "can't read value of '%s'",
490 GetFullName(this, szValue
));
494 bool wxRegKey::SetValue(const char *szValue
, const wxString
& strValue
)
496 if ( CONST_CAST
Open() ) {
498 m_dwLastError
= RegSetValueEx(m_hKey
, szValue
, RESERVED
, REG_SZ
,
499 (RegString
)strValue
.c_str(),
501 if ( m_dwLastError
== ERROR_SUCCESS
)
504 // named registry values don't exist in Win16
505 wxASSERT( IsEmpty(szValue
) );
507 m_dwLastError
= RegSetValue(m_hKey
, NULL
, REG_SZ
, strValue
, NULL
);
508 if ( m_dwLastError
== ERROR_SUCCESS
)
513 wxLogSysError(m_dwLastError
, "can't set value of '%s'",
514 GetFullName(this, szValue
));
518 wxRegKey::operator wxString() const
521 QueryValue(NULL
, str
);
525 // ----------------------------------------------------------------------------
527 // NB: all these functions require an index variable which allows to have
528 // several concurrently running indexations on the same key
529 // ----------------------------------------------------------------------------
532 bool wxRegKey::GetFirstValue(wxString
& strValueName
, long& lIndex
)
537 char szValueName
[1024]; // @@ use RegQueryInfoKey...
538 DWORD dwValueLen
= WXSIZEOF(szValueName
);
541 m_dwLastError
= RegEnumValue(m_hKey
, lIndex
,
542 szValueName
, &dwValueLen
,
545 NULL
, // [out] buffer for value
546 NULL
); // [i/o] it's length
548 if ( m_dwLastError
!= ERROR_SUCCESS
) {
549 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
)
552 wxLogSysError(m_dwLastError
, "can't enumerate values of key '%s'",
559 strValueName
= szValueName
;
563 bool wxRegKey::GetNextValue(wxString
& strValueName
, long& lIndex
) const
565 wxASSERT( IsOpened() );
566 wxASSERT( lIndex
!= -1 );
568 char szValueName
[1024]; // @@ use RegQueryInfoKey...
569 DWORD dwValueLen
= WXSIZEOF(szValueName
);
572 m_dwLastError
= RegEnumValue(m_hKey
, lIndex
,
573 szValueName
, &dwValueLen
,
575 NULL
, // buffer for type
576 NULL
, NULL
); // buffer for value and length
578 if ( m_dwLastError
!= ERROR_SUCCESS
) {
579 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
580 m_dwLastError
= ERROR_SUCCESS
;
584 wxLogSysError(m_dwLastError
, "can't enumerate values of key '%s'",
591 strValueName
= szValueName
;
596 bool wxRegKey::GetFirstKey(wxString
& strKeyName
, long& lIndex
)
601 char szKeyName
[_MAX_PATH
+ 1];
603 m_dwLastError
= RegEnumKey(m_hKey
, lIndex
, szKeyName
, WXSIZEOF(szKeyName
));
605 if ( m_dwLastError
!= ERROR_SUCCESS
) {
606 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
607 m_dwLastError
= ERROR_SUCCESS
;
611 wxLogSysError(m_dwLastError
, "can't enumerate subkeys of key '%s'",
618 strKeyName
= szKeyName
;
622 bool wxRegKey::GetNextKey(wxString
& strKeyName
, long& lIndex
) const
624 wxASSERT( IsOpened() );
625 wxASSERT( lIndex
!= -1 );
627 char szKeyName
[_MAX_PATH
+ 1];
629 m_dwLastError
= RegEnumKey(m_hKey
, lIndex
, szKeyName
, WXSIZEOF(szKeyName
));
631 if ( m_dwLastError
!= ERROR_SUCCESS
) {
632 if ( m_dwLastError
== ERROR_NO_MORE_ITEMS
) {
633 m_dwLastError
= ERROR_SUCCESS
;
637 wxLogSysError(m_dwLastError
, "can't enumerate subkeys of key '%s'",
644 strKeyName
= szKeyName
;
648 // ============================================================================
649 // implementation of global functions
650 // ============================================================================
651 bool KeyExists(HKEY hRootKey
, const char *szKey
)
654 if ( RegOpenKey(hRootKey
, szKey
, &hkeyDummy
) == ERROR_SUCCESS
) {
655 RegCloseKey(hkeyDummy
);
662 const char *GetFullName(const wxRegKey
*pKey
, const char *szValue
)
664 static wxString s_str
;
665 s_str
= pKey
->GetName();
666 if ( !IsEmpty(szValue
) )
667 s_str
<< "\\" << szValue
;
669 return s_str
.c_str();