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