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