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