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