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