RegCreateKeyEx{A,W}: state that class string is constant.
[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 "wine/unicode.h"
39 #include "heap.h"
40 #include "wine/server.h"
41 #include "wine/debug.h"
42 #include "winternl.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(reg);
45
46 /* allowed bits for access mask */
47 #define KEY_ACCESS_MASK (KEY_ALL_ACCESS | MAXIMUM_ALLOWED)
48
49 #define HKEY_SPECIAL_ROOT_FIRST   HKEY_CLASSES_ROOT
50 #define HKEY_SPECIAL_ROOT_LAST    HKEY_DYN_DATA
51 #define NB_SPECIAL_ROOT_KEYS      ((UINT)HKEY_SPECIAL_ROOT_LAST - (UINT)HKEY_SPECIAL_ROOT_FIRST + 1)
52
53 static HKEY special_root_keys[NB_SPECIAL_ROOT_KEYS];
54
55 static const WCHAR name_CLASSES_ROOT[] =
56     {'M','a','c','h','i','n','e','\\',
57      'S','o','f','t','w','a','r','e','\\',
58      'C','l','a','s','s','e','s',0};
59 static const WCHAR name_LOCAL_MACHINE[] =
60     {'M','a','c','h','i','n','e',0};
61 static const WCHAR name_USERS[] =
62     {'U','s','e','r',0};
63 static const WCHAR name_PERFORMANCE_DATA[] =
64     {'P','e','r','f','D','a','t','a',0};
65 static const WCHAR name_CURRENT_CONFIG[] =
66     {'M','a','c','h','i','n','e','\\',
67      'S','y','s','t','e','m','\\',
68      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
69      'H','a','r','d','w','a','r','e','P','r','o','f','i','l','e','s','\\',
70      'C','u','r','r','e','n','t',0};
71 static const WCHAR name_DYN_DATA[] =
72     {'D','y','n','D','a','t','a',0};
73
74 #define DECL_STR(key) { sizeof(name_##key)-sizeof(WCHAR), sizeof(name_##key), (LPWSTR)name_##key }
75 static UNICODE_STRING root_key_names[NB_SPECIAL_ROOT_KEYS] =
76 {
77     DECL_STR(CLASSES_ROOT),
78     { 0, 0, NULL },         /* HKEY_CURRENT_USER is determined dynamically */
79     DECL_STR(LOCAL_MACHINE),
80     DECL_STR(USERS),
81     DECL_STR(PERFORMANCE_DATA),
82     DECL_STR(CURRENT_CONFIG),
83     DECL_STR(DYN_DATA)
84 };
85 #undef DECL_STR
86
87
88 /* check if value type needs string conversion (Ansi<->Unicode) */
89 inline static 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 inline static 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( HKEY hkey, DWORD access )
102 {
103     HKEY ret = 0;
104     int idx = (UINT)hkey - (UINT)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     else
112     {
113         OBJECT_ATTRIBUTES attr;
114
115         attr.Length = sizeof(attr);
116         attr.RootDirectory = 0;
117         attr.ObjectName = &root_key_names[idx];
118         attr.Attributes = 0;
119         attr.SecurityDescriptor = NULL;
120         attr.SecurityQualityOfService = NULL;
121         if (NtCreateKey( &hkey, access, &attr, 0, NULL, 0, NULL )) return 0;
122         TRACE( "%s -> %p\n", debugstr_w(attr.ObjectName->Buffer), hkey );
123     }
124
125     if (!(ret = InterlockedCompareExchangePointer( (void **)&special_root_keys[idx], hkey, 0 )))
126         ret = hkey;
127     else
128         NtClose( hkey );  /* somebody beat us to it */
129     return ret;
130 }
131
132 /* map the hkey from special root to normal key if necessary */
133 inline static HKEY get_special_root_hkey( HKEY hkey )
134 {
135     HKEY ret = hkey;
136
137     if ((hkey >= HKEY_SPECIAL_ROOT_FIRST) && (hkey <= HKEY_SPECIAL_ROOT_LAST))
138     {
139         if (!(ret = special_root_keys[(UINT)hkey - (UINT)HKEY_SPECIAL_ROOT_FIRST]))
140             ret = create_special_root_hkey( hkey, KEY_ALL_ACCESS );
141     }
142     return ret;
143 }
144
145
146 /******************************************************************************
147  *           RegCreateKeyExW   [ADVAPI32.@]
148  *
149  * PARAMS
150  *    hkey       [I] Handle of an open key
151  *    name       [I] Address of subkey name
152  *    reserved   [I] Reserved - must be 0
153  *    class      [I] Address of class string
154  *    options    [I] Special options flag
155  *    access     [I] Desired security access
156  *    sa         [I] Address of key security structure
157  *    retkey     [O] Address of buffer for opened handle
158  *    dispos     [O] Receives REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY
159  *
160  * NOTES
161  *  in case of failing retkey remains untouched
162  *
163  * FIXME MAXIMUM_ALLOWED in access mask not supported by server
164  */
165 DWORD WINAPI RegCreateKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, LPCWSTR class,
166                               DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
167                               PHKEY retkey, LPDWORD dispos )
168 {
169     OBJECT_ATTRIBUTES attr;
170     UNICODE_STRING nameW, classW;
171
172     if (reserved) return ERROR_INVALID_PARAMETER;
173     if (!(access & KEY_ACCESS_MASK) || (access & ~KEY_ACCESS_MASK)) return ERROR_ACCESS_DENIED;
174     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
175
176     attr.Length = sizeof(attr);
177     attr.RootDirectory = hkey;
178     attr.ObjectName = &nameW;
179     attr.Attributes = 0;
180     attr.SecurityDescriptor = NULL;
181     attr.SecurityQualityOfService = NULL;
182     RtlInitUnicodeString( &nameW, name );
183     RtlInitUnicodeString( &classW, class );
184
185     return RtlNtStatusToDosError( NtCreateKey( retkey, access, &attr, 0,
186                                                &classW, options, dispos ) );
187 }
188
189
190 /******************************************************************************
191  *           RegCreateKeyExA   [ADVAPI32.@]
192  *
193  * FIXME MAXIMUM_ALLOWED in access mask not supported by server
194  */
195 DWORD WINAPI RegCreateKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, LPCSTR class,
196                               DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
197                               PHKEY retkey, LPDWORD dispos )
198 {
199     OBJECT_ATTRIBUTES attr;
200     UNICODE_STRING classW;
201     ANSI_STRING nameA, classA;
202     NTSTATUS status;
203
204     if (reserved) return ERROR_INVALID_PARAMETER;
205     if (!is_version_nt()) access = KEY_ALL_ACCESS;  /* Win95 ignores the access mask */
206     else if (!(access & KEY_ACCESS_MASK) || (access & ~KEY_ACCESS_MASK)) return ERROR_ACCESS_DENIED;
207     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
208
209     attr.Length = sizeof(attr);
210     attr.RootDirectory = hkey;
211     attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
212     attr.Attributes = 0;
213     attr.SecurityDescriptor = NULL;
214     attr.SecurityQualityOfService = NULL;
215     RtlInitAnsiString( &nameA, name );
216     RtlInitAnsiString( &classA, class );
217
218     if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
219                                                  &nameA, FALSE )))
220     {
221         if (!(status = RtlAnsiStringToUnicodeString( &classW, &classA, TRUE )))
222         {
223             status = NtCreateKey( retkey, access, &attr, 0, &classW, options, dispos );
224             RtlFreeUnicodeString( &classW );
225         }
226     }
227     return RtlNtStatusToDosError( status );
228 }
229
230
231 /******************************************************************************
232  *           RegCreateKeyW   [ADVAPI32.@]
233  */
234 DWORD WINAPI RegCreateKeyW( HKEY hkey, LPCWSTR name, PHKEY retkey )
235 {
236     /* FIXME: previous implementation converted ERROR_INVALID_HANDLE to ERROR_BADKEY, */
237     /* but at least my version of NT (4.0 SP5) doesn't do this.  -- AJ */
238     return RegCreateKeyExW( hkey, name, 0, NULL, REG_OPTION_NON_VOLATILE,
239                             KEY_ALL_ACCESS, NULL, retkey, NULL );
240 }
241
242
243 /******************************************************************************
244  *           RegCreateKeyA   [ADVAPI32.@]
245  */
246 DWORD WINAPI RegCreateKeyA( HKEY hkey, LPCSTR name, PHKEY retkey )
247 {
248     return RegCreateKeyExA( hkey, name, 0, NULL, REG_OPTION_NON_VOLATILE,
249                             KEY_ALL_ACCESS, NULL, retkey, NULL );
250 }
251
252
253
254 /******************************************************************************
255  *           RegOpenKeyExW   [ADVAPI32.@]
256  *
257  * Opens the specified key
258  *
259  * Unlike RegCreateKeyEx, this does not create the key if it does not exist.
260  *
261  * PARAMS
262  *    hkey       [I] Handle of open key
263  *    name       [I] Name of subkey to open
264  *    reserved   [I] Reserved - must be zero
265  *    access     [I] Security access mask
266  *    retkey     [O] Handle to open key
267  *
268  * RETURNS
269  *    Success: ERROR_SUCCESS
270  *    Failure: Error code
271  *
272  * NOTES
273  *  in case of failing is retkey = 0
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( retkey, access, &attr ) );
290 }
291
292
293 /******************************************************************************
294  *           RegOpenKeyExA   [ADVAPI32.@]
295  */
296 DWORD WINAPI RegOpenKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, REGSAM access, PHKEY retkey )
297 {
298     OBJECT_ATTRIBUTES attr;
299     STRING nameA;
300     NTSTATUS status;
301
302     if (!is_version_nt()) access = KEY_ALL_ACCESS;  /* Win95 ignores the access mask */
303
304     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
305
306     attr.Length = sizeof(attr);
307     attr.RootDirectory = hkey;
308     attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
309     attr.Attributes = 0;
310     attr.SecurityDescriptor = NULL;
311     attr.SecurityQualityOfService = NULL;
312
313     RtlInitAnsiString( &nameA, name );
314     if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
315                                                  &nameA, FALSE )))
316     {
317         status = NtOpenKey( retkey, access, &attr );
318     }
319     return RtlNtStatusToDosError( status );
320 }
321
322
323 /******************************************************************************
324  *           RegOpenKeyW   [ADVAPI32.@]
325  *
326  * PARAMS
327  *    hkey    [I] Handle of open key
328  *    name    [I] Address of name of subkey to open
329  *    retkey  [O] Handle to open key
330  *
331  * RETURNS
332  *    Success: ERROR_SUCCESS
333  *    Failure: Error code
334  *
335  * NOTES
336  *  in case of failing is retkey = 0
337  */
338 DWORD WINAPI RegOpenKeyW( HKEY hkey, LPCWSTR name, PHKEY retkey )
339 {
340     return RegOpenKeyExW( hkey, name, 0, KEY_ALL_ACCESS, retkey );
341 }
342
343
344 /******************************************************************************
345  *           RegOpenKeyA   [ADVAPI32.@]
346  */
347 DWORD WINAPI RegOpenKeyA( HKEY hkey, LPCSTR name, PHKEY retkey )
348 {
349     return RegOpenKeyExA( hkey, name, 0, KEY_ALL_ACCESS, retkey );
350 }
351
352
353 /******************************************************************************
354  *           RegOpenCurrentUser   [ADVAPI32.@]
355  * FIXME: This function is supposed to retrieve a handle to the
356  * HKEY_CURRENT_USER for the user the current thread is impersonating.
357  * Since Wine does not currently allow threads to impersonate other users,
358  * this stub should work fine.
359  */
360 DWORD WINAPI RegOpenCurrentUser( REGSAM access, PHKEY retkey )
361 {
362     return RegOpenKeyExA( HKEY_CURRENT_USER, "", 0, access, retkey );
363 }
364
365
366
367 /******************************************************************************
368  *           RegEnumKeyExW   [ADVAPI32.@]
369  *
370  * PARAMS
371  *    hkey         [I] Handle to key to enumerate
372  *    index        [I] Index of subkey to enumerate
373  *    name         [O] Buffer for subkey name
374  *    name_len     [O] Size of subkey buffer
375  *    reserved     [I] Reserved
376  *    class        [O] Buffer for class string
377  *    class_len    [O] Size of class buffer
378  *    ft           [O] Time key last written to
379  */
380 DWORD WINAPI RegEnumKeyExW( HKEY hkey, DWORD index, LPWSTR name, LPDWORD name_len,
381                             LPDWORD reserved, LPWSTR class, LPDWORD class_len, FILETIME *ft )
382 {
383     NTSTATUS status;
384     char buffer[256], *buf_ptr = buffer;
385     KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
386     DWORD total_size;
387
388     TRACE( "(%p,%ld,%p,%p(%ld),%p,%p,%p,%p)\n", hkey, index, name, name_len,
389            name_len ? *name_len : -1, reserved, class, class_len, ft );
390
391     if (reserved) return ERROR_INVALID_PARAMETER;
392     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
393
394     status = NtEnumerateKey( hkey, index, KeyNodeInformation,
395                              buffer, sizeof(buffer), &total_size );
396
397     while (status == STATUS_BUFFER_OVERFLOW)
398     {
399         /* retry with a dynamically allocated buffer */
400         if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
401         if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
402             return ERROR_NOT_ENOUGH_MEMORY;
403         info = (KEY_NODE_INFORMATION *)buf_ptr;
404         status = NtEnumerateKey( hkey, index, KeyNodeInformation,
405                                  buf_ptr, total_size, &total_size );
406     }
407
408     if (!status)
409     {
410         DWORD len = info->NameLength / sizeof(WCHAR);
411         DWORD cls_len = info->ClassLength / sizeof(WCHAR);
412
413         if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
414
415         if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
416             status = STATUS_BUFFER_OVERFLOW;
417         else
418         {
419             *name_len = len;
420             memcpy( name, info->Name, info->NameLength );
421             name[len] = 0;
422             if (class_len)
423             {
424                 *class_len = cls_len;
425                 if (class)
426                 {
427                     memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
428                     class[cls_len] = 0;
429                 }
430             }
431         }
432     }
433
434     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
435     return RtlNtStatusToDosError( status );
436 }
437
438
439 /******************************************************************************
440  *           RegEnumKeyExA   [ADVAPI32.@]
441  */
442 DWORD WINAPI RegEnumKeyExA( HKEY hkey, DWORD index, LPSTR name, LPDWORD name_len,
443                             LPDWORD reserved, LPSTR class, LPDWORD class_len, FILETIME *ft )
444 {
445     NTSTATUS status;
446     char buffer[256], *buf_ptr = buffer;
447     KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
448     DWORD total_size;
449
450     TRACE( "(%p,%ld,%p,%p(%ld),%p,%p,%p,%p)\n", hkey, index, name, name_len,
451            name_len ? *name_len : -1, reserved, class, class_len, ft );
452
453     if (reserved) return ERROR_INVALID_PARAMETER;
454     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
455
456     status = NtEnumerateKey( hkey, index, KeyNodeInformation,
457                              buffer, sizeof(buffer), &total_size );
458
459     while (status == STATUS_BUFFER_OVERFLOW)
460     {
461         /* retry with a dynamically allocated buffer */
462         if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
463         if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
464             return ERROR_NOT_ENOUGH_MEMORY;
465         info = (KEY_NODE_INFORMATION *)buf_ptr;
466         status = NtEnumerateKey( hkey, index, KeyNodeInformation,
467                                  buf_ptr, total_size, &total_size );
468     }
469
470     if (!status)
471     {
472         DWORD len, cls_len;
473
474         RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
475         RtlUnicodeToMultiByteSize( &cls_len, (WCHAR *)(buf_ptr + info->ClassOffset),
476                                    info->ClassLength );
477         if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
478
479         if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
480             status = STATUS_BUFFER_OVERFLOW;
481         else
482         {
483             *name_len = len;
484             RtlUnicodeToMultiByteN( name, len, NULL, info->Name, info->NameLength );
485             name[len] = 0;
486             if (class_len)
487             {
488                 *class_len = cls_len;
489                 if (class)
490                 {
491                     RtlUnicodeToMultiByteN( class, cls_len, NULL,
492                                             (WCHAR *)(buf_ptr + info->ClassOffset),
493                                             info->ClassLength );
494                     class[cls_len] = 0;
495                 }
496             }
497         }
498     }
499
500     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
501     return RtlNtStatusToDosError( status );
502 }
503
504
505 /******************************************************************************
506  *           RegEnumKeyW   [ADVAPI32.@]
507  */
508 DWORD WINAPI RegEnumKeyW( HKEY hkey, DWORD index, LPWSTR name, DWORD name_len )
509 {
510     return RegEnumKeyExW( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
511 }
512
513
514 /******************************************************************************
515  *           RegEnumKeyA   [ADVAPI32.@]
516  */
517 DWORD WINAPI RegEnumKeyA( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
518 {
519     return RegEnumKeyExA( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
520 }
521
522
523 /******************************************************************************
524  *           RegQueryInfoKeyW   [ADVAPI32.@]
525  *
526  * PARAMS
527  *    hkey       [I] Handle to key to query
528  *    class      [O] Buffer for class string
529  *    class_len  [O] Size of class string buffer
530  *    reserved   [I] Reserved
531  *    subkeys    [O] Buffer for number of subkeys
532  *    max_subkey [O] Buffer for longest subkey name length
533  *    max_class  [O] Buffer for longest class string length
534  *    values     [O] Buffer for number of value entries
535  *    max_value  [O] Buffer for longest value name length
536  *    max_data   [O] Buffer for longest value data length
537  *    security   [O] Buffer for security descriptor length
538  *    modif      [O] Modification time
539  *
540  * - win95 allows class to be valid and class_len to be NULL
541  * - winnt returns ERROR_INVALID_PARAMETER if class is valid and class_len is NULL
542  * - both allow class to be NULL and class_len to be NULL
543  * (it's hard to test validity, so test !NULL instead)
544  */
545 DWORD WINAPI RegQueryInfoKeyW( HKEY hkey, LPWSTR class, LPDWORD class_len, LPDWORD reserved,
546                                LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
547                                LPDWORD values, LPDWORD max_value, LPDWORD max_data,
548                                LPDWORD security, FILETIME *modif )
549 {
550     NTSTATUS status;
551     char buffer[256], *buf_ptr = buffer;
552     KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
553     DWORD total_size;
554
555     TRACE( "(%p,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
556            reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
557
558     if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
559     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
560
561     status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
562     if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
563
564     if (class)
565     {
566         /* retry with a dynamically allocated buffer */
567         while (status == STATUS_BUFFER_OVERFLOW)
568         {
569             if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
570             if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
571                 return ERROR_NOT_ENOUGH_MEMORY;
572             info = (KEY_FULL_INFORMATION *)buf_ptr;
573             status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
574         }
575
576         if (status) goto done;
577
578         if (class_len && (info->ClassLength/sizeof(WCHAR) + 1 > *class_len))
579         {
580             status = STATUS_BUFFER_OVERFLOW;
581         }
582         else
583         {
584             memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
585             class[info->ClassLength/sizeof(WCHAR)] = 0;
586         }
587     }
588     else status = STATUS_SUCCESS;
589
590     if (class_len) *class_len = info->ClassLength / sizeof(WCHAR);
591     if (subkeys) *subkeys = info->SubKeys;
592     if (max_subkey) *max_subkey = info->MaxNameLen;
593     if (max_class) *max_class = info->MaxClassLen;
594     if (values) *values = info->Values;
595     if (max_value) *max_value = info->MaxValueNameLen;
596     if (max_data) *max_data = info->MaxValueDataLen;
597     if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
598
599  done:
600     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
601     return RtlNtStatusToDosError( status );
602 }
603
604
605 /******************************************************************************
606  *           RegQueryMultipleValuesA   [ADVAPI32.@]
607  */
608 DWORD WINAPI RegQueryMultipleValuesA(HKEY hkey, PVALENTA val_list, DWORD num_vals,
609                                      LPSTR lpValueBuf, LPDWORD ldwTotsize)
610 {
611     int i;
612     DWORD maxBytes = *ldwTotsize;
613     HRESULT status;
614     LPSTR bufptr = lpValueBuf;
615     *ldwTotsize = 0;
616
617     TRACE("(%p,%p,%ld,%p,%p=%ld)\n", hkey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
618
619     for(i=0; i < num_vals; ++i)
620     {
621
622         val_list[i].ve_valuelen=0;
623         status = RegQueryValueExA(hkey, val_list[i].ve_valuename, NULL, NULL, NULL, &val_list[i].ve_valuelen);
624         if(status != ERROR_SUCCESS)
625         {
626             return status;
627         }
628
629         if(lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
630         {
631             status = RegQueryValueExA(hkey, val_list[i].ve_valuename, NULL, &val_list[i].ve_type,
632                                       bufptr, &val_list[i].ve_valuelen);
633             if(status != ERROR_SUCCESS)
634             {
635                 return status;
636             }
637
638             val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
639
640             bufptr += val_list[i].ve_valuelen;
641         }
642
643         *ldwTotsize += val_list[i].ve_valuelen;
644     }
645     return lpValueBuf != NULL && *ldwTotsize <= maxBytes ? ERROR_SUCCESS : ERROR_MORE_DATA;
646 }
647
648
649 /******************************************************************************
650  *           RegQueryMultipleValuesW   [ADVAPI32.@]
651  */
652 DWORD WINAPI RegQueryMultipleValuesW(HKEY hkey, PVALENTW val_list, DWORD num_vals,
653                                      LPWSTR lpValueBuf, LPDWORD ldwTotsize)
654 {
655     int i;
656     DWORD maxBytes = *ldwTotsize;
657     HRESULT status;
658     LPSTR bufptr = (LPSTR)lpValueBuf;
659     *ldwTotsize = 0;
660
661     TRACE("(%p,%p,%ld,%p,%p=%ld)\n", hkey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
662
663     for(i=0; i < num_vals; ++i)
664     {
665         val_list[i].ve_valuelen=0;
666         status = RegQueryValueExW(hkey, val_list[i].ve_valuename, NULL, NULL, NULL, &val_list[i].ve_valuelen);
667         if(status != ERROR_SUCCESS)
668         {
669             return status;
670         }
671
672         if(lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
673         {
674             status = RegQueryValueExW(hkey, val_list[i].ve_valuename, NULL, &val_list[i].ve_type,
675                                       bufptr, &val_list[i].ve_valuelen);
676             if(status != ERROR_SUCCESS)
677             {
678                 return status;
679             }
680
681             val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
682
683             bufptr += val_list[i].ve_valuelen;
684         }
685
686         *ldwTotsize += val_list[i].ve_valuelen;
687     }
688     return lpValueBuf != NULL && *ldwTotsize <= maxBytes ? ERROR_SUCCESS : ERROR_MORE_DATA;
689 }
690
691 /******************************************************************************
692  *           RegQueryInfoKeyA   [ADVAPI32.@]
693  */
694 DWORD WINAPI RegQueryInfoKeyA( HKEY hkey, LPSTR class, LPDWORD class_len, LPDWORD reserved,
695                                LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
696                                LPDWORD values, LPDWORD max_value, LPDWORD max_data,
697                                LPDWORD security, FILETIME *modif )
698 {
699     NTSTATUS status;
700     char buffer[256], *buf_ptr = buffer;
701     KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
702     DWORD total_size, len;
703
704     TRACE( "(%p,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
705            reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
706
707     if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
708     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
709
710     status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
711     if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
712
713     if (class || class_len)
714     {
715         /* retry with a dynamically allocated buffer */
716         while (status == STATUS_BUFFER_OVERFLOW)
717         {
718             if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
719             if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
720                 return ERROR_NOT_ENOUGH_MEMORY;
721             info = (KEY_FULL_INFORMATION *)buf_ptr;
722             status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
723         }
724
725         if (status) goto done;
726
727         RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->ClassOffset), info->ClassLength);
728         if (class_len)
729         {
730             if (len + 1 > *class_len) status = STATUS_BUFFER_OVERFLOW;
731             *class_len = len;
732         }
733         if (class && !status)
734         {
735             RtlUnicodeToMultiByteN( class, len, NULL, (WCHAR *)(buf_ptr + info->ClassOffset),
736                                     info->ClassLength );
737             class[len] = 0;
738         }
739     }
740     else status = STATUS_SUCCESS;
741
742     if (subkeys) *subkeys = info->SubKeys;
743     if (max_subkey) *max_subkey = info->MaxNameLen;
744     if (max_class) *max_class = info->MaxClassLen;
745     if (values) *values = info->Values;
746     if (max_value) *max_value = info->MaxValueNameLen;
747     if (max_data) *max_data = info->MaxValueDataLen;
748     if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
749
750  done:
751     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
752     return RtlNtStatusToDosError( status );
753 }
754
755
756 /******************************************************************************
757  *           RegCloseKey   [ADVAPI32.@]
758  *
759  * Releases the handle of the specified key
760  *
761  * PARAMS
762  *    hkey [I] Handle of key to close
763  *
764  * RETURNS
765  *    Success: ERROR_SUCCESS
766  *    Failure: Error code
767  */
768 DWORD WINAPI RegCloseKey( HKEY hkey )
769 {
770     if (!hkey || hkey >= (HKEY)0x80000000) return ERROR_SUCCESS;
771     return RtlNtStatusToDosError( NtClose( hkey ) );
772 }
773
774
775 /******************************************************************************
776  *           RegDeleteKeyW   [ADVAPI32.@]
777  *
778  * PARAMS
779  *    hkey   [I] Handle to open key
780  *    name   [I] Name of subkey to delete
781  *
782  * RETURNS
783  *    Success: ERROR_SUCCESS
784  *    Failure: Error code
785  */
786 DWORD WINAPI RegDeleteKeyW( HKEY hkey, LPCWSTR name )
787 {
788     DWORD ret;
789     HKEY tmp;
790
791     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
792
793     if (!name || !*name)
794     {
795         ret = RtlNtStatusToDosError( NtDeleteKey( hkey ) );
796     }
797     else if (!(ret = RegOpenKeyExW( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
798     {
799         if (!is_version_nt()) /* win95 does recursive key deletes */
800         {
801             WCHAR name[MAX_PATH];
802
803             while(!RegEnumKeyW(tmp, 0, name, sizeof(name)))
804             {
805                 if(RegDeleteKeyW(tmp, name))  /* recurse */
806                     break;
807             }
808         }
809         ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
810         RegCloseKey( tmp );
811     }
812     TRACE("%s ret=%08lx\n", debugstr_w(name), ret);
813     return ret;
814 }
815
816
817 /******************************************************************************
818  *           RegDeleteKeyA   [ADVAPI32.@]
819  */
820 DWORD WINAPI RegDeleteKeyA( HKEY hkey, LPCSTR name )
821 {
822     DWORD ret;
823     HKEY tmp;
824
825     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
826
827     if (!name || !*name)
828     {
829         ret = RtlNtStatusToDosError( NtDeleteKey( hkey ) );
830     }
831     else if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
832     {
833         if (!is_version_nt()) /* win95 does recursive key deletes */
834         {
835             CHAR name[MAX_PATH];
836
837             while(!RegEnumKeyA(tmp, 0, name, sizeof(name)))
838             {
839                 if(RegDeleteKeyA(tmp, name))  /* recurse */
840                     break;
841             }
842         }
843         ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
844         RegCloseKey( tmp );
845     }
846     TRACE("%s ret=%08lx\n", debugstr_a(name), ret);
847     return ret;
848 }
849
850
851
852 /******************************************************************************
853  *           RegSetValueExW   [ADVAPI32.@]
854  *
855  * Sets the data and type of a value under a register key
856  *
857  * PARAMS
858  *    hkey       [I] Handle of key to set value for
859  *    name       [I] Name of value to set
860  *    reserved   [I] Reserved - must be zero
861  *    type       [I] Flag for value type
862  *    data       [I] Address of value data
863  *    count      [I] Size of value data
864  *
865  * RETURNS
866  *    Success: ERROR_SUCCESS
867  *    Failure: Error code
868  *
869  * NOTES
870  *   win95 does not care about count for REG_SZ and finds out the len by itself (js)
871  *   NT does definitely care (aj)
872  */
873 DWORD WINAPI RegSetValueExW( HKEY hkey, LPCWSTR name, DWORD reserved,
874                              DWORD type, CONST BYTE *data, DWORD count )
875 {
876     UNICODE_STRING nameW;
877
878     if (!is_version_nt())  /* win95 */
879     {
880         if (type == REG_SZ) count = (strlenW( (WCHAR *)data ) + 1) * sizeof(WCHAR);
881     }
882     else if (count && is_string(type))
883     {
884         LPCWSTR str = (LPCWSTR)data;
885         /* if user forgot to count terminating null, add it (yes NT does this) */
886         if (str[count / sizeof(WCHAR) - 1] && !str[count / sizeof(WCHAR)])
887             count += sizeof(WCHAR);
888     }
889     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
890
891     RtlInitUnicodeString( &nameW, name );
892     return RtlNtStatusToDosError( NtSetValueKey( hkey, &nameW, 0, type, data, count ) );
893 }
894
895
896 /******************************************************************************
897  *           RegSetValueExA   [ADVAPI32.@]
898  */
899 DWORD WINAPI RegSetValueExA( HKEY hkey, LPCSTR name, DWORD reserved, DWORD type,
900                              CONST BYTE *data, DWORD count )
901 {
902     ANSI_STRING nameA;
903     WCHAR *dataW = NULL;
904     NTSTATUS status;
905
906     if (!is_version_nt())  /* win95 */
907     {
908         if (type == REG_SZ) count = strlen(data) + 1;
909     }
910     else if (count && is_string(type))
911     {
912         /* if user forgot to count terminating null, add it (yes NT does this) */
913         if (data[count-1] && !data[count]) count++;
914     }
915
916     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
917
918     if (is_string( type )) /* need to convert to Unicode */
919     {
920         DWORD lenW;
921         RtlMultiByteToUnicodeSize( &lenW, data, count );
922         if (!(dataW = HeapAlloc( GetProcessHeap(), 0, lenW ))) return ERROR_OUTOFMEMORY;
923         RtlMultiByteToUnicodeN( dataW, lenW, NULL, data, count );
924         count = lenW;
925         data = (BYTE *)dataW;
926     }
927
928     RtlInitAnsiString( &nameA, name );
929     if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
930                                                  &nameA, FALSE )))
931     {
932         status = NtSetValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString, 0, type, data, count );
933     }
934     if (dataW) HeapFree( GetProcessHeap(), 0, dataW );
935     return RtlNtStatusToDosError( status );
936 }
937
938
939 /******************************************************************************
940  *           RegSetValueW   [ADVAPI32.@]
941  */
942 DWORD WINAPI RegSetValueW( HKEY hkey, LPCWSTR name, DWORD type, LPCWSTR data, DWORD count )
943 {
944     HKEY subkey = hkey;
945     DWORD ret;
946
947     TRACE("(%p,%s,%ld,%s,%ld)\n", hkey, debugstr_w(name), type, debugstr_w(data), count );
948
949     if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
950
951     if (name && name[0])  /* need to create the subkey */
952     {
953         if ((ret = RegCreateKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
954     }
955
956     ret = RegSetValueExW( subkey, NULL, 0, REG_SZ, (LPBYTE)data,
957                           (strlenW( data ) + 1) * sizeof(WCHAR) );
958     if (subkey != hkey) RegCloseKey( subkey );
959     return ret;
960 }
961
962
963 /******************************************************************************
964  *           RegSetValueA   [ADVAPI32.@]
965  */
966 DWORD WINAPI RegSetValueA( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
967 {
968     HKEY subkey = hkey;
969     DWORD ret;
970
971     TRACE("(%p,%s,%ld,%s,%ld)\n", hkey, debugstr_a(name), type, debugstr_a(data), count );
972
973     if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
974
975     if (name && name[0])  /* need to create the subkey */
976     {
977         if ((ret = RegCreateKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
978     }
979     ret = RegSetValueExA( subkey, NULL, 0, REG_SZ, (LPBYTE)data, strlen(data)+1 );
980     if (subkey != hkey) RegCloseKey( subkey );
981     return ret;
982 }
983
984
985
986 /******************************************************************************
987  *           RegQueryValueExW   [ADVAPI32.@]
988  *
989  * Retrieves type and data for a specified name associated with an open key
990  *
991  * PARAMS
992  *    hkey      [I]   Handle of key to query
993  *    name      [I]   Name of value to query
994  *    reserved  [I]   Reserved - must be NULL
995  *    type      [O]   Address of buffer for value type.  If NULL, the type
996  *                        is not required.
997  *    data      [O]   Address of data buffer.  If NULL, the actual data is
998  *                        not required.
999  *    count     [I/O] Address of data buffer size
1000  *
1001  * RETURNS
1002  *    ERROR_SUCCESS:   Success
1003  *    ERROR_MORE_DATA: !!! if the specified buffer is not big enough to hold the data
1004  *                     buffer is left untouched. The MS-documentation is wrong (js) !!!
1005  */
1006 DWORD WINAPI RegQueryValueExW( HKEY hkey, LPCWSTR name, LPDWORD reserved, LPDWORD type,
1007                                LPBYTE data, LPDWORD count )
1008 {
1009     NTSTATUS status;
1010     UNICODE_STRING name_str;
1011     DWORD total_size;
1012     char buffer[256], *buf_ptr = buffer;
1013     KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
1014     static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
1015
1016     TRACE("(%p,%s,%p,%p,%p,%p=%ld)\n",
1017           hkey, debugstr_w(name), reserved, type, data, count, count ? *count : 0 );
1018
1019     if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1020     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1021
1022     RtlInitUnicodeString( &name_str, name );
1023
1024     if (data) total_size = min( sizeof(buffer), *count + info_size );
1025     else total_size = info_size;
1026
1027     status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1028                               buffer, total_size, &total_size );
1029     if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1030
1031     if (data)
1032     {
1033         /* retry with a dynamically allocated buffer */
1034         while (status == STATUS_BUFFER_OVERFLOW && total_size - info_size <= *count)
1035         {
1036             if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1037             if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1038                 return ERROR_NOT_ENOUGH_MEMORY;
1039             info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1040             status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1041                                       buf_ptr, total_size, &total_size );
1042         }
1043
1044         if (!status)
1045         {
1046             memcpy( data, buf_ptr + info_size, total_size - info_size );
1047             /* if the type is REG_SZ and data is not 0-terminated
1048              * and there is enough space in the buffer NT appends a \0 */
1049             if (total_size - info_size <= *count-sizeof(WCHAR) && is_string(info->Type))
1050             {
1051                 WCHAR *ptr = (WCHAR *)(data + total_size - info_size);
1052                 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
1053             }
1054         }
1055         else if (status != STATUS_BUFFER_OVERFLOW) goto done;
1056     }
1057     else status = STATUS_SUCCESS;
1058
1059     if (type) *type = info->Type;
1060     if (count) *count = total_size - info_size;
1061
1062  done:
1063     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1064     return RtlNtStatusToDosError(status);
1065 }
1066
1067
1068 /******************************************************************************
1069  *           RegQueryValueExA   [ADVAPI32.@]
1070  *
1071  * NOTES:
1072  * the documentation is wrong: if the buffer is too small it remains untouched
1073  */
1074 DWORD WINAPI RegQueryValueExA( HKEY hkey, LPCSTR name, LPDWORD reserved, LPDWORD type,
1075                                LPBYTE data, LPDWORD count )
1076 {
1077     NTSTATUS status;
1078     ANSI_STRING nameA;
1079     DWORD total_size;
1080     char buffer[256], *buf_ptr = buffer;
1081     KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
1082     static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
1083
1084     TRACE("(%p,%s,%p,%p,%p,%p=%ld)\n",
1085           hkey, debugstr_a(name), reserved, type, data, count, count ? *count : 0 );
1086
1087     if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1088     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1089
1090     RtlInitAnsiString( &nameA, name );
1091     if ((status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
1092                                                 &nameA, FALSE )))
1093         return RtlNtStatusToDosError(status);
1094
1095     status = NtQueryValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString,
1096                               KeyValuePartialInformation, buffer, sizeof(buffer), &total_size );
1097     if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1098
1099     /* we need to fetch the contents for a string type even if not requested,
1100      * because we need to compute the length of the ASCII string. */
1101     if (data || is_string(info->Type))
1102     {
1103         /* retry with a dynamically allocated buffer */
1104         while (status == STATUS_BUFFER_OVERFLOW)
1105         {
1106             if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1107             if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1108             {
1109                 status = STATUS_NO_MEMORY;
1110                 goto done;
1111             }
1112             info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1113             status = NtQueryValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString,
1114                                     KeyValuePartialInformation, buf_ptr, total_size, &total_size );
1115         }
1116
1117         if (status) goto done;
1118
1119         if (is_string(info->Type))
1120         {
1121             DWORD len;
1122
1123             RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info_size),
1124                                        total_size - info_size );
1125             if (data && len)
1126             {
1127                 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
1128                 else
1129                 {
1130                     RtlUnicodeToMultiByteN( data, len, NULL, (WCHAR *)(buf_ptr + info_size),
1131                                             total_size - info_size );
1132                     /* if the type is REG_SZ and data is not 0-terminated
1133                      * and there is enough space in the buffer NT appends a \0 */
1134                     if (len < *count && data[len-1]) data[len] = 0;
1135                 }
1136             }
1137             total_size = len + info_size;
1138         }
1139         else if (data)
1140         {
1141             if (total_size - info_size > *count) status = STATUS_BUFFER_OVERFLOW;
1142             else memcpy( data, buf_ptr + info_size, total_size - info_size );
1143         }
1144     }
1145     else status = STATUS_SUCCESS;
1146
1147     if (type) *type = info->Type;
1148     if (count) *count = total_size - info_size;
1149
1150  done:
1151     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1152     return RtlNtStatusToDosError(status);
1153 }
1154
1155
1156 /******************************************************************************
1157  *           RegQueryValueW   [ADVAPI32.@]
1158  */
1159 DWORD WINAPI RegQueryValueW( HKEY hkey, LPCWSTR name, LPWSTR data, LPLONG count )
1160 {
1161     DWORD ret;
1162     HKEY subkey = hkey;
1163
1164     TRACE("(%p,%s,%p,%ld)\n", hkey, debugstr_w(name), data, count ? *count : 0 );
1165
1166     if (name && name[0])
1167     {
1168         if ((ret = RegOpenKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1169     }
1170     ret = RegQueryValueExW( subkey, NULL, NULL, NULL, (LPBYTE)data, count );
1171     if (subkey != hkey) RegCloseKey( subkey );
1172     if (ret == ERROR_FILE_NOT_FOUND)
1173     {
1174         /* return empty string if default value not found */
1175         if (data) *data = 0;
1176         if (count) *count = 1;
1177         ret = ERROR_SUCCESS;
1178     }
1179     return ret;
1180 }
1181
1182
1183 /******************************************************************************
1184  *           RegQueryValueA   [ADVAPI32.@]
1185  */
1186 DWORD WINAPI RegQueryValueA( HKEY hkey, LPCSTR name, LPSTR data, LPLONG count )
1187 {
1188     DWORD ret;
1189     HKEY subkey = hkey;
1190
1191     TRACE("(%p,%s,%p,%ld)\n", hkey, debugstr_a(name), data, count ? *count : 0 );
1192
1193     if (name && name[0])
1194     {
1195         if ((ret = RegOpenKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1196     }
1197     ret = RegQueryValueExA( subkey, NULL, NULL, NULL, (LPBYTE)data, count );
1198     if (subkey != hkey) RegCloseKey( subkey );
1199     if (ret == ERROR_FILE_NOT_FOUND)
1200     {
1201         /* return empty string if default value not found */
1202         if (data) *data = 0;
1203         if (count) *count = 1;
1204         ret = ERROR_SUCCESS;
1205     }
1206     return ret;
1207 }
1208
1209
1210 /******************************************************************************
1211  *           RegEnumValueW   [ADVAPI32.@]
1212  *
1213  * PARAMS
1214  *    hkey       [I] Handle to key to query
1215  *    index      [I] Index of value to query
1216  *    value      [O] Value string
1217  *    val_count  [I/O] Size of value buffer (in wchars)
1218  *    reserved   [I] Reserved
1219  *    type       [O] Type code
1220  *    data       [O] Value data
1221  *    count      [I/O] Size of data buffer (in bytes)
1222  */
1223
1224 DWORD WINAPI RegEnumValueW( HKEY hkey, DWORD index, LPWSTR value, LPDWORD val_count,
1225                             LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1226 {
1227     NTSTATUS status;
1228     DWORD total_size;
1229     char buffer[256], *buf_ptr = buffer;
1230     KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
1231     static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
1232
1233     TRACE("(%p,%ld,%p,%p,%p,%p,%p,%p)\n",
1234           hkey, index, value, val_count, reserved, type, data, count );
1235
1236     /* NT only checks count, not val_count */
1237     if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1238     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1239
1240     total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
1241     if (data) total_size += *count;
1242     total_size = min( sizeof(buffer), total_size );
1243
1244     status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1245                                   buffer, total_size, &total_size );
1246     if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1247
1248     if (value || data)
1249     {
1250         /* retry with a dynamically allocated buffer */
1251         while (status == STATUS_BUFFER_OVERFLOW)
1252         {
1253             if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1254             if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1255                 return ERROR_NOT_ENOUGH_MEMORY;
1256             info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
1257             status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1258                                           buf_ptr, total_size, &total_size );
1259         }
1260
1261         if (status) goto done;
1262
1263         if (value)
1264         {
1265             if (info->NameLength/sizeof(WCHAR) >= *val_count)
1266             {
1267                 status = STATUS_BUFFER_OVERFLOW;
1268                 goto overflow;
1269             }
1270             memcpy( value, info->Name, info->NameLength );
1271             *val_count = info->NameLength / sizeof(WCHAR);
1272             value[*val_count] = 0;
1273         }
1274
1275         if (data)
1276         {
1277             if (total_size - info->DataOffset > *count)
1278             {
1279                 status = STATUS_BUFFER_OVERFLOW;
1280                 goto overflow;
1281             }
1282             memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
1283             if (total_size - info->DataOffset <= *count-sizeof(WCHAR) && is_string(info->Type))
1284             {
1285                 /* if the type is REG_SZ and data is not 0-terminated
1286                  * and there is enough space in the buffer NT appends a \0 */
1287                 WCHAR *ptr = (WCHAR *)(data + total_size - info->DataOffset);
1288                 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
1289             }
1290         }
1291     }
1292     else status = STATUS_SUCCESS;
1293
1294  overflow:
1295     if (type) *type = info->Type;
1296     if (count) *count = info->DataLength;
1297
1298  done:
1299     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1300     return RtlNtStatusToDosError(status);
1301 }
1302
1303
1304 /******************************************************************************
1305  *           RegEnumValueA   [ADVAPI32.@]
1306  */
1307 DWORD WINAPI RegEnumValueA( HKEY hkey, DWORD index, LPSTR value, LPDWORD val_count,
1308                             LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1309 {
1310     NTSTATUS status;
1311     DWORD total_size;
1312     char buffer[256], *buf_ptr = buffer;
1313     KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
1314     static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
1315
1316     TRACE("(%p,%ld,%p,%p,%p,%p,%p,%p)\n",
1317           hkey, index, value, val_count, reserved, type, data, count );
1318
1319     /* NT only checks count, not val_count */
1320     if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1321     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1322
1323     total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
1324     if (data) total_size += *count;
1325     total_size = min( sizeof(buffer), total_size );
1326
1327     status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1328                                   buffer, total_size, &total_size );
1329     if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1330
1331     /* we need to fetch the contents for a string type even if not requested,
1332      * because we need to compute the length of the ASCII string. */
1333     if (value || data || is_string(info->Type))
1334     {
1335         /* retry with a dynamically allocated buffer */
1336         while (status == STATUS_BUFFER_OVERFLOW)
1337         {
1338             if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1339             if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1340                 return ERROR_NOT_ENOUGH_MEMORY;
1341             info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
1342             status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1343                                           buf_ptr, total_size, &total_size );
1344         }
1345
1346         if (status) goto done;
1347
1348         if (is_string(info->Type))
1349         {
1350             DWORD len;
1351             RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->DataOffset),
1352                                        total_size - info->DataOffset );
1353             if (data && len)
1354             {
1355                 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
1356                 else
1357                 {
1358                     RtlUnicodeToMultiByteN( data, len, NULL, (WCHAR *)(buf_ptr + info->DataOffset),
1359                                             total_size - info->DataOffset );
1360                     /* if the type is REG_SZ and data is not 0-terminated
1361                      * and there is enough space in the buffer NT appends a \0 */
1362                     if (len < *count && data[len-1]) data[len] = 0;
1363                 }
1364             }
1365             info->DataLength = len;
1366         }
1367         else if (data)
1368         {
1369             if (total_size - info->DataOffset > *count) status = STATUS_BUFFER_OVERFLOW;
1370             else memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
1371         }
1372
1373         if (value && !status)
1374         {
1375             DWORD len;
1376
1377             RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
1378             if (len >= *val_count)
1379             {
1380                 status = STATUS_BUFFER_OVERFLOW;
1381                 if (*val_count)
1382                 {
1383                     len = *val_count - 1;
1384                     RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
1385                     value[len] = 0;
1386                 }
1387             }
1388             else
1389             {
1390                 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
1391                 value[len] = 0;
1392                 *val_count = len;
1393             }
1394         }
1395     }
1396     else status = STATUS_SUCCESS;
1397
1398     if (type) *type = info->Type;
1399     if (count) *count = info->DataLength;
1400
1401  done:
1402     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1403     return RtlNtStatusToDosError(status);
1404 }
1405
1406
1407
1408 /******************************************************************************
1409  *           RegDeleteValueW   [ADVAPI32.@]
1410  *
1411  * See RegDeleteValueA.
1412  */
1413 DWORD WINAPI RegDeleteValueW( HKEY hkey, LPCWSTR name )
1414 {
1415     UNICODE_STRING nameW;
1416
1417     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1418
1419     RtlInitUnicodeString( &nameW, name );
1420     return RtlNtStatusToDosError( NtDeleteValueKey( hkey, &nameW ) );
1421 }
1422
1423
1424 /******************************************************************************
1425  *           RegDeleteValueA   [ADVAPI32.@]
1426  *
1427  * Delete a value from the registry.
1428  *
1429  * PARAMS
1430  *  hkey [I] Registry handle of the key holding the value
1431  *  name [I] Name of the value under hkey to delete
1432  *
1433  * RETURNS
1434  *  Success: 0
1435  *  Failure: A standard Windows error code.
1436  */
1437 DWORD WINAPI RegDeleteValueA( HKEY hkey, LPCSTR name )
1438 {
1439     STRING nameA;
1440     NTSTATUS status;
1441
1442     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1443
1444     RtlInitAnsiString( &nameA, name );
1445     if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
1446                                                  &nameA, FALSE )))
1447         status = NtDeleteValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString );
1448     return RtlNtStatusToDosError( status );
1449 }
1450
1451
1452 /******************************************************************************
1453  *           RegLoadKeyW   [ADVAPI32.@]
1454  *
1455  * PARAMS
1456  *    hkey      [I] Handle of open key
1457  *    subkey    [I] Address of name of subkey
1458  *    filename  [I] Address of filename for registry information
1459  */
1460 LONG WINAPI RegLoadKeyW( HKEY hkey, LPCWSTR subkey, LPCWSTR filename )
1461 {
1462     HANDLE file;
1463     DWORD ret, len, err = GetLastError();
1464     HKEY shkey;
1465
1466     TRACE( "(%p,%s,%s)\n", hkey, debugstr_w(subkey), debugstr_w(filename) );
1467
1468     if (!filename || !*filename) return ERROR_INVALID_PARAMETER;
1469     if (!subkey || !*subkey) return ERROR_INVALID_PARAMETER;
1470     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1471
1472     len = strlenW( subkey ) * sizeof(WCHAR);
1473     if (len > MAX_PATH*sizeof(WCHAR)) return ERROR_INVALID_PARAMETER;
1474
1475     if ((file = CreateFileW( filename, GENERIC_READ, 0, NULL, OPEN_EXISTING,
1476                              FILE_ATTRIBUTE_NORMAL, 0 )) == INVALID_HANDLE_VALUE)
1477     {
1478         ret = GetLastError();
1479         goto done;
1480     }
1481
1482     RegCreateKeyW(hkey,subkey,&shkey);
1483
1484     SERVER_START_REQ( load_registry )
1485     {
1486         req->hkey  = shkey;
1487         req->file  = file;
1488         wine_server_add_data( req, subkey, len );
1489         ret = RtlNtStatusToDosError( wine_server_call(req) );
1490     }
1491     SERVER_END_REQ;
1492     CloseHandle( file );
1493     RegCloseKey(shkey);
1494
1495  done:
1496     SetLastError( err );  /* restore the last error code */
1497     return ret;
1498 }
1499
1500
1501 /******************************************************************************
1502  *           RegLoadKeyA   [ADVAPI32.@]
1503  */
1504 LONG WINAPI RegLoadKeyA( HKEY hkey, LPCSTR subkey, LPCSTR filename )
1505 {
1506     WCHAR buffer[MAX_PATH];
1507     HANDLE file;
1508     DWORD ret, len, err = GetLastError();
1509     HKEY shkey;
1510
1511     TRACE( "(%p,%s,%s)\n", hkey, debugstr_a(subkey), debugstr_a(filename) );
1512
1513     if (!filename || !*filename) return ERROR_INVALID_PARAMETER;
1514     if (!subkey || !*subkey) return ERROR_INVALID_PARAMETER;
1515     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1516
1517     if (!(len = MultiByteToWideChar( CP_ACP, 0, subkey, strlen(subkey), buffer, MAX_PATH )))
1518         return ERROR_INVALID_PARAMETER;
1519
1520     if ((file = CreateFileA( filename, GENERIC_READ, 0, NULL, OPEN_EXISTING,
1521                              FILE_ATTRIBUTE_NORMAL, 0 )) == INVALID_HANDLE_VALUE)
1522     {
1523         ret = GetLastError();
1524         goto done;
1525     }
1526
1527     RegCreateKeyA(hkey,subkey,&shkey);
1528
1529     SERVER_START_REQ( load_registry )
1530     {
1531         req->hkey  = shkey;
1532         req->file  = file;
1533         wine_server_add_data( req, buffer, len * sizeof(WCHAR) );
1534         ret = RtlNtStatusToDosError( wine_server_call(req) );
1535     }
1536     SERVER_END_REQ;
1537     CloseHandle( file );
1538     RegCloseKey(shkey);
1539
1540  done:
1541     SetLastError( err );  /* restore the last error code */
1542     return ret;
1543 }
1544
1545
1546 /******************************************************************************
1547  *           RegSaveKeyW   [ADVAPI32.@]
1548  *
1549  * PARAMS
1550  *    hkey   [I] Handle of key where save begins
1551  *    lpFile [I] Address of filename to save to
1552  *    sa     [I] Address of security structure
1553  */
1554 LONG WINAPI RegSaveKeyW( HKEY hkey, LPCWSTR file, LPSECURITY_ATTRIBUTES sa )
1555 {
1556     static const WCHAR format[] =
1557         {'r','e','g','%','0','4','x','.','t','m','p',0};
1558     WCHAR buffer[MAX_PATH];
1559     int count = 0;
1560     LPWSTR nameW;
1561     DWORD ret, err;
1562     HANDLE handle;
1563
1564     TRACE( "(%p,%s,%p)\n", hkey, debugstr_w(file), sa );
1565
1566     if (!file || !*file) return ERROR_INVALID_PARAMETER;
1567     if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1568
1569     err = GetLastError();
1570     GetFullPathNameW( file, sizeof(buffer)/sizeof(WCHAR), buffer, &nameW );
1571
1572     for (;;)
1573     {
1574         snprintfW( nameW, 16, format, count++ );
1575         handle = CreateFileW( buffer, GENERIC_WRITE, 0, NULL,
1576                             CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
1577         if (handle != INVALID_HANDLE_VALUE) break;
1578         if ((ret = GetLastError()) != ERROR_ALREADY_EXISTS) goto done;
1579
1580         /* Something gone haywire ? Please report if this happens abnormally */
1581         if (count >= 100)
1582             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);
1583     }
1584
1585     SERVER_START_REQ( save_registry )
1586     {
1587         req->hkey = hkey;
1588         req->file = handle;
1589         ret = RtlNtStatusToDosError( wine_server_call( req ) );
1590     }
1591     SERVER_END_REQ;
1592
1593     CloseHandle( handle );
1594     if (!ret)
1595     {
1596         if (!MoveFileExW( buffer, file, MOVEFILE_REPLACE_EXISTING ))
1597         {
1598             ERR( "Failed to move %s to %s\n", debugstr_w(buffer),
1599                 debugstr_w(file) );
1600             ret = GetLastError();
1601         }
1602     }
1603     if (ret) DeleteFileW( buffer );
1604
1605 done:
1606     SetLastError( err );  /* restore last error code */
1607     return ret;
1608 }
1609
1610
1611 /******************************************************************************
1612  *           RegSaveKeyA  [ADVAPI32.@]
1613  */
1614 LONG WINAPI RegSaveKeyA( HKEY hkey, LPCSTR file, LPSECURITY_ATTRIBUTES sa )
1615 {
1616     UNICODE_STRING *fileW = &NtCurrentTeb()->StaticUnicodeString;
1617     NTSTATUS status;
1618     STRING fileA;
1619
1620     RtlInitAnsiString(&fileA, file);
1621     if ((status = RtlAnsiStringToUnicodeString(fileW, &fileA, FALSE)))
1622         return RtlNtStatusToDosError( status );
1623     return RegSaveKeyW(hkey, fileW->Buffer, sa);
1624 }
1625
1626
1627 /******************************************************************************
1628  * RegRestoreKeyW [ADVAPI32.@]
1629  *
1630  * PARAMS
1631  *    hkey    [I] Handle of key where restore begins
1632  *    lpFile  [I] Address of filename containing saved tree
1633  *    dwFlags [I] Optional flags
1634  */
1635 LONG WINAPI RegRestoreKeyW( HKEY hkey, LPCWSTR lpFile, DWORD dwFlags )
1636 {
1637     TRACE("(%p,%s,%ld)\n",hkey,debugstr_w(lpFile),dwFlags);
1638
1639     /* It seems to do this check before the hkey check */
1640     if (!lpFile || !*lpFile)
1641         return ERROR_INVALID_PARAMETER;
1642
1643     FIXME("(%p,%s,%ld): stub\n",hkey,debugstr_w(lpFile),dwFlags);
1644
1645     /* Check for file existence */
1646
1647     return ERROR_SUCCESS;
1648 }
1649
1650
1651 /******************************************************************************
1652  * RegRestoreKeyA [ADVAPI32.@]
1653  */
1654 LONG WINAPI RegRestoreKeyA( HKEY hkey, LPCSTR lpFile, DWORD dwFlags )
1655 {
1656     UNICODE_STRING lpFileW;
1657     LONG ret;
1658
1659     RtlCreateUnicodeStringFromAsciiz( &lpFileW, lpFile );
1660     ret = RegRestoreKeyW( hkey, lpFileW.Buffer, dwFlags );
1661     RtlFreeUnicodeString( &lpFileW );
1662     return ret;
1663 }
1664
1665
1666 /******************************************************************************
1667  * RegUnLoadKeyW [ADVAPI32.@]
1668  *
1669  * PARAMS
1670  *    hkey     [I] Handle of open key
1671  *    lpSubKey [I] Address of name of subkey to unload
1672  */
1673 LONG WINAPI RegUnLoadKeyW( HKEY hkey, LPCWSTR lpSubKey )
1674 {
1675     DWORD ret;
1676     HKEY shkey;
1677
1678     TRACE("(%p,%s)\n",hkey, debugstr_w(lpSubKey));
1679
1680     ret = RegOpenKeyW(hkey,lpSubKey,&shkey);
1681     if( ret )
1682         return ERROR_INVALID_PARAMETER;
1683
1684     SERVER_START_REQ( unload_registry )
1685     {
1686         req->hkey  = shkey;
1687         ret = RtlNtStatusToDosError( wine_server_call(req) );
1688     }
1689     SERVER_END_REQ;
1690     RegCloseKey(shkey);
1691
1692     return ret;
1693 }
1694
1695
1696 /******************************************************************************
1697  * RegUnLoadKeyA [ADVAPI32.@]
1698  */
1699 LONG WINAPI RegUnLoadKeyA( HKEY hkey, LPCSTR lpSubKey )
1700 {
1701     UNICODE_STRING lpSubKeyW;
1702     LONG ret;
1703
1704     RtlCreateUnicodeStringFromAsciiz( &lpSubKeyW, lpSubKey );
1705     ret = RegUnLoadKeyW( hkey, lpSubKeyW.Buffer );
1706     RtlFreeUnicodeString( &lpSubKeyW );
1707     return ret;
1708 }
1709
1710
1711 /******************************************************************************
1712  * RegReplaceKeyW [ADVAPI32.@]
1713  *
1714  * PARAMS
1715  *    hkey      [I] Handle of open key
1716  *    lpSubKey  [I] Address of name of subkey
1717  *    lpNewFile [I] Address of filename for file with new data
1718  *    lpOldFile [I] Address of filename for backup file
1719  */
1720 LONG WINAPI RegReplaceKeyW( HKEY hkey, LPCWSTR lpSubKey, LPCWSTR lpNewFile,
1721                               LPCWSTR lpOldFile )
1722 {
1723     FIXME("(%p,%s,%s,%s): stub\n", hkey, debugstr_w(lpSubKey),
1724           debugstr_w(lpNewFile),debugstr_w(lpOldFile));
1725     return ERROR_SUCCESS;
1726 }
1727
1728
1729 /******************************************************************************
1730  * RegReplaceKeyA [ADVAPI32.@]
1731  */
1732 LONG WINAPI RegReplaceKeyA( HKEY hkey, LPCSTR lpSubKey, LPCSTR lpNewFile,
1733                               LPCSTR lpOldFile )
1734 {
1735     UNICODE_STRING lpSubKeyW;
1736     UNICODE_STRING lpNewFileW;
1737     UNICODE_STRING lpOldFileW;
1738     LONG ret;
1739
1740     RtlCreateUnicodeStringFromAsciiz( &lpSubKeyW, lpSubKey );
1741     RtlCreateUnicodeStringFromAsciiz( &lpOldFileW, lpOldFile );
1742     RtlCreateUnicodeStringFromAsciiz( &lpNewFileW, lpNewFile );
1743     ret = RegReplaceKeyW( hkey, lpSubKeyW.Buffer, lpNewFileW.Buffer, lpOldFileW.Buffer );
1744     RtlFreeUnicodeString( &lpOldFileW );
1745     RtlFreeUnicodeString( &lpNewFileW );
1746     RtlFreeUnicodeString( &lpSubKeyW );
1747     return ret;
1748 }
1749
1750
1751 /******************************************************************************
1752  * RegSetKeySecurity [ADVAPI32.@]
1753  *
1754  * PARAMS
1755  *    hkey          [I] Open handle of key to set
1756  *    SecurityInfo  [I] Descriptor contents
1757  *    pSecurityDesc [I] Address of descriptor for key
1758  */
1759 LONG WINAPI RegSetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInfo,
1760                                PSECURITY_DESCRIPTOR pSecurityDesc )
1761 {
1762     TRACE("(%p,%ld,%p)\n",hkey,SecurityInfo,pSecurityDesc);
1763
1764     /* It seems to perform this check before the hkey check */
1765     if ((SecurityInfo & OWNER_SECURITY_INFORMATION) ||
1766         (SecurityInfo & GROUP_SECURITY_INFORMATION) ||
1767         (SecurityInfo & DACL_SECURITY_INFORMATION) ||
1768         (SecurityInfo & SACL_SECURITY_INFORMATION)) {
1769         /* Param OK */
1770     } else
1771         return ERROR_INVALID_PARAMETER;
1772
1773     if (!pSecurityDesc)
1774         return ERROR_INVALID_PARAMETER;
1775
1776     FIXME(":(%p,%ld,%p): stub\n",hkey,SecurityInfo,pSecurityDesc);
1777
1778     return ERROR_SUCCESS;
1779 }
1780
1781
1782 /******************************************************************************
1783  * RegGetKeySecurity [ADVAPI32.@]
1784  * Retrieves a copy of security descriptor protecting the registry key
1785  *
1786  * PARAMS
1787  *    hkey                   [I]   Open handle of key to set
1788  *    SecurityInformation    [I]   Descriptor contents
1789  *    pSecurityDescriptor    [O]   Address of descriptor for key
1790  *    lpcbSecurityDescriptor [I/O] Address of size of buffer and description
1791  *
1792  * RETURNS
1793  *    Success: ERROR_SUCCESS
1794  *    Failure: Error code
1795  */
1796 LONG WINAPI RegGetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInformation,
1797                                PSECURITY_DESCRIPTOR pSecurityDescriptor,
1798                                LPDWORD lpcbSecurityDescriptor )
1799 {
1800     TRACE("(%p,%ld,%p,%ld)\n",hkey,SecurityInformation,pSecurityDescriptor,
1801           lpcbSecurityDescriptor?*lpcbSecurityDescriptor:0);
1802
1803     /* FIXME: Check for valid SecurityInformation values */
1804
1805     if (*lpcbSecurityDescriptor < sizeof(SECURITY_DESCRIPTOR))
1806         return ERROR_INSUFFICIENT_BUFFER;
1807
1808     FIXME("(%p,%ld,%p,%ld): stub\n",hkey,SecurityInformation,
1809           pSecurityDescriptor,lpcbSecurityDescriptor?*lpcbSecurityDescriptor:0);
1810
1811     /* Do not leave security descriptor filled with garbage */
1812     RtlCreateSecurityDescriptor(pSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
1813
1814     return ERROR_SUCCESS;
1815 }
1816
1817
1818 /******************************************************************************
1819  * RegFlushKey [ADVAPI32.@]
1820  * Immediately writes key to registry.
1821  * Only returns after data has been written to disk.
1822  *
1823  * FIXME: does it really wait until data is written ?
1824  *
1825  * PARAMS
1826  *    hkey [I] Handle of key to write
1827  *
1828  * RETURNS
1829  *    Success: ERROR_SUCCESS
1830  *    Failure: Error code
1831  */
1832 DWORD WINAPI RegFlushKey( HKEY hkey )
1833 {
1834     FIXME( "(%p): stub\n", hkey );
1835     return ERROR_SUCCESS;
1836 }
1837
1838
1839 /******************************************************************************
1840  * RegConnectRegistryW [ADVAPI32.@]
1841  *
1842  * PARAMS
1843  *    lpMachineName [I] Address of name of remote computer
1844  *    hHey          [I] Predefined registry handle
1845  *    phkResult     [I] Address of buffer for remote registry handle
1846  */
1847 LONG WINAPI RegConnectRegistryW( LPCWSTR lpMachineName, HKEY hKey,
1848                                    PHKEY phkResult )
1849 {
1850     TRACE("(%s,%p,%p): stub\n",debugstr_w(lpMachineName),hKey,phkResult);
1851
1852     if (!lpMachineName || !*lpMachineName) {
1853         /* Use the local machine name */
1854         return RegOpenKeyW( hKey, NULL, phkResult );
1855     }
1856
1857     FIXME("Cannot connect to %s\n",debugstr_w(lpMachineName));
1858     return ERROR_BAD_NETPATH;
1859 }
1860
1861
1862 /******************************************************************************
1863  * RegConnectRegistryA [ADVAPI32.@]
1864  */
1865 LONG WINAPI RegConnectRegistryA( LPCSTR machine, HKEY hkey, PHKEY reskey )
1866 {
1867     UNICODE_STRING machineW;
1868     LONG ret;
1869
1870     RtlCreateUnicodeStringFromAsciiz( &machineW, machine );
1871     ret = RegConnectRegistryW( machineW.Buffer, hkey, reskey );
1872     RtlFreeUnicodeString( &machineW );
1873     return ret;
1874 }
1875
1876
1877 /******************************************************************************
1878  * RegNotifyChangeKeyValue [ADVAPI32.@]
1879  *
1880  * PARAMS
1881  *    hkey            [I] Handle of key to watch
1882  *    fWatchSubTree   [I] Flag for subkey notification
1883  *    fdwNotifyFilter [I] Changes to be reported
1884  *    hEvent          [I] Handle of signaled event
1885  *    fAsync          [I] Flag for asynchronous reporting
1886  */
1887 LONG WINAPI RegNotifyChangeKeyValue( HKEY hkey, BOOL fWatchSubTree,
1888                                      DWORD fdwNotifyFilter, HANDLE hEvent,
1889                                      BOOL fAsync )
1890 {
1891     LONG ret;
1892
1893     TRACE("(%p,%i,%ld,%p,%i)\n",hkey,fWatchSubTree,fdwNotifyFilter,
1894           hEvent,fAsync);
1895
1896     if( !fAsync )
1897         hEvent = CreateEventA(NULL, 0, 0, NULL);
1898
1899     SERVER_START_REQ( set_registry_notification )
1900     {
1901         req->hkey    = hkey;
1902         req->event   = hEvent;
1903         req->subtree = fWatchSubTree;
1904         req->filter  = fdwNotifyFilter;
1905         ret = RtlNtStatusToDosError( wine_server_call(req) );
1906     }
1907     SERVER_END_REQ;
1908  
1909     if( !fAsync )
1910     {
1911         if( ret == ERROR_SUCCESS )
1912             WaitForSingleObject( hEvent, INFINITE );
1913         CloseHandle( hEvent );
1914     }
1915
1916     return ret;
1917 }