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