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