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