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