Added regedit unit test, a couple minor changes to regedit.
[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)
609     {
610         ret = RtlNtStatusToDosError( NtDeleteKey( hkey ) );
611     }
612     else if (!(ret = RegOpenKeyExW( hkey, name, 0, 0, &tmp )))
613     {
614         if (!is_version_nt()) /* win95 does recursive key deletes */
615         {
616             WCHAR name[MAX_PATH];
617
618             while(!RegEnumKeyW(tmp, 0, name, sizeof name))
619             {
620                 if(RegDeleteKeyW(tmp, name))  /* recurse */
621                     break;
622             }
623         }
624         ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
625         RegCloseKey( tmp );
626     }
627     TRACE("%s ret=%08lx\n", debugstr_w(name), ret);
628     return ret;
629 }
630
631
632 /******************************************************************************
633  *           RegDeleteKeyA   [ADVAPI32.@]
634  */
635 DWORD WINAPI RegDeleteKeyA( HKEY hkey, LPCSTR name )
636 {
637     DWORD ret;
638     HKEY tmp;
639
640     if (!name || !*name)
641     {
642         ret = RtlNtStatusToDosError( NtDeleteKey( hkey ) );
643     }
644     else if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ALL_ACCESS, &tmp )))
645     {
646         if (!is_version_nt()) /* win95 does recursive key deletes */
647         {
648             CHAR name[MAX_PATH];
649
650             while(!RegEnumKeyA(tmp, 0, name, sizeof name))
651             {
652                 if(RegDeleteKeyA(tmp, name))  /* recurse */
653                     break;
654             }
655         }
656         ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
657         RegCloseKey( tmp );
658     }
659     TRACE("%s ret=%08lx\n", debugstr_a(name), ret);
660     return ret;
661 }
662
663
664
665 /******************************************************************************
666  *           RegSetValueExW   [ADVAPI32.@]
667  *
668  * Sets the data and type of a value under a register key
669  *
670  * PARAMS
671  *    hkey       [I] Handle of key to set value for
672  *    name       [I] Name of value to set
673  *    reserved   [I] Reserved - must be zero
674  *    type       [I] Flag for value type
675  *    data       [I] Address of value data
676  *    count      [I] Size of value data
677  *
678  * RETURNS
679  *    Success: ERROR_SUCCESS
680  *    Failure: Error code
681  *
682  * NOTES
683  *   win95 does not care about count for REG_SZ and finds out the len by itself (js)
684  *   NT does definitely care (aj)
685  */
686 DWORD WINAPI RegSetValueExW( HKEY hkey, LPCWSTR name, DWORD reserved,
687                              DWORD type, CONST BYTE *data, DWORD count )
688 {
689     UNICODE_STRING nameW;
690
691     if (!is_version_nt())  /* win95 */
692     {
693         if (type == REG_SZ) count = (strlenW( (WCHAR *)data ) + 1) * sizeof(WCHAR);
694     }
695     else if (count && is_string(type))
696     {
697         LPCWSTR str = (LPCWSTR)data;
698         /* if user forgot to count terminating null, add it (yes NT does this) */
699         if (str[count / sizeof(WCHAR) - 1] && !str[count / sizeof(WCHAR)])
700             count += sizeof(WCHAR);
701     }
702
703     RtlInitUnicodeString( &nameW, name );
704     return RtlNtStatusToDosError( NtSetValueKey( hkey, &nameW, 0, type, data, count ) );
705 }
706
707
708 /******************************************************************************
709  *           RegSetValueExA   [ADVAPI32.@]
710  */
711 DWORD WINAPI RegSetValueExA( HKEY hkey, LPCSTR name, DWORD reserved, DWORD type,
712                              CONST BYTE *data, DWORD count )
713 {
714     ANSI_STRING nameA;
715     WCHAR *dataW = NULL;
716     NTSTATUS status;
717
718     if (!is_version_nt())  /* win95 */
719     {
720         if (type == REG_SZ) count = strlen(data) + 1;
721     }
722     else if (count && is_string(type))
723     {
724         /* if user forgot to count terminating null, add it (yes NT does this) */
725         if (data[count-1] && !data[count]) count++;
726     }
727
728     if (is_string( type )) /* need to convert to Unicode */
729     {
730         DWORD lenW;
731         RtlMultiByteToUnicodeSize( &lenW, data, count );
732         if (!(dataW = HeapAlloc( GetProcessHeap(), 0, lenW ))) return ERROR_OUTOFMEMORY;
733         RtlMultiByteToUnicodeN( dataW, lenW, NULL, data, count );
734         count = lenW;
735         data = (BYTE *)dataW;
736     }
737
738     RtlInitAnsiString( &nameA, name );
739     if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
740                                                  &nameA, FALSE )))
741     {
742         status = NtSetValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString, 0, type, data, count );
743     }
744     if (dataW) HeapFree( GetProcessHeap(), 0, dataW );
745     return RtlNtStatusToDosError( status );
746 }
747
748
749 /******************************************************************************
750  *           RegSetValueW   [ADVAPI32.@]
751  */
752 DWORD WINAPI RegSetValueW( HKEY hkey, LPCWSTR name, DWORD type, LPCWSTR data, DWORD count )
753 {
754     HKEY subkey = hkey;
755     DWORD ret;
756
757     TRACE("(0x%x,%s,%ld,%s,%ld)\n", hkey, debugstr_w(name), type, debugstr_w(data), count );
758
759     if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
760
761     if (name && name[0])  /* need to create the subkey */
762     {
763         if ((ret = RegCreateKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
764     }
765
766     ret = RegSetValueExW( subkey, NULL, 0, REG_SZ, (LPBYTE)data,
767                           (strlenW( data ) + 1) * sizeof(WCHAR) );
768     if (subkey != hkey) RegCloseKey( subkey );
769     return ret;
770 }
771
772
773 /******************************************************************************
774  *           RegSetValueA   [ADVAPI32.@]
775  */
776 DWORD WINAPI RegSetValueA( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
777 {
778     HKEY subkey = hkey;
779     DWORD ret;
780
781     TRACE("(0x%x,%s,%ld,%s,%ld)\n", hkey, debugstr_a(name), type, debugstr_a(data), count );
782
783     if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
784
785     if (name && name[0])  /* need to create the subkey */
786     {
787         if ((ret = RegCreateKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
788     }
789     ret = RegSetValueExA( subkey, NULL, 0, REG_SZ, (LPBYTE)data, strlen(data)+1 );
790     if (subkey != hkey) RegCloseKey( subkey );
791     return ret;
792 }
793
794
795
796 /******************************************************************************
797  *           RegQueryValueExW   [ADVAPI32.@]
798  *
799  * Retrieves type and data for a specified name associated with an open key
800  *
801  * PARAMS
802  *    hkey      [I]   Handle of key to query
803  *    name      [I]   Name of value to query
804  *    reserved  [I]   Reserved - must be NULL
805  *    type      [O]   Address of buffer for value type.  If NULL, the type
806  *                        is not required.
807  *    data      [O]   Address of data buffer.  If NULL, the actual data is
808  *                        not required.
809  *    count     [I/O] Address of data buffer size
810  *
811  * RETURNS
812  *    ERROR_SUCCESS:   Success
813  *    ERROR_MORE_DATA: !!! if the specified buffer is not big enough to hold the data
814  *                     buffer is left untouched. The MS-documentation is wrong (js) !!!
815  */
816 DWORD WINAPI RegQueryValueExW( HKEY hkey, LPCWSTR name, LPDWORD reserved, LPDWORD type,
817                                LPBYTE data, LPDWORD count )
818 {
819     NTSTATUS status;
820     UNICODE_STRING name_str;
821     DWORD total_size;
822     char buffer[256], *buf_ptr = buffer;
823     KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
824     static const int info_size = info->Data - (UCHAR *)info;
825
826     TRACE("(0x%x,%s,%p,%p,%p,%p=%ld)\n",
827           hkey, debugstr_w(name), reserved, type, data, count, count ? *count : 0 );
828
829     if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
830
831     RtlInitUnicodeString( &name_str, name );
832
833     if (data) total_size = min( sizeof(buffer), *count + info_size );
834     else total_size = info_size;
835
836     status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
837                               buffer, total_size, &total_size );
838     if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
839
840     if (data)
841     {
842         /* retry with a dynamically allocated buffer */
843         while (status == STATUS_BUFFER_OVERFLOW && total_size - info_size <= *count)
844         {
845             if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
846             if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
847                 return ERROR_NOT_ENOUGH_MEMORY;
848             info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
849             status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
850                                       buf_ptr, total_size, &total_size );
851         }
852
853         if (!status)
854         {
855             memcpy( data, buf_ptr + info_size, total_size - info_size );
856             /* if the type is REG_SZ and data is not 0-terminated
857              * and there is enough space in the buffer NT appends a \0 */
858             if (total_size - info_size <= *count-sizeof(WCHAR) && is_string(info->Type))
859             {
860                 WCHAR *ptr = (WCHAR *)(data + total_size - info_size);
861                 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
862             }
863         }
864         else if (status != STATUS_BUFFER_OVERFLOW) goto done;
865     }
866     else status = STATUS_SUCCESS;
867
868     if (type) *type = info->Type;
869     if (count) *count = total_size - info_size;
870
871  done:
872     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
873     return RtlNtStatusToDosError(status);
874 }
875
876
877 /******************************************************************************
878  *           RegQueryValueExA   [ADVAPI32.@]
879  *
880  * NOTES:
881  * the documentation is wrong: if the buffer is too small it remains untouched
882  */
883 DWORD WINAPI RegQueryValueExA( HKEY hkey, LPCSTR name, LPDWORD reserved, LPDWORD type,
884                                LPBYTE data, LPDWORD count )
885 {
886     NTSTATUS status;
887     ANSI_STRING nameA;
888     DWORD total_size;
889     char buffer[256], *buf_ptr = buffer;
890     KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
891     static const int info_size = info->Data - (UCHAR *)info;
892
893     TRACE("(0x%x,%s,%p,%p,%p,%p=%ld)\n",
894           hkey, debugstr_a(name), reserved, type, data, count, count ? *count : 0 );
895
896     if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
897
898     RtlInitAnsiString( &nameA, name );
899     if ((status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
900                                                 &nameA, FALSE )))
901         return RtlNtStatusToDosError(status);
902
903     status = NtQueryValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString,
904                               KeyValuePartialInformation, buffer, sizeof(buffer), &total_size );
905     if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
906
907     /* we need to fetch the contents for a string type even if not requested,
908      * because we need to compute the length of the ASCII string. */
909     if (data || is_string(info->Type))
910     {
911         /* retry with a dynamically allocated buffer */
912         while (status == STATUS_BUFFER_OVERFLOW)
913         {
914             if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
915             if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
916             {
917                 status = STATUS_NO_MEMORY;
918                 goto done;
919             }
920             info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
921             status = NtQueryValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString,
922                                     KeyValuePartialInformation, buf_ptr, total_size, &total_size );
923         }
924
925         if (status) goto done;
926
927         if (is_string(info->Type))
928         {
929             DWORD len;
930
931             RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info_size),
932                                        total_size - info_size );
933             if (data && len)
934             {
935                 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
936                 else
937                 {
938                     RtlUnicodeToMultiByteN( data, len, NULL, (WCHAR *)(buf_ptr + info_size),
939                                             total_size - info_size );
940                     /* if the type is REG_SZ and data is not 0-terminated
941                      * and there is enough space in the buffer NT appends a \0 */
942                     if (len < *count && data[len-1]) data[len] = 0;
943                 }
944             }
945             total_size = len + info_size;
946         }
947         else if (data)
948         {
949             if (total_size - info_size > *count) status = STATUS_BUFFER_OVERFLOW;
950             else memcpy( data, buf_ptr + info_size, total_size - info_size );
951         }
952     }
953     else status = STATUS_SUCCESS;
954
955     if (type) *type = info->Type;
956     if (count) *count = total_size - info_size;
957
958  done:
959     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
960     return RtlNtStatusToDosError(status);
961 }
962
963
964 /******************************************************************************
965  *           RegQueryValueW   [ADVAPI32.@]
966  */
967 DWORD WINAPI RegQueryValueW( HKEY hkey, LPCWSTR name, LPWSTR data, LPLONG count )
968 {
969     DWORD ret;
970     HKEY subkey = hkey;
971
972     TRACE("(%x,%s,%p,%ld)\n", hkey, debugstr_w(name), data, count ? *count : 0 );
973
974     if (name && name[0])
975     {
976         if ((ret = RegOpenKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
977     }
978     ret = RegQueryValueExW( subkey, NULL, NULL, NULL, (LPBYTE)data, count );
979     if (subkey != hkey) RegCloseKey( subkey );
980     if (ret == ERROR_FILE_NOT_FOUND)
981     {
982         /* return empty string if default value not found */
983         if (data) *data = 0;
984         if (count) *count = 1;
985         ret = ERROR_SUCCESS;
986     }
987     return ret;
988 }
989
990
991 /******************************************************************************
992  *           RegQueryValueA   [ADVAPI32.@]
993  */
994 DWORD WINAPI RegQueryValueA( HKEY hkey, LPCSTR name, LPSTR data, LPLONG count )
995 {
996     DWORD ret;
997     HKEY subkey = hkey;
998
999     TRACE("(%x,%s,%p,%ld)\n", hkey, debugstr_a(name), data, count ? *count : 0 );
1000
1001     if (name && name[0])
1002     {
1003         if ((ret = RegOpenKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1004     }
1005     ret = RegQueryValueExA( subkey, NULL, NULL, NULL, (LPBYTE)data, count );
1006     if (subkey != hkey) RegCloseKey( subkey );
1007     if (ret == ERROR_FILE_NOT_FOUND)
1008     {
1009         /* return empty string if default value not found */
1010         if (data) *data = 0;
1011         if (count) *count = 1;
1012         ret = ERROR_SUCCESS;
1013     }
1014     return ret;
1015 }
1016
1017
1018 /******************************************************************************
1019  *           RegEnumValueW   [ADVAPI32.@]
1020  *
1021  * PARAMS
1022  *    hkey       [I] Handle to key to query
1023  *    index      [I] Index of value to query
1024  *    value      [O] Value string
1025  *    val_count  [I/O] Size of value buffer (in wchars)
1026  *    reserved   [I] Reserved
1027  *    type       [O] Type code
1028  *    data       [O] Value data
1029  *    count      [I/O] Size of data buffer (in bytes)
1030  */
1031
1032 DWORD WINAPI RegEnumValueW( HKEY hkey, DWORD index, LPWSTR value, LPDWORD val_count,
1033                             LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1034 {
1035     NTSTATUS status;
1036     DWORD total_size;
1037     char buffer[256], *buf_ptr = buffer;
1038     KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
1039     static const int info_size = (char *)info->Name - (char *)info;
1040
1041     TRACE("(%x,%ld,%p,%p,%p,%p,%p,%p)\n",
1042           hkey, index, value, val_count, reserved, type, data, count );
1043
1044     /* NT only checks count, not val_count */
1045     if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1046
1047     total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
1048     if (data) total_size += *count;
1049     total_size = min( sizeof(buffer), total_size );
1050
1051     status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1052                                   buffer, total_size, &total_size );
1053     if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1054
1055     if (value || data)
1056     {
1057         /* retry with a dynamically allocated buffer */
1058         while (status == STATUS_BUFFER_OVERFLOW)
1059         {
1060             if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1061             if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1062                 return ERROR_NOT_ENOUGH_MEMORY;
1063             info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
1064             status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1065                                           buf_ptr, total_size, &total_size );
1066         }
1067
1068         if (status) goto done;
1069
1070         if (value)
1071         {
1072             if (info->NameLength/sizeof(WCHAR) >= *val_count)
1073             {
1074                 status = STATUS_BUFFER_OVERFLOW;
1075                 goto done;
1076             }
1077             memcpy( value, info->Name, info->NameLength );
1078             *val_count = info->NameLength / sizeof(WCHAR);
1079             value[*val_count] = 0;
1080         }
1081
1082         if (data)
1083         {
1084             if (total_size - info->DataOffset > *count)
1085             {
1086                 status = STATUS_BUFFER_OVERFLOW;
1087                 goto done;
1088             }
1089             memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
1090             if (total_size - info->DataOffset <= *count-sizeof(WCHAR) && is_string(info->Type))
1091             {
1092                 /* if the type is REG_SZ and data is not 0-terminated
1093                  * and there is enough space in the buffer NT appends a \0 */
1094                 WCHAR *ptr = (WCHAR *)(data + total_size - info->DataOffset);
1095                 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
1096             }
1097         }
1098     }
1099     else status = STATUS_SUCCESS;
1100
1101     if (type) *type = info->Type;
1102     if (count) *count = info->DataLength;
1103
1104  done:
1105     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1106     return RtlNtStatusToDosError(status);
1107 }
1108
1109
1110 /******************************************************************************
1111  *           RegEnumValueA   [ADVAPI32.@]
1112  */
1113 DWORD WINAPI RegEnumValueA( HKEY hkey, DWORD index, LPSTR value, LPDWORD val_count,
1114                             LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1115 {
1116     NTSTATUS status;
1117     DWORD total_size;
1118     char buffer[256], *buf_ptr = buffer;
1119     KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
1120     static const int info_size = (char *)info->Name - (char *)info;
1121
1122     TRACE("(%x,%ld,%p,%p,%p,%p,%p,%p)\n",
1123           hkey, index, value, val_count, reserved, type, data, count );
1124
1125     /* NT only checks count, not val_count */
1126     if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1127
1128     total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
1129     if (data) total_size += *count;
1130     total_size = min( sizeof(buffer), total_size );
1131
1132     status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1133                                   buffer, total_size, &total_size );
1134     if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1135
1136     /* we need to fetch the contents for a string type even if not requested,
1137      * because we need to compute the length of the ASCII string. */
1138     if (value || data || is_string(info->Type))
1139     {
1140         /* retry with a dynamically allocated buffer */
1141         while (status == STATUS_BUFFER_OVERFLOW)
1142         {
1143             if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1144             if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1145                 return ERROR_NOT_ENOUGH_MEMORY;
1146             info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
1147             status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1148                                           buf_ptr, total_size, &total_size );
1149         }
1150
1151         if (status) goto done;
1152
1153         if (value)
1154         {
1155             DWORD len;
1156
1157             RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
1158             if (len >= *val_count)
1159             {
1160                 status = STATUS_BUFFER_OVERFLOW;
1161                 goto done;
1162             }
1163             RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
1164             value[len] = 0;
1165             *val_count = len;
1166         }
1167
1168         if (is_string(info->Type))
1169         {
1170             DWORD len;
1171             RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->DataOffset),
1172                                        total_size - info->DataOffset );
1173             if (data && len)
1174             {
1175                 if (len > *count)
1176                 {
1177                     status = STATUS_BUFFER_OVERFLOW;
1178                     goto done;
1179                 }
1180                 RtlUnicodeToMultiByteN( data, len, NULL, (WCHAR *)(buf_ptr + info->DataOffset),
1181                                         total_size - info->DataOffset );
1182                 /* if the type is REG_SZ and data is not 0-terminated
1183                  * and there is enough space in the buffer NT appends a \0 */
1184                 if (len < *count && data[len-1]) data[len] = 0;
1185             }
1186             info->DataLength = len;
1187         }
1188         else if (data)
1189         {
1190             if (total_size - info->DataOffset > *count) status = STATUS_BUFFER_OVERFLOW;
1191             else memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
1192         }
1193     }
1194     else status = STATUS_SUCCESS;
1195
1196     if (type) *type = info->Type;
1197     if (count) *count = info->DataLength;
1198
1199  done:
1200     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1201     return RtlNtStatusToDosError(status);
1202 }
1203
1204
1205
1206 /******************************************************************************
1207  *           RegDeleteValueW   [ADVAPI32.@]
1208  *
1209  * PARAMS
1210  *    hkey   [I] handle to key
1211  *    name   [I] name of value to delete
1212  *
1213  * RETURNS
1214  *    error status
1215  */
1216 DWORD WINAPI RegDeleteValueW( HKEY hkey, LPCWSTR name )
1217 {
1218     UNICODE_STRING nameW;
1219     RtlInitUnicodeString( &nameW, name );
1220     return RtlNtStatusToDosError( NtDeleteValueKey( hkey, &nameW ) );
1221 }
1222
1223
1224 /******************************************************************************
1225  *           RegDeleteValueA   [ADVAPI32.@]
1226  */
1227 DWORD WINAPI RegDeleteValueA( HKEY hkey, LPCSTR name )
1228 {
1229     STRING nameA;
1230     NTSTATUS status;
1231
1232     RtlInitAnsiString( &nameA, name );
1233     if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
1234                                                  &nameA, FALSE )))
1235     {
1236         status = NtDeleteValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString );
1237     }
1238     return RtlNtStatusToDosError( status );
1239 }
1240
1241
1242 /******************************************************************************
1243  *           RegLoadKeyW   [ADVAPI32.@]
1244  *
1245  * PARAMS
1246  *    hkey      [I] Handle of open key
1247  *    subkey    [I] Address of name of subkey
1248  *    filename  [I] Address of filename for registry information
1249  */
1250 LONG WINAPI RegLoadKeyW( HKEY hkey, LPCWSTR subkey, LPCWSTR filename )
1251 {
1252     HANDLE file;
1253     DWORD ret, len, err = GetLastError();
1254
1255     TRACE( "(%x,%s,%s)\n", hkey, debugstr_w(subkey), debugstr_w(filename) );
1256
1257     if (!filename || !*filename) return ERROR_INVALID_PARAMETER;
1258     if (!subkey || !*subkey) return ERROR_INVALID_PARAMETER;
1259
1260     len = strlenW( subkey ) * sizeof(WCHAR);
1261     if (len > MAX_PATH*sizeof(WCHAR)) return ERROR_INVALID_PARAMETER;
1262
1263     if ((file = CreateFileW( filename, GENERIC_READ, 0, NULL, OPEN_EXISTING,
1264                              FILE_ATTRIBUTE_NORMAL, 0 )) == INVALID_HANDLE_VALUE)
1265     {
1266         ret = GetLastError();
1267         goto done;
1268     }
1269
1270     SERVER_START_REQ( load_registry )
1271     {
1272         req->hkey  = hkey;
1273         req->file  = file;
1274         wine_server_add_data( req, subkey, len );
1275         ret = RtlNtStatusToDosError( wine_server_call(req) );
1276     }
1277     SERVER_END_REQ;
1278     CloseHandle( file );
1279
1280  done:
1281     SetLastError( err );  /* restore the last error code */
1282     return ret;
1283 }
1284
1285
1286 /******************************************************************************
1287  *           RegLoadKeyA   [ADVAPI32.@]
1288  */
1289 LONG WINAPI RegLoadKeyA( HKEY hkey, LPCSTR subkey, LPCSTR filename )
1290 {
1291     WCHAR buffer[MAX_PATH];
1292     HANDLE file;
1293     DWORD ret, len, err = GetLastError();
1294
1295     TRACE( "(%x,%s,%s)\n", hkey, debugstr_a(subkey), debugstr_a(filename) );
1296
1297     if (!filename || !*filename) return ERROR_INVALID_PARAMETER;
1298     if (!subkey || !*subkey) return ERROR_INVALID_PARAMETER;
1299
1300     if (!(len = MultiByteToWideChar( CP_ACP, 0, subkey, strlen(subkey), buffer, MAX_PATH )))
1301         return ERROR_INVALID_PARAMETER;
1302
1303     if ((file = CreateFileA( filename, GENERIC_READ, 0, NULL, OPEN_EXISTING,
1304                              FILE_ATTRIBUTE_NORMAL, 0 )) == INVALID_HANDLE_VALUE)
1305     {
1306         ret = GetLastError();
1307         goto done;
1308     }
1309
1310     SERVER_START_REQ( load_registry )
1311     {
1312         req->hkey  = hkey;
1313         req->file  = file;
1314         wine_server_add_data( req, buffer, len * sizeof(WCHAR) );
1315         ret = RtlNtStatusToDosError( wine_server_call(req) );
1316     }
1317     SERVER_END_REQ;
1318     CloseHandle( file );
1319
1320  done:
1321     SetLastError( err );  /* restore the last error code */
1322     return ret;
1323 }
1324
1325
1326 /******************************************************************************
1327  *           RegSaveKeyA   [ADVAPI32.@]
1328  *
1329  * PARAMS
1330  *    hkey   [I] Handle of key where save begins
1331  *    lpFile [I] Address of filename to save to
1332  *    sa     [I] Address of security structure
1333  */
1334 LONG WINAPI RegSaveKeyA( HKEY hkey, LPCSTR file, LPSECURITY_ATTRIBUTES sa )
1335 {
1336     char buffer[1024];
1337     int count = 0;
1338     LPSTR name;
1339     DWORD ret, err;
1340     HFILE handle;
1341
1342     TRACE( "(%x,%s,%p)\n", hkey, debugstr_a(file), sa );
1343
1344     if (!file || !*file) return ERROR_INVALID_PARAMETER;
1345
1346     err = GetLastError();
1347     GetFullPathNameA( file, sizeof(buffer), buffer, &name );
1348     for (;;)
1349     {
1350         sprintf( name, "reg%04x.tmp", count++ );
1351         handle = CreateFileA( buffer, GENERIC_WRITE, 0, NULL,
1352                             CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
1353         if (handle != INVALID_HANDLE_VALUE) break;
1354         if ((ret = GetLastError()) != ERROR_ALREADY_EXISTS) goto done;
1355
1356         /* Something gone haywire ? Please report if this happens abnormally */
1357         if (count >= 100)
1358             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);
1359     }
1360
1361     SERVER_START_REQ( save_registry )
1362     {
1363         req->hkey = hkey;
1364         req->file = handle;
1365         ret = RtlNtStatusToDosError( wine_server_call( req ) );
1366     }
1367     SERVER_END_REQ;
1368
1369     CloseHandle( handle );
1370     if (!ret)
1371     {
1372         if (!MoveFileExA( buffer, file, MOVEFILE_REPLACE_EXISTING ))
1373         {
1374             ERR( "Failed to move %s to %s\n", buffer, file );
1375             ret = GetLastError();
1376         }
1377     }
1378     if (ret) DeleteFileA( buffer );
1379
1380 done:
1381     SetLastError( err );  /* restore last error code */
1382     return ret;
1383 }
1384
1385
1386 /******************************************************************************
1387  *           RegSaveKeyW   [ADVAPI32.@]
1388  */
1389 LONG WINAPI RegSaveKeyW( HKEY hkey, LPCWSTR file, LPSECURITY_ATTRIBUTES sa )
1390 {
1391     LPSTR fileA = HEAP_strdupWtoA( GetProcessHeap(), 0, file );
1392     DWORD ret = RegSaveKeyA( hkey, fileA, sa );
1393     if (fileA) HeapFree( GetProcessHeap(), 0, fileA );
1394     return ret;
1395 }
1396
1397
1398 /******************************************************************************
1399  * RegRestoreKeyW [ADVAPI32.@]
1400  *
1401  * PARAMS
1402  *    hkey    [I] Handle of key where restore begins
1403  *    lpFile  [I] Address of filename containing saved tree
1404  *    dwFlags [I] Optional flags
1405  */
1406 LONG WINAPI RegRestoreKeyW( HKEY hkey, LPCWSTR lpFile, DWORD dwFlags )
1407 {
1408     TRACE("(%x,%s,%ld)\n",hkey,debugstr_w(lpFile),dwFlags);
1409
1410     /* It seems to do this check before the hkey check */
1411     if (!lpFile || !*lpFile)
1412         return ERROR_INVALID_PARAMETER;
1413
1414     FIXME("(%x,%s,%ld): stub\n",hkey,debugstr_w(lpFile),dwFlags);
1415
1416     /* Check for file existence */
1417
1418     return ERROR_SUCCESS;
1419 }
1420
1421
1422 /******************************************************************************
1423  * RegRestoreKeyA [ADVAPI32.@]
1424  */
1425 LONG WINAPI RegRestoreKeyA( HKEY hkey, LPCSTR lpFile, DWORD dwFlags )
1426 {
1427     LPWSTR lpFileW = HEAP_strdupAtoW( GetProcessHeap(), 0, lpFile );
1428     LONG ret = RegRestoreKeyW( hkey, lpFileW, dwFlags );
1429     HeapFree( GetProcessHeap(), 0, lpFileW );
1430     return ret;
1431 }
1432
1433
1434 /******************************************************************************
1435  * RegUnLoadKeyW [ADVAPI32.@]
1436  *
1437  * PARAMS
1438  *    hkey     [I] Handle of open key
1439  *    lpSubKey [I] Address of name of subkey to unload
1440  */
1441 LONG WINAPI RegUnLoadKeyW( HKEY hkey, LPCWSTR lpSubKey )
1442 {
1443     FIXME("(%x,%s): stub\n",hkey, debugstr_w(lpSubKey));
1444     return ERROR_SUCCESS;
1445 }
1446
1447
1448 /******************************************************************************
1449  * RegUnLoadKeyA [ADVAPI32.@]
1450  */
1451 LONG WINAPI RegUnLoadKeyA( HKEY hkey, LPCSTR lpSubKey )
1452 {
1453     LPWSTR lpSubKeyW = HEAP_strdupAtoW( GetProcessHeap(), 0, lpSubKey );
1454     LONG ret = RegUnLoadKeyW( hkey, lpSubKeyW );
1455     if(lpSubKeyW) HeapFree( GetProcessHeap(), 0, lpSubKeyW);
1456     return ret;
1457 }
1458
1459
1460 /******************************************************************************
1461  * RegReplaceKeyW [ADVAPI32.@]
1462  *
1463  * PARAMS
1464  *    hkey      [I] Handle of open key
1465  *    lpSubKey  [I] Address of name of subkey
1466  *    lpNewFile [I] Address of filename for file with new data
1467  *    lpOldFile [I] Address of filename for backup file
1468  */
1469 LONG WINAPI RegReplaceKeyW( HKEY hkey, LPCWSTR lpSubKey, LPCWSTR lpNewFile,
1470                               LPCWSTR lpOldFile )
1471 {
1472     FIXME("(%x,%s,%s,%s): stub\n", hkey, debugstr_w(lpSubKey),
1473           debugstr_w(lpNewFile),debugstr_w(lpOldFile));
1474     return ERROR_SUCCESS;
1475 }
1476
1477
1478 /******************************************************************************
1479  * RegReplaceKeyA [ADVAPI32.@]
1480  */
1481 LONG WINAPI RegReplaceKeyA( HKEY hkey, LPCSTR lpSubKey, LPCSTR lpNewFile,
1482                               LPCSTR lpOldFile )
1483 {
1484     LPWSTR lpSubKeyW = HEAP_strdupAtoW( GetProcessHeap(), 0, lpSubKey );
1485     LPWSTR lpNewFileW = HEAP_strdupAtoW( GetProcessHeap(), 0, lpNewFile );
1486     LPWSTR lpOldFileW = HEAP_strdupAtoW( GetProcessHeap(), 0, lpOldFile );
1487     LONG ret = RegReplaceKeyW( hkey, lpSubKeyW, lpNewFileW, lpOldFileW );
1488     HeapFree( GetProcessHeap(), 0, lpOldFileW );
1489     HeapFree( GetProcessHeap(), 0, lpNewFileW );
1490     HeapFree( GetProcessHeap(), 0, lpSubKeyW );
1491     return ret;
1492 }
1493
1494
1495 /******************************************************************************
1496  * RegSetKeySecurity [ADVAPI32.@]
1497  *
1498  * PARAMS
1499  *    hkey          [I] Open handle of key to set
1500  *    SecurityInfo  [I] Descriptor contents
1501  *    pSecurityDesc [I] Address of descriptor for key
1502  */
1503 LONG WINAPI RegSetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInfo,
1504                                PSECURITY_DESCRIPTOR pSecurityDesc )
1505 {
1506     TRACE("(%x,%ld,%p)\n",hkey,SecurityInfo,pSecurityDesc);
1507
1508     /* It seems to perform this check before the hkey check */
1509     if ((SecurityInfo & OWNER_SECURITY_INFORMATION) ||
1510         (SecurityInfo & GROUP_SECURITY_INFORMATION) ||
1511         (SecurityInfo & DACL_SECURITY_INFORMATION) ||
1512         (SecurityInfo & SACL_SECURITY_INFORMATION)) {
1513         /* Param OK */
1514     } else
1515         return ERROR_INVALID_PARAMETER;
1516
1517     if (!pSecurityDesc)
1518         return ERROR_INVALID_PARAMETER;
1519
1520     FIXME(":(%x,%ld,%p): stub\n",hkey,SecurityInfo,pSecurityDesc);
1521
1522     return ERROR_SUCCESS;
1523 }
1524
1525
1526 /******************************************************************************
1527  * RegGetKeySecurity [ADVAPI32.@]
1528  * Retrieves a copy of security descriptor protecting the registry key
1529  *
1530  * PARAMS
1531  *    hkey                   [I]   Open handle of key to set
1532  *    SecurityInformation    [I]   Descriptor contents
1533  *    pSecurityDescriptor    [O]   Address of descriptor for key
1534  *    lpcbSecurityDescriptor [I/O] Address of size of buffer and description
1535  *
1536  * RETURNS
1537  *    Success: ERROR_SUCCESS
1538  *    Failure: Error code
1539  */
1540 LONG WINAPI RegGetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInformation,
1541                                PSECURITY_DESCRIPTOR pSecurityDescriptor,
1542                                LPDWORD lpcbSecurityDescriptor )
1543 {
1544     TRACE("(%x,%ld,%p,%ld)\n",hkey,SecurityInformation,pSecurityDescriptor,
1545           lpcbSecurityDescriptor?*lpcbSecurityDescriptor:0);
1546
1547     /* FIXME: Check for valid SecurityInformation values */
1548
1549     if (*lpcbSecurityDescriptor < sizeof(SECURITY_DESCRIPTOR))
1550         return ERROR_INSUFFICIENT_BUFFER;
1551
1552     FIXME("(%x,%ld,%p,%ld): stub\n",hkey,SecurityInformation,
1553           pSecurityDescriptor,lpcbSecurityDescriptor?*lpcbSecurityDescriptor:0);
1554
1555     /* Do not leave security descriptor filled with garbage */
1556     RtlCreateSecurityDescriptor(pSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
1557
1558     return ERROR_SUCCESS;
1559 }
1560
1561
1562 /******************************************************************************
1563  * RegFlushKey [ADVAPI32.@]
1564  * Immediately writes key to registry.
1565  * Only returns after data has been written to disk.
1566  *
1567  * FIXME: does it really wait until data is written ?
1568  *
1569  * PARAMS
1570  *    hkey [I] Handle of key to write
1571  *
1572  * RETURNS
1573  *    Success: ERROR_SUCCESS
1574  *    Failure: Error code
1575  */
1576 DWORD WINAPI RegFlushKey( HKEY hkey )
1577 {
1578     FIXME( "(%x): stub\n", hkey );
1579     return ERROR_SUCCESS;
1580 }
1581
1582
1583 /******************************************************************************
1584  * RegConnectRegistryW [ADVAPI32.@]
1585  *
1586  * PARAMS
1587  *    lpMachineName [I] Address of name of remote computer
1588  *    hHey          [I] Predefined registry handle
1589  *    phkResult     [I] Address of buffer for remote registry handle
1590  */
1591 LONG WINAPI RegConnectRegistryW( LPCWSTR lpMachineName, HKEY hKey,
1592                                    LPHKEY phkResult )
1593 {
1594     TRACE("(%s,%x,%p): stub\n",debugstr_w(lpMachineName),hKey,phkResult);
1595
1596     if (!lpMachineName || !*lpMachineName) {
1597         /* Use the local machine name */
1598         return RegOpenKeyA( hKey, "", phkResult );
1599     }
1600
1601     FIXME("Cannot connect to %s\n",debugstr_w(lpMachineName));
1602     return ERROR_BAD_NETPATH;
1603 }
1604
1605
1606 /******************************************************************************
1607  * RegConnectRegistryA [ADVAPI32.@]
1608  */
1609 LONG WINAPI RegConnectRegistryA( LPCSTR machine, HKEY hkey, LPHKEY reskey )
1610 {
1611     LPWSTR machineW = HEAP_strdupAtoW( GetProcessHeap(), 0, machine );
1612     DWORD ret = RegConnectRegistryW( machineW, hkey, reskey );
1613     HeapFree( GetProcessHeap(), 0, machineW );
1614     return ret;
1615 }
1616
1617
1618 /******************************************************************************
1619  * RegNotifyChangeKeyValue [ADVAPI32.@]
1620  *
1621  * PARAMS
1622  *    hkey            [I] Handle of key to watch
1623  *    fWatchSubTree   [I] Flag for subkey notification
1624  *    fdwNotifyFilter [I] Changes to be reported
1625  *    hEvent          [I] Handle of signaled event
1626  *    fAsync          [I] Flag for asynchronous reporting
1627  */
1628 LONG WINAPI RegNotifyChangeKeyValue( HKEY hkey, BOOL fWatchSubTree,
1629                                      DWORD fdwNotifyFilter, HANDLE hEvent,
1630                                      BOOL fAsync )
1631 {
1632     FIXME("(%x,%i,%ld,%x,%i): stub\n",hkey,fWatchSubTree,fdwNotifyFilter,
1633           hEvent,fAsync);
1634     return ERROR_SUCCESS;
1635 }