]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/registry.cpp
don't update m_iconized in IsIconized(), this can break wxFrame::HandleSize() logic...
[wxWidgets.git] / src / msw / registry.cpp
... / ...
CommitLineData
1///////////////////////////////////////////////////////////////////////////////
2// Name: msw/registry.cpp
3// Purpose: implementation of registry classes and functions
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 03.04.98
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows licence
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///////////////////////////////////////////////////////////////////////////////
14
15// for compilers that support precompilation, includes "wx.h".
16#include "wx/wxprec.h"
17
18#ifdef __BORLANDC__
19#pragma hdrstop
20#endif
21
22// other wxWidgets headers
23#include "wx/string.h"
24#include "wx/intl.h"
25#include "wx/log.h"
26#include "wx/file.h"
27#include "wx/wfstream.h"
28
29// Windows headers
30#include "wx/msw/wrapwin.h"
31
32#ifdef __WXWINCE__
33#include "wx/msw/private.h"
34#include <winbase.h>
35#include <winreg.h>
36#endif
37
38// other std headers
39#include <stdlib.h> // for _MAX_PATH
40
41#ifndef _MAX_PATH
42 #define _MAX_PATH 512
43#endif
44
45// our header
46#define HKEY_DEFINED // already defined in windows.h
47#include "wx/msw/registry.h"
48
49// some registry functions don't like signed chars
50typedef unsigned char *RegString;
51typedef BYTE* RegBinary;
52
53// ----------------------------------------------------------------------------
54// constants
55// ----------------------------------------------------------------------------
56
57// the standard key names, short names and handles all bundled together for
58// convenient access
59static struct
60{
61 HKEY hkey;
62 const wxChar *szName;
63 const wxChar *szShortName;
64}
65aStdKeys[] =
66{
67 { HKEY_CLASSES_ROOT, wxT("HKEY_CLASSES_ROOT"), wxT("HKCR") },
68 { HKEY_CURRENT_USER, wxT("HKEY_CURRENT_USER"), wxT("HKCU") },
69 { HKEY_LOCAL_MACHINE, wxT("HKEY_LOCAL_MACHINE"), wxT("HKLM") },
70 { HKEY_USERS, wxT("HKEY_USERS"), wxT("HKU") }, // short name?
71#ifndef __WXWINCE__
72 { HKEY_PERFORMANCE_DATA, wxT("HKEY_PERFORMANCE_DATA"), wxT("HKPD") },
73#endif
74#ifdef HKEY_CURRENT_CONFIG
75 { HKEY_CURRENT_CONFIG, wxT("HKEY_CURRENT_CONFIG"), wxT("HKCC") },
76#endif
77#ifdef HKEY_DYN_DATA
78 { HKEY_DYN_DATA, wxT("HKEY_DYN_DATA"), wxT("HKDD") }, // short name?
79#endif
80};
81
82// the registry name separator (perhaps one day MS will change it to '/' ;-)
83#define REG_SEPARATOR wxT('\\')
84
85// useful for Windows programmers: makes somewhat more clear all these zeroes
86// being passed to Windows APIs
87#define RESERVED (0)
88
89// ----------------------------------------------------------------------------
90// macros
91// ----------------------------------------------------------------------------
92
93// const_cast<> is not yet supported by all compilers
94#define CONST_CAST ((wxRegKey *)this)->
95
96// and neither is mutable which m_dwLastError should be
97#define m_dwLastError CONST_CAST m_dwLastError
98
99// ----------------------------------------------------------------------------
100// non member functions
101// ----------------------------------------------------------------------------
102
103// removes the trailing backslash from the string if it has one
104static inline void RemoveTrailingSeparator(wxString& str);
105
106// returns true if given registry key exists
107static bool KeyExists(WXHKEY hRootKey, const wxChar *szKey);
108
109// combines value and key name (uses static buffer!)
110static const wxChar *GetFullName(const wxRegKey *pKey,
111 const wxChar *szValue = NULL);
112
113// ============================================================================
114// implementation of wxRegKey class
115// ============================================================================
116
117// ----------------------------------------------------------------------------
118// static functions and variables
119// ----------------------------------------------------------------------------
120
121const size_t wxRegKey::nStdKeys = WXSIZEOF(aStdKeys);
122
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.
125const wxChar *wxRegKey::GetStdKeyName(size_t key)
126{
127 // return empty string if key is invalid
128 wxCHECK_MSG( key < nStdKeys, wxEmptyString, wxT("invalid key in wxRegKey::GetStdKeyName") );
129
130 return aStdKeys[key].szName;
131}
132
133const wxChar *wxRegKey::GetStdKeyShortName(size_t key)
134{
135 // return empty string if key is invalid
136 wxCHECK( key < nStdKeys, wxEmptyString );
137
138 return aStdKeys[key].szShortName;
139}
140
141wxRegKey::StdKey wxRegKey::ExtractKeyName(wxString& strKey)
142{
143 wxString strRoot = strKey.BeforeFirst(REG_SEPARATOR);
144
145 HKEY hRootKey = 0;
146 size_t ui;
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;
151 break;
152 }
153 }
154
155 if ( ui == nStdKeys ) {
156 wxFAIL_MSG(wxT("invalid key prefix in wxRegKey::ExtractKeyName."));
157
158 hRootKey = HKEY_CLASSES_ROOT;
159 }
160 else {
161 strKey = strKey.After(REG_SEPARATOR);
162 if ( !strKey.empty() && strKey.Last() == REG_SEPARATOR )
163 strKey.Truncate(strKey.Len() - 1);
164 }
165
166 return (wxRegKey::StdKey)(int)hRootKey;
167}
168
169wxRegKey::StdKey wxRegKey::GetStdKeyFromHkey(WXHKEY hkey)
170{
171 for ( size_t ui = 0; ui < nStdKeys; ui++ ) {
172 if ( (int) aStdKeys[ui].hkey == (int) hkey )
173 return (StdKey)ui;
174 }
175
176 wxFAIL_MSG(wxT("non root hkey passed to wxRegKey::GetStdKeyFromHkey."));
177
178 return HKCR;
179}
180
181// ----------------------------------------------------------------------------
182// ctors and dtor
183// ----------------------------------------------------------------------------
184
185wxRegKey::wxRegKey()
186{
187 m_hRootKey = (WXHKEY) aStdKeys[HKCR].hkey;
188
189 Init();
190}
191
192wxRegKey::wxRegKey(const wxString& strKey) : m_strKey(strKey)
193{
194 m_hRootKey = (WXHKEY) aStdKeys[ExtractKeyName(m_strKey)].hkey;
195
196 Init();
197}
198
199// parent is a predefined (and preopened) key
200wxRegKey::wxRegKey(StdKey keyParent, const wxString& strKey) : m_strKey(strKey)
201{
202 RemoveTrailingSeparator(m_strKey);
203 m_hRootKey = (WXHKEY) aStdKeys[keyParent].hkey;
204
205 Init();
206}
207
208// parent is a normal regkey
209wxRegKey::wxRegKey(const wxRegKey& keyParent, const wxString& strKey)
210 : m_strKey(keyParent.m_strKey)
211{
212 // combine our name with parent's to get the full name
213 if ( !m_strKey.empty() &&
214 (strKey.empty() || strKey[0] != REG_SEPARATOR) ) {
215 m_strKey += REG_SEPARATOR;
216 }
217
218 m_strKey += strKey;
219 RemoveTrailingSeparator(m_strKey);
220
221 m_hRootKey = keyParent.m_hRootKey;
222
223 Init();
224}
225
226// dtor closes the key releasing system resource
227wxRegKey::~wxRegKey()
228{
229 Close();
230}
231
232// ----------------------------------------------------------------------------
233// change the key name/hkey
234// ----------------------------------------------------------------------------
235
236// set the full key name
237void wxRegKey::SetName(const wxString& strKey)
238{
239 Close();
240
241 m_strKey = strKey;
242 m_hRootKey = (WXHKEY) aStdKeys[ExtractKeyName(m_strKey)].hkey;
243}
244
245// the name is relative to the parent key
246void wxRegKey::SetName(StdKey keyParent, const wxString& strKey)
247{
248 Close();
249
250 m_strKey = strKey;
251 RemoveTrailingSeparator(m_strKey);
252 m_hRootKey = (WXHKEY) aStdKeys[keyParent].hkey;
253}
254
255// the name is relative to the parent key
256void wxRegKey::SetName(const wxRegKey& keyParent, const wxString& strKey)
257{
258 Close();
259
260 // combine our name with parent's to get the full name
261
262 // NB: this method is called by wxRegConfig::SetPath() which is a performance
263 // critical function and so it preallocates space for our m_strKey to
264 // gain some speed - this is why we only use += here and not = which
265 // would just free the prealloc'd buffer and would have to realloc it the
266 // next line!
267 m_strKey.clear();
268 m_strKey += keyParent.m_strKey;
269 if ( !strKey.empty() && strKey[0] != REG_SEPARATOR )
270 m_strKey += REG_SEPARATOR;
271 m_strKey += strKey;
272
273 RemoveTrailingSeparator(m_strKey);
274
275 m_hRootKey = keyParent.m_hRootKey;
276}
277
278// hKey should be opened and will be closed in wxRegKey dtor
279void wxRegKey::SetHkey(WXHKEY hKey)
280{
281 Close();
282
283 m_hKey = hKey;
284}
285
286// ----------------------------------------------------------------------------
287// info about the key
288// ----------------------------------------------------------------------------
289
290// returns true if the key exists
291bool wxRegKey::Exists() const
292{
293 // opened key has to exist, try to open it if not done yet
294 return IsOpened() ? true : KeyExists(m_hRootKey, m_strKey);
295}
296
297// returns the full name of the key (prefix is abbreviated if bShortPrefix)
298wxString wxRegKey::GetName(bool bShortPrefix) const
299{
300 StdKey key = GetStdKeyFromHkey((WXHKEY) m_hRootKey);
301 wxString str = bShortPrefix ? aStdKeys[key].szShortName
302 : aStdKeys[key].szName;
303 if ( !m_strKey.empty() )
304 str << _T("\\") << m_strKey;
305
306 return str;
307}
308
309bool wxRegKey::GetKeyInfo(size_t *pnSubKeys,
310 size_t *pnMaxKeyLen,
311 size_t *pnValues,
312 size_t *pnMaxValueLen) const
313{
314 // old gcc headers incorrectly prototype RegQueryInfoKey()
315#if defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__)
316 #define REG_PARAM (size_t *)
317#else
318 #define REG_PARAM (LPDWORD)
319#endif
320
321 // it might be unexpected to some that this function doesn't open the key
322 wxASSERT_MSG( IsOpened(), _T("key should be opened in GetKeyInfo") );
323
324 m_dwLastError = ::RegQueryInfoKey
325 (
326 (HKEY) m_hKey,
327 NULL, // class name
328 NULL, // (ptr to) size of class name buffer
329 RESERVED,
330 REG_PARAM
331 pnSubKeys, // [out] number of subkeys
332 REG_PARAM
333 pnMaxKeyLen, // [out] max length of a subkey name
334 NULL, // longest subkey class name
335 REG_PARAM
336 pnValues, // [out] number of values
337 REG_PARAM
338 pnMaxValueLen, // [out] max length of a value name
339 NULL, // longest value data
340 NULL, // security descriptor
341 NULL // time of last modification
342 );
343
344#undef REG_PARAM
345
346 if ( m_dwLastError != ERROR_SUCCESS ) {
347 wxLogSysError(m_dwLastError, _("Can't get info about registry key '%s'"),
348 GetName().c_str());
349 return false;
350 }
351
352 return true;
353}
354
355// ----------------------------------------------------------------------------
356// operations
357// ----------------------------------------------------------------------------
358
359// opens key (it's not an error to call Open() on an already opened key)
360bool wxRegKey::Open(AccessMode mode)
361{
362 if ( IsOpened() )
363 {
364 if ( mode <= m_mode )
365 return true;
366
367 // we had been opened in read mode but now must be reopened in write
368 Close();
369 }
370
371 HKEY tmpKey;
372 m_dwLastError = ::RegOpenKeyEx
373 (
374 (HKEY) m_hRootKey,
375 m_strKey,
376 RESERVED,
377 mode == Read ? KEY_READ : KEY_ALL_ACCESS,
378 &tmpKey
379 );
380
381 if ( m_dwLastError != ERROR_SUCCESS )
382 {
383 wxLogSysError(m_dwLastError, _("Can't open registry key '%s'"),
384 GetName().c_str());
385 return false;
386 }
387
388 m_hKey = (WXHKEY) tmpKey;
389 m_mode = mode;
390
391 return true;
392}
393
394// creates key, failing if it exists and !bOkIfExists
395bool wxRegKey::Create(bool bOkIfExists)
396{
397 // check for existence only if asked (i.e. order is important!)
398 if ( !bOkIfExists && Exists() )
399 return false;
400
401 if ( IsOpened() )
402 return true;
403
404 HKEY tmpKey;
405#ifdef __WXWINCE__
406 DWORD disposition;
407 m_dwLastError = RegCreateKeyEx((HKEY) m_hRootKey, m_strKey,
408 NULL, // reserved
409 NULL, // class string
410 0,
411 0,
412 NULL,
413 &tmpKey,
414 &disposition);
415#else
416 m_dwLastError = RegCreateKey((HKEY) m_hRootKey, m_strKey, &tmpKey);
417#endif
418 if ( m_dwLastError != ERROR_SUCCESS ) {
419 wxLogSysError(m_dwLastError, _("Can't create registry key '%s'"),
420 GetName().c_str());
421 return false;
422 }
423 else
424 {
425 m_hKey = (WXHKEY) tmpKey;
426 return true;
427 }
428}
429
430// close the key, it's not an error to call it when not opened
431bool wxRegKey::Close()
432{
433 if ( IsOpened() ) {
434 m_dwLastError = RegCloseKey((HKEY) m_hKey);
435 m_hKey = 0;
436
437 if ( m_dwLastError != ERROR_SUCCESS ) {
438 wxLogSysError(m_dwLastError, _("Can't close registry key '%s'"),
439 GetName().c_str());
440
441 return false;
442 }
443 }
444
445 return true;
446}
447
448bool wxRegKey::RenameValue(const wxChar *szValueOld, const wxChar *szValueNew)
449{
450 bool ok = true;
451 if ( HasValue(szValueNew) ) {
452 wxLogError(_("Registry value '%s' already exists."), szValueNew);
453
454 ok = false;
455 }
456
457 if ( !ok ||
458 !CopyValue(szValueOld, *this, szValueNew) ||
459 !DeleteValue(szValueOld) ) {
460 wxLogError(_("Failed to rename registry value '%s' to '%s'."),
461 szValueOld, szValueNew);
462
463 return false;
464 }
465
466 return true;
467}
468
469bool wxRegKey::CopyValue(const wxChar *szValue,
470 wxRegKey& keyDst,
471 const wxChar *szValueNew)
472{
473 if ( !szValueNew ) {
474 // by default, use the same name
475 szValueNew = szValue;
476 }
477
478 switch ( GetValueType(szValue) ) {
479 case Type_String:
480 {
481 wxString strVal;
482 return QueryValue(szValue, strVal) &&
483 keyDst.SetValue(szValueNew, strVal);
484 }
485
486 case Type_Dword:
487 /* case Type_Dword_little_endian: == Type_Dword */
488 {
489 long dwVal;
490 return QueryValue(szValue, &dwVal) &&
491 keyDst.SetValue(szValueNew, dwVal);
492 }
493
494 case Type_Binary:
495 {
496 wxMemoryBuffer buf;
497 return QueryValue(szValue,buf) &&
498 keyDst.SetValue(szValueNew,buf);
499 }
500
501 // these types are unsupported because I am not sure about how
502 // exactly they should be copied and because they shouldn't
503 // occur among the application keys (supposedly created with
504 // this class)
505 case Type_None:
506 case Type_Expand_String:
507 case Type_Dword_big_endian:
508 case Type_Link:
509 case Type_Multi_String:
510 case Type_Resource_list:
511 case Type_Full_resource_descriptor:
512 case Type_Resource_requirements_list:
513 default:
514 wxLogError(_("Can't copy values of unsupported type %d."),
515 GetValueType(szValue));
516 return false;
517 }
518}
519
520bool wxRegKey::Rename(const wxChar *szNewName)
521{
522 wxCHECK_MSG( !m_strKey.empty(), false, _T("registry hives can't be renamed") );
523
524 if ( !Exists() ) {
525 wxLogError(_("Registry key '%s' does not exist, cannot rename it."),
526 GetFullName(this));
527
528 return false;
529 }
530
531 // do we stay in the same hive?
532 bool inSameHive = !wxStrchr(szNewName, REG_SEPARATOR);
533
534 // construct the full new name of the key
535 wxRegKey keyDst;
536
537 if ( inSameHive ) {
538 // rename the key to the new name under the same parent
539 wxString strKey = m_strKey.BeforeLast(REG_SEPARATOR);
540 if ( !strKey.empty() ) {
541 // don't add '\\' in the start if strFullNewName is empty
542 strKey += REG_SEPARATOR;
543 }
544
545 strKey += szNewName;
546
547 keyDst.SetName(GetStdKeyFromHkey(m_hRootKey), strKey);
548 }
549 else {
550 // this is the full name already
551 keyDst.SetName(szNewName);
552 }
553
554 bool ok = keyDst.Create(false /* fail if alredy exists */);
555 if ( !ok ) {
556 wxLogError(_("Registry key '%s' already exists."),
557 GetFullName(&keyDst));
558 }
559 else {
560 ok = Copy(keyDst) && DeleteSelf();
561 }
562
563 if ( !ok ) {
564 wxLogError(_("Failed to rename the registry key '%s' to '%s'."),
565 GetFullName(this), GetFullName(&keyDst));
566 }
567 else {
568 m_hRootKey = keyDst.m_hRootKey;
569 m_strKey = keyDst.m_strKey;
570 }
571
572 return ok;
573}
574
575bool wxRegKey::Copy(const wxChar *szNewName)
576{
577 // create the new key first
578 wxRegKey keyDst(szNewName);
579 bool ok = keyDst.Create(false /* fail if alredy exists */);
580 if ( ok ) {
581 ok = Copy(keyDst);
582
583 // we created the dest key but copying to it failed - delete it
584 if ( !ok ) {
585 (void)keyDst.DeleteSelf();
586 }
587 }
588
589 return ok;
590}
591
592bool wxRegKey::Copy(wxRegKey& keyDst)
593{
594 bool ok = true;
595
596 // copy all sub keys to the new location
597 wxString strKey;
598 long lIndex;
599 bool bCont = GetFirstKey(strKey, lIndex);
600 while ( ok && bCont ) {
601 wxRegKey key(*this, strKey);
602 wxString keyName;
603 keyName << GetFullName(&keyDst) << REG_SEPARATOR << strKey;
604 ok = key.Copy((const wxChar*) keyName);
605
606 if ( ok )
607 bCont = GetNextKey(strKey, lIndex);
608 else
609 wxLogError(_("Failed to copy the registry subkey '%s' to '%s'."),
610 GetFullName(&key), keyName.c_str());
611
612 }
613
614 // copy all values
615 wxString strVal;
616 bCont = GetFirstValue(strVal, lIndex);
617 while ( ok && bCont ) {
618 ok = CopyValue(strVal, keyDst);
619
620 if ( !ok ) {
621 wxLogSysError(m_dwLastError,
622 _("Failed to copy registry value '%s'"),
623 strVal.c_str());
624 }
625 else {
626 bCont = GetNextValue(strVal, lIndex);
627 }
628 }
629
630 if ( !ok ) {
631 wxLogError(_("Failed to copy the contents of registry key '%s' to '%s'."),
632 GetFullName(this), GetFullName(&keyDst));
633 }
634
635 return ok;
636}
637
638// ----------------------------------------------------------------------------
639// delete keys/values
640// ----------------------------------------------------------------------------
641bool wxRegKey::DeleteSelf()
642{
643 {
644 wxLogNull nolog;
645 if ( !Open() ) {
646 // it already doesn't exist - ok!
647 return true;
648 }
649 }
650
651 // prevent a buggy program from erasing one of the root registry keys or an
652 // immediate subkey (i.e. one which doesn't have '\\' inside) of any other
653 // key except HKCR (HKCR has some "deleteable" subkeys)
654 if ( m_strKey.empty() ||
655 ((m_hRootKey != (WXHKEY) aStdKeys[HKCR].hkey) &&
656 (m_strKey.Find(REG_SEPARATOR) == wxNOT_FOUND)) ) {
657 wxLogError(_("Registry key '%s' is needed for normal system operation,\ndeleting it will leave your system in unusable state:\noperation aborted."),
658 GetFullName(this));
659
660 return false;
661 }
662
663 // we can't delete keys while enumerating because it confuses GetNextKey, so
664 // we first save the key names and then delete them all
665 wxArrayString astrSubkeys;
666
667 wxString strKey;
668 long lIndex;
669 bool bCont = GetFirstKey(strKey, lIndex);
670 while ( bCont ) {
671 astrSubkeys.Add(strKey);
672
673 bCont = GetNextKey(strKey, lIndex);
674 }
675
676 size_t nKeyCount = astrSubkeys.Count();
677 for ( size_t nKey = 0; nKey < nKeyCount; nKey++ ) {
678 wxRegKey key(*this, astrSubkeys[nKey]);
679 if ( !key.DeleteSelf() )
680 return false;
681 }
682
683 // now delete this key itself
684 Close();
685
686 m_dwLastError = RegDeleteKey((HKEY) m_hRootKey, m_strKey);
687 // deleting a key which doesn't exist is not considered an error
688 if ( m_dwLastError != ERROR_SUCCESS &&
689 m_dwLastError != ERROR_FILE_NOT_FOUND ) {
690 wxLogSysError(m_dwLastError, _("Can't delete key '%s'"),
691 GetName().c_str());
692 return false;
693 }
694
695 return true;
696}
697
698bool wxRegKey::DeleteKey(const wxChar *szKey)
699{
700 if ( !Open() )
701 return false;
702
703 wxRegKey key(*this, szKey);
704 return key.DeleteSelf();
705}
706
707bool wxRegKey::DeleteValue(const wxChar *szValue)
708{
709 if ( !Open() )
710 return false;
711
712 m_dwLastError = RegDeleteValue((HKEY) m_hKey, WXSTRINGCAST szValue);
713
714 // deleting a value which doesn't exist is not considered an error
715 if ( (m_dwLastError != ERROR_SUCCESS) &&
716 (m_dwLastError != ERROR_FILE_NOT_FOUND) )
717 {
718 wxLogSysError(m_dwLastError, _("Can't delete value '%s' from key '%s'"),
719 szValue, GetName().c_str());
720 return false;
721 }
722
723 return true;
724}
725
726// ----------------------------------------------------------------------------
727// access to values and subkeys
728// ----------------------------------------------------------------------------
729
730// return true if value exists
731bool wxRegKey::HasValue(const wxChar *szValue) const
732{
733 // this function should be silent, so suppress possible messages from Open()
734 wxLogNull nolog;
735
736 if ( !CONST_CAST Open(Read) )
737 return false;
738
739 LONG dwRet = ::RegQueryValueEx((HKEY) m_hKey,
740 WXSTRINGCAST szValue,
741 RESERVED,
742 NULL, NULL, NULL);
743 return dwRet == ERROR_SUCCESS;
744}
745
746// returns true if this key has any values
747bool wxRegKey::HasValues() const
748{
749 // suppress possible messages from GetFirstValue()
750 wxLogNull nolog;
751
752 // just call GetFirstValue with dummy parameters
753 wxString str;
754 long l;
755 return CONST_CAST GetFirstValue(str, l);
756}
757
758// returns true if this key has any subkeys
759bool wxRegKey::HasSubkeys() const
760{
761 // suppress possible messages from GetFirstKey()
762 wxLogNull nolog;
763
764 // just call GetFirstKey with dummy parameters
765 wxString str;
766 long l;
767 return CONST_CAST GetFirstKey(str, l);
768}
769
770// returns true if given subkey exists
771bool wxRegKey::HasSubKey(const wxChar *szKey) const
772{
773 // this function should be silent, so suppress possible messages from Open()
774 wxLogNull nolog;
775
776 if ( !CONST_CAST Open(Read) )
777 return false;
778
779 return KeyExists(m_hKey, szKey);
780}
781
782wxRegKey::ValueType wxRegKey::GetValueType(const wxChar *szValue) const
783{
784 if ( ! CONST_CAST Open(Read) )
785 return Type_None;
786
787 DWORD dwType;
788 m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED,
789 &dwType, NULL, NULL);
790 if ( m_dwLastError != ERROR_SUCCESS ) {
791 wxLogSysError(m_dwLastError, _("Can't read value of key '%s'"),
792 GetName().c_str());
793 return Type_None;
794 }
795
796 return (ValueType)dwType;
797}
798
799bool wxRegKey::SetValue(const wxChar *szValue, long lValue)
800{
801 if ( CONST_CAST Open() ) {
802 m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_DWORD,
803 (RegString)&lValue, sizeof(lValue));
804 if ( m_dwLastError == ERROR_SUCCESS )
805 return true;
806 }
807
808 wxLogSysError(m_dwLastError, _("Can't set value of '%s'"),
809 GetFullName(this, szValue));
810 return false;
811}
812
813bool wxRegKey::QueryValue(const wxChar *szValue, long *plValue) const
814{
815 if ( CONST_CAST Open(Read) ) {
816 DWORD dwType, dwSize = sizeof(DWORD);
817 RegString pBuf = (RegString)plValue;
818 m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED,
819 &dwType, pBuf, &dwSize);
820 if ( m_dwLastError != ERROR_SUCCESS ) {
821 wxLogSysError(m_dwLastError, _("Can't read value of key '%s'"),
822 GetName().c_str());
823 return false;
824 }
825 else {
826 // check that we read the value of right type
827 wxASSERT_MSG( IsNumericValue(szValue),
828 wxT("Type mismatch in wxRegKey::QueryValue().") );
829
830 return true;
831 }
832 }
833 else
834 return false;
835}
836
837bool wxRegKey::SetValue(const wxChar *szValue,const wxMemoryBuffer& buffer)
838{
839#ifdef __TWIN32__
840 wxFAIL_MSG("RegSetValueEx not implemented by TWIN32");
841 return false;
842#else
843 if ( CONST_CAST Open() ) {
844 m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_BINARY,
845 (RegBinary)buffer.GetData(),buffer.GetDataLen());
846 if ( m_dwLastError == ERROR_SUCCESS )
847 return true;
848 }
849
850 wxLogSysError(m_dwLastError, _("Can't set value of '%s'"),
851 GetFullName(this, szValue));
852 return false;
853#endif
854}
855
856bool wxRegKey::QueryValue(const wxChar *szValue, wxMemoryBuffer& buffer) const
857{
858 if ( CONST_CAST Open(Read) ) {
859 // first get the type and size of the data
860 DWORD dwType, dwSize;
861 m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED,
862 &dwType, NULL, &dwSize);
863
864 if ( m_dwLastError == ERROR_SUCCESS ) {
865 if ( dwSize ) {
866 const RegBinary pBuf = (RegBinary)buffer.GetWriteBuf(dwSize);
867 m_dwLastError = RegQueryValueEx((HKEY) m_hKey,
868 WXSTRINGCAST szValue,
869 RESERVED,
870 &dwType,
871 pBuf,
872 &dwSize);
873 buffer.UngetWriteBuf(dwSize);
874 } else {
875 buffer.SetDataLen(0);
876 }
877 }
878
879
880 if ( m_dwLastError != ERROR_SUCCESS ) {
881 wxLogSysError(m_dwLastError, _("Can't read value of key '%s'"),
882 GetName().c_str());
883 return false;
884 }
885 return true;
886 }
887 return false;
888}
889
890
891
892bool wxRegKey::QueryValue(const wxChar *szValue,
893 wxString& strValue,
894 bool WXUNUSED_IN_WINCE(raw)) const
895{
896 if ( CONST_CAST Open(Read) )
897 {
898
899 // first get the type and size of the data
900 DWORD dwType=REG_NONE, dwSize=0;
901 m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED,
902 &dwType, NULL, &dwSize);
903 if ( m_dwLastError == ERROR_SUCCESS )
904 {
905 if ( !dwSize )
906 {
907 // must treat this case specially as GetWriteBuf() doesn't like
908 // being called with 0 size
909 strValue.Empty();
910 }
911 else
912 {
913 m_dwLastError = RegQueryValueEx((HKEY) m_hKey,
914 WXSTRINGCAST szValue,
915 RESERVED,
916 &dwType,
917 (RegString)(wxChar*)wxStringBuffer(strValue, dwSize),
918 &dwSize);
919
920 // expand the var expansions in the string unless disabled
921#ifndef __WXWINCE__
922 if ( (dwType == REG_EXPAND_SZ) && !raw )
923 {
924 DWORD dwExpSize = ::ExpandEnvironmentStrings(strValue, NULL, 0);
925 bool ok = dwExpSize != 0;
926 if ( ok )
927 {
928 wxString strExpValue;
929 ok = ::ExpandEnvironmentStrings(strValue,
930 wxStringBuffer(strExpValue, dwExpSize),
931 dwExpSize
932 ) != 0;
933 strValue = strExpValue;
934 }
935
936 if ( !ok )
937 {
938 wxLogLastError(_T("ExpandEnvironmentStrings"));
939 }
940 }
941#endif
942 // __WXWINCE__
943 }
944
945 if ( m_dwLastError == ERROR_SUCCESS )
946 {
947 // check that it was the right type
948 wxASSERT_MSG( !IsNumericValue(szValue),
949 wxT("Type mismatch in wxRegKey::QueryValue().") );
950
951 return true;
952 }
953 }
954 }
955
956 wxLogSysError(m_dwLastError, _("Can't read value of '%s'"),
957 GetFullName(this, szValue));
958 return false;
959}
960
961bool wxRegKey::SetValue(const wxChar *szValue, const wxString& strValue)
962{
963 if ( CONST_CAST Open() ) {
964 m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_SZ,
965 (RegString)strValue.c_str(),
966 (strValue.Len() + 1)*sizeof(wxChar));
967 if ( m_dwLastError == ERROR_SUCCESS )
968 return true;
969 }
970
971 wxLogSysError(m_dwLastError, _("Can't set value of '%s'"),
972 GetFullName(this, szValue));
973 return false;
974}
975
976wxString wxRegKey::QueryDefaultValue() const
977{
978 wxString str;
979 QueryValue(NULL, str);
980 return str;
981}
982
983// ----------------------------------------------------------------------------
984// enumeration
985// NB: all these functions require an index variable which allows to have
986// several concurrently running indexations on the same key
987// ----------------------------------------------------------------------------
988
989bool wxRegKey::GetFirstValue(wxString& strValueName, long& lIndex)
990{
991 if ( !Open(Read) )
992 return false;
993
994 lIndex = 0;
995 return GetNextValue(strValueName, lIndex);
996}
997
998bool wxRegKey::GetNextValue(wxString& strValueName, long& lIndex) const
999{
1000 wxASSERT( IsOpened() );
1001
1002 // are we already at the end of enumeration?
1003 if ( lIndex == -1 )
1004 return false;
1005
1006 wxChar szValueName[1024]; // @@ use RegQueryInfoKey...
1007 DWORD dwValueLen = WXSIZEOF(szValueName);
1008
1009 m_dwLastError = RegEnumValue((HKEY) m_hKey, lIndex++,
1010 szValueName, &dwValueLen,
1011 RESERVED,
1012 NULL, // [out] type
1013 NULL, // [out] buffer for value
1014 NULL); // [i/o] it's length
1015
1016 if ( m_dwLastError != ERROR_SUCCESS ) {
1017 if ( m_dwLastError == ERROR_NO_MORE_ITEMS ) {
1018 m_dwLastError = ERROR_SUCCESS;
1019 lIndex = -1;
1020 }
1021 else {
1022 wxLogSysError(m_dwLastError, _("Can't enumerate values of key '%s'"),
1023 GetName().c_str());
1024 }
1025
1026 return false;
1027 }
1028
1029 strValueName = szValueName;
1030
1031 return true;
1032}
1033
1034bool wxRegKey::GetFirstKey(wxString& strKeyName, long& lIndex)
1035{
1036 if ( !Open(Read) )
1037 return false;
1038
1039 lIndex = 0;
1040 return GetNextKey(strKeyName, lIndex);
1041}
1042
1043bool wxRegKey::GetNextKey(wxString& strKeyName, long& lIndex) const
1044{
1045 wxASSERT( IsOpened() );
1046
1047 // are we already at the end of enumeration?
1048 if ( lIndex == -1 )
1049 return false;
1050
1051 wxChar szKeyName[_MAX_PATH + 1];
1052
1053#ifdef __WXWINCE__
1054 DWORD sizeName = WXSIZEOF(szKeyName);
1055 m_dwLastError = RegEnumKeyEx((HKEY) m_hKey, lIndex++, szKeyName, & sizeName,
1056 0, NULL, NULL, NULL);
1057#else
1058 m_dwLastError = RegEnumKey((HKEY) m_hKey, lIndex++, szKeyName, WXSIZEOF(szKeyName));
1059#endif
1060
1061 if ( m_dwLastError != ERROR_SUCCESS ) {
1062 if ( m_dwLastError == ERROR_NO_MORE_ITEMS ) {
1063 m_dwLastError = ERROR_SUCCESS;
1064 lIndex = -1;
1065 }
1066 else {
1067 wxLogSysError(m_dwLastError, _("Can't enumerate subkeys of key '%s'"),
1068 GetName().c_str());
1069 }
1070
1071 return false;
1072 }
1073
1074 strKeyName = szKeyName;
1075 return true;
1076}
1077
1078// returns true if the value contains a number (else it's some string)
1079bool wxRegKey::IsNumericValue(const wxChar *szValue) const
1080{
1081 ValueType type = GetValueType(szValue);
1082 switch ( type ) {
1083 case Type_Dword:
1084 /* case Type_Dword_little_endian: == Type_Dword */
1085 case Type_Dword_big_endian:
1086 return true;
1087
1088 default:
1089 return false;
1090 }
1091}
1092
1093// ----------------------------------------------------------------------------
1094// exporting registry keys to file
1095// ----------------------------------------------------------------------------
1096
1097#if wxUSE_STREAMS
1098
1099// helper functions for writing ASCII strings (even in Unicode build)
1100static inline bool WriteAsciiChar(wxOutputStream& ostr, char ch)
1101{
1102 ostr.PutC(ch);
1103 return ostr.IsOk();
1104}
1105
1106static inline bool WriteAsciiEOL(wxOutputStream& ostr)
1107{
1108 // as we open the file in text mode, it is enough to write LF without CR
1109 return WriteAsciiChar(ostr, '\n');
1110}
1111
1112static inline bool WriteAsciiString(wxOutputStream& ostr, const char *p)
1113{
1114 return ostr.Write(p, strlen(p)).IsOk();
1115}
1116
1117static inline bool WriteAsciiString(wxOutputStream& ostr, const wxString& s)
1118{
1119#if wxUSE_UNICODE
1120 wxCharBuffer name(s.mb_str());
1121 ostr.Write(name, strlen(name));
1122#else
1123 ostr.Write(s, s.length());
1124#endif
1125
1126 return ostr.IsOk();
1127}
1128
1129#endif // wxUSE_STREAMS
1130
1131bool wxRegKey::Export(const wxString& filename) const
1132{
1133#if wxUSE_FFILE && wxUSE_STREAMS
1134 if ( wxFile::Exists(filename) )
1135 {
1136 wxLogError(_("Exporting registry key: file \"%s\" already exists and won't be overwritten."),
1137 filename.c_str());
1138 return false;
1139 }
1140
1141 wxFFileOutputStream ostr(filename, _T("w"));
1142
1143 return ostr.Ok() && Export(ostr);
1144#else
1145 wxUnusedVar(filename);
1146 return false;
1147#endif
1148}
1149
1150#if wxUSE_STREAMS
1151bool wxRegKey::Export(wxOutputStream& ostr) const
1152{
1153 // write out the header
1154 if ( !WriteAsciiString(ostr, "REGEDIT4\n\n") )
1155 return false;
1156
1157 return DoExport(ostr);
1158}
1159#endif // wxUSE_STREAMS
1160
1161static
1162wxString
1163FormatAsHex(const void *data,
1164 size_t size,
1165 wxRegKey::ValueType type = wxRegKey::Type_Binary)
1166{
1167 wxString value(_T("hex"));
1168
1169 // binary values use just "hex:" prefix while the other ones must indicate
1170 // the real type
1171 if ( type != wxRegKey::Type_Binary )
1172 value << _T('(') << type << _T(')');
1173 value << _T(':');
1174
1175 // write all the rest as comma-separated bytes
1176 value.reserve(3*size + 10);
1177 const char * const p = wx_static_cast(const char *, data);
1178 for ( size_t n = 0; n < size; n++ )
1179 {
1180 // TODO: line wrapping: although not required by regedit, this makes
1181 // the generated files easier to read and compare with the files
1182 // produced by regedit
1183 if ( n )
1184 value << _T(',');
1185
1186 value << wxString::Format(_T("%02x"), (unsigned char)p[n]);
1187 }
1188
1189 return value;
1190}
1191
1192static inline
1193wxString FormatAsHex(const wxString& value, wxRegKey::ValueType type)
1194{
1195 return FormatAsHex(value.c_str(), value.length() + 1, type);
1196}
1197
1198wxString wxRegKey::FormatValue(const wxString& name) const
1199{
1200 wxString rhs;
1201 const ValueType type = GetValueType(name);
1202 switch ( type )
1203 {
1204 case Type_String:
1205 {
1206 wxString value;
1207 if ( !QueryValue(name, value) )
1208 break;
1209
1210 // quotes and backslashes must be quoted, linefeeds are not
1211 // allowed in string values
1212 rhs.reserve(value.length() + 2);
1213 rhs = _T('"');
1214
1215 // there can be no NULs here
1216 bool useHex = false;
1217 for ( const wxChar *p = value.c_str(); *p && !useHex; p++ )
1218 {
1219 switch ( *p )
1220 {
1221 case _T('\n'):
1222 // we can only represent this string in hex
1223 useHex = true;
1224 break;
1225
1226 case _T('"'):
1227 case _T('\\'):
1228 // escape special symbol
1229 rhs += _T('\\');
1230 // fall through
1231
1232 default:
1233 rhs += *p;
1234 }
1235 }
1236
1237 if ( useHex )
1238 rhs = FormatAsHex(value, Type_String);
1239 else
1240 rhs += _T('"');
1241 }
1242 break;
1243
1244 case Type_Dword:
1245 /* case Type_Dword_little_endian: == Type_Dword */
1246 {
1247 long value;
1248 if ( !QueryValue(name, &value) )
1249 break;
1250
1251 rhs.Printf(_T("dword:%08x"), (unsigned int)value);
1252 }
1253 break;
1254
1255 case Type_Expand_String:
1256 case Type_Multi_String:
1257 {
1258 wxString value;
1259 if ( !QueryRawValue(name, value) )
1260 break;
1261
1262 rhs = FormatAsHex(value, type);
1263 }
1264 break;
1265
1266 case Type_Binary:
1267 {
1268 wxMemoryBuffer buf;
1269 if ( !QueryValue(name, buf) )
1270 break;
1271
1272 rhs = FormatAsHex(buf.GetData(), buf.GetDataLen());
1273 }
1274 break;
1275
1276 // no idea how those appear in REGEDIT4 files
1277 case Type_None:
1278 case Type_Dword_big_endian:
1279 case Type_Link:
1280 case Type_Resource_list:
1281 case Type_Full_resource_descriptor:
1282 case Type_Resource_requirements_list:
1283 default:
1284 wxLogWarning(_("Can't export value of unsupported type %d."), type);
1285 }
1286
1287 return rhs;
1288}
1289
1290#if wxUSE_STREAMS
1291
1292bool wxRegKey::DoExportValue(wxOutputStream& ostr, const wxString& name) const
1293{
1294 // first examine the value type: if it's unsupported, simply skip it
1295 // instead of aborting the entire export process because we failed to
1296 // export a single value
1297 wxString value = FormatValue(name);
1298 if ( value.empty() )
1299 {
1300 wxLogWarning(_("Ignoring value \"%s\" of the key \"%s\"."),
1301 name.c_str(), GetName().c_str());
1302 return true;
1303 }
1304
1305 // we do have the text representation of the value, now write everything
1306 // out
1307
1308 // special case: unnamed/default value is represented as just "@"
1309 if ( name.empty() )
1310 {
1311 if ( !WriteAsciiChar(ostr, '@') )
1312 return false;
1313 }
1314 else // normal, named, value
1315 {
1316 if ( !WriteAsciiChar(ostr, '"') ||
1317 !WriteAsciiString(ostr, name) ||
1318 !WriteAsciiChar(ostr, '"') )
1319 return false;
1320 }
1321
1322 if ( !WriteAsciiChar(ostr, '=') )
1323 return false;
1324
1325 return WriteAsciiString(ostr, value) && WriteAsciiEOL(ostr);
1326}
1327
1328bool wxRegKey::DoExport(wxOutputStream& ostr) const
1329{
1330 // write out this key name
1331 if ( !WriteAsciiChar(ostr, '[') )
1332 return false;
1333
1334 if ( !WriteAsciiString(ostr, GetName(false /* no short prefix */)) )
1335 return false;
1336
1337 if ( !WriteAsciiChar(ostr, ']') || !WriteAsciiEOL(ostr) )
1338 return false;
1339
1340 // dump all our values
1341 long dummy;
1342 wxString name;
1343 wxRegKey& self = wx_const_cast(wxRegKey&, *this);
1344 bool cont = self.GetFirstValue(name, dummy);
1345 while ( cont )
1346 {
1347 if ( !DoExportValue(ostr, name) )
1348 return false;
1349
1350 cont = GetNextValue(name, dummy);
1351 }
1352
1353 // always terminate values by blank line, even if there were no values
1354 if ( !WriteAsciiEOL(ostr) )
1355 return false;
1356
1357 // recurse to subkeys
1358 cont = self.GetFirstKey(name, dummy);
1359 while ( cont )
1360 {
1361 wxRegKey subkey(*this, name);
1362 if ( !subkey.DoExport(ostr) )
1363 return false;
1364
1365 cont = GetNextKey(name, dummy);
1366 }
1367
1368 return true;
1369}
1370
1371#endif // wxUSE_STREAMS
1372
1373// ============================================================================
1374// implementation of global private functions
1375// ============================================================================
1376
1377bool KeyExists(WXHKEY hRootKey, const wxChar *szKey)
1378{
1379 // don't close this key itself for the case of empty szKey!
1380 if ( wxIsEmpty(szKey) )
1381 return true;
1382
1383 HKEY hkeyDummy;
1384 if ( ::RegOpenKeyEx
1385 (
1386 (HKEY)hRootKey,
1387 szKey,
1388 RESERVED,
1389 KEY_READ, // we might not have enough rights for rw access
1390 &hkeyDummy
1391 ) == ERROR_SUCCESS )
1392 {
1393 ::RegCloseKey(hkeyDummy);
1394
1395 return true;
1396 }
1397
1398 return false;
1399}
1400
1401const wxChar *GetFullName(const wxRegKey *pKey, const wxChar *szValue)
1402{
1403 static wxString s_str;
1404 s_str = pKey->GetName();
1405 if ( !wxIsEmpty(szValue) )
1406 s_str << wxT("\\") << szValue;
1407
1408 return s_str.c_str();
1409}
1410
1411void RemoveTrailingSeparator(wxString& str)
1412{
1413 if ( !str.empty() && str.Last() == REG_SEPARATOR )
1414 str.Truncate(str.Len() - 1);
1415}
1416