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