Add a test for RtlAllocateAndInitializeSid, make it pass under Wine.
[wine] / dlls / ntdll / reg.c
1 /*
2  * Registry functions
3  *
4  * Copyright (C) 1999 Juergen Schmied
5  * Copyright (C) 2000 Alexandre Julliard
6  * Copyright 2005 Ivan Leo Puoti, Laurent Pinchart
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  * NOTES:
23  *      HKEY_LOCAL_MACHINE      \\REGISTRY\\MACHINE
24  *      HKEY_USERS              \\REGISTRY\\USER
25  *      HKEY_CURRENT_CONFIG     \\REGISTRY\\MACHINE\\SYSTEM\\CURRENTCONTROLSET\\HARDWARE PROFILES\\CURRENT
26   *     HKEY_CLASSES            \\REGISTRY\\MACHINE\\SOFTWARE\\CLASSES
27  */
28
29 #include "config.h"
30 #include "wine/port.h"
31
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <string.h>
35
36 #include "wine/library.h"
37 #include "ntdll_misc.h"
38 #include "wine/debug.h"
39 #include "wine/unicode.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(reg);
42
43 /* maximum length of a key/value name in bytes (without terminating null) */
44 #define MAX_NAME_LENGTH ((MAX_PATH-1) * sizeof(WCHAR))
45
46 /******************************************************************************
47  * NtCreateKey [NTDLL.@]
48  * ZwCreateKey [NTDLL.@]
49  */
50 NTSTATUS WINAPI NtCreateKey( PHANDLE retkey, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr,
51                              ULONG TitleIndex, const UNICODE_STRING *class, ULONG options,
52                              PULONG dispos )
53 {
54     NTSTATUS ret;
55
56     TRACE( "(%p,%s,%s,%lx,%lx,%p)\n", attr->RootDirectory, debugstr_us(attr->ObjectName),
57            debugstr_us(class), options, access, retkey );
58
59     if (attr->ObjectName->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
60     if (!retkey) return STATUS_INVALID_PARAMETER;
61
62     SERVER_START_REQ( create_key )
63     {
64         req->parent  = attr->RootDirectory;
65         req->access  = access;
66         req->options = options;
67         req->modif   = 0;
68         req->namelen = attr->ObjectName->Length;
69         wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
70         if (class) wine_server_add_data( req, class->Buffer, class->Length );
71         if (!(ret = wine_server_call( req )))
72         {
73             *retkey = reply->hkey;
74             if (dispos) *dispos = reply->created ? REG_CREATED_NEW_KEY : REG_OPENED_EXISTING_KEY;
75         }
76     }
77     SERVER_END_REQ;
78     TRACE("<- %p\n", *retkey);
79     return ret;
80 }
81
82 /******************************************************************************
83  *  RtlpNtCreateKey [NTDLL.@]
84  *
85  *  See NtCreateKey.
86  */
87 NTSTATUS WINAPI RtlpNtCreateKey( PHANDLE retkey, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr,
88                                  ULONG TitleIndex, const UNICODE_STRING *class, ULONG options,
89                                  PULONG dispos )
90 {
91     OBJECT_ATTRIBUTES oa;
92
93     if (attr)
94     {
95         memcpy( &oa, attr, sizeof oa );
96         oa.Attributes &= ~(OBJ_PERMANENT|OBJ_EXCLUSIVE);
97         attr = &oa;
98     }
99
100     return NtCreateKey(retkey, access, attr, 0, NULL, 0, dispos);
101 }
102
103 /******************************************************************************
104  * NtOpenKey [NTDLL.@]
105  * ZwOpenKey [NTDLL.@]
106  *
107  *   OUT        HANDLE                  retkey (returns 0 when failure)
108  *   IN         ACCESS_MASK             access
109  *   IN         POBJECT_ATTRIBUTES      attr
110  */
111 NTSTATUS WINAPI NtOpenKey( PHANDLE retkey, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr )
112 {
113     NTSTATUS ret;
114     DWORD len = attr->ObjectName->Length;
115
116     TRACE( "(%p,%s,%lx,%p)\n", attr->RootDirectory,
117            debugstr_us(attr->ObjectName), access, retkey );
118
119     if (len > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
120     if (!retkey) return STATUS_INVALID_PARAMETER;
121
122     SERVER_START_REQ( open_key )
123     {
124         req->parent = attr->RootDirectory;
125         req->access = access;
126         wine_server_add_data( req, attr->ObjectName->Buffer, len );
127         ret = wine_server_call( req );
128         *retkey = reply->hkey;
129     }
130     SERVER_END_REQ;
131     TRACE("<- %p\n", *retkey);
132     return ret;
133 }
134
135 /******************************************************************************
136  * RtlpNtOpenKey [NTDLL.@]
137  *
138  * See NtOpenKey.
139  */
140 NTSTATUS WINAPI RtlpNtOpenKey( PHANDLE retkey, ACCESS_MASK access, OBJECT_ATTRIBUTES *attr )
141 {
142     if (attr)
143         attr->Attributes &= ~(OBJ_PERMANENT|OBJ_EXCLUSIVE);
144     return NtOpenKey(retkey, access, attr);
145 }
146
147 /******************************************************************************
148  * NtDeleteKey [NTDLL.@]
149  * ZwDeleteKey [NTDLL.@]
150  */
151 NTSTATUS WINAPI NtDeleteKey( HANDLE hkey )
152 {
153     NTSTATUS ret;
154
155     TRACE( "(%p)\n", hkey );
156
157     SERVER_START_REQ( delete_key )
158     {
159         req->hkey = hkey;
160         ret = wine_server_call( req );
161     }
162     SERVER_END_REQ;
163     return ret;
164 }
165
166 /******************************************************************************
167  * RtlpNtMakeTemporaryKey [NTDLL.@]
168  *
169  *  See NtDeleteKey.
170  */
171 NTSTATUS WINAPI RtlpNtMakeTemporaryKey( HANDLE hkey )
172 {
173     return NtDeleteKey(hkey);
174 }
175
176 /******************************************************************************
177  * NtDeleteValueKey [NTDLL.@]
178  * ZwDeleteValueKey [NTDLL.@]
179  */
180 NTSTATUS WINAPI NtDeleteValueKey( HANDLE hkey, const UNICODE_STRING *name )
181 {
182     NTSTATUS ret;
183
184     TRACE( "(%p,%s)\n", hkey, debugstr_us(name) );
185     if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
186
187     SERVER_START_REQ( delete_key_value )
188     {
189         req->hkey = hkey;
190         wine_server_add_data( req, name->Buffer, name->Length );
191         ret = wine_server_call( req );
192     }
193     SERVER_END_REQ;
194     return ret;
195 }
196
197
198 /******************************************************************************
199  *     enumerate_key
200  *
201  * Implementation of NtQueryKey and NtEnumerateKey
202  */
203 static NTSTATUS enumerate_key( HANDLE handle, int index, KEY_INFORMATION_CLASS info_class,
204                                void *info, DWORD length, DWORD *result_len )
205
206 {
207     NTSTATUS ret;
208     void *data_ptr;
209     size_t fixed_size;
210
211     switch(info_class)
212     {
213     case KeyBasicInformation: data_ptr = ((KEY_BASIC_INFORMATION *)info)->Name; break;
214     case KeyFullInformation:  data_ptr = ((KEY_FULL_INFORMATION *)info)->Class; break;
215     case KeyNodeInformation:  data_ptr = ((KEY_NODE_INFORMATION *)info)->Name;  break;
216     default:
217         FIXME( "Information class %d not implemented\n", info_class );
218         return STATUS_INVALID_PARAMETER;
219     }
220     fixed_size = (char *)data_ptr - (char *)info;
221
222     SERVER_START_REQ( enum_key )
223     {
224         req->hkey       = handle;
225         req->index      = index;
226         req->info_class = info_class;
227         if (length > fixed_size) wine_server_set_reply( req, data_ptr, length - fixed_size );
228         if (!(ret = wine_server_call( req )))
229         {
230             LARGE_INTEGER modif;
231
232             RtlSecondsSince1970ToTime( reply->modif, &modif );
233
234             switch(info_class)
235             {
236             case KeyBasicInformation:
237                 {
238                     KEY_BASIC_INFORMATION keyinfo;
239                     fixed_size = (char *)keyinfo.Name - (char *)&keyinfo;
240                     keyinfo.LastWriteTime = modif;
241                     keyinfo.TitleIndex = 0;
242                     keyinfo.NameLength = reply->namelen;
243                     memcpy( info, &keyinfo, min( length, fixed_size ) );
244                 }
245                 break;
246             case KeyFullInformation:
247                 {
248                     KEY_FULL_INFORMATION keyinfo;
249                     fixed_size = (char *)keyinfo.Class - (char *)&keyinfo;
250                     keyinfo.LastWriteTime = modif;
251                     keyinfo.TitleIndex = 0;
252                     keyinfo.ClassLength = wine_server_reply_size(reply);
253                     keyinfo.ClassOffset = keyinfo.ClassLength ? fixed_size : -1;
254                     keyinfo.SubKeys = reply->subkeys;
255                     keyinfo.MaxNameLen = reply->max_subkey;
256                     keyinfo.MaxClassLen = reply->max_class;
257                     keyinfo.Values = reply->values;
258                     keyinfo.MaxValueNameLen = reply->max_value;
259                     keyinfo.MaxValueDataLen = reply->max_data;
260                     memcpy( info, &keyinfo, min( length, fixed_size ) );
261                 }
262                 break;
263             case KeyNodeInformation:
264                 {
265                     KEY_NODE_INFORMATION keyinfo;
266                     fixed_size = (char *)keyinfo.Name - (char *)&keyinfo;
267                     keyinfo.LastWriteTime = modif;
268                     keyinfo.TitleIndex = 0;
269                     keyinfo.ClassLength = max( 0, wine_server_reply_size(reply) - reply->namelen );
270                     keyinfo.ClassOffset = keyinfo.ClassLength ? fixed_size + reply->namelen : -1;
271                     keyinfo.NameLength = reply->namelen;
272                     memcpy( info, &keyinfo, min( length, fixed_size ) );
273                 }
274                 break;
275             }
276             *result_len = fixed_size + reply->total;
277             if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
278         }
279     }
280     SERVER_END_REQ;
281     return ret;
282 }
283
284
285
286 /******************************************************************************
287  * NtEnumerateKey [NTDLL.@]
288  * ZwEnumerateKey [NTDLL.@]
289  *
290  * NOTES
291  *  the name copied into the buffer is NOT 0-terminated
292  */
293 NTSTATUS WINAPI NtEnumerateKey( HANDLE handle, ULONG index, KEY_INFORMATION_CLASS info_class,
294                                 void *info, DWORD length, DWORD *result_len )
295 {
296     /* -1 means query key, so avoid it here */
297     if (index == (ULONG)-1) return STATUS_NO_MORE_ENTRIES;
298     return enumerate_key( handle, index, info_class, info, length, result_len );
299 }
300
301
302 /******************************************************************************
303  * RtlpNtEnumerateSubKey [NTDLL.@]
304  *
305  */
306 NTSTATUS WINAPI RtlpNtEnumerateSubKey( HANDLE handle, UNICODE_STRING *out, ULONG index )
307 {
308   KEY_BASIC_INFORMATION *info;
309   DWORD dwLen, dwResultLen;
310   NTSTATUS ret;
311
312   if (out->Length)
313   {
314     dwLen = out->Length + sizeof(KEY_BASIC_INFORMATION);
315     info = (KEY_BASIC_INFORMATION*)RtlAllocateHeap( GetProcessHeap(), 0, dwLen );
316     if (!info)
317       return STATUS_NO_MEMORY;
318   }
319   else
320   {
321     dwLen = 0;
322     info = NULL;
323   }
324
325   ret = NtEnumerateKey( handle, index, KeyBasicInformation, info, dwLen, &dwResultLen );
326   dwResultLen -= sizeof(KEY_BASIC_INFORMATION);
327
328   if (ret == STATUS_BUFFER_OVERFLOW)
329     out->Length = dwResultLen;
330   else if (!ret)
331   {
332     if (out->Length < info->NameLength)
333     {
334       out->Length = dwResultLen;
335       ret = STATUS_BUFFER_OVERFLOW;
336     }
337     else
338     {
339       out->Length = info->NameLength;
340       memcpy(out->Buffer, info->Name, info->NameLength);
341     }
342   }
343
344   if (info)
345     RtlFreeHeap( GetProcessHeap(), 0, info );
346   return ret;
347 }
348
349 /******************************************************************************
350  * NtQueryKey [NTDLL.@]
351  * ZwQueryKey [NTDLL.@]
352  */
353 NTSTATUS WINAPI NtQueryKey( HANDLE handle, KEY_INFORMATION_CLASS info_class,
354                             void *info, DWORD length, DWORD *result_len )
355 {
356     return enumerate_key( handle, -1, info_class, info, length, result_len );
357 }
358
359
360 /* fill the key value info structure for a specific info class */
361 static void copy_key_value_info( KEY_VALUE_INFORMATION_CLASS info_class, void *info,
362                                  DWORD length, int type, int name_len, int data_len )
363 {
364     switch(info_class)
365     {
366     case KeyValueBasicInformation:
367         {
368             KEY_VALUE_BASIC_INFORMATION keyinfo;
369             keyinfo.TitleIndex = 0;
370             keyinfo.Type       = type;
371             keyinfo.NameLength = name_len;
372             length = min( length, (char *)keyinfo.Name - (char *)&keyinfo );
373             memcpy( info, &keyinfo, length );
374             break;
375         }
376     case KeyValueFullInformation:
377         {
378             KEY_VALUE_FULL_INFORMATION keyinfo;
379             keyinfo.TitleIndex = 0;
380             keyinfo.Type       = type;
381             keyinfo.DataOffset = (char *)keyinfo.Name - (char *)&keyinfo + name_len;
382             keyinfo.DataLength = data_len;
383             keyinfo.NameLength = name_len;
384             length = min( length, (char *)keyinfo.Name - (char *)&keyinfo );
385             memcpy( info, &keyinfo, length );
386             break;
387         }
388     case KeyValuePartialInformation:
389         {
390             KEY_VALUE_PARTIAL_INFORMATION keyinfo;
391             keyinfo.TitleIndex = 0;
392             keyinfo.Type       = type;
393             keyinfo.DataLength = data_len;
394             length = min( length, (char *)keyinfo.Data - (char *)&keyinfo );
395             memcpy( info, &keyinfo, length );
396             break;
397         }
398     default:
399         break;
400     }
401 }
402
403
404 /******************************************************************************
405  *  NtEnumerateValueKey [NTDLL.@]
406  *  ZwEnumerateValueKey [NTDLL.@]
407  */
408 NTSTATUS WINAPI NtEnumerateValueKey( HANDLE handle, ULONG index,
409                                      KEY_VALUE_INFORMATION_CLASS info_class,
410                                      void *info, DWORD length, DWORD *result_len )
411 {
412     NTSTATUS ret;
413     void *ptr;
414     size_t fixed_size;
415
416     TRACE( "(%p,%lu,%d,%p,%ld)\n", handle, index, info_class, info, length );
417
418     /* compute the length we want to retrieve */
419     switch(info_class)
420     {
421     case KeyValueBasicInformation:   ptr = ((KEY_VALUE_BASIC_INFORMATION *)info)->Name; break;
422     case KeyValueFullInformation:    ptr = ((KEY_VALUE_FULL_INFORMATION *)info)->Name; break;
423     case KeyValuePartialInformation: ptr = ((KEY_VALUE_PARTIAL_INFORMATION *)info)->Data; break;
424     default:
425         FIXME( "Information class %d not implemented\n", info_class );
426         return STATUS_INVALID_PARAMETER;
427     }
428     fixed_size = (char *)ptr - (char *)info;
429
430     SERVER_START_REQ( enum_key_value )
431     {
432         req->hkey       = handle;
433         req->index      = index;
434         req->info_class = info_class;
435         if (length > fixed_size) wine_server_set_reply( req, ptr, length - fixed_size );
436         if (!(ret = wine_server_call( req )))
437         {
438             copy_key_value_info( info_class, info, length, reply->type, reply->namelen,
439                                  wine_server_reply_size(reply) - reply->namelen );
440             *result_len = fixed_size + reply->total;
441             if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
442         }
443     }
444     SERVER_END_REQ;
445     return ret;
446 }
447
448
449 /******************************************************************************
450  * NtQueryValueKey [NTDLL.@]
451  * ZwQueryValueKey [NTDLL.@]
452  *
453  * NOTES
454  *  the name in the KeyValueInformation is never set
455  */
456 NTSTATUS WINAPI NtQueryValueKey( HANDLE handle, const UNICODE_STRING *name,
457                                  KEY_VALUE_INFORMATION_CLASS info_class,
458                                  void *info, DWORD length, DWORD *result_len )
459 {
460     NTSTATUS ret;
461     UCHAR *data_ptr;
462     unsigned int fixed_size = 0;
463
464     TRACE( "(%p,%s,%d,%p,%ld)\n", handle, debugstr_us(name), info_class, info, length );
465
466     if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
467
468     /* compute the length we want to retrieve */
469     switch(info_class)
470     {
471     case KeyValueBasicInformation:
472         fixed_size = (char *)((KEY_VALUE_BASIC_INFORMATION *)info)->Name - (char *)info;
473         data_ptr = NULL;
474         break;
475     case KeyValueFullInformation:
476         data_ptr = (UCHAR *)((KEY_VALUE_FULL_INFORMATION *)info)->Name;
477         fixed_size = (char *)data_ptr - (char *)info;
478         break;
479     case KeyValuePartialInformation:
480         data_ptr = ((KEY_VALUE_PARTIAL_INFORMATION *)info)->Data;
481         fixed_size = (char *)data_ptr - (char *)info;
482         break;
483     default:
484         FIXME( "Information class %d not implemented\n", info_class );
485         return STATUS_INVALID_PARAMETER;
486     }
487
488     SERVER_START_REQ( get_key_value )
489     {
490         req->hkey = handle;
491         wine_server_add_data( req, name->Buffer, name->Length );
492         if (length > fixed_size) wine_server_set_reply( req, data_ptr, length - fixed_size );
493         if (!(ret = wine_server_call( req )))
494         {
495             copy_key_value_info( info_class, info, length, reply->type,
496                                  0, wine_server_reply_size(reply) );
497             *result_len = fixed_size + reply->total;
498             if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
499         }
500     }
501     SERVER_END_REQ;
502     return ret;
503 }
504
505 /******************************************************************************
506  * RtlpNtQueryValueKey [NTDLL.@]
507  *
508  */
509 NTSTATUS WINAPI RtlpNtQueryValueKey( HANDLE handle, ULONG *result_type, PBYTE dest,
510                                      DWORD *result_len )
511 {
512     KEY_VALUE_PARTIAL_INFORMATION *info;
513     UNICODE_STRING name;
514     NTSTATUS ret;
515     DWORD dwResultLen;
516     DWORD dwLen = sizeof (KEY_VALUE_PARTIAL_INFORMATION) + result_len ? *result_len : 0;
517
518     info = (KEY_VALUE_PARTIAL_INFORMATION*)RtlAllocateHeap( GetProcessHeap(), 0, dwLen );
519     if (!info)
520       return STATUS_NO_MEMORY;
521
522     name.Length = 0;
523     ret = NtQueryValueKey( handle, &name, KeyValuePartialInformation, info, dwLen, &dwResultLen );
524
525     if (!ret || ret == STATUS_BUFFER_OVERFLOW)
526     {
527         if (result_len)
528             *result_len = info->DataLength;
529
530         if (result_type)
531             *result_type = info->Type;
532
533         if (ret != STATUS_BUFFER_OVERFLOW)
534             memcpy( dest, info->Data, info->DataLength );
535     }
536
537     RtlFreeHeap( GetProcessHeap(), 0, info );
538     return ret;
539 }
540
541 /******************************************************************************
542  *  NtFlushKey  [NTDLL.@]
543  *  ZwFlushKey  [NTDLL.@]
544  */
545 NTSTATUS WINAPI NtFlushKey(HANDLE key)
546 {
547     NTSTATUS ret;
548
549     TRACE("key=%p\n", key);
550
551     SERVER_START_REQ( flush_key )
552     {
553         req->hkey = key;
554         ret = wine_server_call( req );
555     }
556     SERVER_END_REQ;
557     
558     return ret;
559 }
560
561 /******************************************************************************
562  *  NtLoadKey   [NTDLL.@]
563  *  ZwLoadKey   [NTDLL.@]
564  */
565 NTSTATUS WINAPI NtLoadKey( const OBJECT_ATTRIBUTES *attr, OBJECT_ATTRIBUTES *file )
566 {
567     NTSTATUS ret;
568     HANDLE hive;
569     IO_STATUS_BLOCK io;
570
571     TRACE("(%p,%p)\n", attr, file);
572
573     ret = NtCreateFile(&hive, GENERIC_READ, file, &io, NULL, FILE_ATTRIBUTE_NORMAL, 0,
574                        OPEN_EXISTING, 0, NULL, 0);
575     if (ret) return ret;
576
577     SERVER_START_REQ( load_registry )
578     {
579         req->hkey = attr->RootDirectory;
580         req->file = hive;
581         wine_server_add_data(req, attr->ObjectName->Buffer, attr->ObjectName->Length);
582         ret = wine_server_call( req );
583     }
584     SERVER_END_REQ;
585
586     NtClose(hive);
587    
588     return ret;
589 }
590
591 /******************************************************************************
592  *  NtNotifyChangeKey   [NTDLL.@]
593  *  ZwNotifyChangeKey   [NTDLL.@]
594  */
595 NTSTATUS WINAPI NtNotifyChangeKey(
596         IN HANDLE KeyHandle,
597         IN HANDLE Event,
598         IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
599         IN PVOID ApcContext OPTIONAL,
600         OUT PIO_STATUS_BLOCK IoStatusBlock,
601         IN ULONG CompletionFilter,
602         IN BOOLEAN Asynchronous,
603         OUT PVOID ChangeBuffer,
604         IN ULONG Length,
605         IN BOOLEAN WatchSubtree)
606 {
607     NTSTATUS ret;
608
609     TRACE("(%p,%p,%p,%p,%p,0x%08lx, 0x%08x,%p,0x%08lx,0x%08x)\n",
610         KeyHandle, Event, ApcRoutine, ApcContext, IoStatusBlock, CompletionFilter,
611         Asynchronous, ChangeBuffer, Length, WatchSubtree);
612
613     if (ApcRoutine || ApcContext || ChangeBuffer || Length)
614         FIXME("Unimplemented optional parameter\n");
615
616     if (!Asynchronous)
617     {
618         OBJECT_ATTRIBUTES attr;
619         InitializeObjectAttributes( &attr, NULL, 0, NULL, NULL );
620         ret = NtCreateEvent( &Event, EVENT_ALL_ACCESS, &attr, FALSE, FALSE );
621         if (ret != STATUS_SUCCESS)
622             return ret;
623     }
624
625     SERVER_START_REQ( set_registry_notification )
626     {
627         req->hkey    = KeyHandle;
628         req->event   = Event;
629         req->subtree = WatchSubtree;
630         req->filter  = CompletionFilter;
631         ret = wine_server_call( req );
632     }
633     SERVER_END_REQ;
634  
635     if (!Asynchronous)
636     {
637         if (ret == STATUS_SUCCESS)
638             NtWaitForSingleObject( Event, FALSE, NULL );
639         NtClose( Event );
640     }
641
642     return STATUS_SUCCESS;
643 }
644
645 /******************************************************************************
646  * NtQueryMultipleValueKey [NTDLL]
647  * ZwQueryMultipleValueKey
648  */
649
650 NTSTATUS WINAPI NtQueryMultipleValueKey(
651         HANDLE KeyHandle,
652         PKEY_MULTIPLE_VALUE_INFORMATION ListOfValuesToQuery,
653         ULONG NumberOfItems,
654         PVOID MultipleValueInformation,
655         ULONG Length,
656         PULONG  ReturnLength)
657 {
658         FIXME("(%p,%p,0x%08lx,%p,0x%08lx,%p) stub!\n",
659         KeyHandle, ListOfValuesToQuery, NumberOfItems, MultipleValueInformation,
660         Length,ReturnLength);
661         return STATUS_SUCCESS;
662 }
663
664 /******************************************************************************
665  * NtReplaceKey [NTDLL.@]
666  * ZwReplaceKey [NTDLL.@]
667  */
668 NTSTATUS WINAPI NtReplaceKey(
669         IN POBJECT_ATTRIBUTES ObjectAttributes,
670         IN HANDLE Key,
671         IN POBJECT_ATTRIBUTES ReplacedObjectAttributes)
672 {
673         FIXME("(%p),stub!\n", Key);
674         dump_ObjectAttributes(ObjectAttributes);
675         dump_ObjectAttributes(ReplacedObjectAttributes);
676         return STATUS_SUCCESS;
677 }
678 /******************************************************************************
679  * NtRestoreKey [NTDLL.@]
680  * ZwRestoreKey [NTDLL.@]
681  */
682 NTSTATUS WINAPI NtRestoreKey(
683         HANDLE KeyHandle,
684         HANDLE FileHandle,
685         ULONG RestoreFlags)
686 {
687         FIXME("(%p,%p,0x%08lx) stub\n",
688         KeyHandle, FileHandle, RestoreFlags);
689         return STATUS_SUCCESS;
690 }
691 /******************************************************************************
692  * NtSaveKey [NTDLL.@]
693  * ZwSaveKey [NTDLL.@]
694  */
695 NTSTATUS WINAPI NtSaveKey(IN HANDLE KeyHandle, IN HANDLE FileHandle)
696 {
697     NTSTATUS ret;
698
699     TRACE("(%p,%p)\n", KeyHandle, FileHandle);
700
701     SERVER_START_REQ( save_registry )
702     {
703         req->hkey = KeyHandle;
704         req->file = FileHandle;
705         ret = wine_server_call( req );
706     }
707     SERVER_END_REQ;
708
709     return ret;
710 }
711 /******************************************************************************
712  * NtSetInformationKey [NTDLL.@]
713  * ZwSetInformationKey [NTDLL.@]
714  */
715 NTSTATUS WINAPI NtSetInformationKey(
716         IN HANDLE KeyHandle,
717         IN const int KeyInformationClass,
718         IN PVOID KeyInformation,
719         IN ULONG KeyInformationLength)
720 {
721         FIXME("(%p,0x%08x,%p,0x%08lx) stub\n",
722         KeyHandle, KeyInformationClass, KeyInformation, KeyInformationLength);
723         return STATUS_SUCCESS;
724 }
725
726
727 /******************************************************************************
728  * NtSetValueKey [NTDLL.@]
729  * ZwSetValueKey [NTDLL.@]
730  *
731  * NOTES
732  *   win95 does not care about count for REG_SZ and finds out the len by itself (js)
733  *   NT does definitely care (aj)
734  */
735 NTSTATUS WINAPI NtSetValueKey( HANDLE hkey, const UNICODE_STRING *name, ULONG TitleIndex,
736                                ULONG type, const void *data, ULONG count )
737 {
738     NTSTATUS ret;
739
740     TRACE( "(%p,%s,%ld,%p,%ld)\n", hkey, debugstr_us(name), type, data, count );
741
742     if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
743
744     SERVER_START_REQ( set_key_value )
745     {
746         req->hkey    = hkey;
747         req->type    = type;
748         req->namelen = name->Length;
749         wine_server_add_data( req, name->Buffer, name->Length );
750         wine_server_add_data( req, data, count );
751         ret = wine_server_call( req );
752     }
753     SERVER_END_REQ;
754     return ret;
755 }
756
757 /******************************************************************************
758  * RtlpNtSetValueKey [NTDLL.@]
759  *
760  */
761 NTSTATUS WINAPI RtlpNtSetValueKey( HANDLE hkey, ULONG type, const void *data,
762                                    ULONG count )
763 {
764     UNICODE_STRING name;
765
766     name.Length = 0;
767     return NtSetValueKey( hkey, &name, 0, type, data, count );
768 }
769
770 /******************************************************************************
771  * NtUnloadKey [NTDLL.@]
772  * ZwUnloadKey [NTDLL.@]
773  */
774 NTSTATUS WINAPI NtUnloadKey(IN HANDLE KeyHandle)
775 {
776     NTSTATUS ret;
777
778     TRACE("(%p)\n", KeyHandle);
779
780     SERVER_START_REQ( unload_registry )
781     {
782         req->hkey  = KeyHandle;
783         ret = wine_server_call(req);
784     }
785     SERVER_END_REQ;
786
787     return ret;
788 }
789
790 /******************************************************************************
791  *  RtlFormatCurrentUserKeyPath         [NTDLL.@]
792  *
793  */
794 NTSTATUS WINAPI RtlFormatCurrentUserKeyPath( IN OUT PUNICODE_STRING KeyPath)
795 {
796     static const WCHAR pathW[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\'};
797     HANDLE token;
798     NTSTATUS status;
799
800     status = NtOpenThreadToken(GetCurrentThread(), TOKEN_READ, TRUE, &token);
801     if (status == STATUS_NO_TOKEN)
802         status = NtOpenProcessToken(GetCurrentProcess(), TOKEN_READ, &token);
803     if (status == STATUS_SUCCESS)
804     {
805         char buffer[sizeof(TOKEN_USER) + sizeof(SID) + sizeof(DWORD)*SID_MAX_SUB_AUTHORITIES];
806         DWORD len = sizeof(buffer);
807
808         status = NtQueryInformationToken(token, TokenUser, buffer, len, &len);
809         if (status == STATUS_SUCCESS)
810         {
811             KeyPath->MaximumLength = 0;
812             status = RtlConvertSidToUnicodeString(KeyPath, ((TOKEN_USER *)buffer)->User.Sid, FALSE);
813             if (status == STATUS_BUFFER_OVERFLOW)
814             {
815                 PWCHAR buf = RtlAllocateHeap(GetProcessHeap(), 0,
816                                              sizeof(pathW) + KeyPath->Length + sizeof(WCHAR));
817                 if (buf)
818                 {
819                     memcpy(buf, pathW, sizeof(pathW));
820                     KeyPath->MaximumLength = KeyPath->Length + sizeof(WCHAR);
821                     KeyPath->Buffer = (PWCHAR)((LPBYTE)buf + sizeof(pathW));
822                     status = RtlConvertSidToUnicodeString(KeyPath,
823                                                           ((TOKEN_USER *)buffer)->User.Sid, FALSE);
824                     KeyPath->Buffer = (PWCHAR)buf;
825                     KeyPath->Length += sizeof(pathW);
826                     KeyPath->MaximumLength += sizeof(pathW);
827                 }
828                 else
829                     status = STATUS_NO_MEMORY;
830             }
831         }
832         NtClose(token);
833     }
834     return status;
835 }
836
837 /******************************************************************************
838  *  RtlOpenCurrentUser          [NTDLL.@]
839  *
840  * NOTES
841  *  If we return just HKEY_CURRENT_USER the advapi tries to find a remote
842  *  registry (odd handle) and fails.
843  */
844 DWORD WINAPI RtlOpenCurrentUser(
845         IN ACCESS_MASK DesiredAccess, /* [in] */
846         OUT PHANDLE KeyHandle)        /* [out] handle of HKEY_CURRENT_USER */
847 {
848         OBJECT_ATTRIBUTES ObjectAttributes;
849         UNICODE_STRING ObjectName;
850         NTSTATUS ret;
851
852         TRACE("(0x%08lx, %p)\n",DesiredAccess, KeyHandle);
853
854         RtlFormatCurrentUserKeyPath(&ObjectName);
855         InitializeObjectAttributes(&ObjectAttributes,&ObjectName,OBJ_CASE_INSENSITIVE,0, NULL);
856         ret = NtCreateKey(KeyHandle, DesiredAccess, &ObjectAttributes, 0, NULL, 0, NULL);
857         RtlFreeUnicodeString(&ObjectName);
858         return ret;
859 }
860
861
862 static NTSTATUS RTL_ReportRegistryValue(PKEY_VALUE_FULL_INFORMATION pInfo,
863                                         PRTL_QUERY_REGISTRY_TABLE pQuery, PVOID pContext, PVOID pEnvironment)
864 {
865     PUNICODE_STRING str;
866     UNICODE_STRING src, dst;
867     LONG *bin;
868     ULONG offset;
869     PWSTR wstr;
870     DWORD res;
871     NTSTATUS status = STATUS_SUCCESS;
872     ULONG len;
873     LPWSTR String;
874     INT count = 0;
875
876     if (pInfo == NULL)
877     {
878         if (pQuery->Flags & RTL_QUERY_REGISTRY_DIRECT)
879             return STATUS_INVALID_PARAMETER;
880         else
881         {
882             status = pQuery->QueryRoutine(pQuery->Name, pQuery->DefaultType, pQuery->DefaultData,
883                                           pQuery->DefaultLength, pContext, pQuery->EntryContext);
884         }
885         return status;
886     }
887     len = pInfo->DataLength;
888
889     if (pQuery->Flags & RTL_QUERY_REGISTRY_DIRECT)
890     {
891         str = (PUNICODE_STRING)pQuery->EntryContext;
892  
893         switch(pInfo->Type)
894         {
895         case REG_EXPAND_SZ:
896             if (!(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND))
897             {
898                 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
899                 res = 0;
900                 dst.MaximumLength = 0;
901                 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
902                 dst.Length = 0;
903                 dst.MaximumLength = res;
904                 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
905                 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
906                 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
907                                      dst.Length, pContext, pQuery->EntryContext);
908                 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
909             }
910
911         case REG_SZ:
912         case REG_LINK:
913             if (str->Buffer == NULL)
914                 RtlCreateUnicodeString(str, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
915             else
916                 RtlAppendUnicodeToString(str, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
917             break;
918
919         case REG_MULTI_SZ:
920             if (!(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND))
921                 return STATUS_INVALID_PARAMETER;
922
923             if (str->Buffer == NULL)
924             {
925                 str->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, len);
926                 str->MaximumLength = len;
927             }
928             len = min(len, str->MaximumLength);
929             memcpy(str->Buffer, ((CHAR*)pInfo) + pInfo->DataOffset, len);
930             str->Length = len;
931             break;
932
933         default:
934             bin = (LONG*)pQuery->EntryContext;
935             if (pInfo->DataLength <= sizeof(ULONG))
936                 memcpy(bin, ((CHAR*)pInfo) + pInfo->DataOffset,
937                     pInfo->DataLength);
938             else
939             {
940                 if (bin[0] <= sizeof(ULONG))
941                 {
942                     memcpy(&bin[1], ((CHAR*)pInfo) + pInfo->DataOffset,
943                     min(-bin[0], pInfo->DataLength));
944                 }
945                 else
946                 {
947                    len = min(bin[0], pInfo->DataLength);
948                     bin[1] = len;
949                     bin[2] = pInfo->Type;
950                     memcpy(&bin[3], ((CHAR*)pInfo) + pInfo->DataOffset, len);
951                 }
952            }
953            break;
954         }
955     }
956     else
957     {
958         if((pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND) ||
959            (pInfo->Type != REG_EXPAND_SZ && pInfo->Type != REG_MULTI_SZ))
960         {
961             status = pQuery->QueryRoutine(pInfo->Name, pInfo->Type,
962                 ((CHAR*)pInfo) + pInfo->DataOffset, pInfo->DataLength,
963                 pContext, pQuery->EntryContext);
964         }
965         else if (pInfo->Type == REG_EXPAND_SZ)
966         {
967             RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
968             res = 0;
969             dst.MaximumLength = 0;
970             RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
971             dst.Length = 0;
972             dst.MaximumLength = res;
973             dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
974             RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
975             status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
976                                           dst.Length, pContext, pQuery->EntryContext);
977             RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
978         }
979         else /* REG_MULTI_SZ */
980         {
981             if(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND)
982             {
983                 for (offset = 0; offset <= pInfo->DataLength; offset += len + sizeof(WCHAR))
984                     {
985                     wstr = (WCHAR*)(((CHAR*)pInfo) + offset);
986                     len = strlenW(wstr) * sizeof(WCHAR);
987                     status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, wstr, len,
988                         pContext, pQuery->EntryContext);
989                     if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
990                         return status;
991                     }
992             }
993             else
994             {
995                 while(count<=pInfo->DataLength)
996                 {
997                     String = (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset)+count;
998                     count+=strlenW(String)+1;
999                     RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
1000                     res = 0;
1001                     dst.MaximumLength = 0;
1002                     RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
1003                     dst.Length = 0;
1004                     dst.MaximumLength = res;
1005                     dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
1006                     RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
1007                     status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
1008                                                   dst.Length, pContext, pQuery->EntryContext);
1009                     RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
1010                     if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1011                         return status;
1012                 }
1013             }
1014         }
1015     }
1016     return status;
1017 }
1018
1019
1020 static NTSTATUS RTL_GetKeyHandle(ULONG RelativeTo, PCWSTR Path, PHANDLE handle)
1021 {
1022     UNICODE_STRING KeyString;
1023     OBJECT_ATTRIBUTES regkey;
1024     PCWSTR base;
1025     INT len;
1026     NTSTATUS status;
1027
1028     static const WCHAR empty[] = {0};
1029     static const WCHAR control[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e',
1030     '\\','S','y','s','t','e','m','\\','C','u','r','r','e','n','t',' ','C','o','n','t','r','o','l','S','e','t','\\',
1031     'C','o','n','t','r','o','l','\\',0};
1032
1033     static const WCHAR devicemap[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1034     'H','a','r','d','w','a','r','e','\\','D','e','v','i','c','e','M','a','p','\\',0};
1035
1036     static const WCHAR services[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1037     'S','y','s','t','e','m','\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1038     'S','e','r','v','i','c','e','s','\\',0};
1039
1040     static const WCHAR user[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\',
1041     'C','u','r','r','e','n','t','U','s','e','r','\\',0};
1042
1043     static const WCHAR windows_nt[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1044     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1045     'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',0};
1046
1047     switch (RelativeTo & 0xff)
1048     {
1049     case RTL_REGISTRY_ABSOLUTE:
1050         base = empty;
1051         break;
1052
1053     case RTL_REGISTRY_CONTROL:
1054         base = control;
1055         break;
1056
1057     case RTL_REGISTRY_DEVICEMAP:
1058         base = devicemap;
1059         break;
1060
1061     case RTL_REGISTRY_SERVICES:
1062         base = services;
1063         break;
1064
1065     case RTL_REGISTRY_USER:
1066         base = user;
1067         break;
1068
1069     case RTL_REGISTRY_WINDOWS_NT:
1070         base = windows_nt;
1071         break;
1072
1073     default:
1074         return STATUS_INVALID_PARAMETER;
1075     }
1076
1077     len = (strlenW(base) + strlenW(Path) + 1) * sizeof(WCHAR);
1078     KeyString.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, len);
1079     if (KeyString.Buffer == NULL)
1080         return STATUS_NO_MEMORY;
1081
1082     strcpyW(KeyString.Buffer, base);
1083     strcatW(KeyString.Buffer, Path);
1084     KeyString.Length = len - sizeof(WCHAR);
1085     KeyString.MaximumLength = len;
1086     InitializeObjectAttributes(&regkey, &KeyString, OBJ_CASE_INSENSITIVE, NULL, NULL);
1087     status = NtOpenKey(handle, KEY_ALL_ACCESS, &regkey);
1088     RtlFreeHeap(GetProcessHeap(), 0, KeyString.Buffer);
1089     return status;
1090 }
1091
1092 /*************************************************************************
1093  * RtlQueryRegistryValues   [NTDLL.@]
1094  *
1095  * Query multiple registry values with a signle call.
1096  *
1097  * PARAMS
1098  *  RelativeTo  [I] Registry path that Path refers to
1099  *  Path        [I] Path to key
1100  *  QueryTable  [I] Table of key values to query
1101  *  Context     [I] Paremeter to pass to the application defined QueryRoutine function
1102  *  Environment [I] Optional parameter to use when performing expantion
1103  *
1104  * RETURNS
1105  *  STATUS_SUCCESS or an appropriate NTSTATUS error code.
1106  */
1107 NTSTATUS WINAPI RtlQueryRegistryValues(IN ULONG RelativeTo, IN PCWSTR Path,
1108                                        IN PRTL_QUERY_REGISTRY_TABLE QueryTable, IN PVOID Context,
1109                                        IN PVOID Environment OPTIONAL)
1110 {
1111     UNICODE_STRING Value;
1112     HANDLE handle, topkey;
1113     PKEY_VALUE_FULL_INFORMATION pInfo = NULL;
1114     ULONG len, buflen = 0;
1115     NTSTATUS status=STATUS_SUCCESS, ret = STATUS_SUCCESS;
1116     INT i;
1117
1118     TRACE("(%ld, %s, %p, %p, %p)\n", RelativeTo, debugstr_w(Path), QueryTable, Context, Environment);
1119
1120     if(Path == NULL)
1121         return STATUS_INVALID_PARAMETER;
1122
1123     /* get a valid handle */
1124     if (RelativeTo & RTL_REGISTRY_HANDLE)
1125         topkey = handle = (HANDLE)Path;
1126     else
1127     {
1128         status = RTL_GetKeyHandle(RelativeTo, Path, &topkey);
1129         handle = topkey;
1130     }
1131     if(status != STATUS_SUCCESS)
1132         return status;
1133
1134     /* Process query table entries */
1135     for (; QueryTable->QueryRoutine != NULL || QueryTable->Name != NULL; ++QueryTable)
1136     {
1137         if (QueryTable->Flags &
1138             (RTL_QUERY_REGISTRY_SUBKEY | RTL_QUERY_REGISTRY_TOPKEY))
1139         {
1140             /* topkey must be kept open just in case we will reuse it later */
1141             if (handle != topkey)
1142                 NtClose(handle);
1143
1144             if (QueryTable->Flags & RTL_QUERY_REGISTRY_SUBKEY)
1145             {
1146                 handle = 0;
1147                 status = RTL_GetKeyHandle((ULONG)QueryTable->Name, Path, &handle);
1148                 if(status != STATUS_SUCCESS)
1149                 {
1150                     ret = status;
1151                     goto out;
1152                 }
1153             }
1154             else
1155                 handle = topkey;
1156         }
1157
1158         if (QueryTable->Flags & RTL_QUERY_REGISTRY_NOVALUE)
1159         {
1160             QueryTable->QueryRoutine(QueryTable->Name, REG_NONE, NULL, 0,
1161                 Context, QueryTable->EntryContext);
1162             continue;
1163         }
1164
1165         if (!handle)
1166         {
1167             if (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED)
1168             {
1169                 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1170                 goto out;
1171             }
1172             continue;
1173         }
1174
1175         if (QueryTable->Name == NULL)
1176         {
1177             if (QueryTable->Flags & RTL_QUERY_REGISTRY_DIRECT)
1178             {
1179                 ret = STATUS_INVALID_PARAMETER;
1180                 goto out;
1181             }
1182
1183             /* Report all subkeys */
1184             for (i = 0;; ++i)
1185             {
1186                 status = NtEnumerateValueKey(handle, i,
1187                     KeyValueFullInformation, pInfo, buflen, &len);
1188                 if (status == STATUS_NO_MORE_ENTRIES)
1189                     break;
1190                 if (status == STATUS_BUFFER_OVERFLOW ||
1191                     status == STATUS_BUFFER_TOO_SMALL)
1192                 {
1193                     buflen = len;
1194                     RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1195                     pInfo = (KEY_VALUE_FULL_INFORMATION*)RtlAllocateHeap(
1196                         GetProcessHeap(), 0, buflen);
1197                     NtEnumerateValueKey(handle, i, KeyValueFullInformation,
1198                         pInfo, buflen, &len);
1199                 }
1200
1201                 status = RTL_ReportRegistryValue(pInfo, QueryTable, Context, Environment);
1202                 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1203                 {
1204                     ret = status;
1205                     goto out;
1206                 }
1207                 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DELETE)
1208                 {
1209                     RtlInitUnicodeString(&Value, pInfo->Name);
1210                     NtDeleteValueKey(handle, &Value);
1211                 }
1212             }
1213
1214             if (i == 0  && (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED))
1215             {
1216                 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1217                 goto out;
1218             }
1219         }
1220         else
1221         {
1222             RtlInitUnicodeString(&Value, QueryTable->Name);
1223             status = NtQueryValueKey(handle, &Value, KeyValueFullInformation,
1224                 pInfo, buflen, &len);
1225             if (status == STATUS_BUFFER_OVERFLOW ||
1226                 status == STATUS_BUFFER_TOO_SMALL)
1227             {
1228                 buflen = len;
1229                 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1230                 pInfo = (KEY_VALUE_FULL_INFORMATION*)RtlAllocateHeap(
1231                     GetProcessHeap(), 0, buflen);
1232                 status = NtQueryValueKey(handle, &Value,
1233                     KeyValueFullInformation, pInfo, buflen, &len);
1234             }
1235             if (status != STATUS_SUCCESS)
1236             {
1237                 if (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED)
1238                 {
1239                     ret = STATUS_OBJECT_NAME_NOT_FOUND;
1240                     goto out;
1241                 }
1242                 status = RTL_ReportRegistryValue(NULL, QueryTable, Context, Environment);
1243                 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1244                 {
1245                     ret = status;
1246                     goto out;
1247                 }
1248             }
1249             else
1250             {
1251                 status = RTL_ReportRegistryValue(pInfo, QueryTable, Context, Environment);
1252                 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1253                 {
1254                     ret = status;
1255                     goto out;
1256                 }
1257                 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DELETE)
1258                     NtDeleteValueKey(handle, &Value);
1259             }
1260         }
1261     }
1262
1263 out:
1264     RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1265     if (handle != topkey)
1266         NtClose(handle);
1267     NtClose(topkey);
1268     return ret;
1269 }
1270
1271 /*************************************************************************
1272  * RtlCheckRegistryKey   [NTDLL.@]
1273  *
1274  * Query multiple registry values with a signle call.
1275  *
1276  * PARAMS
1277  *  RelativeTo [I] Registry path that Path refers to
1278  *  Path       [I] Path to key
1279  *
1280  * RETURNS
1281  *  STATUS_SUCCESS if the specified key exists, or an NTSTATUS error code.
1282  */
1283 NTSTATUS WINAPI RtlCheckRegistryKey(IN ULONG RelativeTo, IN PWSTR Path)
1284 {
1285     HANDLE handle;
1286     NTSTATUS status;
1287
1288     TRACE("(%ld, %s)\n", RelativeTo, debugstr_w(Path));
1289
1290     if((!RelativeTo) && Path == NULL)
1291         return STATUS_OBJECT_PATH_SYNTAX_BAD;
1292     if(RelativeTo & RTL_REGISTRY_HANDLE)
1293         return STATUS_SUCCESS;
1294
1295     status = RTL_GetKeyHandle(RelativeTo, Path, &handle);
1296     if (handle) NtClose(handle);
1297     if (status == STATUS_INVALID_HANDLE) status = STATUS_OBJECT_NAME_NOT_FOUND;
1298     return status;
1299 }
1300
1301 /*************************************************************************
1302  * RtlDeleteRegistryValue   [NTDLL.@]
1303  *
1304  * Query multiple registry values with a signle call.
1305  *
1306  * PARAMS
1307  *  RelativeTo [I] Registry path that Path refers to
1308  *  Path       [I] Path to key
1309  *  ValueName  [I] Name of the value to delete
1310  *
1311  * RETURNS
1312  *  STATUS_SUCCESS if the specified key is successfully deleted, or an NTSTATUS error code.
1313  */
1314 NTSTATUS WINAPI RtlDeleteRegistryValue(IN ULONG RelativeTo, IN PCWSTR Path, IN PCWSTR ValueName)
1315 {
1316     NTSTATUS status;
1317     HANDLE handle;
1318     UNICODE_STRING Value;
1319
1320     TRACE("(%ld, %s, %s)\n", RelativeTo, debugstr_w(Path), debugstr_w(ValueName));
1321
1322     RtlInitUnicodeString(&Value, ValueName);
1323     if(RelativeTo == RTL_REGISTRY_HANDLE)
1324     {
1325         return NtDeleteValueKey((HANDLE)Path, &Value);
1326     }
1327     status = RTL_GetKeyHandle(RelativeTo, Path, &handle);
1328     if (status) return status;
1329     status = NtDeleteValueKey(handle, &Value);
1330     NtClose(handle);
1331     return status;
1332 }