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