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