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