1 /*
2 * Registry management
3 *
4 * Copyright (C) 1999 Alexandre Julliard
5 *
6 * Based on misc/registry.c code
7 * Copyright (C) 1996 Marcus Meissner
8 * Copyright (C) 1998 Matthew Becker
9 * Copyright (C) 1999 Sylvain St-Germain
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winreg.h"
35 #include "winerror.h"
36 #include "winternl.h"
37 #include "winuser.h"
38
39 #include "wine/unicode.h"
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(reg);
43
44 #define HKEY_SPECIAL_ROOT_FIRST HKEY_CLASSES_ROOT
45 #define HKEY_SPECIAL_ROOT_LAST HKEY_DYN_DATA
46 #define NB_SPECIAL_ROOT_KEYS ((UINT)HKEY_SPECIAL_ROOT_LAST - (UINT)HKEY_SPECIAL_ROOT_FIRST + 1)
47
48 static HKEY special_root_keys[NB_SPECIAL_ROOT_KEYS];
49 static BOOL hkcu_cache_disabled;
50
51 static const WCHAR name_CLASSES_ROOT[] =
52 {'M','a','c','h','i','n','e','\\',
53 'S','o','f','t','w','a','r','e','\\',
54 'C','l','a','s','s','e','s',0};
55 static const WCHAR name_LOCAL_MACHINE[] =
56 {'M','a','c','h','i','n','e',0};
57 static const WCHAR name_USERS[] =
58 {'U','s','e','r',0};
59 static const WCHAR name_PERFORMANCE_DATA[] =
60 {'P','e','r','f','D','a','t','a',0};
61 static const WCHAR name_CURRENT_CONFIG[] =
62 {'M','a','c','h','i','n','e','\\',
63 'S','y','s','t','e','m','\\',
64 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
65 'H','a','r','d','w','a','r','e',' ','P','r','o','f','i','l','e','s','\\',
66 'C','u','r','r','e','n','t',0};
67 static const WCHAR name_DYN_DATA[] =
68 {'D','y','n','D','a','t','a',0};
69
70 static const WCHAR * const root_key_names[NB_SPECIAL_ROOT_KEYS] =
71 {
72 name_CLASSES_ROOT,
73 NULL, /* HKEY_CURRENT_USER is determined dynamically */
74 name_LOCAL_MACHINE,
75 name_USERS,
76 name_PERFORMANCE_DATA,
77 name_CURRENT_CONFIG,
78 name_DYN_DATA
79 };
80
81
82 /* check if value type needs string conversion (Ansi<->Unicode) */
83 static inline int is_string( DWORD type )
84 {
85 return (type == REG_SZ) || (type == REG_EXPAND_SZ) || (type == REG_MULTI_SZ);
86 }
87
88 /* check if current version is NT or Win95 */
89 static inline int is_version_nt(void)
90 {
91 return !(GetVersion() & 0x80000000);
92 }
93
94 /* create one of the HKEY_* special root keys */
95 static HKEY create_special_root_hkey( HANDLE hkey, DWORD access )
96 {
97 HKEY ret = 0;
98 int idx = (UINT_PTR)hkey - (UINT_PTR)HKEY_SPECIAL_ROOT_FIRST;
99
100 if (hkey == HKEY_CURRENT_USER)
101 {
102 if (RtlOpenCurrentUser( access, &hkey )) return 0;
103 TRACE( "HKEY_CURRENT_USER -> %p\n", hkey );
104
105 /* don't cache the key in the table if caching is disabled */
106 if (hkcu_cache_disabled)
107 return hkey;
108 }
109 else
110 {
111 OBJECT_ATTRIBUTES attr;
112 UNICODE_STRING name;
113
114 attr.Length = sizeof(attr);
115 attr.RootDirectory = 0;
116 attr.ObjectName = &name;
117 attr.Attributes = 0;
118 attr.SecurityDescriptor = NULL;
119 attr.SecurityQualityOfService = NULL;
120 RtlInitUnicodeString( &name, root_key_names[idx] );
121 if (NtCreateKey( &hkey, access, &attr, 0, NULL, 0, NULL )) return 0;
122 TRACE( "%s -> %p\n", debugstr_w(attr.ObjectName->Buffer), hkey );
123 }
124
125 if (!(ret = InterlockedCompareExchangePointer( (void **)&special_root_keys[idx], hkey, 0 )))
126 ret = hkey;
127 else
128 NtClose( hkey ); /* somebody beat us to it */
129 return ret;
130 }
131
132 /* map the hkey from special root to normal key if necessary */
133 static inline HKEY get_special_root_hkey( HKEY hkey )
134 {
135 HKEY ret = hkey;
136
137 if ((hkey >= HKEY_SPECIAL_ROOT_FIRST) && (hkey <= HKEY_SPECIAL_ROOT_LAST))
138 {
139 if (!(ret = special_root_keys[(UINT_PTR)hkey - (UINT_PTR)HKEY_SPECIAL_ROOT_FIRST]))
140 ret = create_special_root_hkey( hkey, KEY_ALL_ACCESS );
141 }
142 return ret;
143 }
144
145
146 /******************************************************************************
147 * RegOverridePredefKey [ADVAPI32.@]
148 */
149 LSTATUS WINAPI RegOverridePredefKey( HKEY hkey, HKEY override )
150 {
151 HKEY old_key;
152 int idx;
153
154 if ((hkey < HKEY_SPECIAL_ROOT_FIRST) || (hkey > HKEY_SPECIAL_ROOT_LAST))
155 return ERROR_INVALID_PARAMETER;
156 idx = (UINT_PTR)hkey - (UINT_PTR)HKEY_SPECIAL_ROOT_FIRST;
157
158 if (override)
159 {
160 NTSTATUS status = NtDuplicateObject( GetCurrentProcess(), override,
161 GetCurrentProcess(), (HANDLE *)&override,
162 0, 0, DUPLICATE_SAME_ACCESS );
163 if (status) return RtlNtStatusToDosError( status );
164 }
165
166 old_key = InterlockedExchangePointer( (void **)&special_root_keys[idx], override );
167 if (old_key) NtClose( old_key );
168 return ERROR_SUCCESS;
169 }
170
171
172 /******************************************************************************
173 * RegCreateKeyExW [ADVAPI32.@]
174 *
175 * See RegCreateKeyExA.
176 */
177 LSTATUS WINAPI RegCreateKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, LPWSTR class,
178 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
179 PHKEY retkey, LPDWORD dispos )
180 {
181 OBJECT_ATTRIBUTES attr;
182 UNICODE_STRING nameW, classW;
183
184 if (reserved) return ERROR_INVALID_PARAMETER;
185 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
186
187 attr.Length = sizeof(attr);
188 attr.RootDirectory = hkey;
189 attr.ObjectName = &nameW;
190 attr.Attributes = 0;
191 attr.SecurityDescriptor = NULL;
192 attr.SecurityQualityOfService = NULL;
193 RtlInitUnicodeString( &nameW, name );
194 RtlInitUnicodeString( &classW, class );
195
196 return RtlNtStatusToDosError( NtCreateKey( (PHANDLE)retkey, access, &attr, 0,
197 &classW, options, dispos ) );
198 }
199
200
201 /******************************************************************************
202 * RegCreateKeyExA [ADVAPI32.@]
203 *
204 * Open a registry key, creating it if it doesn't exist.
205 *
206 * PARAMS
207 * hkey [I] Handle of the parent registry key
208 * name [I] Name of the new key to open or create
209 * reserved [I] Reserved, pass 0
210 * class [I] The object type of the new key
211 * options [I] Flags controlling the key creation (REG_OPTION_* flags from "winnt.h")
212 * access [I] Access level desired
213 * sa [I] Security attributes for the key
214 * retkey [O] Destination for the resulting handle
215 * dispos [O] Receives REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY
216 *
217 * RETURNS
218 * Success: ERROR_SUCCESS.
219 * Failure: A standard Win32 error code. retkey remains untouched.
220 *
221 * FIXME
222 * MAXIMUM_ALLOWED in access mask not supported by server
223 */
224 LSTATUS WINAPI RegCreateKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, LPSTR class,
225 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
226 PHKEY retkey, LPDWORD dispos )
227 {
228 OBJECT_ATTRIBUTES attr;
229 UNICODE_STRING classW;
230 ANSI_STRING nameA, classA;
231 NTSTATUS status;
232
233 if (reserved) return ERROR_INVALID_PARAMETER;
234 if (!is_version_nt())
235 {
236 access = KEY_ALL_ACCESS; /* Win95 ignores the access mask */
237 if (name && *name == '\\') name++; /* win9x,ME ignores one (and only one) beginning backslash */
238 }
239 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
240
241 attr.Length = sizeof(attr);
242 attr.RootDirectory = hkey;
243 attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
244 attr.Attributes = 0;
245 attr.SecurityDescriptor = NULL;
246 attr.SecurityQualityOfService = NULL;
247 RtlInitAnsiString( &nameA, name );
248 RtlInitAnsiString( &classA, class );
249
250 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
251 &nameA, FALSE )))
252 {
253 if (!(status = RtlAnsiStringToUnicodeString( &classW, &classA, TRUE )))
254 {
255 status = NtCreateKey( (PHANDLE)retkey, access, &attr, 0, &classW, options, dispos );
256 RtlFreeUnicodeString( &classW );
257 }
258 }
259 return RtlNtStatusToDosError( status );
260 }
261
262
263 /******************************************************************************
264 * RegCreateKeyW [ADVAPI32.@]
265 *
266 * Creates the specified reg key.
267 *
268 * PARAMS
269 * hKey [I] Handle to an open key.
270 * lpSubKey [I] Name of a key that will be opened or created.
271 * phkResult [O] Receives a handle to the opened or created key.
272 *
273 * RETURNS
274 * Success: ERROR_SUCCESS
275 * Failure: nonzero error code defined in Winerror.h
276 */
277 LSTATUS WINAPI RegCreateKeyW( HKEY hkey, LPCWSTR lpSubKey, PHKEY phkResult )
278 {
279 /* FIXME: previous implementation converted ERROR_INVALID_HANDLE to ERROR_BADKEY, */
280 /* but at least my version of NT (4.0 SP5) doesn't do this. -- AJ */
281 return RegCreateKeyExW( hkey, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE,
282 KEY_ALL_ACCESS, NULL, phkResult, NULL );
283 }
284
285
286 /******************************************************************************
287 * RegCreateKeyA [ADVAPI32.@]
288 *
289 * See RegCreateKeyW.
290 */
291 LSTATUS WINAPI RegCreateKeyA( HKEY hkey, LPCSTR lpSubKey, PHKEY phkResult )
292 {
293 return RegCreateKeyExA( hkey, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE,
294 KEY_ALL_ACCESS, NULL, phkResult, NULL );
295 }
296
297
298
299 /******************************************************************************
300 * RegOpenKeyExW [ADVAPI32.@]
301 *
302 * See RegOpenKeyExA.
303 */
304 LSTATUS WINAPI RegOpenKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, REGSAM access, PHKEY retkey )
305 {
306 OBJECT_ATTRIBUTES attr;
307 UNICODE_STRING nameW;
308
309 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
310
311 attr.Length = sizeof(attr);
312 attr.RootDirectory = hkey;
313 attr.ObjectName = &nameW;
314 attr.Attributes = 0;
315 attr.SecurityDescriptor = NULL;
316 attr.SecurityQualityOfService = NULL;
317 RtlInitUnicodeString( &nameW, name );
318 return RtlNtStatusToDosError( NtOpenKey( (PHANDLE)retkey, access, &attr ) );
319 }
320
321
322 /******************************************************************************
323 * RegOpenKeyExA [ADVAPI32.@]
324 *
325 * Open a registry key.
326 *
327 * PARAMS
328 * hkey [I] Handle of open key
329 * name [I] Name of subkey to open
330 * reserved [I] Reserved - must be zero
331 * access [I] Security access mask
332 * retkey [O] Handle to open key
333 *
334 * RETURNS
335 * Success: ERROR_SUCCESS
336 * Failure: A standard Win32 error code. retkey is set to 0.
337 *
338 * NOTES
339 * Unlike RegCreateKeyExA(), this function will not create the key if it
340 * does not exist.
341 */
342 LSTATUS WINAPI RegOpenKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, REGSAM access, PHKEY retkey )
343 {
344 OBJECT_ATTRIBUTES attr;
345 STRING nameA;
346 NTSTATUS status;
347
348 if (!is_version_nt()) access = KEY_ALL_ACCESS; /* Win95 ignores the access mask */
349
350 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
351
352 attr.Length = sizeof(attr);
353 attr.RootDirectory = hkey;
354 attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
355 attr.Attributes = 0;
356 attr.SecurityDescriptor = NULL;
357 attr.SecurityQualityOfService = NULL;
358
359 RtlInitAnsiString( &nameA, name );
360 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
361 &nameA, FALSE )))
362 {
363 status = NtOpenKey( (PHANDLE)retkey, access, &attr );
364 }
365 return RtlNtStatusToDosError( status );
366 }
367
368
369 /******************************************************************************
370 * RegOpenKeyW [ADVAPI32.@]
371 *
372 * See RegOpenKeyA.
373 */
374 LSTATUS WINAPI RegOpenKeyW( HKEY hkey, LPCWSTR name, PHKEY retkey )
375 {
376 if (!name || !*name)
377 {
378 *retkey = hkey;
379 return ERROR_SUCCESS;
380 }
381 return RegOpenKeyExW( hkey, name, 0, KEY_ALL_ACCESS, retkey );
382 }
383
384
385 /******************************************************************************
386 * RegOpenKeyA [ADVAPI32.@]
387 *
388 * Open a registry key.
389 *
390 * PARAMS
391 * hkey [I] Handle of parent key to open the new key under
392 * name [I] Name of the key under hkey to open
393 * retkey [O] Destination for the resulting Handle
394 *
395 * RETURNS
396 * Success: ERROR_SUCCESS
397 * Failure: A standard Win32 error code. retkey is set to 0.
398 */
399 LSTATUS WINAPI RegOpenKeyA( HKEY hkey, LPCSTR name, PHKEY retkey )
400 {
401 if (!name || !*name)
402 {
403 *retkey = hkey;
404 return ERROR_SUCCESS;
405 }
406 return RegOpenKeyExA( hkey, name, 0, KEY_ALL_ACCESS, retkey );
407 }
408
409
410 /******************************************************************************
411 * RegOpenCurrentUser [ADVAPI32.@]
412 *
413 * Get a handle to the HKEY_CURRENT_USER key for the user
414 * the current thread is impersonating.
415 *
416 * PARAMS
417 * access [I] Desired access rights to the key
418 * retkey [O] Handle to the opened key
419 *
420 * RETURNS
421 * Success: ERROR_SUCCESS
422 * Failure: nonzero error code from Winerror.h
423 *
424 * FIXME
425 * This function is supposed to retrieve a handle to the
426 * HKEY_CURRENT_USER for the user the current thread is impersonating.
427 * Since Wine does not currently allow threads to impersonate other users,
428 * this stub should work fine.
429 */
430 LSTATUS WINAPI RegOpenCurrentUser( REGSAM access, PHKEY retkey )
431 {
432 return RegOpenKeyExA( HKEY_CURRENT_USER, "", 0, access, retkey );
433 }
434
435
436
437 /******************************************************************************
438 * RegEnumKeyExW [ADVAPI32.@]
439 *
440 * Enumerate subkeys of the specified open registry key.
441 *
442 * PARAMS
443 * hkey [I] Handle to key to enumerate
444 * index [I] Index of subkey to enumerate
445 * name [O] Buffer for subkey name
446 * name_len [O] Size of subkey buffer
447 * reserved [I] Reserved
448 * class [O] Buffer for class string
449 * class_len [O] Size of class buffer
450 * ft [O] Time key last written to
451 *
452 * RETURNS
453 * Success: ERROR_SUCCESS
454 * Failure: System error code. If there are no more subkeys available, the
455 * function returns ERROR_NO_MORE_ITEMS.
456 */
457 LSTATUS WINAPI RegEnumKeyExW( HKEY hkey, DWORD index, LPWSTR name, LPDWORD name_len,
458 LPDWORD reserved, LPWSTR class, LPDWORD class_len, FILETIME *ft )
459 {
460 NTSTATUS status;
461 char buffer[256], *buf_ptr = buffer;
462 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
463 DWORD total_size;
464
465 TRACE( "(%p,%d,%p,%p(%u),%p,%p,%p,%p)\n", hkey, index, name, name_len,
466 name_len ? *name_len : 0, reserved, class, class_len, ft );
467
468 if (reserved) return ERROR_INVALID_PARAMETER;
469 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
470
471 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
472 buffer, sizeof(buffer), &total_size );
473
474 while (status == STATUS_BUFFER_OVERFLOW)
475 {
476 /* retry with a dynamically allocated buffer */
477 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
478 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
479 return ERROR_NOT_ENOUGH_MEMORY;
480 info = (KEY_NODE_INFORMATION *)buf_ptr;
481 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
482 buf_ptr, total_size, &total_size );
483 }
484
485 if (!status)
486 {
487 DWORD len = info->NameLength / sizeof(WCHAR);
488 DWORD cls_len = info->ClassLength / sizeof(WCHAR);
489
490 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
491
492 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
493 status = STATUS_BUFFER_OVERFLOW;
494 else
495 {
496 *name_len = len;
497 memcpy( name, info->Name, info->NameLength );
498 name[len] = 0;
499 if (class_len)
500 {
501 *class_len = cls_len;
502 if (class)
503 {
504 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
505 class[cls_len] = 0;
506 }
507 }
508 }
509 }
510
511 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
512 return RtlNtStatusToDosError( status );
513 }
514
515
516 /******************************************************************************
517 * RegEnumKeyExA [ADVAPI32.@]
518 *
519 * See RegEnumKeyExW.
520 */
521 LSTATUS WINAPI RegEnumKeyExA( HKEY hkey, DWORD index, LPSTR name, LPDWORD name_len,
522 LPDWORD reserved, LPSTR class, LPDWORD class_len, FILETIME *ft )
523 {
524 NTSTATUS status;
525 char buffer[256], *buf_ptr = buffer;
526 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
527 DWORD total_size;
528
529 TRACE( "(%p,%d,%p,%p(%u),%p,%p,%p,%p)\n", hkey, index, name, name_len,
530 name_len ? *name_len : 0, reserved, class, class_len, ft );
531
532 if (reserved) return ERROR_INVALID_PARAMETER;
533 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
534
535 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
536 buffer, sizeof(buffer), &total_size );
537
538 while (status == STATUS_BUFFER_OVERFLOW)
539 {
540 /* retry with a dynamically allocated buffer */
541 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
542 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
543 return ERROR_NOT_ENOUGH_MEMORY;
544 info = (KEY_NODE_INFORMATION *)buf_ptr;
545 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
546 buf_ptr, total_size, &total_size );
547 }
548
549 if (!status)
550 {
551 DWORD len, cls_len;
552
553 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
554 RtlUnicodeToMultiByteSize( &cls_len, (WCHAR *)(buf_ptr + info->ClassOffset),
555 info->ClassLength );
556 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
557
558 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
559 status = STATUS_BUFFER_OVERFLOW;
560 else
561 {
562 *name_len = len;
563 RtlUnicodeToMultiByteN( name, len, NULL, info->Name, info->NameLength );
564 name[len] = 0;
565 if (class_len)
566 {
567 *class_len = cls_len;
568 if (class)
569 {
570 RtlUnicodeToMultiByteN( class, cls_len, NULL,
571 (WCHAR *)(buf_ptr + info->ClassOffset),
572 info->ClassLength );
573 class[cls_len] = 0;
574 }
575 }
576 }
577 }
578
579 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
580 return RtlNtStatusToDosError( status );
581 }
582
583
584 /******************************************************************************
585 * RegEnumKeyW [ADVAPI32.@]
586 *
587 * Enumerates subkeys of the specified open reg key.
588 *
589 * PARAMS
590 * hKey [I] Handle to an open key.
591 * dwIndex [I] Index of the subkey of hKey to retrieve.
592 * lpName [O] Name of the subkey.
593 * cchName [I] Size of lpName in TCHARS.
594 *
595 * RETURNS
596 * Success: ERROR_SUCCESS
597 * Failure: system error code. If there are no more subkeys available, the
598 * function returns ERROR_NO_MORE_ITEMS.
599 */
600 LSTATUS WINAPI RegEnumKeyW( HKEY hkey, DWORD index, LPWSTR name, DWORD name_len )
601 {
602 return RegEnumKeyExW( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
603 }
604
605
606 /******************************************************************************
607 * RegEnumKeyA [ADVAPI32.@]
608 *
609 * See RegEnumKeyW.
610 */
611 LSTATUS WINAPI RegEnumKeyA( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
612 {
613 return RegEnumKeyExA( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
614 }
615
616
617 /******************************************************************************
618 * RegQueryInfoKeyW [ADVAPI32.@]
619 *
620 * Retrieves information about the specified registry key.
621 *
622 * PARAMS
623 * hkey [I] Handle to key to query
624 * class [O] Buffer for class string
625 * class_len [O] Size of class string buffer
626 * reserved [I] Reserved
627 * subkeys [O] Buffer for number of subkeys
628 * max_subkey [O] Buffer for longest subkey name length
629 * max_class [O] Buffer for longest class string length
630 * values [O] Buffer for number of value entries
631 * max_value [O] Buffer for longest value name length
632 * max_data [O] Buffer for longest value data length
633 * security [O] Buffer for security descriptor length
634 * modif [O] Modification time
635 *
636 * RETURNS
637 * Success: ERROR_SUCCESS
638 * Failure: system error code.
639 *
640 * NOTES
641 * - win95 allows class to be valid and class_len to be NULL
642 * - winnt returns ERROR_INVALID_PARAMETER if class is valid and class_len is NULL
643 * - both allow class to be NULL and class_len to be NULL
644 * (it's hard to test validity, so test !NULL instead)
645 */
646 LSTATUS WINAPI RegQueryInfoKeyW( HKEY hkey, LPWSTR class, LPDWORD class_len, LPDWORD reserved,
647 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
648 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
649 LPDWORD security, FILETIME *modif )
650 {
651 NTSTATUS status;
652 char buffer[256], *buf_ptr = buffer;
653 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
654 DWORD total_size;
655
656 TRACE( "(%p,%p,%d,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
657 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
658
659 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
660 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
661
662 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
663 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
664
665 if (class)
666 {
667 /* retry with a dynamically allocated buffer */
668 while (status == STATUS_BUFFER_OVERFLOW)
669 {
670 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
671 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
672 return ERROR_NOT_ENOUGH_MEMORY;
673 info = (KEY_FULL_INFORMATION *)buf_ptr;
674 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
675 }
676
677 if (status) goto done;
678
679 if (class_len && (info->ClassLength/sizeof(WCHAR) + 1 > *class_len))
680 {
681 status = STATUS_BUFFER_OVERFLOW;
682 }
683 else
684 {
685 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
686 class[info->ClassLength/sizeof(WCHAR)] = 0;
687 }
688 }
689 else status = STATUS_SUCCESS;
690
691 if (class_len) *class_len = info->ClassLength / sizeof(WCHAR);
692 if (subkeys) *subkeys = info->SubKeys;
693 if (max_subkey) *max_subkey = info->MaxNameLen;
694 if (max_class) *max_class = info->MaxClassLen;
695 if (values) *values = info->Values;
696 if (max_value) *max_value = info->MaxValueNameLen;
697 if (max_data) *max_data = info->MaxValueDataLen;
698 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
699
700 done:
701 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
702 return RtlNtStatusToDosError( status );
703 }
704
705
706 /******************************************************************************
707 * RegQueryMultipleValuesA [ADVAPI32.@]
708 *
709 * Retrieves the type and data for a list of value names associated with a key.
710 *
711 * PARAMS
712 * hKey [I] Handle to an open key.
713 * val_list [O] Array of VALENT structures that describes the entries.
714 * num_vals [I] Number of elements in val_list.
715 * lpValueBuf [O] Pointer to a buffer that receives the data for each value.
716 * ldwTotsize [I/O] Size of lpValueBuf.
717 *
718 * RETURNS
719 * Success: ERROR_SUCCESS. ldwTotsize contains num bytes copied.
720 * Failure: nonzero error code from Winerror.h ldwTotsize contains num needed
721 * bytes.
722 */
723 LSTATUS WINAPI RegQueryMultipleValuesA( HKEY hkey, PVALENTA val_list, DWORD num_vals,
724 LPSTR lpValueBuf, LPDWORD ldwTotsize )
725 {
726 unsigned int i;
727 DWORD maxBytes = *ldwTotsize;
728 HRESULT status;
729 LPSTR bufptr = lpValueBuf;
730 *ldwTotsize = 0;
731
732 TRACE("(%p,%p,%d,%p,%p=%d)\n", hkey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
733
734 for(i=0; i < num_vals; ++i)
735 {
736
737 val_list[i].ve_valuelen=0;
738 status = RegQueryValueExA(hkey, val_list[i].ve_valuename, NULL, NULL, NULL, &val_list[i].ve_valuelen);
739 if(status != ERROR_SUCCESS)
740 {
741 return status;
742 }
743
744 if(lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
745 {
746 status = RegQueryValueExA(hkey, val_list[i].ve_valuename, NULL, &val_list[i].ve_type,
747 (LPBYTE)bufptr, &val_list[i].ve_valuelen);
748 if(status != ERROR_SUCCESS)
749 {
750 return status;
751 }
752
753 val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
754
755 bufptr += val_list[i].ve_valuelen;
756 }
757
758 *ldwTotsize += val_list[i].ve_valuelen;
759 }
760 return lpValueBuf != NULL && *ldwTotsize <= maxBytes ? ERROR_SUCCESS : ERROR_MORE_DATA;
761 }
762
763
764 /******************************************************************************
765 * RegQueryMultipleValuesW [ADVAPI32.@]
766 *
767 * See RegQueryMultipleValuesA.
768 */
769 LSTATUS WINAPI RegQueryMultipleValuesW( HKEY hkey, PVALENTW val_list, DWORD num_vals,
770 LPWSTR lpValueBuf, LPDWORD ldwTotsize )
771 {
772 unsigned int i;
773 DWORD maxBytes = *ldwTotsize;
774 HRESULT status;
775 LPSTR bufptr = (LPSTR)lpValueBuf;
776 *ldwTotsize = 0;
777
778 TRACE("(%p,%p,%d,%p,%p=%d)\n", hkey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
779
780 for(i=0; i < num_vals; ++i)
781 {
782 val_list[i].ve_valuelen=0;
783 status = RegQueryValueExW(hkey, val_list[i].ve_valuename, NULL, NULL, NULL, &val_list[i].ve_valuelen);
784 if(status != ERROR_SUCCESS)
785 {
786 return status;
787 }
788
789 if(lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
790 {
791 status = RegQueryValueExW(hkey, val_list[i].ve_valuename, NULL, &val_list[i].ve_type,
792 (LPBYTE)bufptr, &val_list[i].ve_valuelen);
793 if(status != ERROR_SUCCESS)
794 {
795 return status;
796 }
797
798 val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
799
800 bufptr += val_list[i].ve_valuelen;
801 }
802
803 *ldwTotsize += val_list[i].ve_valuelen;
804 }
805 return lpValueBuf != NULL && *ldwTotsize <= maxBytes ? ERROR_SUCCESS : ERROR_MORE_DATA;
806 }
807
808 /******************************************************************************
809 * RegQueryInfoKeyA [ADVAPI32.@]
810 *
811 * Retrieves information about a registry key.
812 *
813 * PARAMS
814 * hKey [I] Handle to an open key.
815 * lpClass [O] Class string of the key.
816 * lpcClass [I/O] size of lpClass.
817 * lpReserved [I] Reserved; must be NULL.
818 * lpcSubKeys [O] Number of subkeys contained by the key.
819 * lpcMaxSubKeyLen [O] Size of the key's subkey with the longest name.
820 * lpcMaxClassLen [O] Size of the longest string specifying a subkey
821 * class in TCHARS.
822 * lpcValues [O] Number of values associated with the key.
823 * lpcMaxValueNameLen [O] Size of the key's longest value name in TCHARS.
824 * lpcMaxValueLen [O] Longest data component among the key's values
825 * lpcbSecurityDescriptor [O] Size of the key's security descriptor.
826 * lpftLastWriteTime [O] FILETIME structure that is the last write time.
827 *
828 * RETURNS
829 * Success: ERROR_SUCCESS
830 * Failure: nonzero error code from Winerror.h
831 */
832 LSTATUS WINAPI RegQueryInfoKeyA( HKEY hkey, LPSTR class, LPDWORD class_len, LPDWORD reserved,
833 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
834 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
835 LPDWORD security, FILETIME *modif )
836 {
837 NTSTATUS status;
838 char buffer[256], *buf_ptr = buffer;
839 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
840 DWORD total_size, len;
841
842 TRACE( "(%p,%p,%d,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
843 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
844
845 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
846 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
847
848 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
849 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
850
851 if (class || class_len)
852 {
853 /* retry with a dynamically allocated buffer */
854 while (status == STATUS_BUFFER_OVERFLOW)
855 {
856 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
857 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
858 return ERROR_NOT_ENOUGH_MEMORY;
859 info = (KEY_FULL_INFORMATION *)buf_ptr;
860 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
861 }
862
863 if (status) goto done;
864
865 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->ClassOffset), info->ClassLength);
866 if (class_len)
867 {
868 if (len + 1 > *class_len) status = STATUS_BUFFER_OVERFLOW;
869 *class_len = len;
870 }
871 if (class && !status)
872 {
873 RtlUnicodeToMultiByteN( class, len, NULL, (WCHAR *)(buf_ptr + info->ClassOffset),
874 info->ClassLength );
875 class[len] = 0;
876 }
877 }
878 else status = STATUS_SUCCESS;
879
880