dsound: Add support for 24/32 bits input sound buffers.
[wine] / dlls / advapi32 / registry.c
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 file is concerned about handle management and interaction with the Wine server.
12  * Registry file I/O is in misc/registry.c.
13  *
14  * This library is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU Lesser General Public
16  * License as published by the Free Software Foundation; either
17  * version 2.1 of the License, or (at your option) any later version.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32
33 #include "ntstatus.h"
34 #define WIN32_NO_STATUS
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winreg.h"
38 #include "winerror.h"
39 #include "winternl.h"
40 #include "winuser.h"
41
42 #include "wine/unicode.h"
43 #include "wine/debug.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(reg);
46
47 /* allowed bits for access mask */
48 #define KEY_ACCESS_MASK (KEY_ALL_ACCESS | MAXIMUM_ALLOWED)
49
50 #define HKEY_SPECIAL_ROOT_FIRST   HKEY_CLASSES_ROOT
51 #define HKEY_SPECIAL_ROOT_LAST    HKEY_DYN_DATA
52 #define NB_SPECIAL_ROOT_KEYS      ((UINT)HKEY_SPECIAL_ROOT_LAST - (UINT)HKEY_SPECIAL_ROOT_FIRST + 1)
53
54 static HKEY special_root_keys[NB_SPECIAL_ROOT_KEYS];
55 static BOOL hkcu_cache_disabled;
56
57 static const WCHAR name_CLASSES_ROOT[] =
58     {'M','a','c','h','i','n','e','\\',
59      'S','o','f','t','w','a','r','e','\\',
60      'C','l','a','s','s','e','s',0};
61 static const WCHAR name_LOCAL_MACHINE[] =
62     {'M','a','c','h','i','n','e',0};
63 static const WCHAR name_USERS[] =
64     {'U','s','e','r',0};
65 static const WCHAR name_PERFORMANCE_DATA[] =
66     {'P','e','r','f','D','a','t','a',0};
67 static const WCHAR name_CURRENT_CONFIG[] =
68     {'M','a','c','h','i','n','e','\\',
69      'S','y','s','t','e','m','\\',
70      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
71      'H','a','r','d','w','a','r','e',' ','P','r','o','f','i','l','e','s','\\',
72      'C','u','r','r','e','n','t',0};
73 static const WCHAR name_DYN_DATA[] =
74     {'D','y','n','D','a','t','a',0};
75
76 static const WCHAR * const root_key_names[NB_SPECIAL_ROOT_KEYS] =
77 {
78     name_CLASSES_ROOT,
79     NULL,         /* HKEY_CURRENT_USER is determined dynamically */
80     name_LOCAL_MACHINE,
81     name_USERS,
82     name_PERFORMANCE_DATA,
83     name_CURRENT_CONFIG,
84     name_DYN_DATA
85 };
86
87
88 /* check if value type needs string conversion (Ansi<->Unicode) */
89 static inline int is_string( DWORD type )
90 {
91     return (type == REG_SZ) || (type == REG_EXPAND_SZ) || (type == REG_MULTI_SZ);
92 }
93
94 /* check if current version is NT or Win95 */
95 static inline int is_version_nt(void)
96 {
97     return !(GetVersion() & 0x80000000);
98 }
99
100 /* create one of the HKEY_* special root keys */
101 static HKEY create_special_root_hkey( HANDLE hkey, DWORD access )
102 {
103     HKEY ret = 0;
104     int idx = (UINT_PTR)hkey - (UINT_PTR)HKEY_SPECIAL_ROOT_FIRST;
105
106     if (hkey == HKEY_CURRENT_USER)
107     {
108         if (RtlOpenCurrentUser( access, &hkey )) return 0;
109         TRACE( "HKEY_CURRENT_USER -> %p\n", hkey );
110
111         /* don't cache the key in the table if caching is disabled */
112         if (hkcu_cache_disabled)
113             return hkey;
114     }
115     else
116     {
117         OBJECT_ATTRIBUTES attr;
118         UNICODE_STRING name;
119
120         attr.Length = sizeof(attr);
121         attr.RootDirectory = 0;
122         attr.ObjectName = &name;
123         attr.Attributes = 0;
124         attr.SecurityDescriptor = NULL;
125         attr.SecurityQualityOfService = NULL;
126         RtlInitUnicodeString( &name, root_key_names[idx] );
127         if (NtCreateKey( &hkey, access, &attr, 0, NULL, 0, NULL )) return 0;
128         TRACE( "%s -> %p\n", debugstr_w(attr.ObjectName->Buffer), hkey );
129     }
130
131     if (!(ret = InterlockedCompareExchangePointer( (void **)&special_root_keys[idx], hkey, 0 )))
132         ret = hkey;
133     else
134         NtClose( hkey );  /* somebody beat us to it */
135     return ret;
136 }
137
138 /* map the hkey from special root to normal key if necessary */
139 static inline HKEY get_special_root_hkey( HKEY hkey )
140 {
141     HKEY ret = hkey;
142
143     if ((hkey >= HKEY_SPECIAL_ROOT_FIRST) && (hkey <= HKEY_SPECIAL_ROOT_LAST))
144     {
145         if (!(ret = special_root_keys[(UINT_PTR)hkey - (UINT_PTR)HKEY_SPECIAL_ROOT_FIRST]))
146             ret = create_special_root_hkey( hkey, KEY_ALL_ACCESS );
147     }
148     return ret;
149 }
150
151
152 /******************************************************************************
153  * RegCreateKeyExW   [ADVAPI32.@]
154  *
155  * See RegCreateKeyExA.
156  */
157 LSTATUS WINAPI RegCreateKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, LPWSTR class,
158                              DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
159                              PHKEY retkey, LPDWORD dispos )
160 {
161     OBJECT_ATTRIBUTES attr;
162     UNICODE_STRING nameW, classW;
163
164     if (reserved) return ERROR_INVALID_PARAMETER;
165     if (!(access & KEY_ACCESS_MASK) || (access & ~KEY_ACCESS_MASK)) return ERROR_ACCESS_DENIED;
166     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
167
168     attr.Length = sizeof(attr);
169     attr.RootDirectory = hkey;
170     attr.ObjectName = &nameW;
171     attr.Attributes = 0;
172     attr.SecurityDescriptor = NULL;
173     attr.SecurityQualityOfService = NULL;
174     RtlInitUnicodeString( &nameW, name );
175     RtlInitUnicodeString( &classW, class );
176
177     return RtlNtStatusToDosError( NtCreateKey( (PHANDLE)retkey, access, &attr, 0,
178                                                &classW, options, dispos ) );
179 }
180
181
182 /******************************************************************************
183  * RegCreateKeyExA   [ADVAPI32.@]
184  *
185  * Open a registry key, creating it if it doesn't exist.
186  *
187  * PARAMS
188  *  hkey       [I] Handle of the parent registry key
189  *  name       [I] Name of the new key to open or create
190  *  reserved   [I] Reserved, pass 0
191  *  class      [I] The object type of the new key
192  *  options    [I] Flags controlling the key creation (REG_OPTION_* flags from "winnt.h")
193  *  access     [I] Access level desired
194  *  sa         [I] Security attributes for the key
195  *  retkey     [O] Destination for the resulting handle
196  *  dispos     [O] Receives REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY
197  *
198  * RETURNS
199  *  Success: ERROR_SUCCESS.
200  *  Failure: A standard Win32 error code. retkey remains untouched.
201  *
202  * FIXME
203  *  MAXIMUM_ALLOWED in access mask not supported by server
204  */
205 LSTATUS WINAPI RegCreateKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, LPSTR class,
206                              DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
207                              PHKEY retkey, LPDWORD dispos )
208 {
209     OBJECT_ATTRIBUTES attr;
210     UNICODE_STRING classW;
211     ANSI_STRING nameA, classA;
212     NTSTATUS status;
213
214     if (reserved) return ERROR_INVALID_PARAMETER;
215     if (!is_version_nt())
216     {
217         access = KEY_ALL_ACCESS;  /* Win95 ignores the access mask */
218         if (name && *name == '\\') name++; /* win9x,ME ignores one (and only one) beginning backslash */
219     }
220     else if (!(access & KEY_ACCESS_MASK) || (access & ~KEY_ACCESS_MASK)) return ERROR_ACCESS_DENIED;
221     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
222
223     attr.Length = sizeof(attr);
224     attr.RootDirectory = hkey;
225     attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
226     attr.Attributes = 0;
227     attr.SecurityDescriptor = NULL;
228     attr.SecurityQualityOfService = NULL;
229     RtlInitAnsiString( &nameA, name );
230     RtlInitAnsiString( &classA, class );
231
232     if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
233                                                  &nameA, FALSE )))
234     {
235         if (!(status = RtlAnsiStringToUnicodeString( &classW, &classA, TRUE )))
236         {
237             status = NtCreateKey( (PHANDLE)retkey, access, &attr, 0, &classW, options, dispos );
238             RtlFreeUnicodeString( &classW );
239         }
240     }
241     return RtlNtStatusToDosError( status );
242 }
243
244
245 /******************************************************************************
246  * RegCreateKeyW   [ADVAPI32.@]
247  *
248  * Creates the specified reg key.
249  *
250  * PARAMS
251  *  hKey      [I] Handle to an open key.
252  *  lpSubKey  [I] Name of a key that will be opened or created.
253  *  phkResult [O] Receives a handle to the opened or created key.
254  *
255  * RETURNS
256  *  Success: ERROR_SUCCESS
257  *  Failure: nonzero error code defined in Winerror.h
258  */
259 LSTATUS WINAPI RegCreateKeyW( HKEY hkey, LPCWSTR lpSubKey, PHKEY phkResult )
260 {
261     /* FIXME: previous implementation converted ERROR_INVALID_HANDLE to ERROR_BADKEY, */
262     /* but at least my version of NT (4.0 SP5) doesn't do this.  -- AJ */
263     return RegCreateKeyExW( hkey, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE,
264                             KEY_ALL_ACCESS, NULL, phkResult, NULL );
265 }
266
267
268 /******************************************************************************
269  * RegCreateKeyA   [ADVAPI32.@]
270  *
271  * See RegCreateKeyW.
272  */
273 LSTATUS WINAPI RegCreateKeyA( HKEY hkey, LPCSTR lpSubKey, PHKEY phkResult )
274 {
275     return RegCreateKeyExA( hkey, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE,
276                             KEY_ALL_ACCESS, NULL, phkResult, NULL );
277 }
278
279
280
281 /******************************************************************************
282  * RegOpenKeyExW   [ADVAPI32.@]
283  * 
284  * See RegOpenKeyExA.
285  */
286 LSTATUS WINAPI RegOpenKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, REGSAM access, PHKEY retkey )
287 {
288     OBJECT_ATTRIBUTES attr;
289     UNICODE_STRING nameW;
290
291     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
292
293     attr.Length = sizeof(attr);
294     attr.RootDirectory = hkey;
295     attr.ObjectName = &nameW;
296     attr.Attributes = 0;
297     attr.SecurityDescriptor = NULL;
298     attr.SecurityQualityOfService = NULL;
299     RtlInitUnicodeString( &nameW, name );
300     return RtlNtStatusToDosError( NtOpenKey( (PHANDLE)retkey, access, &attr ) );
301 }
302
303
304 /******************************************************************************
305  * RegOpenKeyExA   [ADVAPI32.@]
306  *
307  * Open a registry key.
308  *
309  * PARAMS
310  *  hkey       [I] Handle of open key
311  *  name       [I] Name of subkey to open
312  *  reserved   [I] Reserved - must be zero
313  *  access     [I] Security access mask
314  *  retkey     [O] Handle to open key
315  *
316  * RETURNS
317  *  Success: ERROR_SUCCESS
318  *  Failure: A standard Win32 error code. retkey is set to 0.
319  *
320  * NOTES
321  *  Unlike RegCreateKeyExA(), this function will not create the key if it
322  *  does not exist.
323  */
324 LSTATUS WINAPI RegOpenKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, REGSAM access, PHKEY retkey )
325 {
326     OBJECT_ATTRIBUTES attr;
327     STRING nameA;
328     NTSTATUS status;
329
330     if (!is_version_nt()) access = KEY_ALL_ACCESS;  /* Win95 ignores the access mask */
331
332     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
333
334     attr.Length = sizeof(attr);
335     attr.RootDirectory = hkey;
336     attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
337     attr.Attributes = 0;
338     attr.SecurityDescriptor = NULL;
339     attr.SecurityQualityOfService = NULL;
340
341     RtlInitAnsiString( &nameA, name );
342     if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
343                                                  &nameA, FALSE )))
344     {
345         status = NtOpenKey( (PHANDLE)retkey, access, &attr );
346     }
347     return RtlNtStatusToDosError( status );
348 }
349
350
351 /******************************************************************************
352  * RegOpenKeyW   [ADVAPI32.@]
353  *
354  * See RegOpenKeyA.
355  */
356 LSTATUS WINAPI RegOpenKeyW( HKEY hkey, LPCWSTR name, PHKEY retkey )
357 {
358     if (!name || !*name)
359     {
360         *retkey = hkey;
361         return ERROR_SUCCESS;
362     }
363     return RegOpenKeyExW( hkey, name, 0, KEY_ALL_ACCESS, retkey );
364 }
365
366
367 /******************************************************************************
368  * RegOpenKeyA   [ADVAPI32.@]
369  *           
370  * Open a registry key.
371  *
372  * PARAMS
373  *  hkey    [I] Handle of parent key to open the new key under
374  *  name    [I] Name of the key under hkey to open
375  *  retkey  [O] Destination for the resulting Handle
376  *
377  * RETURNS
378  *  Success: ERROR_SUCCESS
379  *  Failure: A standard Win32 error code. retkey is set to 0.
380  */
381 LSTATUS WINAPI RegOpenKeyA( HKEY hkey, LPCSTR name, PHKEY retkey )
382 {
383     if (!name || !*name)
384     {
385         *retkey = hkey;
386         return ERROR_SUCCESS;
387     }
388     return RegOpenKeyExA( hkey, name, 0, KEY_ALL_ACCESS, retkey );
389 }
390
391
392 /******************************************************************************
393  * RegOpenCurrentUser   [ADVAPI32.@]
394  *
395  * Get a handle to the HKEY_CURRENT_USER key for the user 
396  * the current thread is impersonating.
397  *
398  * PARAMS
399  *  access [I] Desired access rights to the key
400  *  retkey [O] Handle to the opened key
401  *
402  * RETURNS
403  *  Success: ERROR_SUCCESS
404  *  Failure: nonzero error code from Winerror.h
405  *
406  * FIXME
407  *  This function is supposed to retrieve a handle to the
408  *  HKEY_CURRENT_USER for the user the current thread is impersonating.
409  *  Since Wine does not currently allow threads to impersonate other users,
410  *  this stub should work fine.
411  */
412 LSTATUS WINAPI RegOpenCurrentUser( REGSAM access, PHKEY retkey )
413 {
414     return RegOpenKeyExA( HKEY_CURRENT_USER, "", 0, access, retkey );
415 }
416
417
418
419 /******************************************************************************
420  * RegEnumKeyExW   [ADVAPI32.@]
421  *
422  * Enumerate subkeys of the specified open registry key.
423  *
424  * PARAMS
425  *  hkey         [I] Handle to key to enumerate
426  *  index        [I] Index of subkey to enumerate
427  *  name         [O] Buffer for subkey name
428  *  name_len     [O] Size of subkey buffer
429  *  reserved     [I] Reserved
430  *  class        [O] Buffer for class string
431  *  class_len    [O] Size of class buffer
432  *  ft           [O] Time key last written to
433  *
434  * RETURNS
435  *  Success: ERROR_SUCCESS
436  *  Failure: System error code. If there are no more subkeys available, the
437  *           function returns ERROR_NO_MORE_ITEMS.
438  */
439 LSTATUS WINAPI RegEnumKeyExW( HKEY hkey, DWORD index, LPWSTR name, LPDWORD name_len,
440                            LPDWORD reserved, LPWSTR class, LPDWORD class_len, FILETIME *ft )
441 {
442     NTSTATUS status;
443     char buffer[256], *buf_ptr = buffer;
444     KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
445     DWORD total_size;
446
447     TRACE( "(%p,%d,%p,%p(%d),%p,%p,%p,%p)\n", hkey, index, name, name_len,
448            name_len ? *name_len : -1, reserved, class, class_len, ft );
449
450     if (reserved) return ERROR_INVALID_PARAMETER;
451     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
452
453     status = NtEnumerateKey( hkey, index, KeyNodeInformation,
454                              buffer, sizeof(buffer), &total_size );
455
456     while (status == STATUS_BUFFER_OVERFLOW)
457     {
458         /* retry with a dynamically allocated buffer */
459         if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
460         if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
461             return ERROR_NOT_ENOUGH_MEMORY;
462         info = (KEY_NODE_INFORMATION *)buf_ptr;
463         status = NtEnumerateKey( hkey, index, KeyNodeInformation,
464                                  buf_ptr, total_size, &total_size );
465     }
466
467     if (!status)
468     {
469         DWORD len = info->NameLength / sizeof(WCHAR);
470         DWORD cls_len = info->ClassLength / sizeof(WCHAR);
471
472         if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
473
474         if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
475             status = STATUS_BUFFER_OVERFLOW;
476         else
477         {
478             *name_len = len;
479             memcpy( name, info->Name, info->NameLength );
480             name[len] = 0;
481             if (class_len)
482             {
483                 *class_len = cls_len;
484                 if (class)
485                 {
486                     memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
487                     class[cls_len] = 0;
488                 }
489             }
490         }
491     }
492
493     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
494     return RtlNtStatusToDosError( status );
495 }
496
497
498 /******************************************************************************
499  * RegEnumKeyExA   [ADVAPI32.@]
500  *
501  * See RegEnumKeyExW.
502  */
503 LSTATUS WINAPI RegEnumKeyExA( HKEY hkey, DWORD index, LPSTR name, LPDWORD name_len,
504                            LPDWORD reserved, LPSTR class, LPDWORD class_len, FILETIME *ft )
505 {
506     NTSTATUS status;
507     char buffer[256], *buf_ptr = buffer;
508     KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
509     DWORD total_size;
510
511     TRACE( "(%p,%d,%p,%p(%d),%p,%p,%p,%p)\n", hkey, index, name, name_len,
512            name_len ? *name_len : -1, reserved, class, class_len, ft );
513
514     if (reserved) return ERROR_INVALID_PARAMETER;
515     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
516
517     status = NtEnumerateKey( hkey, index, KeyNodeInformation,
518                              buffer, sizeof(buffer), &total_size );
519
520     while (status == STATUS_BUFFER_OVERFLOW)
521     {
522         /* retry with a dynamically allocated buffer */
523         if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
524         if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
525             return ERROR_NOT_ENOUGH_MEMORY;
526         info = (KEY_NODE_INFORMATION *)buf_ptr;
527         status = NtEnumerateKey( hkey, index, KeyNodeInformation,
528                                  buf_ptr, total_size, &total_size );
529     }
530
531     if (!status)
532     {
533         DWORD len, cls_len;
534
535         RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
536         RtlUnicodeToMultiByteSize( &cls_len, (WCHAR *)(buf_ptr + info->ClassOffset),
537                                    info->ClassLength );
538         if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
539
540         if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
541             status = STATUS_BUFFER_OVERFLOW;
542         else
543         {
544             *name_len = len;
545             RtlUnicodeToMultiByteN( name, len, NULL, info->Name, info->NameLength );
546             name[len] = 0;
547             if (class_len)
548             {
549                 *class_len = cls_len;
550                 if (class)
551                 {
552                     RtlUnicodeToMultiByteN( class, cls_len, NULL,
553                                             (WCHAR *)(buf_ptr + info->ClassOffset),
554                                             info->ClassLength );
555                     class[cls_len] = 0;
556                 }
557             }
558         }
559     }
560
561     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
562     return RtlNtStatusToDosError( status );
563 }
564
565
566 /******************************************************************************
567  * RegEnumKeyW   [ADVAPI32.@]
568  *
569  * Enumerates subkeys of the specified open reg key.
570  *
571  * PARAMS
572  *  hKey    [I] Handle to an open key.
573  *  dwIndex [I] Index of the subkey of hKey to retrieve.
574  *  lpName  [O] Name of the subkey.
575  *  cchName [I] Size of lpName in TCHARS.
576  *
577  * RETURNS
578  *  Success: ERROR_SUCCESS
579  *  Failure: system error code. If there are no more subkeys available, the
580  *           function returns ERROR_NO_MORE_ITEMS.
581  */
582 LSTATUS WINAPI RegEnumKeyW( HKEY hkey, DWORD index, LPWSTR name, DWORD name_len )
583 {
584     return RegEnumKeyExW( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
585 }
586
587
588 /******************************************************************************
589  * RegEnumKeyA   [ADVAPI32.@]
590  *
591  * See RegEnumKeyW.
592  */
593 LSTATUS WINAPI RegEnumKeyA( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
594 {
595     return RegEnumKeyExA( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
596 }
597
598
599 /******************************************************************************
600  * RegQueryInfoKeyW   [ADVAPI32.@]
601  *
602  * Retrieves information about the specified registry key.
603  *
604  * PARAMS
605  *    hkey       [I] Handle to key to query
606  *    class      [O] Buffer for class string
607  *    class_len  [O] Size of class string buffer
608  *    reserved   [I] Reserved
609  *    subkeys    [O] Buffer for number of subkeys
610  *    max_subkey [O] Buffer for longest subkey name length
611  *    max_class  [O] Buffer for longest class string length
612  *    values     [O] Buffer for number of value entries
613  *    max_value  [O] Buffer for longest value name length
614  *    max_data   [O] Buffer for longest value data length
615  *    security   [O] Buffer for security descriptor length
616  *    modif      [O] Modification time
617  *
618  * RETURNS
619  *  Success: ERROR_SUCCESS
620  *  Failure: system error code.
621  *
622  * NOTES
623  *  - win95 allows class to be valid and class_len to be NULL
624  *  - winnt returns ERROR_INVALID_PARAMETER if class is valid and class_len is NULL
625  *  - both allow class to be NULL and class_len to be NULL
626  *    (it's hard to test validity, so test !NULL instead)
627  */
628 LSTATUS WINAPI RegQueryInfoKeyW( HKEY hkey, LPWSTR class, LPDWORD class_len, LPDWORD reserved,
629                               LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
630                               LPDWORD values, LPDWORD max_value, LPDWORD max_data,
631                               LPDWORD security, FILETIME *modif )
632 {
633     NTSTATUS status;
634     char buffer[256], *buf_ptr = buffer;
635     KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
636     DWORD total_size;
637
638     TRACE( "(%p,%p,%d,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
639            reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
640
641     if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
642     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
643
644     status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
645     if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
646
647     if (class)
648     {
649         /* retry with a dynamically allocated buffer */
650         while (status == STATUS_BUFFER_OVERFLOW)
651         {
652             if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
653             if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
654                 return ERROR_NOT_ENOUGH_MEMORY;
655             info = (KEY_FULL_INFORMATION *)buf_ptr;
656             status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
657         }
658
659         if (status) goto done;
660
661         if (class_len && (info->ClassLength/sizeof(WCHAR) + 1 > *class_len))
662         {
663             status = STATUS_BUFFER_OVERFLOW;
664         }
665         else
666         {
667             memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
668             class[info->ClassLength/sizeof(WCHAR)] = 0;
669         }
670     }
671     else status = STATUS_SUCCESS;
672
673     if (class_len) *class_len = info->ClassLength / sizeof(WCHAR);
674     if (subkeys) *subkeys = info->SubKeys;
675     if (max_subkey) *max_subkey = info->MaxNameLen;
676     if (max_class) *max_class = info->MaxClassLen;
677     if (values) *values = info->Values;
678     if (max_value) *max_value = info->MaxValueNameLen;
679     if (max_data) *max_data = info->MaxValueDataLen;
680     if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
681
682  done:
683     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
684     return RtlNtStatusToDosError( status );
685 }
686
687
688 /******************************************************************************
689  * RegQueryMultipleValuesA   [ADVAPI32.@]
690  *
691  * Retrieves the type and data for a list of value names associated with a key.
692  *
693  * PARAMS
694  *  hKey       [I] Handle to an open key.
695  *  val_list   [O] Array of VALENT structures that describes the entries.
696  *  num_vals   [I] Number of elements in val_list.
697  *  lpValueBuf [O] Pointer to a buffer that receives the data for each value.
698  *  ldwTotsize [I/O] Size of lpValueBuf.
699  *
700  * RETURNS
701  *  Success: ERROR_SUCCESS. ldwTotsize contains num bytes copied.
702  *  Failure: nonzero error code from Winerror.h ldwTotsize contains num needed
703  *           bytes.
704  */
705 LSTATUS WINAPI RegQueryMultipleValuesA( HKEY hkey, PVALENTA val_list, DWORD num_vals,
706                                      LPSTR lpValueBuf, LPDWORD ldwTotsize )
707 {
708     unsigned int i;
709     DWORD maxBytes = *ldwTotsize;
710     HRESULT status;
711     LPSTR bufptr = lpValueBuf;
712     *ldwTotsize = 0;
713
714     TRACE("(%p,%p,%d,%p,%p=%d)\n", hkey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
715
716     for(i=0; i < num_vals; ++i)
717     {
718
719         val_list[i].ve_valuelen=0;
720         status = RegQueryValueExA(hkey, val_list[i].ve_valuename, NULL, NULL, NULL, &val_list[i].ve_valuelen);
721         if(status != ERROR_SUCCESS)
722         {
723             return status;
724         }
725
726         if(lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
727         {
728             status = RegQueryValueExA(hkey, val_list[i].ve_valuename, NULL, &val_list[i].ve_type,
729                                       (LPBYTE)bufptr, &val_list[i].ve_valuelen);
730             if(status != ERROR_SUCCESS)
731             {
732                 return status;
733             }
734
735             val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
736
737             bufptr += val_list[i].ve_valuelen;
738         }
739
740         *ldwTotsize += val_list[i].ve_valuelen;
741     }
742     return lpValueBuf != NULL && *ldwTotsize <= maxBytes ? ERROR_SUCCESS : ERROR_MORE_DATA;
743 }
744
745
746 /******************************************************************************
747  * RegQueryMultipleValuesW   [ADVAPI32.@]
748  *
749  * See RegQueryMultipleValuesA.
750  */
751 LSTATUS WINAPI RegQueryMultipleValuesW( HKEY hkey, PVALENTW val_list, DWORD num_vals,
752                                      LPWSTR lpValueBuf, LPDWORD ldwTotsize )
753 {
754     unsigned int i;
755     DWORD maxBytes = *ldwTotsize;
756     HRESULT status;
757     LPSTR bufptr = (LPSTR)lpValueBuf;
758     *ldwTotsize = 0;
759
760     TRACE("(%p,%p,%d,%p,%p=%d)\n", hkey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
761
762     for(i=0; i < num_vals; ++i)
763     {
764         val_list[i].ve_valuelen=0;
765         status = RegQueryValueExW(hkey, val_list[i].ve_valuename, NULL, NULL, NULL, &val_list[i].ve_valuelen);
766         if(status != ERROR_SUCCESS)
767         {
768             return status;
769         }
770
771         if(lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
772         {
773             status = RegQueryValueExW(hkey, val_list[i].ve_valuename, NULL, &val_list[i].ve_type,
774                                       (LPBYTE)bufptr, &val_list[i].ve_valuelen);
775             if(status != ERROR_SUCCESS)
776             {
777                 return status;
778             }
779
780             val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
781
782             bufptr += val_list[i].ve_valuelen;
783         }
784
785         *ldwTotsize += val_list[i].ve_valuelen;
786     }
787     return lpValueBuf != NULL && *ldwTotsize <= maxBytes ? ERROR_SUCCESS : ERROR_MORE_DATA;
788 }
789
790 /******************************************************************************
791  * RegQueryInfoKeyA   [ADVAPI32.@]
792  *
793  * Retrieves information about a registry key.
794  *
795  * PARAMS
796  *  hKey                   [I] Handle to an open key.
797  *  lpClass                [O] Class string of the key.
798  *  lpcClass               [I/O] size of lpClass.
799  *  lpReserved             [I] Reserved; must be NULL.
800  *  lpcSubKeys             [O] Number of subkeys contained by the key.
801  *  lpcMaxSubKeyLen        [O] Size of the key's subkey with the longest name.
802  *  lpcMaxClassLen         [O] Size of the longest string specifying a subkey
803  *                             class in TCHARS.
804  *  lpcValues              [O] Number of values associated with the key.
805  *  lpcMaxValueNameLen     [O] Size of the key's longest value name in TCHARS.
806  *  lpcMaxValueLen         [O] Longest data component among the key's values
807  *  lpcbSecurityDescriptor [O] Size of the key's security descriptor.
808  *  lpftLastWriteTime      [O] FILETIME structure that is the last write time.
809  *
810  *  RETURNS
811  *   Success: ERROR_SUCCESS
812  *   Failure: nonzero error code from Winerror.h
813  */
814 LSTATUS WINAPI RegQueryInfoKeyA( HKEY hkey, LPSTR class, LPDWORD class_len, LPDWORD reserved,
815                               LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
816                               LPDWORD values, LPDWORD max_value, LPDWORD max_data,
817                               LPDWORD security, FILETIME *modif )
818 {
819     NTSTATUS status;
820     char buffer[256], *buf_ptr = buffer;
821     KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
822     DWORD total_size, len;
823
824     TRACE( "(%p,%p,%d,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
825            reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
826
827     if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
828     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
829
830     status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
831     if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
832
833     if (class || class_len)
834     {
835         /* retry with a dynamically allocated buffer */
836         while (status == STATUS_BUFFER_OVERFLOW)
837         {
838             if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
839             if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
840                 return ERROR_NOT_ENOUGH_MEMORY;
841             info = (KEY_FULL_INFORMATION *)buf_ptr;
842             status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
843         }
844
845         if (status) goto done;
846
847         RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->ClassOffset), info->ClassLength);
848         if (class_len)
849         {
850             if (len + 1 > *class_len) status = STATUS_BUFFER_OVERFLOW;
851             *class_len = len;
852         }
853         if (class && !status)
854         {
855             RtlUnicodeToMultiByteN( class, len, NULL, (WCHAR *)(buf_ptr + info->ClassOffset),
856                                     info->ClassLength );
857             class[len] = 0;
858         }
859     }
860     else status = STATUS_SUCCESS;
861
862     if (subkeys) *subkeys = info->SubKeys;
863     if (max_subkey) *max_subkey = info->MaxNameLen;
864     if (max_class) *max_class = info->MaxClassLen;
865     if (values) *values = info->Values;
866     if (max_value) *max_value = info->MaxValueNameLen;
867     if (max_data) *max_data = info->MaxValueDataLen;
868     if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
869
870  done:
871     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
872     return RtlNtStatusToDosError( status );
873 }
874
875
876 /******************************************************************************
877  * RegCloseKey   [ADVAPI32.@]
878  *
879  * Close an open registry key.
880  *
881  * PARAMS
882  *  hkey [I] Handle of key to close
883  *
884  * RETURNS
885  *  Success: ERROR_SUCCESS
886  *  Failure: Error code
887  */
888 LSTATUS WINAPI RegCloseKey( HKEY hkey )
889 {
890     if (!hkey) return ERROR_INVALID_HANDLE;
891     if (hkey >= (HKEY)0x80000000) return ERROR_SUCCESS;
892     return RtlNtStatusToDosError( NtClose( hkey ) );
893 }
894
895
896 /******************************************************************************
897  * RegDeleteKeyW   [ADVAPI32.@]
898  *
899  * See RegDeleteKeyA.
900  */
901 LSTATUS WINAPI RegDeleteKeyW( HKEY hkey, LPCWSTR name )
902 {
903     DWORD ret;
904     HKEY tmp;
905
906     if (!name) return ERROR_INVALID_PARAMETER;
907
908     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
909
910     if (!(ret = RegOpenKeyExW( hkey, name, 0, DELETE, &tmp )))
911     {
912         ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
913         RegCloseKey( tmp );
914     }
915     TRACE("%s ret=%08x\n", debugstr_w(name), ret);
916     return ret;
917 }
918
919
920 /******************************************************************************
921  * RegDeleteKeyA   [ADVAPI32.@]
922  *
923  * Delete a registry key.
924  *
925  * PARAMS
926  *  hkey   [I] Handle to parent key containing the key to delete
927  *  name   [I] Name of the key user hkey to delete
928  *
929  * NOTES
930  *
931  * MSDN is wrong when it says that hkey must be opened with the DELETE access
932  * right. In reality, it opens a new handle with DELETE access.
933  *
934  * RETURNS
935  *  Success: ERROR_SUCCESS
936  *  Failure: Error code
937  */
938 LSTATUS WINAPI RegDeleteKeyA( HKEY hkey, LPCSTR name )
939 {
940     DWORD ret;
941     HKEY tmp;
942
943     if (!name) return ERROR_INVALID_PARAMETER;
944
945     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
946
947     if (!(ret = RegOpenKeyExA( hkey, name, 0, DELETE, &tmp )))
948     {
949         if (!is_version_nt()) /* win95 does recursive key deletes */
950         {
951             CHAR name[MAX_PATH];
952
953             while(!RegEnumKeyA(tmp, 0, name, sizeof(name)))
954             {
955                 if(RegDeleteKeyA(tmp, name))  /* recurse */
956                     break;
957             }
958         }
959         ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
960         RegCloseKey( tmp );
961     }
962     TRACE("%s ret=%08x\n", debugstr_a(name), ret);
963     return ret;
964 }
965
966
967
968 /******************************************************************************
969  * RegSetValueExW   [ADVAPI32.@]
970  *
971  * Set the data and contents of a registry value.
972  *
973  * PARAMS
974  *  hkey       [I] Handle of key to set value for
975  *  name       [I] Name of value to set
976  *  reserved   [I] Reserved, must be zero
977  *  type       [I] Type of the value being set
978  *  data       [I] The new contents of the value to set
979  *  count      [I] Size of data
980  *
981  * RETURNS
982  *  Success: ERROR_SUCCESS
983  *  Failure: Error code
984  */
985 LSTATUS WINAPI RegSetValueExW( HKEY hkey, LPCWSTR name, DWORD reserved,
986                             DWORD type, CONST BYTE *data, DWORD count )
987 {
988     UNICODE_STRING nameW;
989
990     /* no need for version check, not implemented on win9x anyway */
991     if (count && is_string(type))
992     {
993         LPCWSTR str = (LPCWSTR)data;
994         /* if user forgot to count terminating null, add it (yes NT does this) */
995         if (str[count / sizeof(WCHAR) - 1] && !str[count / sizeof(WCHAR)])
996             count += sizeof(WCHAR);
997     }
998     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
999
1000     RtlInitUnicodeString( &nameW, name );
1001     return RtlNtStatusToDosError( NtSetValueKey( hkey, &nameW, 0, type, data, count ) );
1002 }
1003
1004
1005 /******************************************************************************
1006  * RegSetValueExA   [ADVAPI32.@]
1007  *
1008  * See RegSetValueExW.
1009  *
1010  * NOTES
1011  *  win95 does not care about count for REG_SZ and finds out the len by itself (js)
1012  *  NT does definitely care (aj)
1013  */
1014 LSTATUS WINAPI RegSetValueExA( HKEY hkey, LPCSTR name, DWORD reserved, DWORD type,
1015                             CONST BYTE *data, DWORD count )
1016 {
1017     ANSI_STRING nameA;
1018     WCHAR *dataW = NULL;
1019     NTSTATUS status;
1020
1021     if (!is_version_nt())  /* win95 */
1022     {
1023         if (type == REG_SZ)
1024         {
1025             if (!data) return ERROR_INVALID_PARAMETER;
1026             count = strlen((const char *)data) + 1;
1027         }
1028     }
1029     else if (count && is_string(type))
1030     {
1031         /* if user forgot to count terminating null, add it (yes NT does this) */
1032         if (data[count-1] && !data[count]) count++;
1033     }
1034
1035     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1036
1037     if (is_string( type )) /* need to convert to Unicode */
1038     {
1039         DWORD lenW;
1040         RtlMultiByteToUnicodeSize( &lenW, (const char *)data, count );
1041         if (!(dataW = HeapAlloc( GetProcessHeap(), 0, lenW ))) return ERROR_OUTOFMEMORY;
1042         RtlMultiByteToUnicodeN( dataW, lenW, NULL, (const char *)data, count );
1043         count = lenW;
1044         data = (BYTE *)dataW;
1045     }
1046
1047     RtlInitAnsiString( &nameA, name );
1048     if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
1049                                                  &nameA, FALSE )))
1050     {
1051         status = NtSetValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString, 0, type, data, count );
1052     }
1053     HeapFree( GetProcessHeap(), 0, dataW );
1054     return RtlNtStatusToDosError( status );
1055 }
1056
1057
1058 /******************************************************************************
1059  * RegSetValueW   [ADVAPI32.@]
1060  *
1061  * Sets the data for the default or unnamed value of a reg key.
1062  *
1063  * PARAMS
1064  *  hKey     [I] Handle to an open key.
1065  *  lpSubKey [I] Name of a subkey of hKey.
1066  *  dwType   [I] Type of information to store.
1067  *  lpData   [I] String that contains the data to set for the default value.
1068  *  cbData   [I] Size of lpData.
1069  *
1070  * RETURNS
1071  *  Success: ERROR_SUCCESS
1072  *  Failure: nonzero error code from Winerror.h
1073  */
1074 LSTATUS WINAPI RegSetValueW( HKEY hkey, LPCWSTR name, DWORD type, LPCWSTR data, DWORD count )
1075 {
1076     HKEY subkey = hkey;
1077     DWORD ret;
1078
1079     TRACE("(%p,%s,%d,%s,%d)\n", hkey, debugstr_w(name), type, debugstr_w(data), count );
1080
1081     if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
1082
1083     if (name && name[0])  /* need to create the subkey */
1084     {
1085         if ((ret = RegCreateKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1086     }
1087
1088     ret = RegSetValueExW( subkey, NULL, 0, REG_SZ, (const BYTE*)data,
1089                           (strlenW( data ) + 1) * sizeof(WCHAR) );
1090     if (subkey != hkey) RegCloseKey( subkey );
1091     return ret;
1092 }
1093
1094
1095 /******************************************************************************
1096  * RegSetValueA   [ADVAPI32.@]
1097  *
1098  * See RegSetValueW.
1099  */
1100 LSTATUS WINAPI RegSetValueA( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
1101 {
1102     HKEY subkey = hkey;
1103     DWORD ret;
1104
1105     TRACE("(%p,%s,%d,%s,%d)\n", hkey, debugstr_a(name), type, debugstr_a(data), count );
1106
1107     if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
1108
1109     if (name && name[0])  /* need to create the subkey */
1110     {
1111         if ((ret = RegCreateKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1112     }
1113     ret = RegSetValueExA( subkey, NULL, 0, REG_SZ, (const BYTE*)data, strlen(data)+1 );
1114     if (subkey != hkey) RegCloseKey( subkey );
1115     return ret;
1116 }
1117
1118
1119
1120 /******************************************************************************
1121  * RegQueryValueExW   [ADVAPI32.@]
1122  *
1123  * See RegQueryValueExA.
1124  */
1125 LSTATUS WINAPI RegQueryValueExW( HKEY hkey, LPCWSTR name, LPDWORD reserved, LPDWORD type,
1126                               LPBYTE data, LPDWORD count )
1127 {
1128     NTSTATUS status;
1129     UNICODE_STRING name_str;
1130     DWORD total_size;
1131     char buffer[256], *buf_ptr = buffer;
1132     KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
1133     static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
1134
1135     TRACE("(%p,%s,%p,%p,%p,%p=%d)\n",
1136           hkey, debugstr_w(name), reserved, type, data, count,
1137           (count && data) ? *count : 0 );
1138
1139     if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1140     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1141
1142     RtlInitUnicodeString( &name_str, name );
1143
1144     if (data) total_size = min( sizeof(buffer), *count + info_size );
1145     else
1146     {
1147         total_size = info_size;
1148         if (count) *count = 0;
1149     }
1150
1151     status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1152                               buffer, total_size, &total_size );
1153     if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1154
1155     if (data)
1156     {
1157         /* retry with a dynamically allocated buffer */
1158         while (status == STATUS_BUFFER_OVERFLOW && total_size - info_size <= *count)
1159         {
1160             if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1161             if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1162                 return ERROR_NOT_ENOUGH_MEMORY;
1163             info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1164             status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1165                                       buf_ptr, total_size, &total_size );
1166         }
1167
1168         if (!status)
1169         {
1170             memcpy( data, buf_ptr + info_size, total_size - info_size );
1171             /* if the type is REG_SZ and data is not 0-terminated
1172              * and there is enough space in the buffer NT appends a \0 */
1173             if (total_size - info_size <= *count-sizeof(WCHAR) && is_string(info->Type))
1174             {
1175                 WCHAR *ptr = (WCHAR *)(data + total_size - info_size);
1176                 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
1177             }
1178         }
1179         else if (status != STATUS_BUFFER_OVERFLOW) goto done;
1180     }
1181     else status = STATUS_SUCCESS;
1182
1183     if (type) *type = info->Type;
1184     if (count) *count = total_size - info_size;
1185
1186  done:
1187     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1188     return RtlNtStatusToDosError(status);
1189 }
1190
1191
1192 /******************************************************************************
1193  * RegQueryValueExA   [ADVAPI32.@]
1194  *
1195  * Get the type and contents of a specified value under with a key.
1196  *
1197  * PARAMS
1198  *  hkey      [I]   Handle of the key to query
1199  *  name      [I]   Name of value under hkey to query
1200  *  reserved  [I]   Reserved - must be NULL
1201  *  type      [O]   Destination for the value type, or NULL if not required
1202  *  data      [O]   Destination for the values contents, or NULL if not required
1203  *  count     [I/O] Size of data, updated with the number of bytes returned
1204  *
1205  * RETURNS
1206  *  Success: ERROR_SUCCESS. *count is updated with the number of bytes copied to data.
1207  *  Failure: ERROR_INVALID_HANDLE, if hkey is invalid.
1208  *           ERROR_INVALID_PARAMETER, if any other parameter is invalid.
1209  *           ERROR_MORE_DATA, if on input *count is too small to hold the contents.
1210  *                     
1211  * NOTES
1212  *   MSDN states that if data is too small it is partially filled. In reality 
1213  *   it remains untouched.
1214  */
1215 LSTATUS WINAPI RegQueryValueExA( HKEY hkey, LPCSTR name, LPDWORD reserved, LPDWORD type,
1216                               LPBYTE data, LPDWORD count )
1217 {
1218     NTSTATUS status;
1219     ANSI_STRING nameA;
1220     DWORD total_size;
1221     char buffer[256], *buf_ptr = buffer;
1222     KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
1223     static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
1224
1225     TRACE("(%p,%s,%p,%p,%p,%p=%d)\n",
1226           hkey, debugstr_a(name), reserved, type, data, count, count ? *count : 0 );
1227
1228     if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1229     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1230
1231     if (!data && count) *count = 0;
1232
1233     /* this matches Win9x behaviour - NT sets *type to a random value */
1234     if (type) *type = REG_NONE;
1235
1236     RtlInitAnsiString( &nameA, name );
1237     if ((status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
1238                                                 &nameA, FALSE )))
1239         return RtlNtStatusToDosError(status);
1240
1241     status = NtQueryValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString,
1242                               KeyValuePartialInformation, buffer, sizeof(buffer), &total_size );
1243     if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1244
1245     /* we need to fetch the contents for a string type even if not requested,
1246      * because we need to compute the length of the ASCII string. */
1247     if (data || is_string(info->Type))
1248     {
1249         /* retry with a dynamically allocated buffer */
1250         while (status == STATUS_BUFFER_OVERFLOW)
1251         {
1252             if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1253             if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1254             {
1255                 status = STATUS_NO_MEMORY;
1256                 goto done;
1257             }
1258             info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1259             status = NtQueryValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString,
1260                                     KeyValuePartialInformation, buf_ptr, total_size, &total_size );
1261         }
1262
1263         if (status) goto done;
1264
1265         if (is_string(info->Type))
1266         {
1267             DWORD len;
1268
1269             RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info_size),
1270                                        total_size - info_size );
1271             if (data && len)
1272             {
1273                 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
1274                 else
1275                 {
1276                     RtlUnicodeToMultiByteN( (char*)data, len, NULL, (WCHAR *)(buf_ptr + info_size),
1277                                             total_size - info_size );
1278                     /* if the type is REG_SZ and data is not 0-terminated
1279                      * and there is enough space in the buffer NT appends a \0 */
1280                     if (len < *count && data[len-1]) data[len] = 0;
1281                 }
1282             }
1283             total_size = len + info_size;
1284         }
1285         else if (data)
1286         {
1287             if (total_size - info_size > *count) status = STATUS_BUFFER_OVERFLOW;
1288             else memcpy( data, buf_ptr + info_size, total_size - info_size );
1289         }
1290     }
1291     else status = STATUS_SUCCESS;
1292
1293     if (type) *type = info->Type;
1294     if (count) *count = total_size - info_size;
1295
1296  done:
1297     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1298     return RtlNtStatusToDosError(status);
1299 }
1300
1301
1302 /******************************************************************************
1303  * RegQueryValueW   [ADVAPI32.@]
1304  *
1305  * Retrieves the data associated with the default or unnamed value of a key.
1306  *
1307  * PARAMS
1308  *  hkey      [I] Handle to an open key.
1309  *  name      [I] Name of the subkey of hKey.
1310  *  data      [O] Receives the string associated with the default value
1311  *                of the key.
1312  *  count     [I/O] Size of lpValue in bytes.
1313  *
1314  *  RETURNS
1315  *   Success: ERROR_SUCCESS
1316  *   Failure: nonzero error code from Winerror.h
1317  */
1318 LSTATUS WINAPI RegQueryValueW( HKEY hkey, LPCWSTR name, LPWSTR data, LPLONG count )
1319 {
1320     DWORD ret;
1321     HKEY subkey = hkey;
1322
1323     TRACE("(%p,%s,%p,%d)\n", hkey, debugstr_w(name), data, count ? *count : 0 );
1324
1325     if (name && name[0])
1326     {
1327         if ((ret = RegOpenKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1328     }
1329     ret = RegQueryValueExW( subkey, NULL, NULL, NULL, (LPBYTE)data, (LPDWORD)count );
1330     if (subkey != hkey) RegCloseKey( subkey );
1331     if (ret == ERROR_FILE_NOT_FOUND)
1332     {
1333         /* return empty string if default value not found */
1334         if (data) *data = 0;
1335         if (count) *count = sizeof(WCHAR);
1336         ret = ERROR_SUCCESS;
1337     }
1338     return ret;
1339 }
1340
1341
1342 /******************************************************************************
1343  * RegQueryValueA   [ADVAPI32.@]
1344  *
1345  * See RegQueryValueW.
1346  */
1347 LSTATUS WINAPI RegQueryValueA( HKEY hkey, LPCSTR name, LPSTR data, LPLONG count )
1348 {
1349     DWORD ret;
1350     HKEY subkey = hkey;
1351
1352     TRACE("(%p,%s,%p,%d)\n", hkey, debugstr_a(name), data, count ? *count : 0 );
1353
1354     if (name && name[0])
1355     {
1356         if ((ret = RegOpenKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1357     }
1358     ret = RegQueryValueExA( subkey, NULL, NULL, NULL, (LPBYTE)data, (LPDWORD)count );
1359     if (subkey != hkey) RegCloseKey( subkey );
1360     if (ret == ERROR_FILE_NOT_FOUND)
1361     {
1362         /* return empty string if default value not found */
1363         if (data) *data = 0;
1364         if (count) *count = 1;
1365         ret = ERROR_SUCCESS;
1366     }
1367     return ret;
1368 }
1369
1370
1371 /******************************************************************************
1372  * ADVAPI_ApplyRestrictions   [internal]
1373  *
1374  * Helper function for RegGetValueA/W.
1375  */
1376 static VOID ADVAPI_ApplyRestrictions( DWORD dwFlags, DWORD dwType,
1377                                       DWORD cbData, PLONG ret )
1378 {
1379     /* Check if the type is restricted by the passed flags */
1380     if (*ret == ERROR_SUCCESS || *ret == ERROR_MORE_DATA)
1381     {
1382         DWORD dwMask = 0;
1383
1384         switch (dwType)
1385         {
1386         case REG_NONE: dwMask = RRF_RT_REG_NONE; break;
1387         case REG_SZ: dwMask = RRF_RT_REG_SZ; break;
1388         case REG_EXPAND_SZ: dwMask = RRF_RT_REG_EXPAND_SZ; break;
1389         case REG_MULTI_SZ: dwMask = RRF_RT_REG_MULTI_SZ; break;
1390         case REG_BINARY: dwMask = RRF_RT_REG_BINARY; break;
1391         case REG_DWORD: dwMask = RRF_RT_REG_DWORD; break;
1392         case REG_QWORD: dwMask = RRF_RT_REG_QWORD; break;
1393         }
1394
1395         if (dwFlags & dwMask)
1396         {
1397             /* Type is not restricted, check for size mismatch */
1398             if (dwType == REG_BINARY)
1399             {
1400                 DWORD cbExpect = 0;
1401
1402                 if ((dwFlags & RRF_RT_DWORD) == RRF_RT_DWORD)
1403                     cbExpect = 4;
1404                 else if ((dwFlags & RRF_RT_DWORD) == RRF_RT_QWORD)
1405                     cbExpect = 8;
1406
1407                 if (cbExpect && cbData != cbExpect)
1408                     *ret = ERROR_DATATYPE_MISMATCH;
1409             }
1410         }
1411         else *ret = ERROR_UNSUPPORTED_TYPE;
1412     }
1413 }
1414
1415
1416 /******************************************************************************
1417  * RegGetValueW   [ADVAPI32.@]
1418  *
1419  * Retrieves the type and data for a value name associated with a key 
1420  * optionally expanding it's content and restricting it's type.
1421  *
1422  * PARAMS
1423  *  hKey      [I] Handle to an open key.
1424  *  pszSubKey [I] Name of the subkey of hKey.
1425  *  pszValue  [I] Name of value under hKey/szSubKey to query.
1426  *  dwFlags   [I] Flags restricting the value type to retrieve.
1427  *  pdwType   [O] Destination for the values type, may be NULL.
1428  *  pvData    [O] Destination for the values content, may be NULL.
1429  *  pcbData   [I/O] Size of pvData, updated with the size required to 
1430  *                  retrieve the whole content.
1431  *
1432  * RETURNS
1433  *  Success: ERROR_SUCCESS
1434  *  Failure: nonzero error code from Winerror.h
1435  *
1436  * NOTES
1437  *  - Unless RRF_NOEXPAND is specified REG_EXPAND_SZ is automatically expanded
1438  *    and REG_SZ is retrieved instead.
1439  *  - Restrictions are applied after expanding, using RRF_RT_REG_EXPAND_SZ 
1440  *    without RRF_NOEXPAND is thus not allowed.
1441  */
1442 LSTATUS WINAPI RegGetValueW( HKEY hKey, LPCWSTR pszSubKey, LPCWSTR pszValue,
1443                           DWORD dwFlags, LPDWORD pdwType, PVOID pvData,
1444                           LPDWORD pcbData )
1445 {
1446     DWORD dwType, cbData = pcbData ? *pcbData : 0;
1447     PVOID pvBuf = NULL;
1448     LONG ret;
1449
1450     TRACE("(%p,%s,%s,%d,%p,%p,%p=%d)\n", 
1451           hKey, debugstr_w(pszSubKey), debugstr_w(pszValue), dwFlags, pdwType,
1452           pvData, pcbData, cbData);
1453
1454     if (pvData && !pcbData)
1455         return ERROR_INVALID_PARAMETER;
1456     if ((dwFlags & RRF_RT_REG_EXPAND_SZ) && !(dwFlags & RRF_NOEXPAND))
1457         return ERROR_INVALID_PARAMETER;
1458
1459     if (pszSubKey && pszSubKey[0])
1460     {
1461         ret = RegOpenKeyExW(hKey, pszSubKey, 0, KEY_QUERY_VALUE, &hKey);
1462         if (ret != ERROR_SUCCESS) return ret;
1463     }
1464
1465     ret = RegQueryValueExW(hKey, pszValue, NULL, &dwType, pvData, &cbData);
1466     
1467     /* If we are going to expand we need to read in the whole the value even
1468      * if the passed buffer was too small as the expanded string might be
1469      * smaller than the unexpanded one and could fit into cbData bytes. */
1470     if ((ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA) &&
1471         dwType == REG_EXPAND_SZ && !(dwFlags & RRF_NOEXPAND))
1472     {
1473         do {
1474             HeapFree(GetProcessHeap(), 0, pvBuf);
1475             
1476             pvBuf = HeapAlloc(GetProcessHeap(), 0, cbData);
1477             if (!pvBuf)
1478             {
1479                 ret = ERROR_NOT_ENOUGH_MEMORY;
1480                 break;
1481             }
1482
1483             if (ret == ERROR_MORE_DATA || !pvData)
1484                 ret = RegQueryValueExW(hKey, pszValue, NULL, 
1485                                        &dwType, pvBuf, &cbData);
1486             else
1487             {
1488                 /* Even if cbData was large enough we have to copy the 
1489                  * string since ExpandEnvironmentStrings can't handle
1490                  * overlapping buffers. */
1491                 CopyMemory(pvBuf, pvData, cbData);
1492             }
1493
1494             /* Both the type or the value itself could have been modified in
1495              * between so we have to keep retrying until the buffer is large
1496              * enough or we no longer have to expand the value. */
1497         } while (dwType == REG_EXPAND_SZ && ret == ERROR_MORE_DATA);
1498
1499         if (ret == ERROR_SUCCESS)
1500         {
1501             /* Recheck dwType in case it changed since the first call */
1502             if (dwType == REG_EXPAND_SZ)
1503             {
1504                 cbData = ExpandEnvironmentStringsW(pvBuf, pvData,
1505                                                    pcbData ? *pcbData : 0);
1506                 dwType = REG_SZ;
1507                 if(pcbData && cbData > *pcbData)
1508                     ret = ERROR_MORE_DATA;
1509             }
1510             else if (pvData)
1511                 CopyMemory(pvData, pvBuf, *pcbData);
1512         }
1513
1514         HeapFree(GetProcessHeap(), 0, pvBuf);
1515     }
1516
1517     if (pszSubKey && pszSubKey[0])
1518         RegCloseKey(hKey);
1519
1520     ADVAPI_ApplyRestrictions(dwFlags, dwType, cbData, &ret);
1521
1522     if (pvData && ret != ERROR_SUCCESS && (dwFlags & RRF_ZEROONFAILURE))
1523         ZeroMemory(pvData, *pcbData);
1524
1525     if (pdwType) *pdwType = dwType;
1526     if (pcbData) *pcbData = cbData;
1527
1528     return ret;
1529 }
1530
1531
1532 /******************************************************************************
1533  * RegGetValueA   [ADVAPI32.@]
1534  *
1535  * See RegGetValueW.
1536  */
1537 LSTATUS WINAPI RegGetValueA( HKEY hKey, LPCSTR pszSubKey, LPCSTR pszValue,
1538                           DWORD dwFlags, LPDWORD pdwType, PVOID pvData, 
1539                           LPDWORD pcbData )
1540 {
1541     DWORD dwType, cbData = pcbData ? *pcbData : 0;
1542     PVOID pvBuf = NULL;
1543     LONG ret;
1544
1545     TRACE("(%p,%s,%s,%d,%p,%p,%p=%d)\n", 
1546           hKey, pszSubKey, pszValue, dwFlags, pdwType, pvData, pcbData,
1547           cbData);
1548
1549     if (pvData && !pcbData)
1550         return ERROR_INVALID_PARAMETER;
1551     if ((dwFlags & RRF_RT_REG_EXPAND_SZ) && !(dwFlags & RRF_NOEXPAND))
1552         return ERROR_INVALID_PARAMETER;
1553
1554     if (pszSubKey && pszSubKey[0])
1555     {
1556         ret = RegOpenKeyExA(hKey, pszSubKey, 0, KEY_QUERY_VALUE, &hKey);
1557         if (ret != ERROR_SUCCESS) return ret;
1558     }
1559
1560     ret = RegQueryValueExA(hKey, pszValue, NULL, &dwType, pvData, &cbData);
1561
1562     /* If we are going to expand we need to read in the whole the value even
1563      * if the passed buffer was too small as the expanded string might be
1564      * smaller than the unexpanded one and could fit into cbData bytes. */
1565     if ((ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA) &&
1566         dwType == REG_EXPAND_SZ && !(dwFlags & RRF_NOEXPAND))
1567     {
1568         do {
1569             HeapFree(GetProcessHeap(), 0, pvBuf);
1570
1571             pvBuf = HeapAlloc(GetProcessHeap(), 0, cbData);
1572             if (!pvBuf)
1573             {
1574                 ret = ERROR_NOT_ENOUGH_MEMORY;
1575                 break;
1576             }
1577
1578             if (ret == ERROR_MORE_DATA || !pvData)
1579                 ret = RegQueryValueExA(hKey, pszValue, NULL, 
1580                                        &dwType, pvBuf, &cbData);
1581             else
1582             {
1583                 /* Even if cbData was large enough we have to copy the 
1584                  * string since ExpandEnvironmentStrings can't handle
1585                  * overlapping buffers. */
1586                 CopyMemory(pvBuf, pvData, cbData);
1587             }
1588
1589             /* Both the type or the value itself could have been modified in
1590              * between so we have to keep retrying until the buffer is large
1591              * enough or we no longer have to expand the value. */
1592         } while (dwType == REG_EXPAND_SZ && ret == ERROR_MORE_DATA);
1593
1594         if (ret == ERROR_SUCCESS)
1595         {
1596             /* Recheck dwType in case it changed since the first call */
1597             if (dwType == REG_EXPAND_SZ)
1598             {
1599                 cbData = ExpandEnvironmentStringsA(pvBuf, pvData,
1600                                                    pcbData ? *pcbData : 0);
1601                 dwType = REG_SZ;
1602                 if(pcbData && cbData > *pcbData)
1603                     ret = ERROR_MORE_DATA;
1604             }
1605             else if (pvData)
1606                 CopyMemory(pvData, pvBuf, *pcbData);
1607         }
1608
1609         HeapFree(GetProcessHeap(), 0, pvBuf);
1610     }
1611
1612     if (pszSubKey && pszSubKey[0])
1613         RegCloseKey(hKey);
1614
1615     ADVAPI_ApplyRestrictions(dwFlags, dwType, cbData, &ret);
1616
1617     if (pvData && ret != ERROR_SUCCESS && (dwFlags & RRF_ZEROONFAILURE))
1618         ZeroMemory(pvData, *pcbData);
1619
1620     if (pdwType) *pdwType = dwType;
1621     if (pcbData) *pcbData = cbData;
1622
1623     return ret;
1624 }
1625
1626
1627 /******************************************************************************
1628  * RegEnumValueW   [ADVAPI32.@]
1629  *
1630  * Enumerates the values for the specified open registry key.
1631  *
1632  * PARAMS
1633  *  hkey       [I] Handle to key to query
1634  *  index      [I] Index of value to query
1635  *  value      [O] Value string
1636  *  val_count  [I/O] Size of value buffer (in wchars)
1637  *  reserved   [I] Reserved
1638  *  type       [O] Type code
1639  *  data       [O] Value data
1640  *  count      [I/O] Size of data buffer (in bytes)
1641  *
1642  * RETURNS
1643  *  Success: ERROR_SUCCESS
1644  *  Failure: nonzero error code from Winerror.h
1645  */
1646
1647 LSTATUS WINAPI RegEnumValueW( HKEY hkey, DWORD index, LPWSTR value, LPDWORD val_count,
1648                            LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1649 {
1650     NTSTATUS status;
1651     DWORD total_size;
1652     char buffer[256], *buf_ptr = buffer;
1653     KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
1654     static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
1655
1656     TRACE("(%p,%d,%p,%p,%p,%p,%p,%p)\n",
1657           hkey, index, value, val_count, reserved, type, data, count );
1658
1659     /* NT only checks count, not val_count */
1660     if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1661     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1662
1663     total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
1664     if (data) total_size += *count;
1665     total_size = min( sizeof(buffer), total_size );
1666
1667     status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1668                                   buffer, total_size, &total_size );
1669     if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1670
1671     if (value || data)
1672     {
1673         /* retry with a dynamically allocated buffer */
1674         while (status == STATUS_BUFFER_OVERFLOW)
1675         {
1676             if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1677             if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1678                 return ERROR_NOT_ENOUGH_MEMORY;
1679             info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
1680             status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1681                                           buf_ptr, total_size, &total_size );
1682         }
1683
1684         if (status) goto done;
1685
1686         if (value)
1687         {
1688             if (info->NameLength/sizeof(WCHAR) >= *val_count)
1689             {
1690                 status = STATUS_BUFFER_OVERFLOW;
1691                 goto overflow;
1692             }
1693             memcpy( value, info->Name, info->NameLength );
1694             *val_count = info->NameLength / sizeof(WCHAR);
1695             value[*val_count] = 0;
1696         }
1697
1698         if (data)
1699         {
1700             if (total_size - info->DataOffset > *count)
1701             {
1702                 status = STATUS_BUFFER_OVERFLOW;
1703                 goto overflow;
1704             }
1705             memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
1706             if (total_size - info->DataOffset <= *count-sizeof(WCHAR) && is_string(info->Type))
1707             {
1708                 /* if the type is REG_SZ and data is not 0-terminated
1709                  * and there is enough space in the buffer NT appends a \0 */
1710                 WCHAR *ptr = (WCHAR *)(data + total_size - info->DataOffset);
1711                 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
1712             }
1713         }
1714     }
1715     else status = STATUS_SUCCESS;
1716
1717  overflow:
1718     if (type) *type = info->Type;
1719     if (count) *count = info->DataLength;
1720
1721  done:
1722     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1723     return RtlNtStatusToDosError(status);
1724 }
1725
1726
1727 /******************************************************************************
1728  * RegEnumValueA   [ADVAPI32.@]
1729  *
1730  * See RegEnumValueW.
1731  */
1732 LSTATUS WINAPI RegEnumValueA( HKEY hkey, DWORD index, LPSTR value, LPDWORD val_count,
1733                            LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1734 {
1735     NTSTATUS status;
1736     DWORD total_size;
1737     char buffer[256], *buf_ptr = buffer;
1738     KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
1739     static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
1740
1741     TRACE("(%p,%d,%p,%p,%p,%p,%p,%p)\n",
1742           hkey, index, value, val_count, reserved, type, data, count );
1743
1744     /* NT only checks count, not val_count */
1745     if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1746     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1747
1748     total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
1749     if (data) total_size += *count;
1750     total_size = min( sizeof(buffer), total_size );
1751
1752     status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1753                                   buffer, total_size, &total_size );
1754     if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1755
1756     /* we need to fetch the contents for a string type even if not requested,
1757      * because we need to compute the length of the ASCII string. */
1758     if (value || data || is_string(info->Type))
1759     {
1760         /* retry with a dynamically allocated buffer */
1761         while (status == STATUS_BUFFER_OVERFLOW)
1762         {
1763             if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1764             if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1765                 return ERROR_NOT_ENOUGH_MEMORY;
1766             info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
1767             status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1768                                           buf_ptr, total_size, &total_size );
1769         }
1770
1771         if (status) goto done;
1772
1773         if (is_string(info->Type))
1774         {
1775             DWORD len;
1776             RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->DataOffset),
1777                                        total_size - info->DataOffset );
1778             if (data && len)
1779             {
1780                 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
1781                 else
1782                 {
1783                     RtlUnicodeToMultiByteN( (char*)data, len, NULL, (WCHAR *)(buf_ptr + info->DataOffset),
1784                                             total_size - info->DataOffset );
1785                     /* if the type is REG_SZ and data is not 0-terminated
1786                      * and there is enough space in the buffer NT appends a \0 */
1787                     if (len < *count && data[len-1]) data[len] = 0;
1788                 }
1789             }
1790             info->DataLength = len;
1791         }
1792         else if (data)
1793         {
1794             if (total_size - info->DataOffset > *count) status = STATUS_BUFFER_OVERFLOW;
1795             else memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
1796         }
1797
1798         if (value && !status)
1799         {
1800             DWORD len;
1801
1802             RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
1803             if (len >= *val_count)
1804             {
1805                 status = STATUS_BUFFER_OVERFLOW;
1806                 if (*val_count)
1807                 {
1808                     len = *val_count - 1;
1809                     RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
1810                     value[len] = 0;
1811                 }
1812             }
1813             else
1814             {
1815                 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
1816                 value[len] = 0;
1817                 *val_count = len;
1818             }
1819         }
1820     }
1821     else status = STATUS_SUCCESS;
1822
1823     if (type) *type = info->Type;
1824     if (count) *count = info->DataLength;
1825
1826  done:
1827     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1828     return RtlNtStatusToDosError(status);
1829 }
1830
1831
1832
1833 /******************************************************************************
1834  * RegDeleteValueW   [ADVAPI32.@]
1835  *
1836  * See RegDeleteValueA.
1837  */
1838 LSTATUS WINAPI RegDeleteValueW( HKEY hkey, LPCWSTR name )
1839 {
1840     UNICODE_STRING nameW;
1841
1842     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1843
1844     RtlInitUnicodeString( &nameW, name );
1845     return RtlNtStatusToDosError( NtDeleteValueKey( hkey, &nameW ) );
1846 }
1847
1848
1849 /******************************************************************************
1850  * RegDeleteValueA   [ADVAPI32.@]
1851  *
1852  * Delete a value from the registry.
1853  *
1854  * PARAMS
1855  *  hkey [I] Registry handle of the key holding the value
1856  *  name [I] Name of the value under hkey to delete
1857  *
1858  * RETURNS
1859  *  Success: ERROR_SUCCESS
1860  *  Failure: nonzero error code from Winerror.h
1861  */
1862 LSTATUS WINAPI RegDeleteValueA( HKEY hkey, LPCSTR name )
1863 {
1864     STRING nameA;
1865     NTSTATUS status;
1866
1867     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1868
1869     RtlInitAnsiString( &nameA, name );
1870     if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
1871                                                  &nameA, FALSE )))
1872         status = NtDeleteValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString );
1873     return RtlNtStatusToDosError( status );
1874 }
1875
1876
1877 /******************************************************************************
1878  * RegLoadKeyW   [ADVAPI32.@]
1879  *
1880  * Create a subkey under HKEY_USERS or HKEY_LOCAL_MACHINE and store
1881  * registration information from a specified file into that subkey.
1882  *
1883  * PARAMS
1884  *  hkey      [I] Handle of open key
1885  *  subkey    [I] Address of name of subkey
1886  *  filename  [I] Address of filename for registry information
1887  *
1888  * RETURNS
1889  *  Success: ERROR_SUCCESS
1890  *  Failure: nonzero error code from Winerror.h
1891  */
1892 LSTATUS WINAPI RegLoadKeyW( HKEY hkey, LPCWSTR subkey, LPCWSTR filename )
1893 {
1894     OBJECT_ATTRIBUTES destkey, file;
1895     UNICODE_STRING subkeyW, filenameW;
1896     NTSTATUS status;
1897
1898     if (!(hkey = get_special_root_hkey(hkey))) return ERROR_INVALID_HANDLE;
1899
1900     destkey.Length = sizeof(destkey);
1901     destkey.RootDirectory = hkey;               /* root key: HKLM or HKU */
1902     destkey.ObjectName = &subkeyW;              /* name of the key */
1903     destkey.Attributes = 0;
1904     destkey.SecurityDescriptor = NULL;
1905     destkey.SecurityQualityOfService = NULL;
1906     RtlInitUnicodeString(&subkeyW, subkey);
1907
1908     file.Length = sizeof(file);
1909     file.RootDirectory = NULL;
1910     file.ObjectName = &filenameW;               /* file containing the hive */
1911     file.Attributes = OBJ_CASE_INSENSITIVE;
1912     file.SecurityDescriptor = NULL;
1913     file.SecurityQualityOfService = NULL;
1914     RtlDosPathNameToNtPathName_U(filename, &filenameW, NULL, NULL);
1915
1916     status = NtLoadKey(&destkey, &file);
1917     RtlFreeUnicodeString(&filenameW);
1918     return RtlNtStatusToDosError( status );
1919 }
1920
1921
1922 /******************************************************************************
1923  * RegLoadKeyA   [ADVAPI32.@]
1924  *
1925  * See RegLoadKeyW.
1926  */
1927 LSTATUS WINAPI RegLoadKeyA( HKEY hkey, LPCSTR subkey, LPCSTR filename )
1928 {
1929     UNICODE_STRING subkeyW, filenameW;
1930     STRING subkeyA, filenameA;
1931     NTSTATUS status;
1932     LONG ret;
1933
1934     RtlInitAnsiString(&subkeyA, subkey);
1935     RtlInitAnsiString(&filenameA, filename);
1936
1937     RtlInitUnicodeString(&subkeyW, NULL);
1938     RtlInitUnicodeString(&filenameW, NULL);
1939     if (!(status = RtlAnsiStringToUnicodeString(&subkeyW, &subkeyA, TRUE)) &&
1940         !(status = RtlAnsiStringToUnicodeString(&filenameW, &filenameA, TRUE)))
1941     {
1942         ret = RegLoadKeyW(hkey, subkeyW.Buffer, filenameW.Buffer);
1943     }
1944     else ret = RtlNtStatusToDosError(status);
1945     RtlFreeUnicodeString(&subkeyW);
1946     RtlFreeUnicodeString(&filenameW);
1947     return ret;
1948 }
1949
1950
1951 /******************************************************************************
1952  * RegSaveKeyW   [ADVAPI32.@]
1953  *
1954  * Save a key and all of its subkeys and values to a new file in the standard format.
1955  *
1956  * PARAMS
1957  *  hkey   [I] Handle of key where save begins
1958  *  lpFile [I] Address of filename to save to
1959  *  sa     [I] Address of security structure
1960  *
1961  * RETURNS
1962  *  Success: ERROR_SUCCESS
1963  *  Failure: nonzero error code from Winerror.h
1964  */
1965 LSTATUS WINAPI RegSaveKeyW( HKEY hkey, LPCWSTR file, LPSECURITY_ATTRIBUTES sa )
1966 {
1967     static const WCHAR format[] =
1968         {'r','e','g','%','0','4','x','.','t','m','p',0};
1969     WCHAR buffer[MAX_PATH];
1970     int count = 0;
1971     LPWSTR nameW;
1972     DWORD ret, err;
1973     HANDLE handle;
1974
1975     TRACE( "(%p,%s,%p)\n", hkey, debugstr_w(file), sa );
1976
1977     if (!file || !*file) return ERROR_INVALID_PARAMETER;
1978     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1979
1980     err = GetLastError();
1981     GetFullPathNameW( file, sizeof(buffer)/sizeof(WCHAR), buffer, &nameW );
1982
1983     for (;;)
1984     {
1985         snprintfW( nameW, 16, format, count++ );
1986         handle = CreateFileW( buffer, GENERIC_WRITE, 0, NULL,
1987                             CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
1988         if (handle != INVALID_HANDLE_VALUE) break;
1989         if ((ret = GetLastError()) != ERROR_FILE_EXISTS) goto done;
1990
1991         /* Something gone haywire ? Please report if this happens abnormally */
1992         if (count >= 100)
1993             MESSAGE("Wow, we are already fiddling with a temp file %s with an ordinal as high as %d !\nYou might want to delete all corresponding temp files in that directory.\n", debugstr_w(buffer), count);
1994     }
1995
1996     ret = RtlNtStatusToDosError(NtSaveKey(hkey, handle));
1997
1998     CloseHandle( handle );
1999     if (!ret)
2000     {
2001         if (!MoveFileExW( buffer, file, MOVEFILE_REPLACE_EXISTING ))
2002         {
2003             ERR( "Failed to move %s to %s\n", debugstr_w(buffer),
2004                 debugstr_w(file) );
2005             ret = GetLastError();
2006         }
2007     }
2008     if (ret) DeleteFileW( buffer );
2009
2010 done:
2011     SetLastError( err );  /* restore last error code */
2012     return ret;
2013 }
2014
2015
2016 /******************************************************************************
2017  * RegSaveKeyA  [ADVAPI32.@]
2018  *
2019  * See RegSaveKeyW.
2020  */
2021 LSTATUS WINAPI RegSaveKeyA( HKEY hkey, LPCSTR file, LPSECURITY_ATTRIBUTES sa )
2022 {
2023     UNICODE_STRING *fileW = &NtCurrentTeb()->StaticUnicodeString;
2024     NTSTATUS status;
2025     STRING fileA;
2026
2027     RtlInitAnsiString(&fileA, file);
2028     if ((status = RtlAnsiStringToUnicodeString(fileW, &fileA, FALSE)))
2029         return RtlNtStatusToDosError( status );
2030     return RegSaveKeyW(hkey, fileW->Buffer, sa);
2031 }
2032
2033
2034 /******************************************************************************
2035  * RegRestoreKeyW [ADVAPI32.@]
2036  *
2037  * Read the registry information from a file and copy it over a key.
2038  *
2039  * PARAMS
2040  *  hkey    [I] Handle of key where restore begins
2041  *  lpFile  [I] Address of filename containing saved tree
2042  *  dwFlags [I] Optional flags
2043  *
2044  * RETURNS
2045  *  Success: ERROR_SUCCESS
2046  *  Failure: nonzero error code from Winerror.h
2047  */
2048 LSTATUS WINAPI RegRestoreKeyW( HKEY hkey, LPCWSTR lpFile, DWORD dwFlags )
2049 {
2050     TRACE("(%p,%s,%d)\n",hkey,debugstr_w(lpFile),dwFlags);
2051
2052     /* It seems to do this check before the hkey check */
2053     if (!lpFile || !*lpFile)
2054         return ERROR_INVALID_PARAMETER;
2055
2056     FIXME("(%p,%s,%d): stub\n",hkey,debugstr_w(lpFile),dwFlags);
2057
2058     /* Check for file existence */
2059
2060     return ERROR_SUCCESS;
2061 }
2062
2063
2064 /******************************************************************************
2065  * RegRestoreKeyA [ADVAPI32.@]
2066  *
2067  * See RegRestoreKeyW.
2068  */
2069 LSTATUS WINAPI RegRestoreKeyA( HKEY hkey, LPCSTR lpFile, DWORD dwFlags )
2070 {
2071     UNICODE_STRING lpFileW;
2072     LONG ret;
2073
2074     RtlCreateUnicodeStringFromAsciiz( &lpFileW, lpFile );
2075     ret = RegRestoreKeyW( hkey, lpFileW.Buffer, dwFlags );
2076     RtlFreeUnicodeString( &lpFileW );
2077     return ret;
2078 }
2079
2080
2081 /******************************************************************************
2082  * RegUnLoadKeyW [ADVAPI32.@]
2083  *
2084  * Unload a registry key and its subkeys from the registry.
2085  *
2086  * PARAMS
2087  *  hkey     [I] Handle of open key
2088  *  lpSubKey [I] Address of name of subkey to unload
2089  *
2090  * RETURNS
2091  *  Success: ERROR_SUCCESS
2092  *  Failure: nonzero error code from Winerror.h
2093  */
2094 LSTATUS WINAPI RegUnLoadKeyW( HKEY hkey, LPCWSTR lpSubKey )
2095 {
2096     DWORD ret;
2097     HKEY shkey;
2098     OBJECT_ATTRIBUTES attr;
2099     UNICODE_STRING subkey;
2100
2101     TRACE("(%p,%s)\n",hkey, debugstr_w(lpSubKey));
2102
2103     ret = RegOpenKeyW(hkey,lpSubKey,&shkey);
2104     if( ret )
2105         return ERROR_INVALID_PARAMETER;
2106
2107     RtlInitUnicodeString(&subkey, lpSubKey);
2108     InitializeObjectAttributes(&attr, &subkey, OBJ_CASE_INSENSITIVE, shkey, NULL);
2109     ret = RtlNtStatusToDosError(NtUnloadKey(&attr));
2110
2111     RegCloseKey(shkey);
2112
2113     return ret;
2114 }
2115
2116
2117 /******************************************************************************
2118  * RegUnLoadKeyA [ADVAPI32.@]
2119  *
2120  * See RegUnLoadKeyW.
2121  */
2122 LSTATUS WINAPI RegUnLoadKeyA( HKEY hkey, LPCSTR lpSubKey )
2123 {
2124     UNICODE_STRING lpSubKeyW;
2125     LONG ret;
2126
2127     RtlCreateUnicodeStringFromAsciiz( &lpSubKeyW, lpSubKey );
2128     ret = RegUnLoadKeyW( hkey, lpSubKeyW.Buffer );
2129     RtlFreeUnicodeString( &lpSubKeyW );
2130     return ret;
2131 }
2132
2133
2134 /******************************************************************************
2135  * RegReplaceKeyW [ADVAPI32.@]
2136  *
2137  * Replace the file backing a registry key and all its subkeys with another file.
2138  *
2139  * PARAMS
2140  *  hkey      [I] Handle of open key
2141  *  lpSubKey  [I] Address of name of subkey
2142  *  lpNewFile [I] Address of filename for file with new data
2143  *  lpOldFile [I] Address of filename for backup file
2144  *
2145  * RETURNS
2146  *  Success: ERROR_SUCCESS
2147  *  Failure: nonzero error code from Winerror.h
2148  */
2149 LSTATUS WINAPI RegReplaceKeyW( HKEY hkey, LPCWSTR lpSubKey, LPCWSTR lpNewFile,
2150                               LPCWSTR lpOldFile )
2151 {
2152     FIXME("(%p,%s,%s,%s): stub\n", hkey, debugstr_w(lpSubKey),
2153           debugstr_w(lpNewFile),debugstr_w(lpOldFile));
2154     return ERROR_SUCCESS;
2155 }
2156
2157
2158 /******************************************************************************
2159  * RegReplaceKeyA [ADVAPI32.@]
2160  *
2161  * See RegReplaceKeyW.
2162  */
2163 LSTATUS WINAPI RegReplaceKeyA( HKEY hkey, LPCSTR lpSubKey, LPCSTR lpNewFile,
2164                               LPCSTR lpOldFile )
2165 {
2166     UNICODE_STRING lpSubKeyW;
2167     UNICODE_STRING lpNewFileW;
2168     UNICODE_STRING lpOldFileW;
2169     LONG ret;
2170
2171     RtlCreateUnicodeStringFromAsciiz( &lpSubKeyW, lpSubKey );
2172     RtlCreateUnicodeStringFromAsciiz( &lpOldFileW, lpOldFile );
2173     RtlCreateUnicodeStringFromAsciiz( &lpNewFileW, lpNewFile );
2174     ret = RegReplaceKeyW( hkey, lpSubKeyW.Buffer, lpNewFileW.Buffer, lpOldFileW.Buffer );
2175     RtlFreeUnicodeString( &lpOldFileW );
2176     RtlFreeUnicodeString( &lpNewFileW );
2177     RtlFreeUnicodeString( &lpSubKeyW );
2178     return ret;
2179 }
2180
2181
2182 /******************************************************************************
2183  * RegSetKeySecurity [ADVAPI32.@]
2184  *
2185  * Set the security of an open registry key.
2186  *
2187  * PARAMS
2188  *  hkey          [I] Open handle of key to set
2189  *  SecurityInfo  [I] Descriptor contents
2190  *  pSecurityDesc [I] Address of descriptor for key
2191  *
2192  * RETURNS
2193  *  Success: ERROR_SUCCESS
2194  *  Failure: nonzero error code from Winerror.h
2195  */
2196 LSTATUS WINAPI RegSetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInfo,
2197                                PSECURITY_DESCRIPTOR pSecurityDesc )
2198 {
2199     TRACE("(%p,%d,%p)\n",hkey,SecurityInfo,pSecurityDesc);
2200
2201     /* It seems to perform this check before the hkey check */
2202     if ((SecurityInfo & OWNER_SECURITY_INFORMATION) ||
2203         (SecurityInfo & GROUP_SECURITY_INFORMATION) ||
2204         (SecurityInfo & DACL_SECURITY_INFORMATION) ||
2205         (SecurityInfo & SACL_SECURITY_INFORMATION)) {
2206         /* Param OK */
2207     } else
2208         return ERROR_INVALID_PARAMETER;
2209
2210     if (!pSecurityDesc)
2211         return ERROR_INVALID_PARAMETER;
2212
2213     FIXME(":(%p,%d,%p): stub\n",hkey,SecurityInfo,pSecurityDesc);
2214
2215     return ERROR_SUCCESS;
2216 }
2217
2218
2219 /******************************************************************************
2220  * RegGetKeySecurity [ADVAPI32.@]
2221  *
2222  * Get a copy of the security descriptor for a given registry key.
2223  *
2224  * PARAMS
2225  *  hkey                   [I]   Open handle of key to set
2226  *  SecurityInformation    [I]   Descriptor contents
2227  *  pSecurityDescriptor    [O]   Address of descriptor for key
2228  *  lpcbSecurityDescriptor [I/O] Address of size of buffer and description
2229  *
2230  * RETURNS
2231  *  Success: ERROR_SUCCESS
2232  *  Failure: Error code
2233  */
2234 LSTATUS WINAPI RegGetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInformation,
2235                                PSECURITY_DESCRIPTOR pSecurityDescriptor,
2236                                LPDWORD lpcbSecurityDescriptor )
2237 {
2238     TRACE("(%p,%d,%p,%d)\n",hkey,SecurityInformation,pSecurityDescriptor,
2239           lpcbSecurityDescriptor?*lpcbSecurityDescriptor:0);
2240
2241     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
2242
2243     return RtlNtStatusToDosError( NtQuerySecurityObject( hkey,
2244                 SecurityInformation, pSecurityDescriptor,
2245                 *lpcbSecurityDescriptor, lpcbSecurityDescriptor ) );
2246 }
2247
2248
2249 /******************************************************************************
2250  * RegFlushKey [ADVAPI32.@]
2251  * 
2252  * Immediately write a registry key to registry.
2253  *
2254  * PARAMS
2255  *  hkey [I] Handle of key to write
2256  *
2257  * RETURNS
2258  *  Success: ERROR_SUCCESS
2259  *  Failure: Error code
2260  */
2261 LSTATUS WINAPI RegFlushKey( HKEY hkey )
2262 {
2263     hkey = get_special_root_hkey( hkey );
2264     if (!hkey) return ERROR_INVALID_HANDLE;
2265
2266     return RtlNtStatusToDosError( NtFlushKey( hkey ) );
2267 }
2268
2269
2270 /******************************************************************************
2271  * RegConnectRegistryW [ADVAPI32.@]
2272  *
2273  * Establish a connection to a predefined registry key on another computer.
2274  *
2275  * PARAMS
2276  *  lpMachineName [I] Address of name of remote computer
2277  *  hHey          [I] Predefined registry handle
2278  *  phkResult     [I] Address of buffer for remote registry handle
2279  *
2280  * RETURNS
2281  *  Success: ERROR_SUCCESS
2282  *  Failure: nonzero error code from Winerror.h
2283  */
2284 LSTATUS WINAPI RegConnectRegistryW( LPCWSTR lpMachineName, HKEY hKey,
2285                                    PHKEY phkResult )
2286 {
2287     LONG ret;
2288
2289     TRACE("(%s,%p,%p): stub\n",debugstr_w(lpMachineName),hKey,phkResult);
2290
2291     if (!lpMachineName || !*lpMachineName) {
2292         /* Use the local machine name */
2293         ret = RegOpenKeyW( hKey, NULL, phkResult );
2294     }
2295     else {
2296         WCHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
2297         DWORD len = sizeof(compName) / sizeof(WCHAR);
2298
2299         /* MSDN says lpMachineName must start with \\ : not so */
2300         if( lpMachineName[0] == '\\' &&  lpMachineName[1] == '\\')
2301             lpMachineName += 2;
2302         if (GetComputerNameW(compName, &len))
2303         {
2304             if (!strcmpiW(lpMachineName, compName))
2305                 ret = RegOpenKeyW(hKey, NULL, phkResult);
2306             else
2307             {
2308                 FIXME("Connect to %s is not supported.\n",debugstr_w(lpMachineName));
2309                 ret = ERROR_BAD_NETPATH;
2310             }
2311         }
2312         else
2313             ret = GetLastError();
2314     }
2315     return ret;
2316 }
2317
2318
2319 /******************************************************************************
2320  * RegConnectRegistryA [ADVAPI32.@]
2321  *
2322  * See RegConnectRegistryW.
2323  */
2324 LSTATUS WINAPI RegConnectRegistryA( LPCSTR machine, HKEY hkey, PHKEY reskey )
2325 {
2326     UNICODE_STRING machineW;
2327     LONG ret;
2328
2329     RtlCreateUnicodeStringFromAsciiz( &machineW, machine );
2330     ret = RegConnectRegistryW( machineW.Buffer, hkey, reskey );
2331     RtlFreeUnicodeString( &machineW );
2332     return ret;
2333 }
2334
2335
2336 /******************************************************************************
2337  * RegNotifyChangeKeyValue [ADVAPI32.@]
2338  *
2339  * Notify the caller about changes to the attributes or contents of a registry key.
2340  *
2341  * PARAMS
2342  *  hkey            [I] Handle of key to watch
2343  *  fWatchSubTree   [I] Flag for subkey notification
2344  *  fdwNotifyFilter [I] Changes to be reported
2345  *  hEvent          [I] Handle of signaled event
2346  *  fAsync          [I] Flag for asynchronous reporting
2347  *
2348  * RETURNS
2349  *  Success: ERROR_SUCCESS
2350  *  Failure: nonzero error code from Winerror.h
2351  */
2352 LSTATUS WINAPI RegNotifyChangeKeyValue( HKEY hkey, BOOL fWatchSubTree,
2353                                      DWORD fdwNotifyFilter, HANDLE hEvent,
2354                                      BOOL fAsync )
2355 {
2356     NTSTATUS status;
2357     IO_STATUS_BLOCK iosb;
2358
2359     hkey = get_special_root_hkey( hkey );
2360     if (!hkey) return ERROR_INVALID_HANDLE;
2361
2362     TRACE("(%p,%i,%d,%p,%i)\n", hkey, fWatchSubTree, fdwNotifyFilter,
2363           hEvent, fAsync);
2364
2365     status = NtNotifyChangeKey( hkey, hEvent, NULL, NULL, &iosb,
2366                                 fdwNotifyFilter, fAsync, NULL, 0,
2367                                 fWatchSubTree);
2368
2369     if (status && status != STATUS_TIMEOUT)
2370         return RtlNtStatusToDosError( status );
2371
2372     return ERROR_SUCCESS;
2373 }
2374
2375 /******************************************************************************
2376  * RegOpenUserClassesRoot [ADVAPI32.@]
2377  *
2378  * Open the HKEY_CLASSES_ROOT key for a user.
2379  *
2380  * PARAMS
2381  *  hToken     [I] Handle of token representing the user
2382  *  dwOptions  [I] Reserved, must be 0
2383  *  samDesired [I] Desired access rights
2384  *  phkResult  [O] Destination for the resulting key handle
2385  *
2386  * RETURNS
2387  *  Success: ERROR_SUCCESS
2388  *  Failure: nonzero error code from Winerror.h
2389  * 
2390  * NOTES
2391  *  On Windows 2000 and upwards the HKEY_CLASSES_ROOT key is a view of the
2392  *  "HKEY_LOCAL_MACHINE\Software\Classes" and the
2393  *  "HKEY_CURRENT_USER\Software\Classes" keys merged together.
2394  */
2395 LSTATUS WINAPI RegOpenUserClassesRoot(
2396     HANDLE hToken,
2397     DWORD dwOptions,
2398     REGSAM samDesired,
2399     PHKEY phkResult
2400 )
2401 {
2402     FIXME("(%p, 0x%x, 0x%x, %p) semi-stub\n", hToken, dwOptions, samDesired, phkResult);
2403
2404     *phkResult = HKEY_CLASSES_ROOT;
2405     return ERROR_SUCCESS;
2406 }
2407
2408 /******************************************************************************
2409  * load_string [Internal]
2410  *
2411  * This is basically a copy of user32/resource.c's LoadStringW. Necessary to
2412  * avoid importing user32, which is higher level than advapi32. Helper for
2413  * RegLoadMUIString.
2414  */
2415 static int load_string(HINSTANCE hModule, UINT resId, LPWSTR pwszBuffer, INT cMaxChars)
2416 {
2417     HGLOBAL hMemory;
2418     HRSRC hResource;
2419     WCHAR *pString;
2420     int idxString;
2421
2422     /* Negative values have to be inverted. */
2423     if (HIWORD(resId) == 0xffff)
2424         resId = (UINT)(-((INT)resId));
2425
2426     /* Load the resource into memory and get a pointer to it. */
2427     hResource = FindResourceW(hModule, MAKEINTRESOURCEW(LOWORD(resId >> 4) + 1), (LPWSTR)RT_STRING);
2428     if (!hResource) return 0;
2429     hMemory = LoadResource(hModule, hResource);
2430     if (!hMemory) return 0;
2431     pString = LockResource(hMemory);
2432
2433     /* Strings are length-prefixed. Lowest nibble of resId is an index. */
2434     idxString = resId & 0xf;
2435     while (idxString--) pString += *pString + 1;
2436
2437     /* If no buffer is given, return length of the string. */
2438     if (!pwszBuffer) return *pString;
2439
2440     /* Else copy over the string, respecting the buffer size. */
2441     cMaxChars = (*pString < cMaxChars) ? *pString : (cMaxChars - 1);
2442     if (cMaxChars >= 0) {
2443         memcpy(pwszBuffer, pString+1, cMaxChars * sizeof(WCHAR));
2444         pwszBuffer[cMaxChars] = '\0';
2445     }
2446
2447     return cMaxChars;
2448 }
2449
2450 /******************************************************************************
2451  * RegLoadMUIStringW [ADVAPI32.@]
2452  *
2453  * Load the localized version of a string resource from some PE, respective 
2454  * id and path of which are given in the registry value in the format 
2455  * @[path]\dllname,-resourceId
2456  *
2457  * PARAMS
2458  *  hKey       [I] Key, of which to load the string value from.
2459  *  pszValue   [I] The value to be loaded (Has to be of REG_EXPAND_SZ or REG_SZ type).
2460  *  pszBuffer  [O] Buffer to store the localized string in. 
2461  *  cbBuffer   [I] Size of the destination buffer in bytes.
2462  *  pcbData    [O] Number of bytes written to pszBuffer (optional, may be NULL).
2463  *  dwFlags    [I] None supported yet.
2464  *  pszBaseDir [I] Not supported yet.
2465  *
2466  * RETURNS
2467  *  Success: ERROR_SUCCESS,
2468  *  Failure: nonzero error code from winerror.h
2469  *
2470  * NOTES
2471  *  This is an API of Windows Vista, which wasn't available at the time this code
2472  *  was written. We have to check for the correct behaviour once it's available. 
2473  */
2474 LSTATUS WINAPI RegLoadMUIStringW(HKEY hKey, LPCWSTR pwszValue, LPWSTR pwszBuffer, DWORD cbBuffer,
2475     LPDWORD pcbData, DWORD dwFlags, LPCWSTR pwszBaseDir)
2476 {
2477     DWORD dwValueType, cbData;
2478     LPWSTR pwszTempBuffer = NULL, pwszExpandedBuffer = NULL;
2479     LONG result;
2480         
2481     TRACE("(hKey = %p, pwszValue = %s, pwszBuffer = %p, cbBuffer = %d, pcbData = %p, "
2482           "dwFlags = %d, pwszBaseDir = %s) stub\n", hKey, debugstr_w(pwszValue), pwszBuffer, 
2483           cbBuffer, pcbData, dwFlags, debugstr_w(pwszBaseDir));
2484
2485     /* Parameter sanity checks. */
2486     if (!hKey || !pwszBuffer)
2487         return ERROR_INVALID_PARAMETER;
2488
2489     if (pwszBaseDir && *pwszBaseDir) {
2490         FIXME("BaseDir parameter not yet supported!\n");
2491         return ERROR_INVALID_PARAMETER;
2492     }
2493
2494     /* Check for value existence and correctness of it's type, allocate a buffer and load it. */
2495     result = RegQueryValueExW(hKey, pwszValue, NULL, &dwValueType, NULL, &cbData);
2496     if (result != ERROR_SUCCESS) goto cleanup;
2497     if (!(dwValueType == REG_SZ || dwValueType == REG_EXPAND_SZ) || !cbData) {
2498         result = ERROR_FILE_NOT_FOUND;
2499         goto cleanup;
2500     }
2501     pwszTempBuffer = HeapAlloc(GetProcessHeap(), 0, cbData);
2502     if (!pwszTempBuffer) {
2503         result = ERROR_NOT_ENOUGH_MEMORY;
2504         goto cleanup;
2505     }
2506     result = RegQueryValueExW(hKey, pwszValue, NULL, &dwValueType, (LPBYTE)pwszTempBuffer, &cbData);
2507     if (result != ERROR_SUCCESS) goto cleanup;
2508
2509     /* Expand environment variables, if appropriate, or copy the original string over. */
2510     if (dwValueType == REG_EXPAND_SZ) {
2511         cbData = ExpandEnvironmentStringsW(pwszTempBuffer, NULL, 0) * sizeof(WCHAR);
2512         if (!cbData) goto cleanup;
2513         pwszExpandedBuffer = HeapAlloc(GetProcessHeap(), 0, cbData);
2514         if (!pwszExpandedBuffer) {
2515             result = ERROR_NOT_ENOUGH_MEMORY;
2516             goto cleanup;
2517         }
2518         ExpandEnvironmentStringsW(pwszTempBuffer, pwszExpandedBuffer, cbData);
2519     } else {
2520         pwszExpandedBuffer = HeapAlloc(GetProcessHeap(), 0, cbData);
2521         memcpy(pwszExpandedBuffer, pwszTempBuffer, cbData);
2522     }
2523
2524     /* If the value references a resource based string, parse the value and load the string.
2525      * Else just copy over the original value. */
2526     result = ERROR_SUCCESS;
2527     if (*pwszExpandedBuffer != '@') { /* '@' is the prefix for resource based string entries. */
2528         lstrcpynW(pwszBuffer, pwszExpandedBuffer, cbBuffer / sizeof(WCHAR));
2529     } else {
2530         WCHAR *pComma = strrchrW(pwszExpandedBuffer, ',');
2531         UINT uiStringId;
2532         HMODULE hModule;
2533
2534         /* Format of the expanded value is 'path_to_dll,-resId' */
2535         if (!pComma || pComma[1] != '-') {
2536             result = ERROR_BADKEY;
2537             goto cleanup;
2538         }
2539  
2540         uiStringId = atoiW(pComma+2);
2541         *pComma = '\0';
2542
2543         hModule = LoadLibraryW(pwszExpandedBuffer + 1);
2544         if (!hModule || !load_string(hModule, uiStringId, pwszBuffer, cbBuffer/sizeof(WCHAR)))
2545             result = ERROR_BADKEY;
2546         FreeLibrary(hModule);
2547     }
2548  
2549 cleanup:
2550     HeapFree(GetProcessHeap(), 0, pwszTempBuffer);
2551     HeapFree(GetProcessHeap(), 0, pwszExpandedBuffer);
2552     return result;
2553 }
2554
2555 /******************************************************************************
2556  * RegLoadMUIStringA [ADVAPI32.@]
2557  *
2558  * See RegLoadMUIStringW
2559  */
2560 LSTATUS WINAPI RegLoadMUIStringA(HKEY hKey, LPCSTR pszValue, LPSTR pszBuffer, DWORD cbBuffer,
2561     LPDWORD pcbData, DWORD dwFlags, LPCSTR pszBaseDir)
2562 {
2563     UNICODE_STRING valueW, baseDirW;
2564     WCHAR *pwszBuffer;
2565     DWORD cbData = cbBuffer * sizeof(WCHAR);
2566     LONG result;
2567
2568     valueW.Buffer = baseDirW.Buffer = pwszBuffer = NULL;
2569     if (!RtlCreateUnicodeStringFromAsciiz(&valueW, pszValue) ||
2570         !RtlCreateUnicodeStringFromAsciiz(&baseDirW, pszBaseDir) ||
2571         !(pwszBuffer = HeapAlloc(GetProcessHeap(), 0, cbData)))
2572     {
2573         result = ERROR_NOT_ENOUGH_MEMORY;
2574         goto cleanup;
2575     }
2576
2577     result = RegLoadMUIStringW(hKey, valueW.Buffer, pwszBuffer, cbData, NULL, dwFlags, 
2578                                baseDirW.Buffer);
2579  
2580     if (result == ERROR_SUCCESS) {
2581         cbData = WideCharToMultiByte(CP_ACP, 0, pwszBuffer, -1, pszBuffer, cbBuffer, NULL, NULL);
2582         if (pcbData)
2583             *pcbData = cbData;
2584     }
2585
2586 cleanup:
2587     HeapFree(GetProcessHeap(), 0, pwszBuffer);
2588     RtlFreeUnicodeString(&baseDirW);
2589     RtlFreeUnicodeString(&valueW);
2590  
2591     return result;
2592 }
2593
2594 /******************************************************************************
2595  * RegDisablePredefinedCache [ADVAPI32.@]
2596  *
2597  * Disables the caching of the HKEY_CLASSES_ROOT key for the process.
2598  *
2599  * PARAMS
2600  *  None.
2601  *
2602  * RETURNS
2603  *  Success: ERROR_SUCCESS
2604  *  Failure: nonzero error code from Winerror.h
2605  * 
2606  * NOTES
2607  *  This is useful for services that use impersonation.
2608  */
2609 LSTATUS WINAPI RegDisablePredefinedCache(void)
2610 {
2611     HKEY hkey_current_user;
2612     int idx = (UINT_PTR)HKEY_CURRENT_USER - (UINT_PTR)HKEY_SPECIAL_ROOT_FIRST;
2613
2614     /* prevent caching of future requests */
2615     hkcu_cache_disabled = TRUE;
2616
2617     hkey_current_user = InterlockedExchangePointer( (void **)&special_root_keys[idx], NULL );
2618
2619     if (hkey_current_user)
2620         NtClose( hkey_current_user );
2621
2622     return ERROR_SUCCESS;
2623 }
2624
2625 /******************************************************************************
2626  * RegDeleteTreeW [ADVAPI32.@]
2627  *
2628  */
2629 LSTATUS WINAPI RegDeleteTreeW(HKEY hKey, LPCWSTR lpszSubKey)
2630 {
2631     LONG ret;
2632     DWORD dwMaxSubkeyLen, dwMaxValueLen;
2633     DWORD dwMaxLen, dwSize;
2634     WCHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
2635     HKEY hSubKey = hKey;
2636
2637     TRACE("(hkey=%p,%p %s)\n", hKey, lpszSubKey, debugstr_w(lpszSubKey));
2638
2639     if(lpszSubKey)
2640     {
2641         ret = RegOpenKeyExW(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
2642         if (ret) return ret;
2643     }
2644
2645     /* Get highest length for keys, values */
2646     ret = RegQueryInfoKeyW(hSubKey, NULL, NULL, NULL, NULL,
2647             &dwMaxSubkeyLen, NULL, NULL, &dwMaxValueLen, NULL, NULL, NULL);
2648     if (ret) goto cleanup;
2649
2650     dwMaxSubkeyLen++;
2651     dwMaxValueLen++;
2652     dwMaxLen = max(dwMaxSubkeyLen, dwMaxValueLen);
2653     if (dwMaxLen > sizeof(szNameBuf)/sizeof(WCHAR))
2654     {
2655         /* Name too big: alloc a buffer for it */
2656         if (!(lpszName = HeapAlloc( GetProcessHeap(), 0, dwMaxLen*sizeof(WCHAR))))
2657         {
2658             ret = ERROR_NOT_ENOUGH_MEMORY;
2659             goto cleanup;
2660         }
2661     }
2662
2663
2664     /* Recursively delete all the subkeys */
2665     while (TRUE)
2666     {
2667         dwSize = dwMaxLen;
2668         if (RegEnumKeyExW(hSubKey, 0, lpszName, &dwSize, NULL,
2669                           NULL, NULL, NULL)) break;
2670
2671         ret = RegDeleteTreeW(hSubKey, lpszName);
2672         if (ret) goto cleanup;
2673     }
2674
2675     if (lpszSubKey)
2676         ret = RegDeleteKeyW(hKey, lpszSubKey);
2677     else
2678         while (TRUE)
2679         {
2680             dwSize = dwMaxLen;
2681             if (RegEnumValueW(hKey, 0, lpszName, &dwSize,
2682                   NULL, NULL, NULL, NULL)) break;
2683
2684             ret = RegDeleteValueW(hKey, lpszName);
2685             if (ret) goto cleanup;
2686         }
2687
2688 cleanup:
2689     /* Free buffer if allocated */
2690     if (lpszName != szNameBuf)
2691         HeapFree( GetProcessHeap(), 0, lpszName);
2692     if(lpszSubKey)
2693         RegCloseKey(hSubKey);
2694     return ret;
2695 }
2696
2697 /******************************************************************************
2698  * RegDeleteTreeA [ADVAPI32.@]
2699  *
2700  */
2701 LSTATUS WINAPI RegDeleteTreeA(HKEY hKey, LPCSTR lpszSubKey)
2702 {
2703     LONG ret;
2704     UNICODE_STRING lpszSubKeyW;
2705
2706     if (lpszSubKey) RtlCreateUnicodeStringFromAsciiz( &lpszSubKeyW, lpszSubKey);
2707     else lpszSubKeyW.Buffer = NULL;
2708     ret = RegDeleteTreeW( hKey, lpszSubKeyW.Buffer);
2709     RtlFreeUnicodeString( &lpszSubKeyW );
2710     return ret;
2711 }