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