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