]> git.saurik.com Git - wxWidgets.git/blame - src/msw/registry.cpp
reverted last commit, still broken for VC6
[wxWidgets.git] / src / msw / registry.cpp
CommitLineData
2bda0e17
KB
1///////////////////////////////////////////////////////////////////////////////
2// Name: msw/registry.cpp
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
14f355c2 15#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
27529614
JS
16#pragma implementation "registry.h"
17#endif
18
2bda0e17
KB
19// for compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23#pragma hdrstop
24#endif
25
77ffb593 26// other wxWidgets headers
2bda0e17
KB
27#include "wx/string.h"
28#include "wx/intl.h"
29#include "wx/log.h"
41286812 30
2bda0e17 31// Windows headers
57a7b7c1 32/*
2bda0e17
KB
33#define STRICT
34#define WIN32_LEAN_AND_MEAN
57a7b7c1
JS
35*/
36
9ed0d735 37#include "wx/msw/wrapwin.h"
2bda0e17 38
4676948b
JS
39#ifdef __WXWINCE__
40#include "wx/msw/private.h"
41#include <winbase.h>
42#include <winreg.h>
43#endif
44
2bda0e17
KB
45// other std headers
46#include <stdlib.h> // for _MAX_PATH
47
48#ifndef _MAX_PATH
225fe9d6 49 #define _MAX_PATH 512
2bda0e17
KB
50#endif
51
52// our header
53#define HKEY_DEFINED // already defined in windows.h
54#include "wx/msw/registry.h"
55
56// some registry functions don't like signed chars
57typedef unsigned char *RegString;
9b386eca 58typedef BYTE* RegBinary;
2bda0e17
KB
59
60// ----------------------------------------------------------------------------
61// constants
62// ----------------------------------------------------------------------------
63
64// the standard key names, short names and handles all bundled together for
65// convenient access
66static struct
67{
68 HKEY hkey;
837e5743
OK
69 const wxChar *szName;
70 const wxChar *szShortName;
2bda0e17 71}
23f681ec
VZ
72aStdKeys[] =
73{
223d09f6 74 { HKEY_CLASSES_ROOT, wxT("HKEY_CLASSES_ROOT"), wxT("HKCR") },
2bda0e17 75#ifdef __WIN32__
223d09f6
KB
76 { HKEY_CURRENT_USER, wxT("HKEY_CURRENT_USER"), wxT("HKCU") },
77 { HKEY_LOCAL_MACHINE, wxT("HKEY_LOCAL_MACHINE"), wxT("HKLM") },
78 { HKEY_USERS, wxT("HKEY_USERS"), wxT("HKU") }, // short name?
4676948b 79#ifndef __WXWINCE__
223d09f6 80 { HKEY_PERFORMANCE_DATA, wxT("HKEY_PERFORMANCE_DATA"), wxT("HKPD") },
4676948b
JS
81#endif
82#if WINVER >= 0x0400 && !defined(__WXWINCE__)
223d09f6 83 { HKEY_CURRENT_CONFIG, wxT("HKEY_CURRENT_CONFIG"), wxT("HKCC") },
4676948b 84#if !defined(__GNUWIN32__) && !defined(__WXWINCE__)
223d09f6 85 { HKEY_DYN_DATA, wxT("HKEY_DYN_DATA"), wxT("HKDD") }, // short name?
0b1c5a6c 86#endif //GNUWIN32
2bda0e17
KB
87#endif //WINVER >= 4.0
88#endif //WIN32
89};
90
91// the registry name separator (perhaps one day MS will change it to '/' ;-)
223d09f6 92#define REG_SEPARATOR wxT('\\')
2bda0e17 93
c86f1403
VZ
94// useful for Windows programmers: makes somewhat more clear all these zeroes
95// being passed to Windows APIs
007007c5 96#define RESERVED (0)
c86f1403 97
2bda0e17
KB
98// ----------------------------------------------------------------------------
99// macros
100// ----------------------------------------------------------------------------
807a903e
VZ
101
102// const_cast<> is not yet supported by all compilers
2bda0e17
KB
103#define CONST_CAST ((wxRegKey *)this)->
104
807a903e
VZ
105// and neither is mutable which m_dwLastError should be
106#define m_dwLastError CONST_CAST m_dwLastError
2bda0e17
KB
107
108// ----------------------------------------------------------------------------
109// non member functions
110// ----------------------------------------------------------------------------
111
0b1c5a6c
VZ
112// removes the trailing backslash from the string if it has one
113static inline void RemoveTrailingSeparator(wxString& str);
114
4d08943e 115// returns true if given registry key exists
837e5743 116static bool KeyExists(WXHKEY hRootKey, const wxChar *szKey);
2bda0e17
KB
117
118// combines value and key name (uses static buffer!)
23f681ec 119static const wxChar *GetFullName(const wxRegKey *pKey,
837e5743 120 const wxChar *szValue = NULL);
2bda0e17
KB
121
122// ============================================================================
123// implementation of wxRegKey class
124// ============================================================================
125
126// ----------------------------------------------------------------------------
127// static functions and variables
128// ----------------------------------------------------------------------------
129
130const size_t wxRegKey::nStdKeys = WXSIZEOF(aStdKeys);
131
132// @@ should take a `StdKey key', but as it's often going to be used in loops
23f681ec 133// it would require casts in user code.
837e5743 134const wxChar *wxRegKey::GetStdKeyName(size_t key)
2bda0e17
KB
135{
136 // return empty string if key is invalid
fda7962d 137 wxCHECK_MSG( key < nStdKeys, wxEmptyString, wxT("invalid key in wxRegKey::GetStdKeyName") );
2bda0e17
KB
138
139 return aStdKeys[key].szName;
140}
141
837e5743 142const wxChar *wxRegKey::GetStdKeyShortName(size_t key)
2bda0e17
KB
143{
144 // return empty string if key is invalid
fda7962d 145 wxCHECK( key < nStdKeys, wxEmptyString );
2bda0e17
KB
146
147 return aStdKeys[key].szShortName;
148}
149
150wxRegKey::StdKey wxRegKey::ExtractKeyName(wxString& strKey)
151{
b32719cc 152 wxString strRoot = strKey.BeforeFirst(REG_SEPARATOR);
2bda0e17 153
fd3f686c 154 HKEY hRootKey = 0;
c86f1403 155 size_t ui;
2bda0e17 156 for ( ui = 0; ui < nStdKeys; ui++ ) {
23f681ec 157 if ( strRoot.CmpNoCase(aStdKeys[ui].szName) == 0 ||
2bda0e17
KB
158 strRoot.CmpNoCase(aStdKeys[ui].szShortName) == 0 ) {
159 hRootKey = aStdKeys[ui].hkey;
160 break;
161 }
162 }
163
164 if ( ui == nStdKeys ) {
223d09f6 165 wxFAIL_MSG(wxT("invalid key prefix in wxRegKey::ExtractKeyName."));
2bda0e17
KB
166
167 hRootKey = HKEY_CLASSES_ROOT;
168 }
169 else {
170 strKey = strKey.After(REG_SEPARATOR);
171 if ( !strKey.IsEmpty() && strKey.Last() == REG_SEPARATOR )
172 strKey.Truncate(strKey.Len() - 1);
173 }
174
175 return (wxRegKey::StdKey)(int)hRootKey;
176}
177
fd6c844b 178wxRegKey::StdKey wxRegKey::GetStdKeyFromHkey(WXHKEY hkey)
2bda0e17 179{
c86f1403 180 for ( size_t ui = 0; ui < nStdKeys; ui++ ) {
fd6c844b 181 if ( (int) aStdKeys[ui].hkey == (int) hkey )
2bda0e17
KB
182 return (StdKey)ui;
183 }
23f681ec 184
223d09f6 185 wxFAIL_MSG(wxT("non root hkey passed to wxRegKey::GetStdKeyFromHkey."));
2bda0e17
KB
186
187 return HKCR;
188}
189
190// ----------------------------------------------------------------------------
191// ctors and dtor
192// ----------------------------------------------------------------------------
193
194wxRegKey::wxRegKey()
195{
fd6c844b 196 m_hRootKey = (WXHKEY) aStdKeys[HKCR].hkey;
807a903e
VZ
197
198 Init();
2bda0e17
KB
199}
200
201wxRegKey::wxRegKey(const wxString& strKey) : m_strKey(strKey)
202{
fd6c844b 203 m_hRootKey = (WXHKEY) aStdKeys[ExtractKeyName(m_strKey)].hkey;
807a903e
VZ
204
205 Init();
2bda0e17
KB
206}
207
208// parent is a predefined (and preopened) key
209wxRegKey::wxRegKey(StdKey keyParent, const wxString& strKey) : m_strKey(strKey)
210{
0b1c5a6c 211 RemoveTrailingSeparator(m_strKey);
fd6c844b 212 m_hRootKey = (WXHKEY) aStdKeys[keyParent].hkey;
807a903e
VZ
213
214 Init();
2bda0e17
KB
215}
216
217// parent is a normal regkey
218wxRegKey::wxRegKey(const wxRegKey& keyParent, const wxString& strKey)
219 : m_strKey(keyParent.m_strKey)
220{
221 // combine our name with parent's to get the full name
23f681ec 222 if ( !m_strKey.IsEmpty() &&
32c66ea2
VZ
223 (strKey.IsEmpty() || strKey[0] != REG_SEPARATOR) ) {
224 m_strKey += REG_SEPARATOR;
225 }
2bda0e17
KB
226
227 m_strKey += strKey;
0b1c5a6c 228 RemoveTrailingSeparator(m_strKey);
2bda0e17
KB
229
230 m_hRootKey = keyParent.m_hRootKey;
807a903e
VZ
231
232 Init();
2bda0e17
KB
233}
234
235// dtor closes the key releasing system resource
236wxRegKey::~wxRegKey()
237{
238 Close();
239}
240
0b1c5a6c
VZ
241// ----------------------------------------------------------------------------
242// change the key name/hkey
243// ----------------------------------------------------------------------------
244
245// set the full key name
246void wxRegKey::SetName(const wxString& strKey)
247{
248 Close();
249
250 m_strKey = strKey;
fd6c844b 251 m_hRootKey = (WXHKEY) aStdKeys[ExtractKeyName(m_strKey)].hkey;
0b1c5a6c
VZ
252}
253
254// the name is relative to the parent key
255void wxRegKey::SetName(StdKey keyParent, const wxString& strKey)
256{
257 Close();
258
259 m_strKey = strKey;
260 RemoveTrailingSeparator(m_strKey);
fd6c844b 261 m_hRootKey = (WXHKEY) aStdKeys[keyParent].hkey;
0b1c5a6c
VZ
262}
263
264// the name is relative to the parent key
265void wxRegKey::SetName(const wxRegKey& keyParent, const wxString& strKey)
266{
267 Close();
268
269 // combine our name with parent's to get the full name
807a903e
VZ
270
271 // NB: this method is called by wxRegConfig::SetPath() which is a performance
272 // critical function and so it preallocates space for our m_strKey to
273 // gain some speed - this is why we only use += here and not = which
274 // would just free the prealloc'd buffer and would have to realloc it the
275 // next line!
276 m_strKey.clear();
277 m_strKey += keyParent.m_strKey;
0b1c5a6c
VZ
278 if ( !strKey.IsEmpty() && strKey[0] != REG_SEPARATOR )
279 m_strKey += REG_SEPARATOR;
02569ba8 280 m_strKey += strKey;
0b1c5a6c
VZ
281
282 RemoveTrailingSeparator(m_strKey);
283
284 m_hRootKey = keyParent.m_hRootKey;
285}
286
287// hKey should be opened and will be closed in wxRegKey dtor
fd6c844b 288void wxRegKey::SetHkey(WXHKEY hKey)
0b1c5a6c
VZ
289{
290 Close();
291
292 m_hKey = hKey;
293}
294
2bda0e17
KB
295// ----------------------------------------------------------------------------
296// info about the key
297// ----------------------------------------------------------------------------
298
4d08943e 299// returns true if the key exists
2bda0e17
KB
300bool wxRegKey::Exists() const
301{
302 // opened key has to exist, try to open it if not done yet
4d08943e 303 return IsOpened() ? true : KeyExists(m_hRootKey, m_strKey);
2bda0e17
KB
304}
305
306// returns the full name of the key (prefix is abbreviated if bShortPrefix)
307wxString wxRegKey::GetName(bool bShortPrefix) const
308{
333b7dac 309 StdKey key = GetStdKeyFromHkey((WXHKEY) m_hRootKey);
23f681ec 310 wxString str = bShortPrefix ? aStdKeys[key].szShortName
2bda0e17
KB
311 : aStdKeys[key].szName;
312 if ( !m_strKey.IsEmpty() )
2b5f62a0 313 str << _T("\\") << m_strKey;
2bda0e17
KB
314
315 return str;
316}
317
23f681ec
VZ
318bool wxRegKey::GetKeyInfo(size_t *pnSubKeys,
319 size_t *pnMaxKeyLen,
320 size_t *pnValues,
321 size_t *pnMaxValueLen) const
02569ba8 322{
23f681ec 323 // old gcc headers incorrectly prototype RegQueryInfoKey()
ae090fdb 324#if defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__)
23f681ec
VZ
325 #define REG_PARAM (size_t *)
326#else
327 #define REG_PARAM (LPDWORD)
328#endif
329
6dfec4b8
VZ
330 // it might be unexpected to some that this function doesn't open the key
331 wxASSERT_MSG( IsOpened(), _T("key should be opened in GetKeyInfo") );
332
02569ba8
VZ
333 m_dwLastError = ::RegQueryInfoKey
334 (
fd6c844b 335 (HKEY) m_hKey,
02569ba8
VZ
336 NULL, // class name
337 NULL, // (ptr to) size of class name buffer
338 RESERVED,
23f681ec 339 REG_PARAM
02569ba8 340 pnSubKeys, // [out] number of subkeys
23f681ec 341 REG_PARAM
02569ba8
VZ
342 pnMaxKeyLen, // [out] max length of a subkey name
343 NULL, // longest subkey class name
23f681ec 344 REG_PARAM
02569ba8 345 pnValues, // [out] number of values
23f681ec 346 REG_PARAM
02569ba8
VZ
347 pnMaxValueLen, // [out] max length of a value name
348 NULL, // longest value data
349 NULL, // security descriptor
350 NULL // time of last modification
351 );
352
23f681ec
VZ
353#undef REG_PARAM
354
02569ba8 355 if ( m_dwLastError != ERROR_SUCCESS ) {
23f681ec 356 wxLogSysError(m_dwLastError, _("Can't get info about registry key '%s'"),
02569ba8 357 GetName().c_str());
4d08943e 358 return false;
02569ba8 359 }
6dfec4b8 360
4d08943e 361 return true;
02569ba8
VZ
362}
363
2bda0e17
KB
364// ----------------------------------------------------------------------------
365// operations
366// ----------------------------------------------------------------------------
367
368// opens key (it's not an error to call Open() on an already opened key)
bee96abf 369bool wxRegKey::Open(AccessMode mode)
2bda0e17 370{
8a8c41dd 371 if ( IsOpened() )
4d08943e 372 return true;
8a8c41dd
VZ
373
374 HKEY tmpKey;
375 m_dwLastError = ::RegOpenKeyEx
376 (
377 (HKEY) m_hRootKey,
378 m_strKey,
379 RESERVED,
bee96abf 380 mode == Read ? KEY_READ : KEY_ALL_ACCESS,
8a8c41dd
VZ
381 &tmpKey
382 );
383
384 if ( m_dwLastError != ERROR_SUCCESS )
385 {
386 wxLogSysError(m_dwLastError, _("Can't open registry key '%s'"),
387 GetName().c_str());
4d08943e 388 return false;
8a8c41dd 389 }
2bda0e17 390
fd6c844b 391 m_hKey = (WXHKEY) tmpKey;
4d08943e 392 return true;
2bda0e17
KB
393}
394
395// creates key, failing if it exists and !bOkIfExists
396bool wxRegKey::Create(bool bOkIfExists)
397{
398 // check for existence only if asked (i.e. order is important!)
8a8c41dd 399 if ( !bOkIfExists && Exists() )
4d08943e 400 return false;
2bda0e17
KB
401
402 if ( IsOpened() )
4d08943e 403 return true;
2bda0e17 404
fd6c844b 405 HKEY tmpKey;
4676948b
JS
406#ifdef __WXWINCE__
407 DWORD disposition;
408 m_dwLastError = RegCreateKeyEx((HKEY) m_hRootKey, m_strKey,
409 NULL, // reserved
410 NULL, // class string
411 0,
412 0,
413 NULL,
414 &tmpKey,
415 &disposition);
416#else
fd6c844b 417 m_dwLastError = RegCreateKey((HKEY) m_hRootKey, m_strKey, &tmpKey);
4676948b 418#endif
2bda0e17 419 if ( m_dwLastError != ERROR_SUCCESS ) {
23f681ec 420 wxLogSysError(m_dwLastError, _("Can't create registry key '%s'"),
2bda0e17 421 GetName().c_str());
4d08943e 422 return false;
2bda0e17
KB
423 }
424 else
fd6c844b
JS
425 {
426 m_hKey = (WXHKEY) tmpKey;
4d08943e 427 return true;
fd6c844b 428 }
2bda0e17
KB
429}
430
0b1c5a6c
VZ
431// close the key, it's not an error to call it when not opened
432bool wxRegKey::Close()
433{
434 if ( IsOpened() ) {
fd6c844b 435 m_dwLastError = RegCloseKey((HKEY) m_hKey);
807a903e
VZ
436 m_hKey = 0;
437
0b1c5a6c 438 if ( m_dwLastError != ERROR_SUCCESS ) {
23f681ec 439 wxLogSysError(m_dwLastError, _("Can't close registry key '%s'"),
0b1c5a6c
VZ
440 GetName().c_str());
441
4d08943e 442 return false;
0b1c5a6c 443 }
0b1c5a6c
VZ
444 }
445
4d08943e 446 return true;
0b1c5a6c
VZ
447}
448
23f681ec
VZ
449bool wxRegKey::RenameValue(const wxChar *szValueOld, const wxChar *szValueNew)
450{
4d08943e 451 bool ok = true;
23f681ec
VZ
452 if ( HasValue(szValueNew) ) {
453 wxLogError(_("Registry value '%s' already exists."), szValueNew);
454
4d08943e 455 ok = false;
23f681ec
VZ
456 }
457
225fe9d6
VZ
458 if ( !ok ||
459 !CopyValue(szValueOld, *this, szValueNew) ||
460 !DeleteValue(szValueOld) ) {
23f681ec
VZ
461 wxLogError(_("Failed to rename registry value '%s' to '%s'."),
462 szValueOld, szValueNew);
463
4d08943e 464 return false;
23f681ec
VZ
465 }
466
4d08943e 467 return true;
23f681ec
VZ
468}
469
470bool wxRegKey::CopyValue(const wxChar *szValue,
471 wxRegKey& keyDst,
472 const wxChar *szValueNew)
473{
5af3f0ef
VZ
474 if ( !szValueNew ) {
475 // by default, use the same name
476 szValueNew = szValue;
477 }
478
23f681ec
VZ
479 switch ( GetValueType(szValue) ) {
480 case Type_String:
481 {
482 wxString strVal;
483 return QueryValue(szValue, strVal) &&
484 keyDst.SetValue(szValueNew, strVal);
485 }
486
487 case Type_Dword:
488 /* case Type_Dword_little_endian: == Type_Dword */
489 {
490 long dwVal;
491 return QueryValue(szValue, &dwVal) &&
492 keyDst.SetValue(szValueNew, dwVal);
493 }
494
9b386eca
RG
495#ifdef __WIN32__
496 case Type_Binary:
4d08943e
WS
497 {
498 wxMemoryBuffer buf;
499 return QueryValue(szValue,buf) &&
500 keyDst.SetValue(szValueNew,buf);
501 }
23f681ec
VZ
502 // these types are unsupported because I am not sure about how
503 // exactly they should be copied and because they shouldn't
504 // occur among the application keys (supposedly created with
505 // this class)
23f681ec
VZ
506 case Type_None:
507 case Type_Expand_String:
23f681ec
VZ
508 case Type_Dword_big_endian:
509 case Type_Link:
510 case Type_Multi_String:
511 case Type_Resource_list:
512 case Type_Full_resource_descriptor:
513 case Type_Resource_requirements_list:
514#endif // Win32
515 default:
516 wxLogError(_("Can't copy values of unsupported type %d."),
517 GetValueType(szValue));
4d08943e 518 return false;
23f681ec
VZ
519 }
520}
521
225fe9d6
VZ
522bool wxRegKey::Rename(const wxChar *szNewName)
523{
4d08943e 524 wxCHECK_MSG( !m_strKey.IsEmpty(), false, _T("registry hives can't be renamed") );
225fe9d6
VZ
525
526 if ( !Exists() ) {
527 wxLogError(_("Registry key '%s' does not exist, cannot rename it."),
528 GetFullName(this));
529
4d08943e 530 return false;
225fe9d6
VZ
531 }
532
533 // do we stay in the same hive?
534 bool inSameHive = !wxStrchr(szNewName, REG_SEPARATOR);
535
536 // construct the full new name of the key
537 wxRegKey keyDst;
538
539 if ( inSameHive ) {
540 // rename the key to the new name under the same parent
541 wxString strKey = m_strKey.BeforeLast(REG_SEPARATOR);
4d08943e 542 if ( !strKey.IsEmpty() ) {
225fe9d6
VZ
543 // don't add '\\' in the start if strFullNewName is empty
544 strKey += REG_SEPARATOR;
545 }
546
547 strKey += szNewName;
548
549 keyDst.SetName(GetStdKeyFromHkey(m_hRootKey), strKey);
550 }
551 else {
552 // this is the full name already
553 keyDst.SetName(szNewName);
554 }
555
4d08943e 556 bool ok = keyDst.Create(false /* fail if alredy exists */);
225fe9d6
VZ
557 if ( !ok ) {
558 wxLogError(_("Registry key '%s' already exists."),
559 GetFullName(&keyDst));
560 }
561 else {
562 ok = Copy(keyDst) && DeleteSelf();
563 }
564
565 if ( !ok ) {
566 wxLogError(_("Failed to rename the registry key '%s' to '%s'."),
567 GetFullName(this), GetFullName(&keyDst));
568 }
569 else {
570 m_hRootKey = keyDst.m_hRootKey;
571 m_strKey = keyDst.m_strKey;
572 }
573
574 return ok;
575}
576
577bool wxRegKey::Copy(const wxChar *szNewName)
23f681ec
VZ
578{
579 // create the new key first
225fe9d6 580 wxRegKey keyDst(szNewName);
4d08943e 581 bool ok = keyDst.Create(false /* fail if alredy exists */);
23f681ec
VZ
582 if ( ok ) {
583 ok = Copy(keyDst);
584
585 // we created the dest key but copying to it failed - delete it
586 if ( !ok ) {
587 (void)keyDst.DeleteSelf();
588 }
589 }
590
591 return ok;
592}
593
594bool wxRegKey::Copy(wxRegKey& keyDst)
595{
4d08943e 596 bool ok = true;
23f681ec
VZ
597
598 // copy all sub keys to the new location
599 wxString strKey;
600 long lIndex;
601 bool bCont = GetFirstKey(strKey, lIndex);
602 while ( ok && bCont ) {
603 wxRegKey key(*this, strKey);
604 wxString keyName;
605 keyName << GetFullName(&keyDst) << REG_SEPARATOR << strKey;
f6bcfd97 606 ok = key.Copy((const wxChar*) keyName);
23f681ec
VZ
607
608 if ( ok )
609 bCont = GetNextKey(strKey, lIndex);
4d08943e
WS
610 else
611 wxLogError(_("Failed to copy the registry subkey '%s' to '%s'."),
835ab90d 612 GetFullName(&key), keyName.c_str());
4d08943e 613
23f681ec
VZ
614 }
615
616 // copy all values
617 wxString strVal;
618 bCont = GetFirstValue(strVal, lIndex);
619 while ( ok && bCont ) {
620 ok = CopyValue(strVal, keyDst);
621
622 if ( !ok ) {
623 wxLogSysError(m_dwLastError,
624 _("Failed to copy registry value '%s'"),
625 strVal.c_str());
626 }
627 else {
628 bCont = GetNextValue(strVal, lIndex);
629 }
630 }
631
632 if ( !ok ) {
835ab90d
VZ
633 wxLogError(_("Failed to copy the contents of registry key '%s' to '%s'."),
634 GetFullName(this), GetFullName(&keyDst));
23f681ec
VZ
635 }
636
637 return ok;
638}
639
0b1c5a6c
VZ
640// ----------------------------------------------------------------------------
641// delete keys/values
642// ----------------------------------------------------------------------------
2bda0e17
KB
643bool wxRegKey::DeleteSelf()
644{
0b1c5a6c
VZ
645 {
646 wxLogNull nolog;
647 if ( !Open() ) {
648 // it already doesn't exist - ok!
4d08943e 649 return true;
0b1c5a6c
VZ
650 }
651 }
652
90186e52
VZ
653 // prevent a buggy program from erasing one of the root registry keys or an
654 // immediate subkey (i.e. one which doesn't have '\\' inside) of any other
655 // key except HKCR (HKCR has some "deleteable" subkeys)
3cc487d1
VZ
656 if ( m_strKey.IsEmpty() ||
657 ((m_hRootKey != (WXHKEY) aStdKeys[HKCR].hkey) &&
658 (m_strKey.Find(REG_SEPARATOR) == wxNOT_FOUND)) ) {
835ab90d
VZ
659 wxLogError(_("Registry key '%s' is needed for normal system operation,\ndeleting it will leave your system in unusable state:\noperation aborted."),
660 GetFullName(this));
90186e52 661
4d08943e 662 return false;
90186e52
VZ
663 }
664
0b1c5a6c
VZ
665 // we can't delete keys while enumerating because it confuses GetNextKey, so
666 // we first save the key names and then delete them all
667 wxArrayString astrSubkeys;
2bda0e17
KB
668
669 wxString strKey;
670 long lIndex;
671 bool bCont = GetFirstKey(strKey, lIndex);
672 while ( bCont ) {
0b1c5a6c 673 astrSubkeys.Add(strKey);
2bda0e17
KB
674
675 bCont = GetNextKey(strKey, lIndex);
676 }
677
c86f1403
VZ
678 size_t nKeyCount = astrSubkeys.Count();
679 for ( size_t nKey = 0; nKey < nKeyCount; nKey++ ) {
0b1c5a6c
VZ
680 wxRegKey key(*this, astrSubkeys[nKey]);
681 if ( !key.DeleteSelf() )
4d08943e 682 return false;
0b1c5a6c
VZ
683 }
684
685 // now delete this key itself
2bda0e17
KB
686 Close();
687
fd6c844b 688 m_dwLastError = RegDeleteKey((HKEY) m_hRootKey, m_strKey);
86a7257f
VZ
689 // deleting a key which doesn't exist is not considered an error
690 if ( m_dwLastError != ERROR_SUCCESS &&
863e83fa 691 m_dwLastError != ERROR_FILE_NOT_FOUND ) {
23f681ec 692 wxLogSysError(m_dwLastError, _("Can't delete key '%s'"),
02569ba8 693 GetName().c_str());
4d08943e 694 return false;
2bda0e17
KB
695 }
696
4d08943e 697 return true;
2bda0e17
KB
698}
699
837e5743 700bool wxRegKey::DeleteKey(const wxChar *szKey)
2bda0e17
KB
701{
702 if ( !Open() )
4d08943e 703 return false;
2bda0e17
KB
704
705 wxRegKey key(*this, szKey);
706 return key.DeleteSelf();
707}
708
837e5743 709bool wxRegKey::DeleteValue(const wxChar *szValue)
2bda0e17
KB
710{
711 if ( !Open() )
4d08943e 712 return false;
2bda0e17 713
837e5743 714 m_dwLastError = RegDeleteValue((HKEY) m_hKey, WXSTRINGCAST szValue);
886dd7d2
VZ
715
716 // deleting a value which doesn't exist is not considered an error
717 if ( (m_dwLastError != ERROR_SUCCESS) &&
718 (m_dwLastError != ERROR_FILE_NOT_FOUND) ) {
23f681ec 719 wxLogSysError(m_dwLastError, _("Can't delete value '%s' from key '%s'"),
2bda0e17 720 szValue, GetName().c_str());
4d08943e 721 return false;
2bda0e17 722 }
2bda0e17 723
4d08943e 724 return true;
2bda0e17
KB
725}
726
727// ----------------------------------------------------------------------------
728// access to values and subkeys
729// ----------------------------------------------------------------------------
730
4d08943e 731// return true if value exists
837e5743 732bool wxRegKey::HasValue(const wxChar *szValue) const
0b1c5a6c 733{
32c66ea2
VZ
734 // this function should be silent, so suppress possible messages from Open()
735 wxLogNull nolog;
23f681ec 736
d305f9df 737 if ( !CONST_CAST Open(Read) )
4d08943e 738 return false;
f6bcfd97
BP
739
740 LONG dwRet = ::RegQueryValueEx((HKEY) m_hKey,
741 WXSTRINGCAST szValue,
742 RESERVED,
743 NULL, NULL, NULL);
744 return dwRet == ERROR_SUCCESS;
0b1c5a6c
VZ
745}
746
4d08943e 747// returns true if this key has any values
92049cd4
VZ
748bool wxRegKey::HasValues() const
749{
750 // suppress possible messages from GetFirstValue()
751 wxLogNull nolog;
23f681ec 752
92049cd4
VZ
753 // just call GetFirstValue with dummy parameters
754 wxString str;
755 long l;
756 return CONST_CAST GetFirstValue(str, l);
757}
758
4d08943e 759// returns true if this key has any subkeys
2bda0e17
KB
760bool wxRegKey::HasSubkeys() const
761{
c19a8a9a
VZ
762 // suppress possible messages from GetFirstKey()
763 wxLogNull nolog;
23f681ec 764
2bda0e17
KB
765 // just call GetFirstKey with dummy parameters
766 wxString str;
767 long l;
768 return CONST_CAST GetFirstKey(str, l);
769}
770
4d08943e 771// returns true if given subkey exists
837e5743 772bool wxRegKey::HasSubKey(const wxChar *szKey) const
2bda0e17 773{
c19a8a9a
VZ
774 // this function should be silent, so suppress possible messages from Open()
775 wxLogNull nolog;
23f681ec 776
d305f9df 777 if ( !CONST_CAST Open(Read) )
4d08943e 778 return false;
f6bcfd97
BP
779
780 return KeyExists(m_hKey, szKey);
2bda0e17
KB
781}
782
837e5743 783wxRegKey::ValueType wxRegKey::GetValueType(const wxChar *szValue) const
2bda0e17 784{
d305f9df 785 if ( ! CONST_CAST Open(Read) )
2bda0e17
KB
786 return Type_None;
787
788 DWORD dwType;
837e5743 789 m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED,
2bda0e17
KB
790 &dwType, NULL, NULL);
791 if ( m_dwLastError != ERROR_SUCCESS ) {
23f681ec 792 wxLogSysError(m_dwLastError, _("Can't read value of key '%s'"),
2bda0e17
KB
793 GetName().c_str());
794 return Type_None;
795 }
796
797 return (ValueType)dwType;
2bda0e17
KB
798}
799
800#ifdef __WIN32__
837e5743 801bool wxRegKey::SetValue(const wxChar *szValue, long lValue)
2bda0e17
KB
802{
803 if ( CONST_CAST Open() ) {
6b037754 804 m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_DWORD,
2bda0e17
KB
805 (RegString)&lValue, sizeof(lValue));
806 if ( m_dwLastError == ERROR_SUCCESS )
4d08943e 807 return true;
2bda0e17
KB
808 }
809
23f681ec 810 wxLogSysError(m_dwLastError, _("Can't set value of '%s'"),
2bda0e17 811 GetFullName(this, szValue));
4d08943e 812 return false;
2bda0e17
KB
813}
814
837e5743 815bool wxRegKey::QueryValue(const wxChar *szValue, long *plValue) const
2bda0e17 816{
d305f9df 817 if ( CONST_CAST Open(Read) ) {
2bda0e17
KB
818 DWORD dwType, dwSize = sizeof(DWORD);
819 RegString pBuf = (RegString)plValue;
837e5743 820 m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED,
2bda0e17
KB
821 &dwType, pBuf, &dwSize);
822 if ( m_dwLastError != ERROR_SUCCESS ) {
23f681ec 823 wxLogSysError(m_dwLastError, _("Can't read value of key '%s'"),
2bda0e17 824 GetName().c_str());
4d08943e 825 return false;
2bda0e17
KB
826 }
827 else {
828 // check that we read the value of right type
23f681ec 829 wxASSERT_MSG( IsNumericValue(szValue),
223d09f6 830 wxT("Type mismatch in wxRegKey::QueryValue().") );
2bda0e17 831
4d08943e 832 return true;
2bda0e17
KB
833 }
834 }
835 else
4d08943e 836 return false;
2bda0e17
KB
837}
838
9b386eca
RG
839bool wxRegKey::SetValue(const wxChar *szValue,const wxMemoryBuffer& buffer)
840{
841#ifdef __TWIN32__
842 wxFAIL_MSG("RegSetValueEx not implemented by TWIN32");
4d08943e 843 return false;
9b386eca
RG
844#else
845 if ( CONST_CAST Open() ) {
846 m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_BINARY,
847 (RegBinary)buffer.GetData(),buffer.GetDataLen());
848 if ( m_dwLastError == ERROR_SUCCESS )
4d08943e 849 return true;
9b386eca
RG
850 }
851
852 wxLogSysError(m_dwLastError, _("Can't set value of '%s'"),
853 GetFullName(this, szValue));
4d08943e 854 return false;
9b386eca
RG
855#endif
856}
857
858bool wxRegKey::QueryValue(const wxChar *szValue, wxMemoryBuffer& buffer) const
859{
860 if ( CONST_CAST Open() ) {
861 // first get the type and size of the data
862 DWORD dwType, dwSize;
863 m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED,
864 &dwType, NULL, &dwSize);
4d08943e 865
9b386eca
RG
866 if ( m_dwLastError == ERROR_SUCCESS ) {
867 if ( dwSize ) {
868 const RegBinary pBuf = (RegBinary)buffer.GetWriteBuf(dwSize);
869 m_dwLastError = RegQueryValueEx((HKEY) m_hKey,
870 WXSTRINGCAST szValue,
871 RESERVED,
872 &dwType,
873 pBuf,
874 &dwSize);
875 buffer.UngetWriteBuf(dwSize);
4d08943e
WS
876 } else {
877 buffer.SetDataLen(0);
9b386eca
RG
878 }
879 }
880
4d08943e 881
9b386eca
RG
882 if ( m_dwLastError != ERROR_SUCCESS ) {
883 wxLogSysError(m_dwLastError, _("Can't read value of key '%s'"),
884 GetName().c_str());
4d08943e 885 return false;
9b386eca 886 }
4d08943e 887 return true;
9b386eca 888 }
4d08943e 889 return false;
9b386eca
RG
890}
891
892
893
2bda0e17
KB
894#endif //Win32
895
6dfec4b8
VZ
896bool wxRegKey::QueryValue(const wxChar *szValue,
897 wxString& strValue,
898 bool raw) const
2bda0e17 899{
d305f9df 900 if ( CONST_CAST Open(Read) ) {
3a5bcc4d 901
2bda0e17
KB
902 // first get the type and size of the data
903 DWORD dwType, dwSize;
837e5743 904 m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED,
2bda0e17
KB
905 &dwType, NULL, &dwSize);
906 if ( m_dwLastError == ERROR_SUCCESS ) {
23f681ec
VZ
907 if ( !dwSize ) {
908 // must treat this case specially as GetWriteBuf() doesn't like
909 // being called with 0 size
910 strValue.Empty();
911 }
912 else {
23f681ec
VZ
913 m_dwLastError = RegQueryValueEx((HKEY) m_hKey,
914 WXSTRINGCAST szValue,
915 RESERVED,
916 &dwType,
de564874 917 (RegString)(wxChar*)wxStringBuffer(strValue, dwSize),
23f681ec 918 &dwSize);
6dfec4b8
VZ
919
920 // expand the var expansions in the string unless disabled
4676948b 921#ifndef __WXWINCE__
6dfec4b8
VZ
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
930 (
931 strValue,
de564874 932 wxStringBuffer(strExpValue, dwExpSize),
6dfec4b8
VZ
933 dwExpSize
934 ) != 0;
6dfec4b8
VZ
935 strValue = strExpValue;
936 }
937
938 if ( !ok )
939 {
940 wxLogLastError(_T("ExpandEnvironmentStrings"));
941 }
942 }
7010702f
WS
943#else
944 wxUnusedVar(raw);
4676948b
JS
945#endif
946 // __WXWINCE__
23f681ec
VZ
947 }
948
2bda0e17
KB
949 if ( m_dwLastError == ERROR_SUCCESS ) {
950 // check that it was the right type
23f681ec 951 wxASSERT_MSG( !IsNumericValue(szValue),
223d09f6 952 wxT("Type mismatch in wxRegKey::QueryValue().") );
2bda0e17 953
4d08943e 954 return true;
2bda0e17
KB
955 }
956 }
2bda0e17
KB
957 }
958
23f681ec 959 wxLogSysError(m_dwLastError, _("Can't read value of '%s'"),
2bda0e17 960 GetFullName(this, szValue));
4d08943e 961 return false;
2bda0e17
KB
962}
963
837e5743 964bool wxRegKey::SetValue(const wxChar *szValue, const wxString& strValue)
2bda0e17
KB
965{
966 if ( CONST_CAST Open() ) {
6b037754 967 m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_SZ,
23f681ec 968 (RegString)strValue.c_str(),
f6bcfd97 969 (strValue.Len() + 1)*sizeof(wxChar));
2bda0e17 970 if ( m_dwLastError == ERROR_SUCCESS )
4d08943e 971 return true;
2bda0e17
KB
972 }
973
23f681ec 974 wxLogSysError(m_dwLastError, _("Can't set value of '%s'"),
2bda0e17 975 GetFullName(this, szValue));
4d08943e 976 return false;
2bda0e17
KB
977}
978
50e42404 979wxString wxRegKey::QueryDefaultValue() const
2bda0e17
KB
980{
981 wxString str;
982 QueryValue(NULL, str);
983 return str;
984}
985
986// ----------------------------------------------------------------------------
987// enumeration
988// NB: all these functions require an index variable which allows to have
989// several concurrently running indexations on the same key
990// ----------------------------------------------------------------------------
991
2bda0e17
KB
992bool wxRegKey::GetFirstValue(wxString& strValueName, long& lIndex)
993{
d305f9df 994 if ( !Open(Read) )
4d08943e 995 return false;
2bda0e17
KB
996
997 lIndex = 0;
0b1c5a6c 998 return GetNextValue(strValueName, lIndex);
2bda0e17
KB
999}
1000
1001bool wxRegKey::GetNextValue(wxString& strValueName, long& lIndex) const
1002{
1003 wxASSERT( IsOpened() );
2bda0e17 1004
0b1c5a6c
VZ
1005 // are we already at the end of enumeration?
1006 if ( lIndex == -1 )
4d08943e 1007 return false;
2bda0e17 1008
837e5743 1009 wxChar szValueName[1024]; // @@ use RegQueryInfoKey...
0b1c5a6c 1010 DWORD dwValueLen = WXSIZEOF(szValueName);
2bda0e17 1011
92049cd4 1012 m_dwLastError = RegEnumValue((HKEY) m_hKey, lIndex++,
0b1c5a6c 1013 szValueName, &dwValueLen,
23f681ec
VZ
1014 RESERVED,
1015 NULL, // [out] type
0b1c5a6c
VZ
1016 NULL, // [out] buffer for value
1017 NULL); // [i/o] it's length
1018
1019 if ( m_dwLastError != ERROR_SUCCESS ) {
1020 if ( m_dwLastError == ERROR_NO_MORE_ITEMS ) {
1021 m_dwLastError = ERROR_SUCCESS;
1022 lIndex = -1;
1023 }
1024 else {
23f681ec 1025 wxLogSysError(m_dwLastError, _("Can't enumerate values of key '%s'"),
0b1c5a6c
VZ
1026 GetName().c_str());
1027 }
1028
4d08943e 1029 return false;
2bda0e17
KB
1030 }
1031
0b1c5a6c 1032 strValueName = szValueName;
0b1c5a6c 1033
4d08943e 1034 return true;
2bda0e17 1035}
2bda0e17
KB
1036
1037bool wxRegKey::GetFirstKey(wxString& strKeyName, long& lIndex)
1038{
d305f9df 1039 if ( !Open(Read) )
4d08943e 1040 return false;
2bda0e17 1041
2bda0e17 1042 lIndex = 0;
0b1c5a6c 1043 return GetNextKey(strKeyName, lIndex);
2bda0e17
KB
1044}
1045
1046bool wxRegKey::GetNextKey(wxString& strKeyName, long& lIndex) const
1047{
1048 wxASSERT( IsOpened() );
0b1c5a6c
VZ
1049
1050 // are we already at the end of enumeration?
1051 if ( lIndex == -1 )
4d08943e 1052 return false;
2bda0e17 1053
837e5743 1054 wxChar szKeyName[_MAX_PATH + 1];
4676948b
JS
1055
1056#ifdef __WXWINCE__
1057 DWORD sizeName = WXSIZEOF(szKeyName);
1058 m_dwLastError = RegEnumKeyEx((HKEY) m_hKey, lIndex++, szKeyName, & sizeName,
1059 0, NULL, NULL, NULL);
1060#else
fd6c844b 1061 m_dwLastError = RegEnumKey((HKEY) m_hKey, lIndex++, szKeyName, WXSIZEOF(szKeyName));
4676948b 1062#endif
2bda0e17
KB
1063
1064 if ( m_dwLastError != ERROR_SUCCESS ) {
1065 if ( m_dwLastError == ERROR_NO_MORE_ITEMS ) {
1066 m_dwLastError = ERROR_SUCCESS;
1067 lIndex = -1;
1068 }
1069 else {
23f681ec 1070 wxLogSysError(m_dwLastError, _("Can't enumerate subkeys of key '%s'"),
2bda0e17
KB
1071 GetName().c_str());
1072 }
1073
4d08943e 1074 return false;
2bda0e17
KB
1075 }
1076
1077 strKeyName = szKeyName;
4d08943e 1078 return true;
2bda0e17
KB
1079}
1080
4d08943e 1081// returns true if the value contains a number (else it's some string)
837e5743 1082bool wxRegKey::IsNumericValue(const wxChar *szValue) const
4d08943e
WS
1083{
1084 ValueType type = GetValueType(szValue);
1085 switch ( type ) {
03ab016d 1086 case Type_Dword:
23f681ec 1087 /* case Type_Dword_little_endian: == Type_Dword */
03ab016d 1088 case Type_Dword_big_endian:
4d08943e 1089 return true;
03ab016d
JS
1090
1091 default:
4d08943e
WS
1092 return false;
1093 }
1094}
03ab016d 1095
2bda0e17 1096// ============================================================================
1880e452 1097// implementation of global private functions
2bda0e17 1098// ============================================================================
f6bcfd97 1099
837e5743 1100bool KeyExists(WXHKEY hRootKey, const wxChar *szKey)
2bda0e17 1101{
8a8c41dd
VZ
1102 // don't close this key itself for the case of empty szKey!
1103 if ( wxIsEmpty(szKey) )
4d08943e 1104 return true;
8a8c41dd
VZ
1105
1106 HKEY hkeyDummy;
1107 if ( ::RegOpenKeyEx
1108 (
1109 (HKEY)hRootKey,
1110 szKey,
1111 RESERVED,
bee96abf 1112 KEY_READ, // we might not have enough rights for rw access
8a8c41dd
VZ
1113 &hkeyDummy
1114 ) == ERROR_SUCCESS )
1115 {
1116 ::RegCloseKey(hkeyDummy);
1117
4d08943e 1118 return true;
8a8c41dd 1119 }
f6bcfd97 1120
4d08943e 1121 return false;
2bda0e17
KB
1122}
1123
837e5743 1124const wxChar *GetFullName(const wxRegKey *pKey, const wxChar *szValue)
2bda0e17
KB
1125{
1126 static wxString s_str;
1127 s_str = pKey->GetName();
837e5743 1128 if ( !wxIsEmpty(szValue) )
223d09f6 1129 s_str << wxT("\\") << szValue;
2bda0e17
KB
1130
1131 return s_str.c_str();
0b1c5a6c
VZ
1132}
1133
1134void RemoveTrailingSeparator(wxString& str)
1135{
1136 if ( !str.IsEmpty() && str.Last() == REG_SEPARATOR )
1137 str.Truncate(str.Len() - 1);
1138}
3d05544e 1139