]>
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 | 77 | #endif |
6f94aa86 | 78 | #ifdef HKEY_CURRENT_CONFIG |
223d09f6 | 79 | { HKEY_CURRENT_CONFIG, wxT("HKEY_CURRENT_CONFIG"), wxT("HKCC") }, |
6f94aa86 VZ |
80 | #endif |
81 | #ifdef HKEY_DYN_DATA | |
223d09f6 | 82 | { HKEY_DYN_DATA, wxT("HKEY_DYN_DATA"), wxT("HKDD") }, // short name? |
6f94aa86 | 83 | #endif |
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); | |
532d575b | 166 | if ( !strKey.empty() && strKey.Last() == REG_SEPARATOR ) |
2bda0e17 KB |
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 | |
532d575b WS |
217 | if ( !m_strKey.empty() && |
218 | (strKey.empty() || strKey[0] != REG_SEPARATOR) ) { | |
32c66ea2 VZ |
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; | |
532d575b | 273 | if ( !strKey.empty() && strKey[0] != REG_SEPARATOR ) |
0b1c5a6c | 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 | 306 | : aStdKeys[key].szName; |
532d575b | 307 | if ( !m_strKey.empty() ) |
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() ) |
fb5e0dcf VZ |
367 | { |
368 | if ( mode <= m_mode ) | |
369 | return true; | |
370 | ||
371 | // we had been opened in read mode but now must be reopened in write | |
372 | Close(); | |
373 | } | |
8a8c41dd VZ |
374 | |
375 | HKEY tmpKey; | |
376 | m_dwLastError = ::RegOpenKeyEx | |
377 | ( | |
378 | (HKEY) m_hRootKey, | |
379 | m_strKey, | |
380 | RESERVED, | |
bee96abf | 381 | mode == Read ? KEY_READ : KEY_ALL_ACCESS, |
8a8c41dd VZ |
382 | &tmpKey |
383 | ); | |
384 | ||
385 | if ( m_dwLastError != ERROR_SUCCESS ) | |
386 | { | |
387 | wxLogSysError(m_dwLastError, _("Can't open registry key '%s'"), | |
388 | GetName().c_str()); | |
4d08943e | 389 | return false; |
8a8c41dd | 390 | } |
2bda0e17 | 391 | |
fd6c844b | 392 | m_hKey = (WXHKEY) tmpKey; |
fb5e0dcf VZ |
393 | m_mode = mode; |
394 | ||
4d08943e | 395 | return true; |
2bda0e17 KB |
396 | } |
397 | ||
398 | // creates key, failing if it exists and !bOkIfExists | |
399 | bool wxRegKey::Create(bool bOkIfExists) | |
400 | { | |
401 | // check for existence only if asked (i.e. order is important!) | |
8a8c41dd | 402 | if ( !bOkIfExists && Exists() ) |
4d08943e | 403 | return false; |
2bda0e17 KB |
404 | |
405 | if ( IsOpened() ) | |
4d08943e | 406 | return true; |
2bda0e17 | 407 | |
fd6c844b | 408 | HKEY tmpKey; |
4676948b JS |
409 | #ifdef __WXWINCE__ |
410 | DWORD disposition; | |
411 | m_dwLastError = RegCreateKeyEx((HKEY) m_hRootKey, m_strKey, | |
412 | NULL, // reserved | |
413 | NULL, // class string | |
414 | 0, | |
415 | 0, | |
416 | NULL, | |
417 | &tmpKey, | |
418 | &disposition); | |
419 | #else | |
fd6c844b | 420 | m_dwLastError = RegCreateKey((HKEY) m_hRootKey, m_strKey, &tmpKey); |
4676948b | 421 | #endif |
2bda0e17 | 422 | if ( m_dwLastError != ERROR_SUCCESS ) { |
23f681ec | 423 | wxLogSysError(m_dwLastError, _("Can't create registry key '%s'"), |
2bda0e17 | 424 | GetName().c_str()); |
4d08943e | 425 | return false; |
2bda0e17 KB |
426 | } |
427 | else | |
fd6c844b JS |
428 | { |
429 | m_hKey = (WXHKEY) tmpKey; | |
4d08943e | 430 | return true; |
fd6c844b | 431 | } |
2bda0e17 KB |
432 | } |
433 | ||
0b1c5a6c VZ |
434 | // close the key, it's not an error to call it when not opened |
435 | bool wxRegKey::Close() | |
436 | { | |
437 | if ( IsOpened() ) { | |
fd6c844b | 438 | m_dwLastError = RegCloseKey((HKEY) m_hKey); |
807a903e VZ |
439 | m_hKey = 0; |
440 | ||
0b1c5a6c | 441 | if ( m_dwLastError != ERROR_SUCCESS ) { |
23f681ec | 442 | wxLogSysError(m_dwLastError, _("Can't close registry key '%s'"), |
0b1c5a6c VZ |
443 | GetName().c_str()); |
444 | ||
4d08943e | 445 | return false; |
0b1c5a6c | 446 | } |
0b1c5a6c VZ |
447 | } |
448 | ||
4d08943e | 449 | return true; |
0b1c5a6c VZ |
450 | } |
451 | ||
23f681ec VZ |
452 | bool wxRegKey::RenameValue(const wxChar *szValueOld, const wxChar *szValueNew) |
453 | { | |
4d08943e | 454 | bool ok = true; |
23f681ec VZ |
455 | if ( HasValue(szValueNew) ) { |
456 | wxLogError(_("Registry value '%s' already exists."), szValueNew); | |
457 | ||
4d08943e | 458 | ok = false; |
23f681ec VZ |
459 | } |
460 | ||
225fe9d6 VZ |
461 | if ( !ok || |
462 | !CopyValue(szValueOld, *this, szValueNew) || | |
463 | !DeleteValue(szValueOld) ) { | |
23f681ec VZ |
464 | wxLogError(_("Failed to rename registry value '%s' to '%s'."), |
465 | szValueOld, szValueNew); | |
466 | ||
4d08943e | 467 | return false; |
23f681ec VZ |
468 | } |
469 | ||
4d08943e | 470 | return true; |
23f681ec VZ |
471 | } |
472 | ||
473 | bool wxRegKey::CopyValue(const wxChar *szValue, | |
474 | wxRegKey& keyDst, | |
475 | const wxChar *szValueNew) | |
476 | { | |
5af3f0ef VZ |
477 | if ( !szValueNew ) { |
478 | // by default, use the same name | |
479 | szValueNew = szValue; | |
480 | } | |
481 | ||
23f681ec VZ |
482 | switch ( GetValueType(szValue) ) { |
483 | case Type_String: | |
484 | { | |
485 | wxString strVal; | |
486 | return QueryValue(szValue, strVal) && | |
487 | keyDst.SetValue(szValueNew, strVal); | |
488 | } | |
489 | ||
490 | case Type_Dword: | |
491 | /* case Type_Dword_little_endian: == Type_Dword */ | |
492 | { | |
493 | long dwVal; | |
494 | return QueryValue(szValue, &dwVal) && | |
495 | keyDst.SetValue(szValueNew, dwVal); | |
496 | } | |
497 | ||
9b386eca | 498 | case Type_Binary: |
4d08943e WS |
499 | { |
500 | wxMemoryBuffer buf; | |
501 | return QueryValue(szValue,buf) && | |
502 | keyDst.SetValue(szValueNew,buf); | |
503 | } | |
20d8c319 | 504 | |
23f681ec VZ |
505 | // these types are unsupported because I am not sure about how |
506 | // exactly they should be copied and because they shouldn't | |
507 | // occur among the application keys (supposedly created with | |
508 | // this class) | |
23f681ec VZ |
509 | case Type_None: |
510 | case Type_Expand_String: | |
23f681ec VZ |
511 | case Type_Dword_big_endian: |
512 | case Type_Link: | |
513 | case Type_Multi_String: | |
514 | case Type_Resource_list: | |
515 | case Type_Full_resource_descriptor: | |
516 | case Type_Resource_requirements_list: | |
23f681ec VZ |
517 | default: |
518 | wxLogError(_("Can't copy values of unsupported type %d."), | |
519 | GetValueType(szValue)); | |
4d08943e | 520 | return false; |
23f681ec VZ |
521 | } |
522 | } | |
523 | ||
225fe9d6 VZ |
524 | bool wxRegKey::Rename(const wxChar *szNewName) |
525 | { | |
532d575b | 526 | wxCHECK_MSG( !m_strKey.empty(), false, _T("registry hives can't be renamed") ); |
225fe9d6 VZ |
527 | |
528 | if ( !Exists() ) { | |
529 | wxLogError(_("Registry key '%s' does not exist, cannot rename it."), | |
530 | GetFullName(this)); | |
531 | ||
4d08943e | 532 | return false; |
225fe9d6 VZ |
533 | } |
534 | ||
535 | // do we stay in the same hive? | |
536 | bool inSameHive = !wxStrchr(szNewName, REG_SEPARATOR); | |
537 | ||
538 | // construct the full new name of the key | |
539 | wxRegKey keyDst; | |
540 | ||
541 | if ( inSameHive ) { | |
542 | // rename the key to the new name under the same parent | |
543 | wxString strKey = m_strKey.BeforeLast(REG_SEPARATOR); | |
532d575b | 544 | if ( !strKey.empty() ) { |
225fe9d6 VZ |
545 | // don't add '\\' in the start if strFullNewName is empty |
546 | strKey += REG_SEPARATOR; | |
547 | } | |
548 | ||
549 | strKey += szNewName; | |
550 | ||
551 | keyDst.SetName(GetStdKeyFromHkey(m_hRootKey), strKey); | |
552 | } | |
553 | else { | |
554 | // this is the full name already | |
555 | keyDst.SetName(szNewName); | |
556 | } | |
557 | ||
4d08943e | 558 | bool ok = keyDst.Create(false /* fail if alredy exists */); |
225fe9d6 VZ |
559 | if ( !ok ) { |
560 | wxLogError(_("Registry key '%s' already exists."), | |
561 | GetFullName(&keyDst)); | |
562 | } | |
563 | else { | |
564 | ok = Copy(keyDst) && DeleteSelf(); | |
565 | } | |
566 | ||
567 | if ( !ok ) { | |
568 | wxLogError(_("Failed to rename the registry key '%s' to '%s'."), | |
569 | GetFullName(this), GetFullName(&keyDst)); | |
570 | } | |
571 | else { | |
572 | m_hRootKey = keyDst.m_hRootKey; | |
573 | m_strKey = keyDst.m_strKey; | |
574 | } | |
575 | ||
576 | return ok; | |
577 | } | |
578 | ||
579 | bool wxRegKey::Copy(const wxChar *szNewName) | |
23f681ec VZ |
580 | { |
581 | // create the new key first | |
225fe9d6 | 582 | wxRegKey keyDst(szNewName); |
4d08943e | 583 | bool ok = keyDst.Create(false /* fail if alredy exists */); |
23f681ec VZ |
584 | if ( ok ) { |
585 | ok = Copy(keyDst); | |
586 | ||
587 | // we created the dest key but copying to it failed - delete it | |
588 | if ( !ok ) { | |
589 | (void)keyDst.DeleteSelf(); | |
590 | } | |
591 | } | |
592 | ||
593 | return ok; | |
594 | } | |
595 | ||
596 | bool wxRegKey::Copy(wxRegKey& keyDst) | |
597 | { | |
4d08943e | 598 | bool ok = true; |
23f681ec VZ |
599 | |
600 | // copy all sub keys to the new location | |
601 | wxString strKey; | |
602 | long lIndex; | |
603 | bool bCont = GetFirstKey(strKey, lIndex); | |
604 | while ( ok && bCont ) { | |
605 | wxRegKey key(*this, strKey); | |
606 | wxString keyName; | |
607 | keyName << GetFullName(&keyDst) << REG_SEPARATOR << strKey; | |
f6bcfd97 | 608 | ok = key.Copy((const wxChar*) keyName); |
23f681ec VZ |
609 | |
610 | if ( ok ) | |
611 | bCont = GetNextKey(strKey, lIndex); | |
4d08943e WS |
612 | else |
613 | wxLogError(_("Failed to copy the registry subkey '%s' to '%s'."), | |
835ab90d | 614 | GetFullName(&key), keyName.c_str()); |
4d08943e | 615 | |
23f681ec VZ |
616 | } |
617 | ||
618 | // copy all values | |
619 | wxString strVal; | |
620 | bCont = GetFirstValue(strVal, lIndex); | |
621 | while ( ok && bCont ) { | |
622 | ok = CopyValue(strVal, keyDst); | |
623 | ||
624 | if ( !ok ) { | |
625 | wxLogSysError(m_dwLastError, | |
626 | _("Failed to copy registry value '%s'"), | |
627 | strVal.c_str()); | |
628 | } | |
629 | else { | |
630 | bCont = GetNextValue(strVal, lIndex); | |
631 | } | |
632 | } | |
633 | ||
634 | if ( !ok ) { | |
835ab90d VZ |
635 | wxLogError(_("Failed to copy the contents of registry key '%s' to '%s'."), |
636 | GetFullName(this), GetFullName(&keyDst)); | |
23f681ec VZ |
637 | } |
638 | ||
639 | return ok; | |
640 | } | |
641 | ||
0b1c5a6c VZ |
642 | // ---------------------------------------------------------------------------- |
643 | // delete keys/values | |
644 | // ---------------------------------------------------------------------------- | |
2bda0e17 KB |
645 | bool wxRegKey::DeleteSelf() |
646 | { | |
0b1c5a6c VZ |
647 | { |
648 | wxLogNull nolog; | |
649 | if ( !Open() ) { | |
650 | // it already doesn't exist - ok! | |
4d08943e | 651 | return true; |
0b1c5a6c VZ |
652 | } |
653 | } | |
654 | ||
90186e52 VZ |
655 | // prevent a buggy program from erasing one of the root registry keys or an |
656 | // immediate subkey (i.e. one which doesn't have '\\' inside) of any other | |
657 | // key except HKCR (HKCR has some "deleteable" subkeys) | |
532d575b | 658 | if ( m_strKey.empty() || |
3cc487d1 VZ |
659 | ((m_hRootKey != (WXHKEY) aStdKeys[HKCR].hkey) && |
660 | (m_strKey.Find(REG_SEPARATOR) == wxNOT_FOUND)) ) { | |
835ab90d VZ |
661 | wxLogError(_("Registry key '%s' is needed for normal system operation,\ndeleting it will leave your system in unusable state:\noperation aborted."), |
662 | GetFullName(this)); | |
90186e52 | 663 | |
4d08943e | 664 | return false; |
90186e52 VZ |
665 | } |
666 | ||
0b1c5a6c VZ |
667 | // we can't delete keys while enumerating because it confuses GetNextKey, so |
668 | // we first save the key names and then delete them all | |
669 | wxArrayString astrSubkeys; | |
2bda0e17 KB |
670 | |
671 | wxString strKey; | |
672 | long lIndex; | |
673 | bool bCont = GetFirstKey(strKey, lIndex); | |
674 | while ( bCont ) { | |
0b1c5a6c | 675 | astrSubkeys.Add(strKey); |
2bda0e17 KB |
676 | |
677 | bCont = GetNextKey(strKey, lIndex); | |
678 | } | |
679 | ||
c86f1403 VZ |
680 | size_t nKeyCount = astrSubkeys.Count(); |
681 | for ( size_t nKey = 0; nKey < nKeyCount; nKey++ ) { | |
0b1c5a6c VZ |
682 | wxRegKey key(*this, astrSubkeys[nKey]); |
683 | if ( !key.DeleteSelf() ) | |
4d08943e | 684 | return false; |
0b1c5a6c VZ |
685 | } |
686 | ||
687 | // now delete this key itself | |
2bda0e17 KB |
688 | Close(); |
689 | ||
fd6c844b | 690 | m_dwLastError = RegDeleteKey((HKEY) m_hRootKey, m_strKey); |
86a7257f VZ |
691 | // deleting a key which doesn't exist is not considered an error |
692 | if ( m_dwLastError != ERROR_SUCCESS && | |
863e83fa | 693 | m_dwLastError != ERROR_FILE_NOT_FOUND ) { |
23f681ec | 694 | wxLogSysError(m_dwLastError, _("Can't delete key '%s'"), |
02569ba8 | 695 | GetName().c_str()); |
4d08943e | 696 | return false; |
2bda0e17 KB |
697 | } |
698 | ||
4d08943e | 699 | return true; |
2bda0e17 KB |
700 | } |
701 | ||
837e5743 | 702 | bool wxRegKey::DeleteKey(const wxChar *szKey) |
2bda0e17 KB |
703 | { |
704 | if ( !Open() ) | |
4d08943e | 705 | return false; |
2bda0e17 KB |
706 | |
707 | wxRegKey key(*this, szKey); | |
708 | return key.DeleteSelf(); | |
709 | } | |
710 | ||
837e5743 | 711 | bool wxRegKey::DeleteValue(const wxChar *szValue) |
2bda0e17 | 712 | { |
92218ce6 WS |
713 | if ( !Open() ) |
714 | return false; | |
2bda0e17 | 715 | |
837e5743 | 716 | m_dwLastError = RegDeleteValue((HKEY) m_hKey, WXSTRINGCAST szValue); |
886dd7d2 VZ |
717 | |
718 | // deleting a value which doesn't exist is not considered an error | |
719 | if ( (m_dwLastError != ERROR_SUCCESS) && | |
92218ce6 WS |
720 | (m_dwLastError != ERROR_FILE_NOT_FOUND) ) |
721 | { | |
722 | wxLogSysError(m_dwLastError, _("Can't delete value '%s' from key '%s'"), | |
723 | szValue, GetName().c_str()); | |
724 | return false; | |
2bda0e17 | 725 | } |
2bda0e17 | 726 | |
92218ce6 | 727 | return true; |
2bda0e17 KB |
728 | } |
729 | ||
730 | // ---------------------------------------------------------------------------- | |
731 | // access to values and subkeys | |
732 | // ---------------------------------------------------------------------------- | |
733 | ||
4d08943e | 734 | // return true if value exists |
837e5743 | 735 | bool wxRegKey::HasValue(const wxChar *szValue) const |
0b1c5a6c | 736 | { |
92218ce6 WS |
737 | // this function should be silent, so suppress possible messages from Open() |
738 | wxLogNull nolog; | |
23f681ec | 739 | |
d305f9df | 740 | if ( !CONST_CAST Open(Read) ) |
4d08943e | 741 | return false; |
f6bcfd97 BP |
742 | |
743 | LONG dwRet = ::RegQueryValueEx((HKEY) m_hKey, | |
744 | WXSTRINGCAST szValue, | |
745 | RESERVED, | |
746 | NULL, NULL, NULL); | |
747 | return dwRet == ERROR_SUCCESS; | |
0b1c5a6c VZ |
748 | } |
749 | ||
4d08943e | 750 | // returns true if this key has any values |
92049cd4 VZ |
751 | bool wxRegKey::HasValues() const |
752 | { | |
753 | // suppress possible messages from GetFirstValue() | |
754 | wxLogNull nolog; | |
23f681ec | 755 | |
92049cd4 VZ |
756 | // just call GetFirstValue with dummy parameters |
757 | wxString str; | |
758 | long l; | |
759 | return CONST_CAST GetFirstValue(str, l); | |
760 | } | |
761 | ||
4d08943e | 762 | // returns true if this key has any subkeys |
2bda0e17 KB |
763 | bool wxRegKey::HasSubkeys() const |
764 | { | |
c19a8a9a VZ |
765 | // suppress possible messages from GetFirstKey() |
766 | wxLogNull nolog; | |
23f681ec | 767 | |
2bda0e17 KB |
768 | // just call GetFirstKey with dummy parameters |
769 | wxString str; | |
770 | long l; | |
771 | return CONST_CAST GetFirstKey(str, l); | |
772 | } | |
773 | ||
4d08943e | 774 | // returns true if given subkey exists |
837e5743 | 775 | bool wxRegKey::HasSubKey(const wxChar *szKey) const |
2bda0e17 | 776 | { |
c19a8a9a VZ |
777 | // this function should be silent, so suppress possible messages from Open() |
778 | wxLogNull nolog; | |
23f681ec | 779 | |
d305f9df | 780 | if ( !CONST_CAST Open(Read) ) |
4d08943e | 781 | return false; |
f6bcfd97 BP |
782 | |
783 | return KeyExists(m_hKey, szKey); | |
2bda0e17 KB |
784 | } |
785 | ||
837e5743 | 786 | wxRegKey::ValueType wxRegKey::GetValueType(const wxChar *szValue) const |
2bda0e17 | 787 | { |
d305f9df | 788 | if ( ! CONST_CAST Open(Read) ) |
2bda0e17 KB |
789 | return Type_None; |
790 | ||
791 | DWORD dwType; | |
837e5743 | 792 | m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED, |
2bda0e17 KB |
793 | &dwType, NULL, NULL); |
794 | if ( m_dwLastError != ERROR_SUCCESS ) { | |
23f681ec | 795 | wxLogSysError(m_dwLastError, _("Can't read value of key '%s'"), |
2bda0e17 KB |
796 | GetName().c_str()); |
797 | return Type_None; | |
798 | } | |
799 | ||
800 | return (ValueType)dwType; | |
2bda0e17 KB |
801 | } |
802 | ||
837e5743 | 803 | bool wxRegKey::SetValue(const wxChar *szValue, long lValue) |
2bda0e17 KB |
804 | { |
805 | if ( CONST_CAST Open() ) { | |
6b037754 | 806 | m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_DWORD, |
2bda0e17 KB |
807 | (RegString)&lValue, sizeof(lValue)); |
808 | if ( m_dwLastError == ERROR_SUCCESS ) | |
4d08943e | 809 | return true; |
2bda0e17 KB |
810 | } |
811 | ||
23f681ec | 812 | wxLogSysError(m_dwLastError, _("Can't set value of '%s'"), |
2bda0e17 | 813 | GetFullName(this, szValue)); |
4d08943e | 814 | return false; |
2bda0e17 KB |
815 | } |
816 | ||
837e5743 | 817 | bool wxRegKey::QueryValue(const wxChar *szValue, long *plValue) const |
2bda0e17 | 818 | { |
d305f9df | 819 | if ( CONST_CAST Open(Read) ) { |
2bda0e17 KB |
820 | DWORD dwType, dwSize = sizeof(DWORD); |
821 | RegString pBuf = (RegString)plValue; | |
837e5743 | 822 | m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED, |
2bda0e17 KB |
823 | &dwType, pBuf, &dwSize); |
824 | if ( m_dwLastError != ERROR_SUCCESS ) { | |
23f681ec | 825 | wxLogSysError(m_dwLastError, _("Can't read value of key '%s'"), |
2bda0e17 | 826 | GetName().c_str()); |
4d08943e | 827 | return false; |
2bda0e17 KB |
828 | } |
829 | else { | |
830 | // check that we read the value of right type | |
23f681ec | 831 | wxASSERT_MSG( IsNumericValue(szValue), |
223d09f6 | 832 | wxT("Type mismatch in wxRegKey::QueryValue().") ); |
2bda0e17 | 833 | |
4d08943e | 834 | return true; |
2bda0e17 KB |
835 | } |
836 | } | |
837 | else | |
4d08943e | 838 | return false; |
2bda0e17 KB |
839 | } |
840 | ||
9b386eca RG |
841 | bool wxRegKey::SetValue(const wxChar *szValue,const wxMemoryBuffer& buffer) |
842 | { | |
843 | #ifdef __TWIN32__ | |
844 | wxFAIL_MSG("RegSetValueEx not implemented by TWIN32"); | |
4d08943e | 845 | return false; |
9b386eca RG |
846 | #else |
847 | if ( CONST_CAST Open() ) { | |
848 | m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_BINARY, | |
849 | (RegBinary)buffer.GetData(),buffer.GetDataLen()); | |
850 | if ( m_dwLastError == ERROR_SUCCESS ) | |
4d08943e | 851 | return true; |
9b386eca RG |
852 | } |
853 | ||
854 | wxLogSysError(m_dwLastError, _("Can't set value of '%s'"), | |
855 | GetFullName(this, szValue)); | |
4d08943e | 856 | return false; |
9b386eca RG |
857 | #endif |
858 | } | |
859 | ||
860 | bool wxRegKey::QueryValue(const wxChar *szValue, wxMemoryBuffer& buffer) const | |
861 | { | |
873aefb8 | 862 | if ( CONST_CAST Open(Read) ) { |
9b386eca RG |
863 | // first get the type and size of the data |
864 | DWORD dwType, dwSize; | |
865 | m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED, | |
866 | &dwType, NULL, &dwSize); | |
4d08943e | 867 | |
9b386eca RG |
868 | if ( m_dwLastError == ERROR_SUCCESS ) { |
869 | if ( dwSize ) { | |
870 | const RegBinary pBuf = (RegBinary)buffer.GetWriteBuf(dwSize); | |
871 | m_dwLastError = RegQueryValueEx((HKEY) m_hKey, | |
872 | WXSTRINGCAST szValue, | |
873 | RESERVED, | |
874 | &dwType, | |
875 | pBuf, | |
876 | &dwSize); | |
877 | buffer.UngetWriteBuf(dwSize); | |
4d08943e WS |
878 | } else { |
879 | buffer.SetDataLen(0); | |
9b386eca RG |
880 | } |
881 | } | |
882 | ||
4d08943e | 883 | |
9b386eca RG |
884 | if ( m_dwLastError != ERROR_SUCCESS ) { |
885 | wxLogSysError(m_dwLastError, _("Can't read value of key '%s'"), | |
886 | GetName().c_str()); | |
4d08943e | 887 | return false; |
9b386eca | 888 | } |
4d08943e | 889 | return true; |
9b386eca | 890 | } |
4d08943e | 891 | return false; |
9b386eca RG |
892 | } |
893 | ||
894 | ||
895 | ||
6dfec4b8 VZ |
896 | bool wxRegKey::QueryValue(const wxChar *szValue, |
897 | wxString& strValue, | |
0c0d1521 | 898 | bool WXUNUSED_IN_WINCE(raw)) const |
2bda0e17 | 899 | { |
0c0d1521 WS |
900 | if ( CONST_CAST Open(Read) ) |
901 | { | |
6dfec4b8 | 902 | |
0c0d1521 WS |
903 | // first get the type and size of the data |
904 | DWORD dwType, dwSize; | |
905 | m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED, | |
906 | &dwType, NULL, &dwSize); | |
907 | if ( m_dwLastError == ERROR_SUCCESS ) | |
908 | { | |
909 | if ( !dwSize ) | |
6dfec4b8 | 910 | { |
0c0d1521 WS |
911 | // must treat this case specially as GetWriteBuf() doesn't like |
912 | // being called with 0 size | |
913 | strValue.Empty(); | |
914 | } | |
915 | else | |
916 | { | |
917 | m_dwLastError = RegQueryValueEx((HKEY) m_hKey, | |
918 | WXSTRINGCAST szValue, | |
919 | RESERVED, | |
920 | &dwType, | |
921 | (RegString)(wxChar*)wxStringBuffer(strValue, dwSize), | |
922 | &dwSize); | |
923 | ||
924 | // expand the var expansions in the string unless disabled | |
925 | #ifndef __WXWINCE__ | |
926 | if ( (dwType == REG_EXPAND_SZ) && !raw ) | |
6dfec4b8 | 927 | { |
0c0d1521 WS |
928 | DWORD dwExpSize = ::ExpandEnvironmentStrings(strValue, NULL, 0); |
929 | bool ok = dwExpSize != 0; | |
930 | if ( ok ) | |
931 | { | |
932 | wxString strExpValue; | |
933 | ok = ::ExpandEnvironmentStrings(strValue, | |
934 | wxStringBuffer(strExpValue, dwExpSize), | |
935 | dwExpSize | |
936 | ) != 0; | |
937 | strValue = strExpValue; | |
938 | } | |
939 | ||
940 | if ( !ok ) | |
941 | { | |
942 | wxLogLastError(_T("ExpandEnvironmentStrings")); | |
943 | } | |
6dfec4b8 | 944 | } |
4676948b | 945 | #endif |
0c0d1521 WS |
946 | // __WXWINCE__ |
947 | } | |
23f681ec | 948 | |
0c0d1521 WS |
949 | if ( m_dwLastError == ERROR_SUCCESS ) |
950 | { | |
951 | // check that it was the right type | |
952 | wxASSERT_MSG( !IsNumericValue(szValue), | |
953 | wxT("Type mismatch in wxRegKey::QueryValue().") ); | |
2bda0e17 | 954 | |
0c0d1521 WS |
955 | return true; |
956 | } | |
2bda0e17 | 957 | } |
0c0d1521 | 958 | } |
2bda0e17 | 959 | |
0c0d1521 WS |
960 | wxLogSysError(m_dwLastError, _("Can't read value of '%s'"), |
961 | GetFullName(this, szValue)); | |
962 | return false; | |
2bda0e17 KB |
963 | } |
964 | ||
837e5743 | 965 | bool wxRegKey::SetValue(const wxChar *szValue, const wxString& strValue) |
2bda0e17 KB |
966 | { |
967 | if ( CONST_CAST Open() ) { | |
6b037754 | 968 | m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_SZ, |
23f681ec | 969 | (RegString)strValue.c_str(), |
f6bcfd97 | 970 | (strValue.Len() + 1)*sizeof(wxChar)); |
2bda0e17 | 971 | if ( m_dwLastError == ERROR_SUCCESS ) |
4d08943e | 972 | return true; |
2bda0e17 KB |
973 | } |
974 | ||
23f681ec | 975 | wxLogSysError(m_dwLastError, _("Can't set value of '%s'"), |
2bda0e17 | 976 | GetFullName(this, szValue)); |
4d08943e | 977 | return false; |
2bda0e17 KB |
978 | } |
979 | ||
50e42404 | 980 | wxString wxRegKey::QueryDefaultValue() const |
2bda0e17 KB |
981 | { |
982 | wxString str; | |
983 | QueryValue(NULL, str); | |
984 | return str; | |
985 | } | |
986 | ||
987 | // ---------------------------------------------------------------------------- | |
988 | // enumeration | |
989 | // NB: all these functions require an index variable which allows to have | |
990 | // several concurrently running indexations on the same key | |
991 | // ---------------------------------------------------------------------------- | |
992 | ||
2bda0e17 KB |
993 | bool wxRegKey::GetFirstValue(wxString& strValueName, long& lIndex) |
994 | { | |
d305f9df | 995 | if ( !Open(Read) ) |
4d08943e | 996 | return false; |
2bda0e17 KB |
997 | |
998 | lIndex = 0; | |
0b1c5a6c | 999 | return GetNextValue(strValueName, lIndex); |
2bda0e17 KB |
1000 | } |
1001 | ||
1002 | bool wxRegKey::GetNextValue(wxString& strValueName, long& lIndex) const | |
1003 | { | |
1004 | wxASSERT( IsOpened() ); | |
2bda0e17 | 1005 | |
0b1c5a6c VZ |
1006 | // are we already at the end of enumeration? |
1007 | if ( lIndex == -1 ) | |
4d08943e | 1008 | return false; |
2bda0e17 | 1009 | |
837e5743 | 1010 | wxChar szValueName[1024]; // @@ use RegQueryInfoKey... |
0b1c5a6c | 1011 | DWORD dwValueLen = WXSIZEOF(szValueName); |
2bda0e17 | 1012 | |
92049cd4 | 1013 | m_dwLastError = RegEnumValue((HKEY) m_hKey, lIndex++, |
0b1c5a6c | 1014 | szValueName, &dwValueLen, |
23f681ec VZ |
1015 | RESERVED, |
1016 | NULL, // [out] type | |
0b1c5a6c VZ |
1017 | NULL, // [out] buffer for value |
1018 | NULL); // [i/o] it's length | |
1019 | ||
1020 | if ( m_dwLastError != ERROR_SUCCESS ) { | |
1021 | if ( m_dwLastError == ERROR_NO_MORE_ITEMS ) { | |
1022 | m_dwLastError = ERROR_SUCCESS; | |
1023 | lIndex = -1; | |
1024 | } | |
1025 | else { | |
23f681ec | 1026 | wxLogSysError(m_dwLastError, _("Can't enumerate values of key '%s'"), |
0b1c5a6c VZ |
1027 | GetName().c_str()); |
1028 | } | |
1029 | ||
4d08943e | 1030 | return false; |
2bda0e17 KB |
1031 | } |
1032 | ||
0b1c5a6c | 1033 | strValueName = szValueName; |
0b1c5a6c | 1034 | |
4d08943e | 1035 | return true; |
2bda0e17 | 1036 | } |
2bda0e17 KB |
1037 | |
1038 | bool wxRegKey::GetFirstKey(wxString& strKeyName, long& lIndex) | |
1039 | { | |
d305f9df | 1040 | if ( !Open(Read) ) |
4d08943e | 1041 | return false; |
2bda0e17 | 1042 | |
2bda0e17 | 1043 | lIndex = 0; |
0b1c5a6c | 1044 | return GetNextKey(strKeyName, lIndex); |
2bda0e17 KB |
1045 | } |
1046 | ||
1047 | bool wxRegKey::GetNextKey(wxString& strKeyName, long& lIndex) const | |
1048 | { | |
1049 | wxASSERT( IsOpened() ); | |
0b1c5a6c VZ |
1050 | |
1051 | // are we already at the end of enumeration? | |
1052 | if ( lIndex == -1 ) | |
4d08943e | 1053 | return false; |
2bda0e17 | 1054 | |
837e5743 | 1055 | wxChar szKeyName[_MAX_PATH + 1]; |
4676948b JS |
1056 | |
1057 | #ifdef __WXWINCE__ | |
1058 | DWORD sizeName = WXSIZEOF(szKeyName); | |
1059 | m_dwLastError = RegEnumKeyEx((HKEY) m_hKey, lIndex++, szKeyName, & sizeName, | |
1060 | 0, NULL, NULL, NULL); | |
1061 | #else | |
fd6c844b | 1062 | m_dwLastError = RegEnumKey((HKEY) m_hKey, lIndex++, szKeyName, WXSIZEOF(szKeyName)); |
4676948b | 1063 | #endif |
2bda0e17 KB |
1064 | |
1065 | if ( m_dwLastError != ERROR_SUCCESS ) { | |
1066 | if ( m_dwLastError == ERROR_NO_MORE_ITEMS ) { | |
1067 | m_dwLastError = ERROR_SUCCESS; | |
1068 | lIndex = -1; | |
1069 | } | |
1070 | else { | |
23f681ec | 1071 | wxLogSysError(m_dwLastError, _("Can't enumerate subkeys of key '%s'"), |
2bda0e17 KB |
1072 | GetName().c_str()); |
1073 | } | |
1074 | ||
4d08943e | 1075 | return false; |
2bda0e17 KB |
1076 | } |
1077 | ||
1078 | strKeyName = szKeyName; | |
4d08943e | 1079 | return true; |
2bda0e17 KB |
1080 | } |
1081 | ||
4d08943e | 1082 | // returns true if the value contains a number (else it's some string) |
837e5743 | 1083 | bool wxRegKey::IsNumericValue(const wxChar *szValue) const |
4d08943e WS |
1084 | { |
1085 | ValueType type = GetValueType(szValue); | |
1086 | switch ( type ) { | |
03ab016d | 1087 | case Type_Dword: |
23f681ec | 1088 | /* case Type_Dword_little_endian: == Type_Dword */ |
03ab016d | 1089 | case Type_Dword_big_endian: |
4d08943e | 1090 | return true; |
03ab016d JS |
1091 | |
1092 | default: | |
4d08943e WS |
1093 | return false; |
1094 | } | |
1095 | } | |
03ab016d | 1096 | |
20d8c319 VZ |
1097 | // ---------------------------------------------------------------------------- |
1098 | // exporting registry keys to file | |
1099 | // ---------------------------------------------------------------------------- | |
1100 | ||
532d575b WS |
1101 | #if wxUSE_STREAMS |
1102 | ||
20d8c319 VZ |
1103 | // helper functions for writing ASCII strings (even in Unicode build) |
1104 | static inline bool WriteAsciiChar(wxOutputStream& ostr, char ch) | |
1105 | { | |
1106 | ostr.PutC(ch); | |
1107 | return ostr.IsOk(); | |
1108 | } | |
1109 | ||
1110 | static inline bool WriteAsciiEOL(wxOutputStream& ostr) | |
1111 | { | |
1112 | // as we open the file in text mode, it is enough to write LF without CR | |
1113 | return WriteAsciiChar(ostr, '\n'); | |
1114 | } | |
1115 | ||
1116 | static inline bool WriteAsciiString(wxOutputStream& ostr, const char *p) | |
1117 | { | |
1118 | return ostr.Write(p, strlen(p)).IsOk(); | |
1119 | } | |
1120 | ||
1121 | static inline bool WriteAsciiString(wxOutputStream& ostr, const wxString& s) | |
1122 | { | |
1123 | #if wxUSE_UNICODE | |
1124 | wxCharBuffer name(s.mb_str()); | |
1125 | ostr.Write(name, strlen(name)); | |
1126 | #else | |
1127 | ostr.Write(s, s.length()); | |
1128 | #endif | |
1129 | ||
1130 | return ostr.IsOk(); | |
1131 | } | |
1132 | ||
532d575b WS |
1133 | #endif // wxUSE_STREAMS |
1134 | ||
20d8c319 VZ |
1135 | bool wxRegKey::Export(const wxString& filename) const |
1136 | { | |
532d575b | 1137 | #if wxUSE_FFILE && wxUSE_STREAMS |
20d8c319 VZ |
1138 | if ( wxFile::Exists(filename) ) |
1139 | { | |
1140 | wxLogError(_("Exporting registry key: file \"%s\" already exists and won't be overwritten."), | |
1141 | filename.c_str()); | |
1142 | return false; | |
1143 | } | |
1144 | ||
1145 | wxFFileOutputStream ostr(filename, _T("w")); | |
1146 | ||
1147 | return ostr.Ok() && Export(ostr); | |
532d575b WS |
1148 | #else |
1149 | wxUnusedVar(filename); | |
1150 | return false; | |
1151 | #endif | |
20d8c319 VZ |
1152 | } |
1153 | ||
532d575b | 1154 | #if wxUSE_STREAMS |
20d8c319 VZ |
1155 | bool wxRegKey::Export(wxOutputStream& ostr) const |
1156 | { | |
1157 | // write out the header | |
1158 | if ( !WriteAsciiString(ostr, "REGEDIT4\n\n") ) | |
1159 | return false; | |
1160 | ||
1161 | return DoExport(ostr); | |
1162 | } | |
532d575b | 1163 | #endif // wxUSE_STREAMS |
20d8c319 VZ |
1164 | |
1165 | static | |
1166 | wxString | |
1167 | FormatAsHex(const void *data, | |
1168 | size_t size, | |
1169 | wxRegKey::ValueType type = wxRegKey::Type_Binary) | |
1170 | { | |
1171 | wxString value(_T("hex")); | |
1172 | ||
1173 | // binary values use just "hex:" prefix while the other ones must indicate | |
1174 | // the real type | |
1175 | if ( type != wxRegKey::Type_Binary ) | |
1176 | value << _T('(') << type << _T(')'); | |
1177 | value << _T(':'); | |
1178 | ||
1179 | // write all the rest as comma-separated bytes | |
1180 | value.reserve(3*size + 10); | |
1181 | const char * const p = wx_static_cast(const char *, data); | |
1182 | for ( size_t n = 0; n < size; n++ ) | |
1183 | { | |
1184 | // TODO: line wrapping: although not required by regedit, this makes | |
1185 | // the generated files easier to read and compare with the files | |
1186 | // produced by regedit | |
1187 | if ( n ) | |
1188 | value << _T(','); | |
1189 | ||
f1a9125b | 1190 | value << wxString::Format(_T("%02x"), (unsigned char)p[n]); |
20d8c319 VZ |
1191 | } |
1192 | ||
1193 | return value; | |
1194 | } | |
1195 | ||
1196 | static inline | |
1197 | wxString FormatAsHex(const wxString& value, wxRegKey::ValueType type) | |
1198 | { | |
1199 | return FormatAsHex(value.c_str(), value.length() + 1, type); | |
1200 | } | |
1201 | ||
1202 | wxString wxRegKey::FormatValue(const wxString& name) const | |
1203 | { | |
1204 | wxString rhs; | |
1205 | const ValueType type = GetValueType(name); | |
1206 | switch ( type ) | |
1207 | { | |
1208 | case Type_String: | |
1209 | { | |
1210 | wxString value; | |
1211 | if ( !QueryValue(name, value) ) | |
1212 | break; | |
1213 | ||
1214 | // quotes and backslashes must be quoted, linefeeds are not | |
1215 | // allowed in string values | |
1216 | rhs.reserve(value.length() + 2); | |
1217 | rhs = _T('"'); | |
1218 | ||
1219 | // there can be no NULs here | |
1220 | bool useHex = false; | |
1221 | for ( const wxChar *p = value.c_str(); *p && !useHex; p++ ) | |
1222 | { | |
1223 | switch ( *p ) | |
1224 | { | |
1225 | case _T('\n'): | |
1226 | // we can only represent this string in hex | |
1227 | useHex = true; | |
1228 | break; | |
1229 | ||
1230 | case _T('"'): | |
1231 | case _T('\\'): | |
1232 | // escape special symbol | |
1233 | rhs += _T('\\'); | |
1234 | // fall through | |
1235 | ||
1236 | default: | |
1237 | rhs += *p; | |
1238 | } | |
1239 | } | |
1240 | ||
1241 | if ( useHex ) | |
1242 | rhs = FormatAsHex(value, Type_String); | |
1243 | else | |
1244 | rhs += _T('"'); | |
1245 | } | |
1246 | break; | |
1247 | ||
1248 | case Type_Dword: | |
1249 | /* case Type_Dword_little_endian: == Type_Dword */ | |
1250 | { | |
1251 | long value; | |
1252 | if ( !QueryValue(name, &value) ) | |
1253 | break; | |
1254 | ||
04a18b0d | 1255 | rhs.Printf(_T("dword:%08x"), (unsigned int)value); |
20d8c319 VZ |
1256 | } |
1257 | break; | |
1258 | ||
1259 | case Type_Expand_String: | |
1260 | case Type_Multi_String: | |
1261 | { | |
1262 | wxString value; | |
1263 | if ( !QueryRawValue(name, value) ) | |
1264 | break; | |
1265 | ||
1266 | rhs = FormatAsHex(value, type); | |
1267 | } | |
1268 | break; | |
1269 | ||
1270 | case Type_Binary: | |
1271 | { | |
1272 | wxMemoryBuffer buf; | |
1273 | if ( !QueryValue(name, buf) ) | |
1274 | break; | |
1275 | ||
1276 | rhs = FormatAsHex(buf.GetData(), buf.GetDataLen()); | |
1277 | } | |
1278 | break; | |
1279 | ||
1280 | // no idea how those appear in REGEDIT4 files | |
1281 | case Type_None: | |
1282 | case Type_Dword_big_endian: | |
1283 | case Type_Link: | |
1284 | case Type_Resource_list: | |
1285 | case Type_Full_resource_descriptor: | |
1286 | case Type_Resource_requirements_list: | |
1287 | default: | |
1288 | wxLogWarning(_("Can't export value of unsupported type %d."), type); | |
1289 | } | |
1290 | ||
1291 | return rhs; | |
1292 | } | |
1293 | ||
532d575b WS |
1294 | #if wxUSE_STREAMS |
1295 | ||
20d8c319 VZ |
1296 | bool wxRegKey::DoExportValue(wxOutputStream& ostr, const wxString& name) const |
1297 | { | |
1298 | // first examine the value type: if it's unsupported, simply skip it | |
1299 | // instead of aborting the entire export process because we failed to | |
1300 | // export a single value | |
1301 | wxString value = FormatValue(name); | |
1302 | if ( value.empty() ) | |
1303 | { | |
1304 | wxLogWarning(_("Ignoring value \"%s\" of the key \"%s\"."), | |
1305 | name.c_str(), GetName().c_str()); | |
1306 | return true; | |
1307 | } | |
1308 | ||
1309 | // we do have the text representation of the value, now write everything | |
1310 | // out | |
1311 | ||
1312 | // special case: unnamed/default value is represented as just "@" | |
1313 | if ( name.empty() ) | |
1314 | { | |
1315 | if ( !WriteAsciiChar(ostr, '@') ) | |
1316 | return false; | |
1317 | } | |
1318 | else // normal, named, value | |
1319 | { | |
1320 | if ( !WriteAsciiChar(ostr, '"') || | |
1321 | !WriteAsciiString(ostr, name) || | |
1322 | !WriteAsciiChar(ostr, '"') ) | |
1323 | return false; | |
1324 | } | |
1325 | ||
1326 | if ( !WriteAsciiChar(ostr, '=') ) | |
1327 | return false; | |
1328 | ||
1329 | return WriteAsciiString(ostr, value) && WriteAsciiEOL(ostr); | |
1330 | } | |
1331 | ||
1332 | bool wxRegKey::DoExport(wxOutputStream& ostr) const | |
1333 | { | |
1334 | // write out this key name | |
1335 | if ( !WriteAsciiChar(ostr, '[') ) | |
1336 | return false; | |
1337 | ||
1338 | if ( !WriteAsciiString(ostr, GetName(false /* no short prefix */)) ) | |
1339 | return false; | |
1340 | ||
1341 | if ( !WriteAsciiChar(ostr, ']') || !WriteAsciiEOL(ostr) ) | |
1342 | return false; | |
1343 | ||
1344 | // dump all our values | |
1345 | long dummy; | |
1346 | wxString name; | |
1347 | wxRegKey& self = wx_const_cast(wxRegKey&, *this); | |
1348 | bool cont = self.GetFirstValue(name, dummy); | |
1349 | while ( cont ) | |
1350 | { | |
1351 | if ( !DoExportValue(ostr, name) ) | |
1352 | return false; | |
1353 | ||
1354 | cont = GetNextValue(name, dummy); | |
1355 | } | |
1356 | ||
1357 | // always terminate values by blank line, even if there were no values | |
1358 | if ( !WriteAsciiEOL(ostr) ) | |
1359 | return false; | |
1360 | ||
1361 | // recurse to subkeys | |
1362 | cont = self.GetFirstKey(name, dummy); | |
1363 | while ( cont ) | |
1364 | { | |
1365 | wxRegKey subkey(*this, name); | |
1366 | if ( !subkey.DoExport(ostr) ) | |
1367 | return false; | |
1368 | ||
1369 | cont = GetNextKey(name, dummy); | |
1370 | } | |
1371 | ||
1372 | return true; | |
1373 | } | |
1374 | ||
532d575b WS |
1375 | #endif // wxUSE_STREAMS |
1376 | ||
2bda0e17 | 1377 | // ============================================================================ |
1880e452 | 1378 | // implementation of global private functions |
2bda0e17 | 1379 | // ============================================================================ |
f6bcfd97 | 1380 | |
837e5743 | 1381 | bool KeyExists(WXHKEY hRootKey, const wxChar *szKey) |
2bda0e17 | 1382 | { |
8a8c41dd VZ |
1383 | // don't close this key itself for the case of empty szKey! |
1384 | if ( wxIsEmpty(szKey) ) | |
4d08943e | 1385 | return true; |
8a8c41dd VZ |
1386 | |
1387 | HKEY hkeyDummy; | |
1388 | if ( ::RegOpenKeyEx | |
1389 | ( | |
1390 | (HKEY)hRootKey, | |
1391 | szKey, | |
1392 | RESERVED, | |
bee96abf | 1393 | KEY_READ, // we might not have enough rights for rw access |
8a8c41dd VZ |
1394 | &hkeyDummy |
1395 | ) == ERROR_SUCCESS ) | |
1396 | { | |
1397 | ::RegCloseKey(hkeyDummy); | |
1398 | ||
4d08943e | 1399 | return true; |
8a8c41dd | 1400 | } |
f6bcfd97 | 1401 | |
4d08943e | 1402 | return false; |
2bda0e17 KB |
1403 | } |
1404 | ||
837e5743 | 1405 | const wxChar *GetFullName(const wxRegKey *pKey, const wxChar *szValue) |
2bda0e17 KB |
1406 | { |
1407 | static wxString s_str; | |
1408 | s_str = pKey->GetName(); | |
837e5743 | 1409 | if ( !wxIsEmpty(szValue) ) |
223d09f6 | 1410 | s_str << wxT("\\") << szValue; |
2bda0e17 KB |
1411 | |
1412 | return s_str.c_str(); | |
0b1c5a6c VZ |
1413 | } |
1414 | ||
1415 | void RemoveTrailingSeparator(wxString& str) | |
1416 | { | |
532d575b | 1417 | if ( !str.empty() && str.Last() == REG_SEPARATOR ) |
0b1c5a6c VZ |
1418 | str.Truncate(str.Len() - 1); |
1419 | } | |
3d05544e | 1420 |