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