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