]>
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> | |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | // TODO: - parsing of registry key names |
11 | // - support of other (than REG_SZ/REG_DWORD) registry types | |
12 | // - add high level functions (RegisterOleServer, ...) | |
13 | /////////////////////////////////////////////////////////////////////////////// | |
14 | ||
14f355c2 | 15 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
27529614 JS |
16 | #pragma implementation "registry.h" |
17 | #endif | |
18 | ||
2bda0e17 KB |
19 | // for compilers that support precompilation, includes "wx.h". |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
77ffb593 | 26 | // other wxWidgets headers |
2bda0e17 KB |
27 | #include "wx/string.h" |
28 | #include "wx/intl.h" | |
29 | #include "wx/log.h" | |
20d8c319 VZ |
30 | #include "wx/file.h" |
31 | #include "wx/wfstream.h" | |
41286812 | 32 | |
2bda0e17 | 33 | // Windows headers |
9ed0d735 | 34 | #include "wx/msw/wrapwin.h" |
2bda0e17 | 35 | |
4676948b JS |
36 | #ifdef __WXWINCE__ |
37 | #include "wx/msw/private.h" | |
38 | #include <winbase.h> | |
39 | #include <winreg.h> | |
40 | #endif | |
41 | ||
2bda0e17 KB |
42 | // other std headers |
43 | #include <stdlib.h> // for _MAX_PATH | |
44 | ||
45 | #ifndef _MAX_PATH | |
225fe9d6 | 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; | |
9b386eca | 55 | typedef BYTE* RegBinary; |
2bda0e17 KB |
56 | |
57 | // ---------------------------------------------------------------------------- | |
58 | // constants | |
59 | // ---------------------------------------------------------------------------- | |
60 | ||
61 | // the standard key names, short names and handles all bundled together for | |
62 | // convenient access | |
63 | static struct | |
64 | { | |
65 | HKEY hkey; | |
837e5743 OK |
66 | const wxChar *szName; |
67 | const wxChar *szShortName; | |
2bda0e17 | 68 | } |
23f681ec VZ |
69 | aStdKeys[] = |
70 | { | |
223d09f6 | 71 | { HKEY_CLASSES_ROOT, wxT("HKEY_CLASSES_ROOT"), wxT("HKCR") }, |
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? | |
4676948b | 75 | #ifndef __WXWINCE__ |
223d09f6 | 76 | { HKEY_PERFORMANCE_DATA, wxT("HKEY_PERFORMANCE_DATA"), wxT("HKPD") }, |
4676948b JS |
77 | #endif |
78 | #if WINVER >= 0x0400 && !defined(__WXWINCE__) | |
223d09f6 | 79 | { HKEY_CURRENT_CONFIG, wxT("HKEY_CURRENT_CONFIG"), wxT("HKCC") }, |
4676948b | 80 | #if !defined(__GNUWIN32__) && !defined(__WXWINCE__) |
223d09f6 | 81 | { HKEY_DYN_DATA, wxT("HKEY_DYN_DATA"), wxT("HKDD") }, // short name? |
0b1c5a6c | 82 | #endif //GNUWIN32 |
2bda0e17 | 83 | #endif //WINVER >= 4.0 |
2bda0e17 KB |
84 | }; |
85 | ||
86 | // the registry name separator (perhaps one day MS will change it to '/' ;-) | |
223d09f6 | 87 | #define REG_SEPARATOR wxT('\\') |
2bda0e17 | 88 | |
c86f1403 VZ |
89 | // useful for Windows programmers: makes somewhat more clear all these zeroes |
90 | // being passed to Windows APIs | |
007007c5 | 91 | #define RESERVED (0) |
c86f1403 | 92 | |
2bda0e17 KB |
93 | // ---------------------------------------------------------------------------- |
94 | // macros | |
95 | // ---------------------------------------------------------------------------- | |
807a903e VZ |
96 | |
97 | // const_cast<> is not yet supported by all compilers | |
2bda0e17 KB |
98 | #define CONST_CAST ((wxRegKey *)this)-> |
99 | ||
807a903e VZ |
100 | // and neither is mutable which m_dwLastError should be |
101 | #define m_dwLastError CONST_CAST m_dwLastError | |
2bda0e17 KB |
102 | |
103 | // ---------------------------------------------------------------------------- | |
104 | // non member functions | |
105 | // ---------------------------------------------------------------------------- | |
106 | ||
0b1c5a6c VZ |
107 | // removes the trailing backslash from the string if it has one |
108 | static inline void RemoveTrailingSeparator(wxString& str); | |
109 | ||
4d08943e | 110 | // returns true if given registry key exists |
837e5743 | 111 | static bool KeyExists(WXHKEY hRootKey, const wxChar *szKey); |
2bda0e17 KB |
112 | |
113 | // combines value and key name (uses static buffer!) | |
23f681ec | 114 | static const wxChar *GetFullName(const wxRegKey *pKey, |
837e5743 | 115 | const wxChar *szValue = NULL); |
2bda0e17 KB |
116 | |
117 | // ============================================================================ | |
118 | // implementation of wxRegKey class | |
119 | // ============================================================================ | |
120 | ||
121 | // ---------------------------------------------------------------------------- | |
122 | // static functions and variables | |
123 | // ---------------------------------------------------------------------------- | |
124 | ||
125 | const size_t wxRegKey::nStdKeys = WXSIZEOF(aStdKeys); | |
126 | ||
127 | // @@ should take a `StdKey key', but as it's often going to be used in loops | |
23f681ec | 128 | // it would require casts in user code. |
837e5743 | 129 | const wxChar *wxRegKey::GetStdKeyName(size_t key) |
2bda0e17 KB |
130 | { |
131 | // return empty string if key is invalid | |
fda7962d | 132 | wxCHECK_MSG( key < nStdKeys, wxEmptyString, wxT("invalid key in wxRegKey::GetStdKeyName") ); |
2bda0e17 KB |
133 | |
134 | return aStdKeys[key].szName; | |
135 | } | |
136 | ||
837e5743 | 137 | const wxChar *wxRegKey::GetStdKeyShortName(size_t key) |
2bda0e17 KB |
138 | { |
139 | // return empty string if key is invalid | |
fda7962d | 140 | wxCHECK( key < nStdKeys, wxEmptyString ); |
2bda0e17 KB |
141 | |
142 | return aStdKeys[key].szShortName; | |
143 | } | |
144 | ||
145 | wxRegKey::StdKey wxRegKey::ExtractKeyName(wxString& strKey) | |
146 | { | |
b32719cc | 147 | wxString strRoot = strKey.BeforeFirst(REG_SEPARATOR); |
2bda0e17 | 148 | |
fd3f686c | 149 | HKEY hRootKey = 0; |
c86f1403 | 150 | size_t ui; |
2bda0e17 | 151 | for ( ui = 0; ui < nStdKeys; ui++ ) { |
23f681ec | 152 | if ( strRoot.CmpNoCase(aStdKeys[ui].szName) == 0 || |
2bda0e17 KB |
153 | strRoot.CmpNoCase(aStdKeys[ui].szShortName) == 0 ) { |
154 | hRootKey = aStdKeys[ui].hkey; | |
155 | break; | |
156 | } | |
157 | } | |
158 | ||
159 | if ( ui == nStdKeys ) { | |
223d09f6 | 160 | wxFAIL_MSG(wxT("invalid key prefix in wxRegKey::ExtractKeyName.")); |
2bda0e17 KB |
161 | |
162 | hRootKey = HKEY_CLASSES_ROOT; | |
163 | } | |
164 | else { | |
165 | strKey = strKey.After(REG_SEPARATOR); | |
166 | if ( !strKey.IsEmpty() && strKey.Last() == REG_SEPARATOR ) | |
167 | strKey.Truncate(strKey.Len() - 1); | |
168 | } | |
169 | ||
170 | return (wxRegKey::StdKey)(int)hRootKey; | |
171 | } | |
172 | ||
fd6c844b | 173 | wxRegKey::StdKey wxRegKey::GetStdKeyFromHkey(WXHKEY hkey) |
2bda0e17 | 174 | { |
c86f1403 | 175 | for ( size_t ui = 0; ui < nStdKeys; ui++ ) { |
fd6c844b | 176 | if ( (int) aStdKeys[ui].hkey == (int) hkey ) |
2bda0e17 KB |
177 | return (StdKey)ui; |
178 | } | |
23f681ec | 179 | |
223d09f6 | 180 | wxFAIL_MSG(wxT("non root hkey passed to wxRegKey::GetStdKeyFromHkey.")); |
2bda0e17 KB |
181 | |
182 | return HKCR; | |
183 | } | |
184 | ||
185 | // ---------------------------------------------------------------------------- | |
186 | // ctors and dtor | |
187 | // ---------------------------------------------------------------------------- | |
188 | ||
189 | wxRegKey::wxRegKey() | |
190 | { | |
fd6c844b | 191 | m_hRootKey = (WXHKEY) aStdKeys[HKCR].hkey; |
807a903e VZ |
192 | |
193 | Init(); | |
2bda0e17 KB |
194 | } |
195 | ||
196 | wxRegKey::wxRegKey(const wxString& strKey) : m_strKey(strKey) | |
197 | { | |
fd6c844b | 198 | m_hRootKey = (WXHKEY) aStdKeys[ExtractKeyName(m_strKey)].hkey; |
807a903e VZ |
199 | |
200 | Init(); | |
2bda0e17 KB |
201 | } |
202 | ||
203 | // parent is a predefined (and preopened) key | |
204 | wxRegKey::wxRegKey(StdKey keyParent, const wxString& strKey) : m_strKey(strKey) | |
205 | { | |
0b1c5a6c | 206 | RemoveTrailingSeparator(m_strKey); |
fd6c844b | 207 | m_hRootKey = (WXHKEY) aStdKeys[keyParent].hkey; |
807a903e VZ |
208 | |
209 | Init(); | |
2bda0e17 KB |
210 | } |
211 | ||
212 | // parent is a normal regkey | |
213 | wxRegKey::wxRegKey(const wxRegKey& keyParent, const wxString& strKey) | |
214 | : m_strKey(keyParent.m_strKey) | |
215 | { | |
216 | // combine our name with parent's to get the full name | |
23f681ec | 217 | if ( !m_strKey.IsEmpty() && |
32c66ea2 VZ |
218 | (strKey.IsEmpty() || strKey[0] != REG_SEPARATOR) ) { |
219 | m_strKey += REG_SEPARATOR; | |
220 | } | |
2bda0e17 KB |
221 | |
222 | m_strKey += strKey; | |
0b1c5a6c | 223 | RemoveTrailingSeparator(m_strKey); |
2bda0e17 KB |
224 | |
225 | m_hRootKey = keyParent.m_hRootKey; | |
807a903e VZ |
226 | |
227 | Init(); | |
2bda0e17 KB |
228 | } |
229 | ||
230 | // dtor closes the key releasing system resource | |
231 | wxRegKey::~wxRegKey() | |
232 | { | |
233 | Close(); | |
234 | } | |
235 | ||
0b1c5a6c VZ |
236 | // ---------------------------------------------------------------------------- |
237 | // change the key name/hkey | |
238 | // ---------------------------------------------------------------------------- | |
239 | ||
240 | // set the full key name | |
241 | void wxRegKey::SetName(const wxString& strKey) | |
242 | { | |
243 | Close(); | |
244 | ||
245 | m_strKey = strKey; | |
fd6c844b | 246 | m_hRootKey = (WXHKEY) aStdKeys[ExtractKeyName(m_strKey)].hkey; |
0b1c5a6c VZ |
247 | } |
248 | ||
249 | // the name is relative to the parent key | |
250 | void wxRegKey::SetName(StdKey keyParent, const wxString& strKey) | |
251 | { | |
252 | Close(); | |
253 | ||
254 | m_strKey = strKey; | |
255 | RemoveTrailingSeparator(m_strKey); | |
fd6c844b | 256 | m_hRootKey = (WXHKEY) aStdKeys[keyParent].hkey; |
0b1c5a6c VZ |
257 | } |
258 | ||
259 | // the name is relative to the parent key | |
260 | void wxRegKey::SetName(const wxRegKey& keyParent, const wxString& strKey) | |
261 | { | |
262 | Close(); | |
263 | ||
264 | // combine our name with parent's to get the full name | |
807a903e VZ |
265 | |
266 | // NB: this method is called by wxRegConfig::SetPath() which is a performance | |
267 | // critical function and so it preallocates space for our m_strKey to | |
268 | // gain some speed - this is why we only use += here and not = which | |
269 | // would just free the prealloc'd buffer and would have to realloc it the | |
270 | // next line! | |
271 | m_strKey.clear(); | |
272 | m_strKey += keyParent.m_strKey; | |
0b1c5a6c VZ |
273 | if ( !strKey.IsEmpty() && strKey[0] != REG_SEPARATOR ) |
274 | m_strKey += REG_SEPARATOR; | |
02569ba8 | 275 | m_strKey += strKey; |
0b1c5a6c VZ |
276 | |
277 | RemoveTrailingSeparator(m_strKey); | |
278 | ||
279 | m_hRootKey = keyParent.m_hRootKey; | |
280 | } | |
281 | ||
282 | // hKey should be opened and will be closed in wxRegKey dtor | |
fd6c844b | 283 | void wxRegKey::SetHkey(WXHKEY hKey) |
0b1c5a6c VZ |
284 | { |
285 | Close(); | |
286 | ||
287 | m_hKey = hKey; | |
288 | } | |
289 | ||
2bda0e17 KB |
290 | // ---------------------------------------------------------------------------- |
291 | // info about the key | |
292 | // ---------------------------------------------------------------------------- | |
293 | ||
4d08943e | 294 | // returns true if the key exists |
2bda0e17 KB |
295 | bool wxRegKey::Exists() const |
296 | { | |
297 | // opened key has to exist, try to open it if not done yet | |
4d08943e | 298 | return IsOpened() ? true : KeyExists(m_hRootKey, m_strKey); |
2bda0e17 KB |
299 | } |
300 | ||
301 | // returns the full name of the key (prefix is abbreviated if bShortPrefix) | |
302 | wxString wxRegKey::GetName(bool bShortPrefix) const | |
303 | { | |
333b7dac | 304 | StdKey key = GetStdKeyFromHkey((WXHKEY) m_hRootKey); |
23f681ec | 305 | wxString str = bShortPrefix ? aStdKeys[key].szShortName |
2bda0e17 KB |
306 | : aStdKeys[key].szName; |
307 | if ( !m_strKey.IsEmpty() ) | |
2b5f62a0 | 308 | str << _T("\\") << m_strKey; |
2bda0e17 KB |
309 | |
310 | return str; | |
311 | } | |
312 | ||
23f681ec VZ |
313 | bool wxRegKey::GetKeyInfo(size_t *pnSubKeys, |
314 | size_t *pnMaxKeyLen, | |
315 | size_t *pnValues, | |
316 | size_t *pnMaxValueLen) const | |
02569ba8 | 317 | { |
23f681ec | 318 | // old gcc headers incorrectly prototype RegQueryInfoKey() |
ae090fdb | 319 | #if defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__) |
23f681ec VZ |
320 | #define REG_PARAM (size_t *) |
321 | #else | |
322 | #define REG_PARAM (LPDWORD) | |
323 | #endif | |
324 | ||
6dfec4b8 VZ |
325 | // it might be unexpected to some that this function doesn't open the key |
326 | wxASSERT_MSG( IsOpened(), _T("key should be opened in GetKeyInfo") ); | |
327 | ||
02569ba8 VZ |
328 | m_dwLastError = ::RegQueryInfoKey |
329 | ( | |
fd6c844b | 330 | (HKEY) m_hKey, |
02569ba8 VZ |
331 | NULL, // class name |
332 | NULL, // (ptr to) size of class name buffer | |
333 | RESERVED, | |
23f681ec | 334 | REG_PARAM |
02569ba8 | 335 | pnSubKeys, // [out] number of subkeys |
23f681ec | 336 | REG_PARAM |
02569ba8 VZ |
337 | pnMaxKeyLen, // [out] max length of a subkey name |
338 | NULL, // longest subkey class name | |
23f681ec | 339 | REG_PARAM |
02569ba8 | 340 | pnValues, // [out] number of values |
23f681ec | 341 | REG_PARAM |
02569ba8 VZ |
342 | pnMaxValueLen, // [out] max length of a value name |
343 | NULL, // longest value data | |
344 | NULL, // security descriptor | |
345 | NULL // time of last modification | |
346 | ); | |
347 | ||
23f681ec VZ |
348 | #undef REG_PARAM |
349 | ||
02569ba8 | 350 | if ( m_dwLastError != ERROR_SUCCESS ) { |
23f681ec | 351 | wxLogSysError(m_dwLastError, _("Can't get info about registry key '%s'"), |
02569ba8 | 352 | GetName().c_str()); |
4d08943e | 353 | return false; |
02569ba8 | 354 | } |
6dfec4b8 | 355 | |
4d08943e | 356 | return true; |
02569ba8 VZ |
357 | } |
358 | ||
2bda0e17 KB |
359 | // ---------------------------------------------------------------------------- |
360 | // operations | |
361 | // ---------------------------------------------------------------------------- | |
362 | ||
363 | // opens key (it's not an error to call Open() on an already opened key) | |
bee96abf | 364 | bool wxRegKey::Open(AccessMode mode) |
2bda0e17 | 365 | { |
8a8c41dd | 366 | if ( IsOpened() ) |
4d08943e | 367 | return true; |
8a8c41dd VZ |
368 | |
369 | HKEY tmpKey; | |
370 | m_dwLastError = ::RegOpenKeyEx | |
371 | ( | |
372 | (HKEY) m_hRootKey, | |
373 | m_strKey, | |
374 | RESERVED, | |
bee96abf | 375 | mode == Read ? KEY_READ : KEY_ALL_ACCESS, |
8a8c41dd VZ |
376 | &tmpKey |
377 | ); | |
378 | ||
379 | if ( m_dwLastError != ERROR_SUCCESS ) | |
380 | { | |
381 | wxLogSysError(m_dwLastError, _("Can't open registry key '%s'"), | |
382 | GetName().c_str()); | |
4d08943e | 383 | return false; |
8a8c41dd | 384 | } |
2bda0e17 | 385 | |
fd6c844b | 386 | m_hKey = (WXHKEY) tmpKey; |
4d08943e | 387 | return true; |
2bda0e17 KB |
388 | } |
389 | ||
390 | // creates key, failing if it exists and !bOkIfExists | |
391 | bool wxRegKey::Create(bool bOkIfExists) | |
392 | { | |
393 | // check for existence only if asked (i.e. order is important!) | |
8a8c41dd | 394 | if ( !bOkIfExists && Exists() ) |
4d08943e | 395 | return false; |
2bda0e17 KB |
396 | |
397 | if ( IsOpened() ) | |
4d08943e | 398 | return true; |
2bda0e17 | 399 | |
fd6c844b | 400 | HKEY tmpKey; |
4676948b JS |
401 | #ifdef __WXWINCE__ |
402 | DWORD disposition; | |
403 | m_dwLastError = RegCreateKeyEx((HKEY) m_hRootKey, m_strKey, | |
404 | NULL, // reserved | |
405 | NULL, // class string | |
406 | 0, | |
407 | 0, | |
408 | NULL, | |
409 | &tmpKey, | |
410 | &disposition); | |
411 | #else | |
fd6c844b | 412 | m_dwLastError = RegCreateKey((HKEY) m_hRootKey, m_strKey, &tmpKey); |
4676948b | 413 | #endif |
2bda0e17 | 414 | if ( m_dwLastError != ERROR_SUCCESS ) { |
23f681ec | 415 | wxLogSysError(m_dwLastError, _("Can't create registry key '%s'"), |
2bda0e17 | 416 | GetName().c_str()); |
4d08943e | 417 | return false; |
2bda0e17 KB |
418 | } |
419 | else | |
fd6c844b JS |
420 | { |
421 | m_hKey = (WXHKEY) tmpKey; | |
4d08943e | 422 | return true; |
fd6c844b | 423 | } |
2bda0e17 KB |
424 | } |
425 | ||
0b1c5a6c VZ |
426 | // close the key, it's not an error to call it when not opened |
427 | bool wxRegKey::Close() | |
428 | { | |
429 | if ( IsOpened() ) { | |
fd6c844b | 430 | m_dwLastError = RegCloseKey((HKEY) m_hKey); |
807a903e VZ |
431 | m_hKey = 0; |
432 | ||
0b1c5a6c | 433 | if ( m_dwLastError != ERROR_SUCCESS ) { |
23f681ec | 434 | wxLogSysError(m_dwLastError, _("Can't close registry key '%s'"), |
0b1c5a6c VZ |
435 | GetName().c_str()); |
436 | ||
4d08943e | 437 | return false; |
0b1c5a6c | 438 | } |
0b1c5a6c VZ |
439 | } |
440 | ||
4d08943e | 441 | return true; |
0b1c5a6c VZ |
442 | } |
443 | ||
23f681ec VZ |
444 | bool wxRegKey::RenameValue(const wxChar *szValueOld, const wxChar *szValueNew) |
445 | { | |
4d08943e | 446 | bool ok = true; |
23f681ec VZ |
447 | if ( HasValue(szValueNew) ) { |
448 | wxLogError(_("Registry value '%s' already exists."), szValueNew); | |
449 | ||
4d08943e | 450 | ok = false; |
23f681ec VZ |
451 | } |
452 | ||
225fe9d6 VZ |
453 | if ( !ok || |
454 | !CopyValue(szValueOld, *this, szValueNew) || | |
455 | !DeleteValue(szValueOld) ) { | |
23f681ec VZ |
456 | wxLogError(_("Failed to rename registry value '%s' to '%s'."), |
457 | szValueOld, szValueNew); | |
458 | ||
4d08943e | 459 | return false; |
23f681ec VZ |
460 | } |
461 | ||
4d08943e | 462 | return true; |
23f681ec VZ |
463 | } |
464 | ||
465 | bool wxRegKey::CopyValue(const wxChar *szValue, | |
466 | wxRegKey& keyDst, | |
467 | const wxChar *szValueNew) | |
468 | { | |
5af3f0ef VZ |
469 | if ( !szValueNew ) { |
470 | // by default, use the same name | |
471 | szValueNew = szValue; | |
472 | } | |
473 | ||
23f681ec VZ |
474 | switch ( GetValueType(szValue) ) { |
475 | case Type_String: | |
476 | { | |
477 | wxString strVal; | |
478 | return QueryValue(szValue, strVal) && | |
479 | keyDst.SetValue(szValueNew, strVal); | |
480 | } | |
481 | ||
482 | case Type_Dword: | |
483 | /* case Type_Dword_little_endian: == Type_Dword */ | |
484 | { | |
485 | long dwVal; | |
486 | return QueryValue(szValue, &dwVal) && | |
487 | keyDst.SetValue(szValueNew, dwVal); | |
488 | } | |
489 | ||
9b386eca | 490 | case Type_Binary: |
4d08943e WS |
491 | { |
492 | wxMemoryBuffer buf; | |
493 | return QueryValue(szValue,buf) && | |
494 | keyDst.SetValue(szValueNew,buf); | |
495 | } | |
20d8c319 | 496 | |
23f681ec VZ |
497 | // these types are unsupported because I am not sure about how |
498 | // exactly they should be copied and because they shouldn't | |
499 | // occur among the application keys (supposedly created with | |
500 | // this class) | |
23f681ec VZ |
501 | case Type_None: |
502 | case Type_Expand_String: | |
23f681ec VZ |
503 | case Type_Dword_big_endian: |
504 | case Type_Link: | |
505 | case Type_Multi_String: | |
506 | case Type_Resource_list: | |
507 | case Type_Full_resource_descriptor: | |
508 | case Type_Resource_requirements_list: | |
23f681ec VZ |
509 | default: |
510 | wxLogError(_("Can't copy values of unsupported type %d."), | |
511 | GetValueType(szValue)); | |
4d08943e | 512 | return false; |
23f681ec VZ |
513 | } |
514 | } | |
515 | ||
225fe9d6 VZ |
516 | bool wxRegKey::Rename(const wxChar *szNewName) |
517 | { | |
4d08943e | 518 | wxCHECK_MSG( !m_strKey.IsEmpty(), false, _T("registry hives can't be renamed") ); |
225fe9d6 VZ |
519 | |
520 | if ( !Exists() ) { | |
521 | wxLogError(_("Registry key '%s' does not exist, cannot rename it."), | |
522 | GetFullName(this)); | |
523 | ||
4d08943e | 524 | return false; |
225fe9d6 VZ |
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); | |
4d08943e | 536 | if ( !strKey.IsEmpty() ) { |
225fe9d6 VZ |
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 | ||
4d08943e | 550 | bool ok = keyDst.Create(false /* fail if alredy exists */); |
225fe9d6 VZ |
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) | |
23f681ec VZ |
572 | { |
573 | // create the new key first | |
225fe9d6 | 574 | wxRegKey keyDst(szNewName); |
4d08943e | 575 | bool ok = keyDst.Create(false /* fail if alredy exists */); |
23f681ec VZ |
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 | { | |
4d08943e | 590 | bool ok = true; |
23f681ec VZ |
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; | |
f6bcfd97 | 600 | ok = key.Copy((const wxChar*) keyName); |
23f681ec VZ |
601 | |
602 | if ( ok ) | |
603 | bCont = GetNextKey(strKey, lIndex); | |
4d08943e WS |
604 | else |
605 | wxLogError(_("Failed to copy the registry subkey '%s' to '%s'."), | |
835ab90d | 606 | GetFullName(&key), keyName.c_str()); |
4d08943e | 607 | |
23f681ec VZ |
608 | } |
609 | ||
610 | // copy all values | |
611 | wxString strVal; | |
612 | bCont = GetFirstValue(strVal, lIndex); | |
613 | while ( ok && bCont ) { | |
614 | ok = CopyValue(strVal, keyDst); | |
615 | ||
616 | if ( !ok ) { | |
617 | wxLogSysError(m_dwLastError, | |
618 | _("Failed to copy registry value '%s'"), | |
619 | strVal.c_str()); | |
620 | } | |
621 | else { | |
622 | bCont = GetNextValue(strVal, lIndex); | |
623 | } | |
624 | } | |
625 | ||
626 | if ( !ok ) { | |
835ab90d VZ |
627 | wxLogError(_("Failed to copy the contents of registry key '%s' to '%s'."), |
628 | GetFullName(this), GetFullName(&keyDst)); | |
23f681ec VZ |
629 | } |
630 | ||
631 | return ok; | |
632 | } | |
633 | ||
0b1c5a6c VZ |
634 | // ---------------------------------------------------------------------------- |
635 | // delete keys/values | |
636 | // ---------------------------------------------------------------------------- | |
2bda0e17 KB |
637 | bool wxRegKey::DeleteSelf() |
638 | { | |
0b1c5a6c VZ |
639 | { |
640 | wxLogNull nolog; | |
641 | if ( !Open() ) { | |
642 | // it already doesn't exist - ok! | |
4d08943e | 643 | return true; |
0b1c5a6c VZ |
644 | } |
645 | } | |
646 | ||
90186e52 VZ |
647 | // prevent a buggy program from erasing one of the root registry keys or an |
648 | // immediate subkey (i.e. one which doesn't have '\\' inside) of any other | |
649 | // key except HKCR (HKCR has some "deleteable" subkeys) | |
3cc487d1 VZ |
650 | if ( m_strKey.IsEmpty() || |
651 | ((m_hRootKey != (WXHKEY) aStdKeys[HKCR].hkey) && | |
652 | (m_strKey.Find(REG_SEPARATOR) == wxNOT_FOUND)) ) { | |
835ab90d VZ |
653 | wxLogError(_("Registry key '%s' is needed for normal system operation,\ndeleting it will leave your system in unusable state:\noperation aborted."), |
654 | GetFullName(this)); | |
90186e52 | 655 | |
4d08943e | 656 | return false; |
90186e52 VZ |
657 | } |
658 | ||
0b1c5a6c VZ |
659 | // we can't delete keys while enumerating because it confuses GetNextKey, so |
660 | // we first save the key names and then delete them all | |
661 | wxArrayString astrSubkeys; | |
2bda0e17 KB |
662 | |
663 | wxString strKey; | |
664 | long lIndex; | |
665 | bool bCont = GetFirstKey(strKey, lIndex); | |
666 | while ( bCont ) { | |
0b1c5a6c | 667 | astrSubkeys.Add(strKey); |
2bda0e17 KB |
668 | |
669 | bCont = GetNextKey(strKey, lIndex); | |
670 | } | |
671 | ||
c86f1403 VZ |
672 | size_t nKeyCount = astrSubkeys.Count(); |
673 | for ( size_t nKey = 0; nKey < nKeyCount; nKey++ ) { | |
0b1c5a6c VZ |
674 | wxRegKey key(*this, astrSubkeys[nKey]); |
675 | if ( !key.DeleteSelf() ) | |
4d08943e | 676 | return false; |
0b1c5a6c VZ |
677 | } |
678 | ||
679 | // now delete this key itself | |
2bda0e17 KB |
680 | Close(); |
681 | ||
fd6c844b | 682 | m_dwLastError = RegDeleteKey((HKEY) m_hRootKey, m_strKey); |
86a7257f VZ |
683 | // deleting a key which doesn't exist is not considered an error |
684 | if ( m_dwLastError != ERROR_SUCCESS && | |
863e83fa | 685 | m_dwLastError != ERROR_FILE_NOT_FOUND ) { |
23f681ec | 686 | wxLogSysError(m_dwLastError, _("Can't delete key '%s'"), |
02569ba8 | 687 | GetName().c_str()); |
4d08943e | 688 | return false; |
2bda0e17 KB |
689 | } |
690 | ||
4d08943e | 691 | return true; |
2bda0e17 KB |
692 | } |
693 | ||
837e5743 | 694 | bool wxRegKey::DeleteKey(const wxChar *szKey) |
2bda0e17 KB |
695 | { |
696 | if ( !Open() ) | |
4d08943e | 697 | return false; |
2bda0e17 KB |
698 | |
699 | wxRegKey key(*this, szKey); | |
700 | return key.DeleteSelf(); | |
701 | } | |
702 | ||
837e5743 | 703 | bool wxRegKey::DeleteValue(const wxChar *szValue) |
2bda0e17 KB |
704 | { |
705 | if ( !Open() ) | |
4d08943e | 706 | return false; |
2bda0e17 | 707 | |
837e5743 | 708 | m_dwLastError = RegDeleteValue((HKEY) m_hKey, WXSTRINGCAST szValue); |
886dd7d2 VZ |
709 | |
710 | // deleting a value which doesn't exist is not considered an error | |
711 | if ( (m_dwLastError != ERROR_SUCCESS) && | |
712 | (m_dwLastError != ERROR_FILE_NOT_FOUND) ) { | |
23f681ec | 713 | wxLogSysError(m_dwLastError, _("Can't delete value '%s' from key '%s'"), |
2bda0e17 | 714 | szValue, GetName().c_str()); |
4d08943e | 715 | return false; |
2bda0e17 | 716 | } |
2bda0e17 | 717 | |
4d08943e | 718 | return true; |
2bda0e17 KB |
719 | } |
720 | ||
721 | // ---------------------------------------------------------------------------- | |
722 | // access to values and subkeys | |
723 | // ---------------------------------------------------------------------------- | |
724 | ||
4d08943e | 725 | // return true if value exists |
837e5743 | 726 | bool wxRegKey::HasValue(const wxChar *szValue) const |
0b1c5a6c | 727 | { |
32c66ea2 VZ |
728 | // this function should be silent, so suppress possible messages from Open() |
729 | wxLogNull nolog; | |
23f681ec | 730 | |
d305f9df | 731 | if ( !CONST_CAST Open(Read) ) |
4d08943e | 732 | return false; |
f6bcfd97 BP |
733 | |
734 | LONG dwRet = ::RegQueryValueEx((HKEY) m_hKey, | |
735 | WXSTRINGCAST szValue, | |
736 | RESERVED, | |
737 | NULL, NULL, NULL); | |
738 | return dwRet == ERROR_SUCCESS; | |
0b1c5a6c VZ |
739 | } |
740 | ||
4d08943e | 741 | // returns true if this key has any values |
92049cd4 VZ |
742 | bool wxRegKey::HasValues() const |
743 | { | |
744 | // suppress possible messages from GetFirstValue() | |
745 | wxLogNull nolog; | |
23f681ec | 746 | |
92049cd4 VZ |
747 | // just call GetFirstValue with dummy parameters |
748 | wxString str; | |
749 | long l; | |
750 | return CONST_CAST GetFirstValue(str, l); | |
751 | } | |
752 | ||
4d08943e | 753 | // returns true if this key has any subkeys |
2bda0e17 KB |
754 | bool wxRegKey::HasSubkeys() const |
755 | { | |
c19a8a9a VZ |
756 | // suppress possible messages from GetFirstKey() |
757 | wxLogNull nolog; | |
23f681ec | 758 | |
2bda0e17 KB |
759 | // just call GetFirstKey with dummy parameters |
760 | wxString str; | |
761 | long l; | |
762 | return CONST_CAST GetFirstKey(str, l); | |
763 | } | |
764 | ||
4d08943e | 765 | // returns true if given subkey exists |
837e5743 | 766 | bool wxRegKey::HasSubKey(const wxChar *szKey) const |
2bda0e17 | 767 | { |
c19a8a9a VZ |
768 | // this function should be silent, so suppress possible messages from Open() |
769 | wxLogNull nolog; | |
23f681ec | 770 | |
d305f9df | 771 | if ( !CONST_CAST Open(Read) ) |
4d08943e | 772 | return false; |
f6bcfd97 BP |
773 | |
774 | return KeyExists(m_hKey, szKey); | |
2bda0e17 KB |
775 | } |
776 | ||
837e5743 | 777 | wxRegKey::ValueType wxRegKey::GetValueType(const wxChar *szValue) const |
2bda0e17 | 778 | { |
d305f9df | 779 | if ( ! CONST_CAST Open(Read) ) |
2bda0e17 KB |
780 | return Type_None; |
781 | ||
782 | DWORD dwType; | |
837e5743 | 783 | m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED, |
2bda0e17 KB |
784 | &dwType, NULL, NULL); |
785 | if ( m_dwLastError != ERROR_SUCCESS ) { | |
23f681ec | 786 | wxLogSysError(m_dwLastError, _("Can't read value of key '%s'"), |
2bda0e17 KB |
787 | GetName().c_str()); |
788 | return Type_None; | |
789 | } | |
790 | ||
791 | return (ValueType)dwType; | |
2bda0e17 KB |
792 | } |
793 | ||
837e5743 | 794 | bool wxRegKey::SetValue(const wxChar *szValue, long lValue) |
2bda0e17 KB |
795 | { |
796 | if ( CONST_CAST Open() ) { | |
6b037754 | 797 | m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_DWORD, |
2bda0e17 KB |
798 | (RegString)&lValue, sizeof(lValue)); |
799 | if ( m_dwLastError == ERROR_SUCCESS ) | |
4d08943e | 800 | return true; |
2bda0e17 KB |
801 | } |
802 | ||
23f681ec | 803 | wxLogSysError(m_dwLastError, _("Can't set value of '%s'"), |
2bda0e17 | 804 | GetFullName(this, szValue)); |
4d08943e | 805 | return false; |
2bda0e17 KB |
806 | } |
807 | ||
837e5743 | 808 | bool wxRegKey::QueryValue(const wxChar *szValue, long *plValue) const |
2bda0e17 | 809 | { |
d305f9df | 810 | if ( CONST_CAST Open(Read) ) { |
2bda0e17 KB |
811 | DWORD dwType, dwSize = sizeof(DWORD); |
812 | RegString pBuf = (RegString)plValue; | |
837e5743 | 813 | m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED, |
2bda0e17 KB |
814 | &dwType, pBuf, &dwSize); |
815 | if ( m_dwLastError != ERROR_SUCCESS ) { | |
23f681ec | 816 | wxLogSysError(m_dwLastError, _("Can't read value of key '%s'"), |
2bda0e17 | 817 | GetName().c_str()); |
4d08943e | 818 | return false; |
2bda0e17 KB |
819 | } |
820 | else { | |
821 | // check that we read the value of right type | |
23f681ec | 822 | wxASSERT_MSG( IsNumericValue(szValue), |
223d09f6 | 823 | wxT("Type mismatch in wxRegKey::QueryValue().") ); |
2bda0e17 | 824 | |
4d08943e | 825 | return true; |
2bda0e17 KB |
826 | } |
827 | } | |
828 | else | |
4d08943e | 829 | return false; |
2bda0e17 KB |
830 | } |
831 | ||
9b386eca RG |
832 | bool wxRegKey::SetValue(const wxChar *szValue,const wxMemoryBuffer& buffer) |
833 | { | |
834 | #ifdef __TWIN32__ | |
835 | wxFAIL_MSG("RegSetValueEx not implemented by TWIN32"); | |
4d08943e | 836 | return false; |
9b386eca RG |
837 | #else |
838 | if ( CONST_CAST Open() ) { | |
839 | m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_BINARY, | |
840 | (RegBinary)buffer.GetData(),buffer.GetDataLen()); | |
841 | if ( m_dwLastError == ERROR_SUCCESS ) | |
4d08943e | 842 | return true; |
9b386eca RG |
843 | } |
844 | ||
845 | wxLogSysError(m_dwLastError, _("Can't set value of '%s'"), | |
846 | GetFullName(this, szValue)); | |
4d08943e | 847 | return false; |
9b386eca RG |
848 | #endif |
849 | } | |
850 | ||
851 | bool wxRegKey::QueryValue(const wxChar *szValue, wxMemoryBuffer& buffer) const | |
852 | { | |
853 | if ( CONST_CAST Open() ) { | |
854 | // first get the type and size of the data | |
855 | DWORD dwType, dwSize; | |
856 | m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED, | |
857 | &dwType, NULL, &dwSize); | |
4d08943e | 858 | |
9b386eca RG |
859 | if ( m_dwLastError == ERROR_SUCCESS ) { |
860 | if ( dwSize ) { | |
861 | const RegBinary pBuf = (RegBinary)buffer.GetWriteBuf(dwSize); | |
862 | m_dwLastError = RegQueryValueEx((HKEY) m_hKey, | |
863 | WXSTRINGCAST szValue, | |
864 | RESERVED, | |
865 | &dwType, | |
866 | pBuf, | |
867 | &dwSize); | |
868 | buffer.UngetWriteBuf(dwSize); | |
4d08943e WS |
869 | } else { |
870 | buffer.SetDataLen(0); | |
9b386eca RG |
871 | } |
872 | } | |
873 | ||
4d08943e | 874 | |
9b386eca RG |
875 | if ( m_dwLastError != ERROR_SUCCESS ) { |
876 | wxLogSysError(m_dwLastError, _("Can't read value of key '%s'"), | |
877 | GetName().c_str()); | |
4d08943e | 878 | return false; |
9b386eca | 879 | } |
4d08943e | 880 | return true; |
9b386eca | 881 | } |
4d08943e | 882 | return false; |
9b386eca RG |
883 | } |
884 | ||
885 | ||
886 | ||
6dfec4b8 VZ |
887 | bool wxRegKey::QueryValue(const wxChar *szValue, |
888 | wxString& strValue, | |
889 | bool raw) const | |
2bda0e17 | 890 | { |
d305f9df | 891 | if ( CONST_CAST Open(Read) ) { |
3a5bcc4d | 892 | |
2bda0e17 KB |
893 | // first get the type and size of the data |
894 | DWORD dwType, dwSize; | |
837e5743 | 895 | m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED, |
2bda0e17 KB |
896 | &dwType, NULL, &dwSize); |
897 | if ( m_dwLastError == ERROR_SUCCESS ) { | |
23f681ec VZ |
898 | if ( !dwSize ) { |
899 | // must treat this case specially as GetWriteBuf() doesn't like | |
900 | // being called with 0 size | |
901 | strValue.Empty(); | |
902 | } | |
903 | else { | |
23f681ec VZ |
904 | m_dwLastError = RegQueryValueEx((HKEY) m_hKey, |
905 | WXSTRINGCAST szValue, | |
906 | RESERVED, | |
907 | &dwType, | |
de564874 | 908 | (RegString)(wxChar*)wxStringBuffer(strValue, dwSize), |
23f681ec | 909 | &dwSize); |
6dfec4b8 VZ |
910 | |
911 | // expand the var expansions in the string unless disabled | |
4676948b | 912 | #ifndef __WXWINCE__ |
6dfec4b8 VZ |
913 | if ( (dwType == REG_EXPAND_SZ) && !raw ) |
914 | { | |
915 | DWORD dwExpSize = ::ExpandEnvironmentStrings(strValue, NULL, 0); | |
916 | bool ok = dwExpSize != 0; | |
917 | if ( ok ) | |
918 | { | |
919 | wxString strExpValue; | |
920 | ok = ::ExpandEnvironmentStrings | |
921 | ( | |
922 | strValue, | |
de564874 | 923 | wxStringBuffer(strExpValue, dwExpSize), |
6dfec4b8 VZ |
924 | dwExpSize |
925 | ) != 0; | |
6dfec4b8 VZ |
926 | strValue = strExpValue; |
927 | } | |
928 | ||
929 | if ( !ok ) | |
930 | { | |
931 | wxLogLastError(_T("ExpandEnvironmentStrings")); | |
932 | } | |
933 | } | |
7010702f WS |
934 | #else |
935 | wxUnusedVar(raw); | |
4676948b JS |
936 | #endif |
937 | // __WXWINCE__ | |
23f681ec VZ |
938 | } |
939 | ||
2bda0e17 KB |
940 | if ( m_dwLastError == ERROR_SUCCESS ) { |
941 | // check that it was the right type | |
23f681ec | 942 | wxASSERT_MSG( !IsNumericValue(szValue), |
223d09f6 | 943 | wxT("Type mismatch in wxRegKey::QueryValue().") ); |
2bda0e17 | 944 | |
4d08943e | 945 | return true; |
2bda0e17 KB |
946 | } |
947 | } | |
2bda0e17 KB |
948 | } |
949 | ||
23f681ec | 950 | wxLogSysError(m_dwLastError, _("Can't read value of '%s'"), |
2bda0e17 | 951 | GetFullName(this, szValue)); |
4d08943e | 952 | return false; |
2bda0e17 KB |
953 | } |
954 | ||
837e5743 | 955 | bool wxRegKey::SetValue(const wxChar *szValue, const wxString& strValue) |
2bda0e17 KB |
956 | { |
957 | if ( CONST_CAST Open() ) { | |
6b037754 | 958 | m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_SZ, |
23f681ec | 959 | (RegString)strValue.c_str(), |
f6bcfd97 | 960 | (strValue.Len() + 1)*sizeof(wxChar)); |
2bda0e17 | 961 | if ( m_dwLastError == ERROR_SUCCESS ) |
4d08943e | 962 | return true; |
2bda0e17 KB |
963 | } |
964 | ||
23f681ec | 965 | wxLogSysError(m_dwLastError, _("Can't set value of '%s'"), |
2bda0e17 | 966 | GetFullName(this, szValue)); |
4d08943e | 967 | return false; |
2bda0e17 KB |
968 | } |
969 | ||
50e42404 | 970 | wxString wxRegKey::QueryDefaultValue() const |
2bda0e17 KB |
971 | { |
972 | wxString str; | |
973 | QueryValue(NULL, str); | |
974 | return str; | |
975 | } | |
976 | ||
977 | // ---------------------------------------------------------------------------- | |
978 | // enumeration | |
979 | // NB: all these functions require an index variable which allows to have | |
980 | // several concurrently running indexations on the same key | |
981 | // ---------------------------------------------------------------------------- | |
982 | ||
2bda0e17 KB |
983 | bool wxRegKey::GetFirstValue(wxString& strValueName, long& lIndex) |
984 | { | |
d305f9df | 985 | if ( !Open(Read) ) |
4d08943e | 986 | return false; |
2bda0e17 KB |
987 | |
988 | lIndex = 0; | |
0b1c5a6c | 989 | return GetNextValue(strValueName, lIndex); |
2bda0e17 KB |
990 | } |
991 | ||
992 | bool wxRegKey::GetNextValue(wxString& strValueName, long& lIndex) const | |
993 | { | |
994 | wxASSERT( IsOpened() ); | |
2bda0e17 | 995 | |
0b1c5a6c VZ |
996 | // are we already at the end of enumeration? |
997 | if ( lIndex == -1 ) | |
4d08943e | 998 | return false; |
2bda0e17 | 999 | |
837e5743 | 1000 | wxChar szValueName[1024]; // @@ use RegQueryInfoKey... |
0b1c5a6c | 1001 | DWORD dwValueLen = WXSIZEOF(szValueName); |
2bda0e17 | 1002 | |
92049cd4 | 1003 | m_dwLastError = RegEnumValue((HKEY) m_hKey, lIndex++, |
0b1c5a6c | 1004 | szValueName, &dwValueLen, |
23f681ec VZ |
1005 | RESERVED, |
1006 | NULL, // [out] type | |
0b1c5a6c VZ |
1007 | NULL, // [out] buffer for value |
1008 | NULL); // [i/o] it's length | |
1009 | ||
1010 | if ( m_dwLastError != ERROR_SUCCESS ) { | |
1011 | if ( m_dwLastError == ERROR_NO_MORE_ITEMS ) { | |
1012 | m_dwLastError = ERROR_SUCCESS; | |
1013 | lIndex = -1; | |
1014 | } | |
1015 | else { | |
23f681ec | 1016 | wxLogSysError(m_dwLastError, _("Can't enumerate values of key '%s'"), |
0b1c5a6c VZ |
1017 | GetName().c_str()); |
1018 | } | |
1019 | ||
4d08943e | 1020 | return false; |
2bda0e17 KB |
1021 | } |
1022 | ||
0b1c5a6c | 1023 | strValueName = szValueName; |
0b1c5a6c | 1024 | |
4d08943e | 1025 | return true; |
2bda0e17 | 1026 | } |
2bda0e17 KB |
1027 | |
1028 | bool wxRegKey::GetFirstKey(wxString& strKeyName, long& lIndex) | |
1029 | { | |
d305f9df | 1030 | if ( !Open(Read) ) |
4d08943e | 1031 | return false; |
2bda0e17 | 1032 | |
2bda0e17 | 1033 | lIndex = 0; |
0b1c5a6c | 1034 | return GetNextKey(strKeyName, lIndex); |
2bda0e17 KB |
1035 | } |
1036 | ||
1037 | bool wxRegKey::GetNextKey(wxString& strKeyName, long& lIndex) const | |
1038 | { | |
1039 | wxASSERT( IsOpened() ); | |
0b1c5a6c VZ |
1040 | |
1041 | // are we already at the end of enumeration? | |
1042 | if ( lIndex == -1 ) | |
4d08943e | 1043 | return false; |
2bda0e17 | 1044 | |
837e5743 | 1045 | wxChar szKeyName[_MAX_PATH + 1]; |
4676948b JS |
1046 | |
1047 | #ifdef __WXWINCE__ | |
1048 | DWORD sizeName = WXSIZEOF(szKeyName); | |
1049 | m_dwLastError = RegEnumKeyEx((HKEY) m_hKey, lIndex++, szKeyName, & sizeName, | |
1050 | 0, NULL, NULL, NULL); | |
1051 | #else | |
fd6c844b | 1052 | m_dwLastError = RegEnumKey((HKEY) m_hKey, lIndex++, szKeyName, WXSIZEOF(szKeyName)); |
4676948b | 1053 | #endif |
2bda0e17 KB |
1054 | |
1055 | if ( m_dwLastError != ERROR_SUCCESS ) { | |
1056 | if ( m_dwLastError == ERROR_NO_MORE_ITEMS ) { | |
1057 | m_dwLastError = ERROR_SUCCESS; | |
1058 | lIndex = -1; | |
1059 | } | |
1060 | else { | |
23f681ec | 1061 | wxLogSysError(m_dwLastError, _("Can't enumerate subkeys of key '%s'"), |
2bda0e17 KB |
1062 | GetName().c_str()); |
1063 | } | |
1064 | ||
4d08943e | 1065 | return false; |
2bda0e17 KB |
1066 | } |
1067 | ||
1068 | strKeyName = szKeyName; | |
4d08943e | 1069 | return true; |
2bda0e17 KB |
1070 | } |
1071 | ||
4d08943e | 1072 | // returns true if the value contains a number (else it's some string) |
837e5743 | 1073 | bool wxRegKey::IsNumericValue(const wxChar *szValue) const |
4d08943e WS |
1074 | { |
1075 | ValueType type = GetValueType(szValue); | |
1076 | switch ( type ) { | |
03ab016d | 1077 | case Type_Dword: |
23f681ec | 1078 | /* case Type_Dword_little_endian: == Type_Dword */ |
03ab016d | 1079 | case Type_Dword_big_endian: |
4d08943e | 1080 | return true; |
03ab016d JS |
1081 | |
1082 | default: | |
4d08943e WS |
1083 | return false; |
1084 | } | |
1085 | } | |
03ab016d | 1086 | |
20d8c319 VZ |
1087 | // ---------------------------------------------------------------------------- |
1088 | // exporting registry keys to file | |
1089 | // ---------------------------------------------------------------------------- | |
1090 | ||
1091 | // helper functions for writing ASCII strings (even in Unicode build) | |
1092 | static inline bool WriteAsciiChar(wxOutputStream& ostr, char ch) | |
1093 | { | |
1094 | ostr.PutC(ch); | |
1095 | return ostr.IsOk(); | |
1096 | } | |
1097 | ||
1098 | static inline bool WriteAsciiEOL(wxOutputStream& ostr) | |
1099 | { | |
1100 | // as we open the file in text mode, it is enough to write LF without CR | |
1101 | return WriteAsciiChar(ostr, '\n'); | |
1102 | } | |
1103 | ||
1104 | static inline bool WriteAsciiString(wxOutputStream& ostr, const char *p) | |
1105 | { | |
1106 | return ostr.Write(p, strlen(p)).IsOk(); | |
1107 | } | |
1108 | ||
1109 | static inline bool WriteAsciiString(wxOutputStream& ostr, const wxString& s) | |
1110 | { | |
1111 | #if wxUSE_UNICODE | |
1112 | wxCharBuffer name(s.mb_str()); | |
1113 | ostr.Write(name, strlen(name)); | |
1114 | #else | |
1115 | ostr.Write(s, s.length()); | |
1116 | #endif | |
1117 | ||
1118 | return ostr.IsOk(); | |
1119 | } | |
1120 | ||
1121 | bool wxRegKey::Export(const wxString& filename) const | |
1122 | { | |
1123 | if ( wxFile::Exists(filename) ) | |
1124 | { | |
1125 | wxLogError(_("Exporting registry key: file \"%s\" already exists and won't be overwritten."), | |
1126 | filename.c_str()); | |
1127 | return false; | |
1128 | } | |
1129 | ||
1130 | wxFFileOutputStream ostr(filename, _T("w")); | |
1131 | ||
1132 | return ostr.Ok() && Export(ostr); | |
1133 | } | |
1134 | ||
1135 | bool wxRegKey::Export(wxOutputStream& ostr) const | |
1136 | { | |
1137 | // write out the header | |
1138 | if ( !WriteAsciiString(ostr, "REGEDIT4\n\n") ) | |
1139 | return false; | |
1140 | ||
1141 | return DoExport(ostr); | |
1142 | } | |
1143 | ||
1144 | static | |
1145 | wxString | |
1146 | FormatAsHex(const void *data, | |
1147 | size_t size, | |
1148 | wxRegKey::ValueType type = wxRegKey::Type_Binary) | |
1149 | { | |
1150 | wxString value(_T("hex")); | |
1151 | ||
1152 | // binary values use just "hex:" prefix while the other ones must indicate | |
1153 | // the real type | |
1154 | if ( type != wxRegKey::Type_Binary ) | |
1155 | value << _T('(') << type << _T(')'); | |
1156 | value << _T(':'); | |
1157 | ||
1158 | // write all the rest as comma-separated bytes | |
1159 | value.reserve(3*size + 10); | |
1160 | const char * const p = wx_static_cast(const char *, data); | |
1161 | for ( size_t n = 0; n < size; n++ ) | |
1162 | { | |
1163 | // TODO: line wrapping: although not required by regedit, this makes | |
1164 | // the generated files easier to read and compare with the files | |
1165 | // produced by regedit | |
1166 | if ( n ) | |
1167 | value << _T(','); | |
1168 | ||
f1a9125b | 1169 | value << wxString::Format(_T("%02x"), (unsigned char)p[n]); |
20d8c319 VZ |
1170 | } |
1171 | ||
1172 | return value; | |
1173 | } | |
1174 | ||
1175 | static inline | |
1176 | wxString FormatAsHex(const wxString& value, wxRegKey::ValueType type) | |
1177 | { | |
1178 | return FormatAsHex(value.c_str(), value.length() + 1, type); | |
1179 | } | |
1180 | ||
1181 | wxString wxRegKey::FormatValue(const wxString& name) const | |
1182 | { | |
1183 | wxString rhs; | |
1184 | const ValueType type = GetValueType(name); | |
1185 | switch ( type ) | |
1186 | { | |
1187 | case Type_String: | |
1188 | { | |
1189 | wxString value; | |
1190 | if ( !QueryValue(name, value) ) | |
1191 | break; | |
1192 | ||
1193 | // quotes and backslashes must be quoted, linefeeds are not | |
1194 | // allowed in string values | |
1195 | rhs.reserve(value.length() + 2); | |
1196 | rhs = _T('"'); | |
1197 | ||
1198 | // there can be no NULs here | |
1199 | bool useHex = false; | |
1200 | for ( const wxChar *p = value.c_str(); *p && !useHex; p++ ) | |
1201 | { | |
1202 | switch ( *p ) | |
1203 | { | |
1204 | case _T('\n'): | |
1205 | // we can only represent this string in hex | |
1206 | useHex = true; | |
1207 | break; | |
1208 | ||
1209 | case _T('"'): | |
1210 | case _T('\\'): | |
1211 | // escape special symbol | |
1212 | rhs += _T('\\'); | |
1213 | // fall through | |
1214 | ||
1215 | default: | |
1216 | rhs += *p; | |
1217 | } | |
1218 | } | |
1219 | ||
1220 | if ( useHex ) | |
1221 | rhs = FormatAsHex(value, Type_String); | |
1222 | else | |
1223 | rhs += _T('"'); | |
1224 | } | |
1225 | break; | |
1226 | ||
1227 | case Type_Dword: | |
1228 | /* case Type_Dword_little_endian: == Type_Dword */ | |
1229 | { | |
1230 | long value; | |
1231 | if ( !QueryValue(name, &value) ) | |
1232 | break; | |
1233 | ||
04a18b0d | 1234 | rhs.Printf(_T("dword:%08x"), (unsigned int)value); |
20d8c319 VZ |
1235 | } |
1236 | break; | |
1237 | ||
1238 | case Type_Expand_String: | |
1239 | case Type_Multi_String: | |
1240 | { | |
1241 | wxString value; | |
1242 | if ( !QueryRawValue(name, value) ) | |
1243 | break; | |
1244 | ||
1245 | rhs = FormatAsHex(value, type); | |
1246 | } | |
1247 | break; | |
1248 | ||
1249 | case Type_Binary: | |
1250 | { | |
1251 | wxMemoryBuffer buf; | |
1252 | if ( !QueryValue(name, buf) ) | |
1253 | break; | |
1254 | ||
1255 | rhs = FormatAsHex(buf.GetData(), buf.GetDataLen()); | |
1256 | } | |
1257 | break; | |
1258 | ||
1259 | // no idea how those appear in REGEDIT4 files | |
1260 | case Type_None: | |
1261 | case Type_Dword_big_endian: | |
1262 | case Type_Link: | |
1263 | case Type_Resource_list: | |
1264 | case Type_Full_resource_descriptor: | |
1265 | case Type_Resource_requirements_list: | |
1266 | default: | |
1267 | wxLogWarning(_("Can't export value of unsupported type %d."), type); | |
1268 | } | |
1269 | ||
1270 | return rhs; | |
1271 | } | |
1272 | ||
1273 | bool wxRegKey::DoExportValue(wxOutputStream& ostr, const wxString& name) const | |
1274 | { | |
1275 | // first examine the value type: if it's unsupported, simply skip it | |
1276 | // instead of aborting the entire export process because we failed to | |
1277 | // export a single value | |
1278 | wxString value = FormatValue(name); | |
1279 | if ( value.empty() ) | |
1280 | { | |
1281 | wxLogWarning(_("Ignoring value \"%s\" of the key \"%s\"."), | |
1282 | name.c_str(), GetName().c_str()); | |
1283 | return true; | |
1284 | } | |
1285 | ||
1286 | // we do have the text representation of the value, now write everything | |
1287 | // out | |
1288 | ||
1289 | // special case: unnamed/default value is represented as just "@" | |
1290 | if ( name.empty() ) | |
1291 | { | |
1292 | if ( !WriteAsciiChar(ostr, '@') ) | |
1293 | return false; | |
1294 | } | |
1295 | else // normal, named, value | |
1296 | { | |
1297 | if ( !WriteAsciiChar(ostr, '"') || | |
1298 | !WriteAsciiString(ostr, name) || | |
1299 | !WriteAsciiChar(ostr, '"') ) | |
1300 | return false; | |
1301 | } | |
1302 | ||
1303 | if ( !WriteAsciiChar(ostr, '=') ) | |
1304 | return false; | |
1305 | ||
1306 | return WriteAsciiString(ostr, value) && WriteAsciiEOL(ostr); | |
1307 | } | |
1308 | ||
1309 | bool wxRegKey::DoExport(wxOutputStream& ostr) const | |
1310 | { | |
1311 | // write out this key name | |
1312 | if ( !WriteAsciiChar(ostr, '[') ) | |
1313 | return false; | |
1314 | ||
1315 | if ( !WriteAsciiString(ostr, GetName(false /* no short prefix */)) ) | |
1316 | return false; | |
1317 | ||
1318 | if ( !WriteAsciiChar(ostr, ']') || !WriteAsciiEOL(ostr) ) | |
1319 | return false; | |
1320 | ||
1321 | // dump all our values | |
1322 | long dummy; | |
1323 | wxString name; | |
1324 | wxRegKey& self = wx_const_cast(wxRegKey&, *this); | |
1325 | bool cont = self.GetFirstValue(name, dummy); | |
1326 | while ( cont ) | |
1327 | { | |
1328 | if ( !DoExportValue(ostr, name) ) | |
1329 | return false; | |
1330 | ||
1331 | cont = GetNextValue(name, dummy); | |
1332 | } | |
1333 | ||
1334 | // always terminate values by blank line, even if there were no values | |
1335 | if ( !WriteAsciiEOL(ostr) ) | |
1336 | return false; | |
1337 | ||
1338 | // recurse to subkeys | |
1339 | cont = self.GetFirstKey(name, dummy); | |
1340 | while ( cont ) | |
1341 | { | |
1342 | wxRegKey subkey(*this, name); | |
1343 | if ( !subkey.DoExport(ostr) ) | |
1344 | return false; | |
1345 | ||
1346 | cont = GetNextKey(name, dummy); | |
1347 | } | |
1348 | ||
1349 | return true; | |
1350 | } | |
1351 | ||
2bda0e17 | 1352 | // ============================================================================ |
1880e452 | 1353 | // implementation of global private functions |
2bda0e17 | 1354 | // ============================================================================ |
f6bcfd97 | 1355 | |
837e5743 | 1356 | bool KeyExists(WXHKEY hRootKey, const wxChar *szKey) |
2bda0e17 | 1357 | { |
8a8c41dd VZ |
1358 | // don't close this key itself for the case of empty szKey! |
1359 | if ( wxIsEmpty(szKey) ) | |
4d08943e | 1360 | return true; |
8a8c41dd VZ |
1361 | |
1362 | HKEY hkeyDummy; | |
1363 | if ( ::RegOpenKeyEx | |
1364 | ( | |
1365 | (HKEY)hRootKey, | |
1366 | szKey, | |
1367 | RESERVED, | |
bee96abf | 1368 | KEY_READ, // we might not have enough rights for rw access |
8a8c41dd VZ |
1369 | &hkeyDummy |
1370 | ) == ERROR_SUCCESS ) | |
1371 | { | |
1372 | ::RegCloseKey(hkeyDummy); | |
1373 | ||
4d08943e | 1374 | return true; |
8a8c41dd | 1375 | } |
f6bcfd97 | 1376 | |
4d08943e | 1377 | return false; |
2bda0e17 KB |
1378 | } |
1379 | ||
837e5743 | 1380 | const wxChar *GetFullName(const wxRegKey *pKey, const wxChar *szValue) |
2bda0e17 KB |
1381 | { |
1382 | static wxString s_str; | |
1383 | s_str = pKey->GetName(); | |
837e5743 | 1384 | if ( !wxIsEmpty(szValue) ) |
223d09f6 | 1385 | s_str << wxT("\\") << szValue; |
2bda0e17 KB |
1386 | |
1387 | return s_str.c_str(); | |
0b1c5a6c VZ |
1388 | } |
1389 | ||
1390 | void RemoveTrailingSeparator(wxString& str) | |
1391 | { | |
1392 | if ( !str.IsEmpty() && str.Last() == REG_SEPARATOR ) | |
1393 | str.Truncate(str.Len() - 1); | |
1394 | } | |
3d05544e | 1395 |