]> git.saurik.com Git - wxWidgets.git/blame - src/msw/registry.cpp
Added GUI part of AppTraits for OS/2.
[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
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
9ed0d735 39#include "wx/msw/wrapwin.h"
2bda0e17 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
007007c5 97#define RESERVED (0)
c86f1403 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{
8a8c41dd
VZ
379 if ( IsOpened() )
380 return TRUE;
381
382 HKEY tmpKey;
383 m_dwLastError = ::RegOpenKeyEx
384 (
385 (HKEY) m_hRootKey,
386 m_strKey,
387 RESERVED,
388 KEY_ALL_ACCESS,
389 &tmpKey
390 );
391
392 if ( m_dwLastError != ERROR_SUCCESS )
393 {
394 wxLogSysError(m_dwLastError, _("Can't open registry key '%s'"),
395 GetName().c_str());
396 return FALSE;
397 }
2bda0e17 398
fd6c844b 399 m_hKey = (WXHKEY) tmpKey;
cf447356 400 return TRUE;
2bda0e17
KB
401}
402
403// creates key, failing if it exists and !bOkIfExists
404bool wxRegKey::Create(bool bOkIfExists)
405{
406 // check for existence only if asked (i.e. order is important!)
8a8c41dd 407 if ( !bOkIfExists && Exists() )
cf447356 408 return FALSE;
2bda0e17
KB
409
410 if ( IsOpened() )
cf447356 411 return TRUE;
2bda0e17 412
fd6c844b 413 HKEY tmpKey;
4676948b
JS
414#ifdef __WXWINCE__
415 DWORD disposition;
416 m_dwLastError = RegCreateKeyEx((HKEY) m_hRootKey, m_strKey,
417 NULL, // reserved
418 NULL, // class string
419 0,
420 0,
421 NULL,
422 &tmpKey,
423 &disposition);
424#else
fd6c844b 425 m_dwLastError = RegCreateKey((HKEY) m_hRootKey, m_strKey, &tmpKey);
4676948b 426#endif
2bda0e17 427 if ( m_dwLastError != ERROR_SUCCESS ) {
23f681ec 428 wxLogSysError(m_dwLastError, _("Can't create registry key '%s'"),
2bda0e17 429 GetName().c_str());
cf447356 430 return FALSE;
2bda0e17
KB
431 }
432 else
fd6c844b
JS
433 {
434 m_hKey = (WXHKEY) tmpKey;
cf447356 435 return TRUE;
fd6c844b 436 }
2bda0e17
KB
437}
438
0b1c5a6c
VZ
439// close the key, it's not an error to call it when not opened
440bool wxRegKey::Close()
441{
442 if ( IsOpened() ) {
fd6c844b 443 m_dwLastError = RegCloseKey((HKEY) m_hKey);
807a903e
VZ
444 m_hKey = 0;
445
0b1c5a6c 446 if ( m_dwLastError != ERROR_SUCCESS ) {
23f681ec 447 wxLogSysError(m_dwLastError, _("Can't close registry key '%s'"),
0b1c5a6c
VZ
448 GetName().c_str());
449
cf447356 450 return FALSE;
0b1c5a6c 451 }
0b1c5a6c
VZ
452 }
453
cf447356 454 return TRUE;
0b1c5a6c
VZ
455}
456
23f681ec
VZ
457bool wxRegKey::RenameValue(const wxChar *szValueOld, const wxChar *szValueNew)
458{
459 bool ok = TRUE;
460 if ( HasValue(szValueNew) ) {
461 wxLogError(_("Registry value '%s' already exists."), szValueNew);
462
463 ok = FALSE;
464 }
465
225fe9d6
VZ
466 if ( !ok ||
467 !CopyValue(szValueOld, *this, szValueNew) ||
468 !DeleteValue(szValueOld) ) {
23f681ec
VZ
469 wxLogError(_("Failed to rename registry value '%s' to '%s'."),
470 szValueOld, szValueNew);
471
472 return FALSE;
473 }
474
475 return TRUE;
476}
477
478bool wxRegKey::CopyValue(const wxChar *szValue,
479 wxRegKey& keyDst,
480 const wxChar *szValueNew)
481{
5af3f0ef
VZ
482 if ( !szValueNew ) {
483 // by default, use the same name
484 szValueNew = szValue;
485 }
486
23f681ec
VZ
487 switch ( GetValueType(szValue) ) {
488 case Type_String:
489 {
490 wxString strVal;
491 return QueryValue(szValue, strVal) &&
492 keyDst.SetValue(szValueNew, strVal);
493 }
494
495 case Type_Dword:
496 /* case Type_Dword_little_endian: == Type_Dword */
497 {
498 long dwVal;
499 return QueryValue(szValue, &dwVal) &&
500 keyDst.SetValue(szValueNew, dwVal);
501 }
502
503 // these types are unsupported because I am not sure about how
504 // exactly they should be copied and because they shouldn't
505 // occur among the application keys (supposedly created with
506 // this class)
507#ifdef __WIN32__
508 case Type_None:
509 case Type_Expand_String:
510 case Type_Binary:
511 case Type_Dword_big_endian:
512 case Type_Link:
513 case Type_Multi_String:
514 case Type_Resource_list:
515 case Type_Full_resource_descriptor:
516 case Type_Resource_requirements_list:
517#endif // Win32
518 default:
519 wxLogError(_("Can't copy values of unsupported type %d."),
520 GetValueType(szValue));
521 return FALSE;
522 }
523}
524
225fe9d6
VZ
525bool wxRegKey::Rename(const wxChar *szNewName)
526{
527 wxCHECK_MSG( !!m_strKey, FALSE, _T("registry hives can't be renamed") );
528
529 if ( !Exists() ) {
530 wxLogError(_("Registry key '%s' does not exist, cannot rename it."),
531 GetFullName(this));
532
533 return FALSE;
534 }
535
536 // do we stay in the same hive?
537 bool inSameHive = !wxStrchr(szNewName, REG_SEPARATOR);
538
539 // construct the full new name of the key
540 wxRegKey keyDst;
541
542 if ( inSameHive ) {
543 // rename the key to the new name under the same parent
544 wxString strKey = m_strKey.BeforeLast(REG_SEPARATOR);
545 if ( !!strKey ) {
546 // don't add '\\' in the start if strFullNewName is empty
547 strKey += REG_SEPARATOR;
548 }
549
550 strKey += szNewName;
551
552 keyDst.SetName(GetStdKeyFromHkey(m_hRootKey), strKey);
553 }
554 else {
555 // this is the full name already
556 keyDst.SetName(szNewName);
557 }
558
559 bool ok = keyDst.Create(FALSE /* fail if alredy exists */);
560 if ( !ok ) {
561 wxLogError(_("Registry key '%s' already exists."),
562 GetFullName(&keyDst));
563 }
564 else {
565 ok = Copy(keyDst) && DeleteSelf();
566 }
567
568 if ( !ok ) {
569 wxLogError(_("Failed to rename the registry key '%s' to '%s'."),
570 GetFullName(this), GetFullName(&keyDst));
571 }
572 else {
573 m_hRootKey = keyDst.m_hRootKey;
574 m_strKey = keyDst.m_strKey;
575 }
576
577 return ok;
578}
579
580bool wxRegKey::Copy(const wxChar *szNewName)
23f681ec
VZ
581{
582 // create the new key first
225fe9d6 583 wxRegKey keyDst(szNewName);
23f681ec
VZ
584 bool ok = keyDst.Create(FALSE /* fail if alredy exists */);
585 if ( ok ) {
586 ok = Copy(keyDst);
587
588 // we created the dest key but copying to it failed - delete it
589 if ( !ok ) {
590 (void)keyDst.DeleteSelf();
591 }
592 }
593
594 return ok;
595}
596
597bool wxRegKey::Copy(wxRegKey& keyDst)
598{
599 bool ok = TRUE;
600
601 // copy all sub keys to the new location
602 wxString strKey;
603 long lIndex;
604 bool bCont = GetFirstKey(strKey, lIndex);
605 while ( ok && bCont ) {
606 wxRegKey key(*this, strKey);
607 wxString keyName;
608 keyName << GetFullName(&keyDst) << REG_SEPARATOR << strKey;
f6bcfd97 609 ok = key.Copy((const wxChar*) keyName);
23f681ec
VZ
610
611 if ( ok )
612 bCont = GetNextKey(strKey, lIndex);
613 }
614
615 // copy all values
616 wxString strVal;
617 bCont = GetFirstValue(strVal, lIndex);
618 while ( ok && bCont ) {
619 ok = CopyValue(strVal, keyDst);
620
621 if ( !ok ) {
622 wxLogSysError(m_dwLastError,
623 _("Failed to copy registry value '%s'"),
624 strVal.c_str());
625 }
626 else {
627 bCont = GetNextValue(strVal, lIndex);
628 }
629 }
630
631 if ( !ok ) {
f6bcfd97 632 wxLogError(_("Failed to copy the contents of registry key '%s' to '%s'."), GetFullName(this), GetFullName(&keyDst));
23f681ec
VZ
633 }
634
635 return ok;
636}
637
0b1c5a6c
VZ
638// ----------------------------------------------------------------------------
639// delete keys/values
640// ----------------------------------------------------------------------------
2bda0e17
KB
641bool wxRegKey::DeleteSelf()
642{
0b1c5a6c
VZ
643 {
644 wxLogNull nolog;
645 if ( !Open() ) {
646 // it already doesn't exist - ok!
647 return TRUE;
648 }
649 }
650
90186e52
VZ
651 // prevent a buggy program from erasing one of the root registry keys or an
652 // immediate subkey (i.e. one which doesn't have '\\' inside) of any other
653 // key except HKCR (HKCR has some "deleteable" subkeys)
3cc487d1
VZ
654 if ( m_strKey.IsEmpty() ||
655 ((m_hRootKey != (WXHKEY) aStdKeys[HKCR].hkey) &&
656 (m_strKey.Find(REG_SEPARATOR) == wxNOT_FOUND)) ) {
f6bcfd97 657 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
658
659 return FALSE;
660 }
661
0b1c5a6c
VZ
662 // we can't delete keys while enumerating because it confuses GetNextKey, so
663 // we first save the key names and then delete them all
664 wxArrayString astrSubkeys;
2bda0e17
KB
665
666 wxString strKey;
667 long lIndex;
668 bool bCont = GetFirstKey(strKey, lIndex);
669 while ( bCont ) {
0b1c5a6c 670 astrSubkeys.Add(strKey);
2bda0e17
KB
671
672 bCont = GetNextKey(strKey, lIndex);
673 }
674
c86f1403
VZ
675 size_t nKeyCount = astrSubkeys.Count();
676 for ( size_t nKey = 0; nKey < nKeyCount; nKey++ ) {
0b1c5a6c
VZ
677 wxRegKey key(*this, astrSubkeys[nKey]);
678 if ( !key.DeleteSelf() )
679 return FALSE;
680 }
681
682 // now delete this key itself
2bda0e17
KB
683 Close();
684
fd6c844b 685 m_dwLastError = RegDeleteKey((HKEY) m_hRootKey, m_strKey);
2bda0e17 686 if ( m_dwLastError != ERROR_SUCCESS ) {
23f681ec 687 wxLogSysError(m_dwLastError, _("Can't delete key '%s'"),
02569ba8 688 GetName().c_str());
2bda0e17
KB
689 return FALSE;
690 }
691
692 return TRUE;
693}
694
837e5743 695bool wxRegKey::DeleteKey(const wxChar *szKey)
2bda0e17
KB
696{
697 if ( !Open() )
cf447356 698 return FALSE;
2bda0e17
KB
699
700 wxRegKey key(*this, szKey);
701 return key.DeleteSelf();
702}
703
837e5743 704bool wxRegKey::DeleteValue(const wxChar *szValue)
2bda0e17
KB
705{
706 if ( !Open() )
cf447356 707 return FALSE;
2bda0e17 708
b39dbf34 709#if defined(__WIN32__)
837e5743 710 m_dwLastError = RegDeleteValue((HKEY) m_hKey, WXSTRINGCAST szValue);
886dd7d2
VZ
711
712 // deleting a value which doesn't exist is not considered an error
713 if ( (m_dwLastError != ERROR_SUCCESS) &&
714 (m_dwLastError != ERROR_FILE_NOT_FOUND) ) {
23f681ec 715 wxLogSysError(m_dwLastError, _("Can't delete value '%s' from key '%s'"),
2bda0e17 716 szValue, GetName().c_str());
cf447356 717 return FALSE;
2bda0e17 718 }
b39dbf34 719#else //WIN16
2bda0e17
KB
720 // named registry values don't exist in Win16 world
721 wxASSERT( IsEmpty(szValue) );
722
723 // just set the (default and unique) value of the key to ""
fd6c844b 724 m_dwLastError = RegSetValue((HKEY) m_hKey, NULL, REG_SZ, "", RESERVED);
2bda0e17 725 if ( m_dwLastError != ERROR_SUCCESS ) {
23f681ec 726 wxLogSysError(m_dwLastError, _("Can't delete value of key '%s'"),
2bda0e17 727 GetName().c_str());
cf447356 728 return FALSE;
2bda0e17 729 }
b39dbf34 730#endif //WIN16/32
2bda0e17 731
cf447356 732 return TRUE;
2bda0e17
KB
733}
734
735// ----------------------------------------------------------------------------
736// access to values and subkeys
737// ----------------------------------------------------------------------------
738
cf447356 739// return TRUE if value exists
837e5743 740bool wxRegKey::HasValue(const wxChar *szValue) const
0b1c5a6c 741{
32c66ea2
VZ
742 // this function should be silent, so suppress possible messages from Open()
743 wxLogNull nolog;
23f681ec 744
0b1c5a6c 745 #ifdef __WIN32__
f6bcfd97
BP
746 if ( !CONST_CAST Open() )
747 return FALSE;
748
749 LONG dwRet = ::RegQueryValueEx((HKEY) m_hKey,
750 WXSTRINGCAST szValue,
751 RESERVED,
752 NULL, NULL, NULL);
753 return dwRet == ERROR_SUCCESS;
0b1c5a6c
VZ
754 #else // WIN16
755 // only unnamed value exists
756 return IsEmpty(szValue);
757 #endif // WIN16/32
758}
759
92049cd4
VZ
760// returns TRUE if this key has any values
761bool wxRegKey::HasValues() const
762{
763 // suppress possible messages from GetFirstValue()
764 wxLogNull nolog;
23f681ec 765
92049cd4
VZ
766 // just call GetFirstValue with dummy parameters
767 wxString str;
768 long l;
769 return CONST_CAST GetFirstValue(str, l);
770}
771
cf447356 772// returns TRUE if this key has any subkeys
2bda0e17
KB
773bool wxRegKey::HasSubkeys() const
774{
c19a8a9a
VZ
775 // suppress possible messages from GetFirstKey()
776 wxLogNull nolog;
23f681ec 777
2bda0e17
KB
778 // just call GetFirstKey with dummy parameters
779 wxString str;
780 long l;
781 return CONST_CAST GetFirstKey(str, l);
782}
783
cf447356 784// returns TRUE if given subkey exists
837e5743 785bool wxRegKey::HasSubKey(const wxChar *szKey) const
2bda0e17 786{
c19a8a9a
VZ
787 // this function should be silent, so suppress possible messages from Open()
788 wxLogNull nolog;
23f681ec 789
f6bcfd97 790 if ( !CONST_CAST Open() )
cf447356 791 return FALSE;
f6bcfd97
BP
792
793 return KeyExists(m_hKey, szKey);
2bda0e17
KB
794}
795
837e5743 796wxRegKey::ValueType wxRegKey::GetValueType(const wxChar *szValue) const
2bda0e17
KB
797{
798 #ifdef __WIN32__
89077ebc 799 if ( ! CONST_CAST Open() )
2bda0e17
KB
800 return Type_None;
801
802 DWORD dwType;
837e5743 803 m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED,
2bda0e17
KB
804 &dwType, NULL, NULL);
805 if ( m_dwLastError != ERROR_SUCCESS ) {
23f681ec 806 wxLogSysError(m_dwLastError, _("Can't read value of key '%s'"),
2bda0e17
KB
807 GetName().c_str());
808 return Type_None;
809 }
810
811 return (ValueType)dwType;
812 #else //WIN16
813 return IsEmpty(szValue) ? Type_String : Type_None;
814 #endif //WIN16/32
815}
816
817#ifdef __WIN32__
837e5743 818bool wxRegKey::SetValue(const wxChar *szValue, long lValue)
2bda0e17
KB
819{
820 if ( CONST_CAST Open() ) {
6b037754 821 m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_DWORD,
2bda0e17
KB
822 (RegString)&lValue, sizeof(lValue));
823 if ( m_dwLastError == ERROR_SUCCESS )
cf447356 824 return TRUE;
2bda0e17
KB
825 }
826
23f681ec 827 wxLogSysError(m_dwLastError, _("Can't set value of '%s'"),
2bda0e17 828 GetFullName(this, szValue));
cf447356 829 return FALSE;
2bda0e17
KB
830}
831
837e5743 832bool wxRegKey::QueryValue(const wxChar *szValue, long *plValue) const
2bda0e17
KB
833{
834 if ( CONST_CAST Open() ) {
835 DWORD dwType, dwSize = sizeof(DWORD);
836 RegString pBuf = (RegString)plValue;
837e5743 837 m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED,
2bda0e17
KB
838 &dwType, pBuf, &dwSize);
839 if ( m_dwLastError != ERROR_SUCCESS ) {
23f681ec 840 wxLogSysError(m_dwLastError, _("Can't read value of key '%s'"),
2bda0e17 841 GetName().c_str());
cf447356 842 return FALSE;
2bda0e17
KB
843 }
844 else {
845 // check that we read the value of right type
23f681ec 846 wxASSERT_MSG( IsNumericValue(szValue),
223d09f6 847 wxT("Type mismatch in wxRegKey::QueryValue().") );
2bda0e17 848
cf447356 849 return TRUE;
2bda0e17
KB
850 }
851 }
852 else
cf447356 853 return FALSE;
2bda0e17
KB
854}
855
856#endif //Win32
857
6dfec4b8
VZ
858bool wxRegKey::QueryValue(const wxChar *szValue,
859 wxString& strValue,
860 bool raw) const
2bda0e17
KB
861{
862 if ( CONST_CAST Open() ) {
863 #ifdef __WIN32__
864 // first get the type and size of the data
865 DWORD dwType, dwSize;
837e5743 866 m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED,
2bda0e17
KB
867 &dwType, NULL, &dwSize);
868 if ( m_dwLastError == ERROR_SUCCESS ) {
23f681ec
VZ
869 if ( !dwSize ) {
870 // must treat this case specially as GetWriteBuf() doesn't like
871 // being called with 0 size
872 strValue.Empty();
873 }
874 else {
23f681ec
VZ
875 m_dwLastError = RegQueryValueEx((HKEY) m_hKey,
876 WXSTRINGCAST szValue,
877 RESERVED,
878 &dwType,
de564874 879 (RegString)(wxChar*)wxStringBuffer(strValue, dwSize),
23f681ec 880 &dwSize);
6dfec4b8
VZ
881
882 // expand the var expansions in the string unless disabled
4676948b 883#ifndef __WXWINCE__
6dfec4b8
VZ
884 if ( (dwType == REG_EXPAND_SZ) && !raw )
885 {
886 DWORD dwExpSize = ::ExpandEnvironmentStrings(strValue, NULL, 0);
887 bool ok = dwExpSize != 0;
888 if ( ok )
889 {
890 wxString strExpValue;
891 ok = ::ExpandEnvironmentStrings
892 (
893 strValue,
de564874 894 wxStringBuffer(strExpValue, dwExpSize),
6dfec4b8
VZ
895 dwExpSize
896 ) != 0;
6dfec4b8
VZ
897 strValue = strExpValue;
898 }
899
900 if ( !ok )
901 {
902 wxLogLastError(_T("ExpandEnvironmentStrings"));
903 }
904 }
4676948b
JS
905#endif
906 // __WXWINCE__
23f681ec
VZ
907 }
908
2bda0e17
KB
909 if ( m_dwLastError == ERROR_SUCCESS ) {
910 // check that it was the right type
23f681ec 911 wxASSERT_MSG( !IsNumericValue(szValue),
223d09f6 912 wxT("Type mismatch in wxRegKey::QueryValue().") );
2bda0e17 913
cf447356 914 return TRUE;
2bda0e17
KB
915 }
916 }
917 #else //WIN16
918 // named registry values don't exist in Win16
919 wxASSERT( IsEmpty(szValue) );
920
fd6c844b 921 m_dwLastError = RegQueryValue((HKEY) m_hKey, 0, strValue.GetWriteBuf(256), &l);
44a6c8e6 922 strValue.UngetWriteBuf();
2bda0e17 923 if ( m_dwLastError == ERROR_SUCCESS )
cf447356 924 return TRUE;
2bda0e17
KB
925 #endif //WIN16/32
926 }
927
23f681ec 928 wxLogSysError(m_dwLastError, _("Can't read value of '%s'"),
2bda0e17 929 GetFullName(this, szValue));
cf447356 930 return FALSE;
2bda0e17
KB
931}
932
837e5743 933bool wxRegKey::SetValue(const wxChar *szValue, const wxString& strValue)
2bda0e17
KB
934{
935 if ( CONST_CAST Open() ) {
b39dbf34 936#if defined( __WIN32__)
6b037754 937 m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_SZ,
23f681ec 938 (RegString)strValue.c_str(),
f6bcfd97 939 (strValue.Len() + 1)*sizeof(wxChar));
2bda0e17 940 if ( m_dwLastError == ERROR_SUCCESS )
cf447356 941 return TRUE;
b39dbf34 942#else //WIN16
2bda0e17
KB
943 // named registry values don't exist in Win16
944 wxASSERT( IsEmpty(szValue) );
945
fd6c844b 946 m_dwLastError = RegSetValue((HKEY) m_hKey, NULL, REG_SZ, strValue, NULL);
2bda0e17 947 if ( m_dwLastError == ERROR_SUCCESS )
cf447356 948 return TRUE;
b39dbf34 949#endif //WIN16/32
2bda0e17
KB
950 }
951
23f681ec 952 wxLogSysError(m_dwLastError, _("Can't set value of '%s'"),
2bda0e17 953 GetFullName(this, szValue));
cf447356 954 return FALSE;
2bda0e17
KB
955}
956
50e42404 957wxString wxRegKey::QueryDefaultValue() const
2bda0e17
KB
958{
959 wxString str;
960 QueryValue(NULL, str);
961 return str;
962}
963
964// ----------------------------------------------------------------------------
965// enumeration
966// NB: all these functions require an index variable which allows to have
967// several concurrently running indexations on the same key
968// ----------------------------------------------------------------------------
969
2bda0e17
KB
970bool wxRegKey::GetFirstValue(wxString& strValueName, long& lIndex)
971{
972 if ( !Open() )
cf447356 973 return FALSE;
2bda0e17
KB
974
975 lIndex = 0;
0b1c5a6c 976 return GetNextValue(strValueName, lIndex);
2bda0e17
KB
977}
978
979bool wxRegKey::GetNextValue(wxString& strValueName, long& lIndex) const
980{
981 wxASSERT( IsOpened() );
2bda0e17 982
0b1c5a6c
VZ
983 // are we already at the end of enumeration?
984 if ( lIndex == -1 )
cf447356 985 return FALSE;
2bda0e17 986
b39dbf34 987#if defined( __WIN32__)
837e5743 988 wxChar szValueName[1024]; // @@ use RegQueryInfoKey...
0b1c5a6c 989 DWORD dwValueLen = WXSIZEOF(szValueName);
2bda0e17 990
92049cd4 991 m_dwLastError = RegEnumValue((HKEY) m_hKey, lIndex++,
0b1c5a6c 992 szValueName, &dwValueLen,
23f681ec
VZ
993 RESERVED,
994 NULL, // [out] type
0b1c5a6c
VZ
995 NULL, // [out] buffer for value
996 NULL); // [i/o] it's length
997
998 if ( m_dwLastError != ERROR_SUCCESS ) {
999 if ( m_dwLastError == ERROR_NO_MORE_ITEMS ) {
1000 m_dwLastError = ERROR_SUCCESS;
1001 lIndex = -1;
1002 }
1003 else {
23f681ec 1004 wxLogSysError(m_dwLastError, _("Can't enumerate values of key '%s'"),
0b1c5a6c
VZ
1005 GetName().c_str());
1006 }
1007
cf447356 1008 return FALSE;
2bda0e17
KB
1009 }
1010
0b1c5a6c 1011 strValueName = szValueName;
b39dbf34 1012#else //WIN16
0b1c5a6c
VZ
1013 // only one unnamed value
1014 wxASSERT( lIndex == 0 );
2bda0e17 1015
0b1c5a6c
VZ
1016 lIndex = -1;
1017 strValueName.Empty();
b39dbf34 1018#endif
0b1c5a6c 1019
cf447356 1020 return TRUE;
2bda0e17 1021}
2bda0e17
KB
1022
1023bool wxRegKey::GetFirstKey(wxString& strKeyName, long& lIndex)
1024{
1025 if ( !Open() )
cf447356 1026 return FALSE;
2bda0e17 1027
2bda0e17 1028 lIndex = 0;
0b1c5a6c 1029 return GetNextKey(strKeyName, lIndex);
2bda0e17
KB
1030}
1031
1032bool wxRegKey::GetNextKey(wxString& strKeyName, long& lIndex) const
1033{
1034 wxASSERT( IsOpened() );
0b1c5a6c
VZ
1035
1036 // are we already at the end of enumeration?
1037 if ( lIndex == -1 )
cf447356 1038 return FALSE;
2bda0e17 1039
837e5743 1040 wxChar szKeyName[_MAX_PATH + 1];
4676948b
JS
1041
1042#ifdef __WXWINCE__
1043 DWORD sizeName = WXSIZEOF(szKeyName);
1044 m_dwLastError = RegEnumKeyEx((HKEY) m_hKey, lIndex++, szKeyName, & sizeName,
1045 0, NULL, NULL, NULL);
1046#else
fd6c844b 1047 m_dwLastError = RegEnumKey((HKEY) m_hKey, lIndex++, szKeyName, WXSIZEOF(szKeyName));
4676948b 1048#endif
2bda0e17
KB
1049
1050 if ( m_dwLastError != ERROR_SUCCESS ) {
1051 if ( m_dwLastError == ERROR_NO_MORE_ITEMS ) {
1052 m_dwLastError = ERROR_SUCCESS;
1053 lIndex = -1;
1054 }
1055 else {
23f681ec 1056 wxLogSysError(m_dwLastError, _("Can't enumerate subkeys of key '%s'"),
2bda0e17
KB
1057 GetName().c_str());
1058 }
1059
cf447356 1060 return FALSE;
2bda0e17
KB
1061 }
1062
1063 strKeyName = szKeyName;
cf447356 1064 return TRUE;
2bda0e17
KB
1065}
1066
03ab016d 1067// returns TRUE if the value contains a number (else it's some string)
837e5743 1068bool wxRegKey::IsNumericValue(const wxChar *szValue) const
23f681ec 1069 {
03ab016d
JS
1070 ValueType type = GetValueType(szValue);
1071 switch ( type ) {
1072 case Type_Dword:
23f681ec 1073 /* case Type_Dword_little_endian: == Type_Dword */
03ab016d
JS
1074 case Type_Dword_big_endian:
1075 return TRUE;
1076
1077 default:
1078 return FALSE;
1079 }
1080 }
1081
2bda0e17 1082// ============================================================================
1880e452 1083// implementation of global private functions
2bda0e17 1084// ============================================================================
f6bcfd97 1085
837e5743 1086bool KeyExists(WXHKEY hRootKey, const wxChar *szKey)
2bda0e17 1087{
8a8c41dd
VZ
1088 // don't close this key itself for the case of empty szKey!
1089 if ( wxIsEmpty(szKey) )
1090 return TRUE;
1091
1092 HKEY hkeyDummy;
1093 if ( ::RegOpenKeyEx
1094 (
1095 (HKEY)hRootKey,
1096 szKey,
1097 RESERVED,
1098 KEY_ALL_ACCESS,
1099 &hkeyDummy
1100 ) == ERROR_SUCCESS )
1101 {
1102 ::RegCloseKey(hkeyDummy);
1103
1104 return TRUE;
1105 }
f6bcfd97 1106
cf447356 1107 return FALSE;
2bda0e17
KB
1108}
1109
837e5743 1110const wxChar *GetFullName(const wxRegKey *pKey, const wxChar *szValue)
2bda0e17
KB
1111{
1112 static wxString s_str;
1113 s_str = pKey->GetName();
837e5743 1114 if ( !wxIsEmpty(szValue) )
223d09f6 1115 s_str << wxT("\\") << szValue;
2bda0e17
KB
1116
1117 return s_str.c_str();
0b1c5a6c
VZ
1118}
1119
1120void RemoveTrailingSeparator(wxString& str)
1121{
1122 if ( !str.IsEmpty() && str.Last() == REG_SEPARATOR )
1123 str.Truncate(str.Len() - 1);
1124}
3d05544e
JS
1125
1126#endif
1127 // __WIN16__
1128