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