ntdll: Fix typo in HAVE_STRUCT_MTGET_MT_GSTAT ifdef.
[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   if (info)
349     RtlFreeHeap( GetProcessHeap(), 0, info );
350   return ret;
351 }
352
353 /******************************************************************************
354  * NtQueryKey [NTDLL.@]
355  * ZwQueryKey [NTDLL.@]
356  */
357 NTSTATUS WINAPI NtQueryKey( HANDLE handle, KEY_INFORMATION_CLASS info_class,
358                             void *info, DWORD length, DWORD *result_len )
359 {
360     return enumerate_key( handle, -1, info_class, info, length, result_len );
361 }
362
363
364 /* fill the key value info structure for a specific info class */
365 static void copy_key_value_info( KEY_VALUE_INFORMATION_CLASS info_class, void *info,
366                                  DWORD length, int type, int name_len, int data_len )
367 {
368     switch(info_class)
369     {
370     case KeyValueBasicInformation:
371         {
372             KEY_VALUE_BASIC_INFORMATION keyinfo;
373             keyinfo.TitleIndex = 0;
374             keyinfo.Type       = type;
375             keyinfo.NameLength = name_len;
376             length = min( length, (char *)keyinfo.Name - (char *)&keyinfo );
377             memcpy( info, &keyinfo, length );
378             break;
379         }
380     case KeyValueFullInformation:
381         {
382             KEY_VALUE_FULL_INFORMATION keyinfo;
383             keyinfo.TitleIndex = 0;
384             keyinfo.Type       = type;
385             keyinfo.DataOffset = (char *)keyinfo.Name - (char *)&keyinfo + name_len;
386             keyinfo.DataLength = data_len;
387             keyinfo.NameLength = name_len;
388             length = min( length, (char *)keyinfo.Name - (char *)&keyinfo );
389             memcpy( info, &keyinfo, length );
390             break;
391         }
392     case KeyValuePartialInformation:
393         {
394             KEY_VALUE_PARTIAL_INFORMATION keyinfo;
395             keyinfo.TitleIndex = 0;
396             keyinfo.Type       = type;
397             keyinfo.DataLength = data_len;
398             length = min( length, (char *)keyinfo.Data - (char *)&keyinfo );
399             memcpy( info, &keyinfo, length );
400             break;
401         }
402     default:
403         break;
404     }
405 }
406
407
408 /******************************************************************************
409  *  NtEnumerateValueKey [NTDLL.@]
410  *  ZwEnumerateValueKey [NTDLL.@]
411  */
412 NTSTATUS WINAPI NtEnumerateValueKey( HANDLE handle, ULONG index,
413                                      KEY_VALUE_INFORMATION_CLASS info_class,
414                                      void *info, DWORD length, DWORD *result_len )
415 {
416     NTSTATUS ret;
417     void *ptr;
418     size_t fixed_size;
419
420     TRACE( "(%p,%lu,%d,%p,%ld)\n", handle, index, info_class, info, length );
421
422     /* compute the length we want to retrieve */
423     switch(info_class)
424     {
425     case KeyValueBasicInformation:   ptr = ((KEY_VALUE_BASIC_INFORMATION *)info)->Name; break;
426     case KeyValueFullInformation:    ptr = ((KEY_VALUE_FULL_INFORMATION *)info)->Name; break;
427     case KeyValuePartialInformation: ptr = ((KEY_VALUE_PARTIAL_INFORMATION *)info)->Data; break;
428     default:
429         FIXME( "Information class %d not implemented\n", info_class );
430         return STATUS_INVALID_PARAMETER;
431     }
432     fixed_size = (char *)ptr - (char *)info;
433
434     SERVER_START_REQ( enum_key_value )
435     {
436         req->hkey       = handle;
437         req->index      = index;
438         req->info_class = info_class;
439         if (length > fixed_size) wine_server_set_reply( req, ptr, length - fixed_size );
440         if (!(ret = wine_server_call( req )))
441         {
442             copy_key_value_info( info_class, info, length, reply->type, reply->namelen,
443                                  wine_server_reply_size(reply) - reply->namelen );
444             *result_len = fixed_size + reply->total;
445             if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
446         }
447     }
448     SERVER_END_REQ;
449     return ret;
450 }
451
452
453 /******************************************************************************
454  * NtQueryValueKey [NTDLL.@]
455  * ZwQueryValueKey [NTDLL.@]
456  *
457  * NOTES
458  *  the name in the KeyValueInformation is never set
459  */
460 NTSTATUS WINAPI NtQueryValueKey( HANDLE handle, const UNICODE_STRING *name,
461                                  KEY_VALUE_INFORMATION_CLASS info_class,
462                                  void *info, DWORD length, DWORD *result_len )
463 {
464     NTSTATUS ret;
465     UCHAR *data_ptr;
466     unsigned int fixed_size = 0;
467
468     TRACE( "(%p,%s,%d,%p,%ld)\n", handle, debugstr_us(name), info_class, info, length );
469
470     if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
471
472     /* compute the length we want to retrieve */
473     switch(info_class)
474     {
475     case KeyValueBasicInformation:
476         fixed_size = (char *)((KEY_VALUE_BASIC_INFORMATION *)info)->Name - (char *)info;
477         data_ptr = NULL;
478         break;
479     case KeyValueFullInformation:
480         data_ptr = (UCHAR *)((KEY_VALUE_FULL_INFORMATION *)info)->Name;
481         fixed_size = (char *)data_ptr - (char *)info;
482         break;
483     case KeyValuePartialInformation:
484         data_ptr = ((KEY_VALUE_PARTIAL_INFORMATION *)info)->Data;
485         fixed_size = (char *)data_ptr - (char *)info;
486         break;
487     default:
488         FIXME( "Information class %d not implemented\n", info_class );
489         return STATUS_INVALID_PARAMETER;
490     }
491
492     SERVER_START_REQ( get_key_value )
493     {
494         req->hkey = handle;
495         wine_server_add_data( req, name->Buffer, name->Length );
496         if (length > fixed_size) wine_server_set_reply( req, data_ptr, length - fixed_size );
497         if (!(ret = wine_server_call( req )))
498         {
499             copy_key_value_info( info_class, info, length, reply->type,
500                                  0, wine_server_reply_size(reply) );
501             *result_len = fixed_size + reply->total;
502             if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
503         }
504     }
505     SERVER_END_REQ;
506     return ret;
507 }
508
509 /******************************************************************************
510  * RtlpNtQueryValueKey [NTDLL.@]
511  *
512  */
513 NTSTATUS WINAPI RtlpNtQueryValueKey( HANDLE handle, ULONG *result_type, PBYTE dest,
514                                      DWORD *result_len )
515 {
516     KEY_VALUE_PARTIAL_INFORMATION *info;
517     UNICODE_STRING name;
518     NTSTATUS ret;
519     DWORD dwResultLen;
520     DWORD dwLen = sizeof (KEY_VALUE_PARTIAL_INFORMATION) + result_len ? *result_len : 0;
521
522     info = (KEY_VALUE_PARTIAL_INFORMATION*)RtlAllocateHeap( GetProcessHeap(), 0, dwLen );
523     if (!info)
524       return STATUS_NO_MEMORY;
525
526     name.Length = 0;
527     ret = NtQueryValueKey( handle, &name, KeyValuePartialInformation, info, dwLen, &dwResultLen );
528
529     if (!ret || ret == STATUS_BUFFER_OVERFLOW)
530     {
531         if (result_len)
532             *result_len = info->DataLength;
533
534         if (result_type)
535             *result_type = info->Type;
536
537         if (ret != STATUS_BUFFER_OVERFLOW)
538             memcpy( dest, info->Data, info->DataLength );
539     }
540
541     RtlFreeHeap( GetProcessHeap(), 0, info );
542     return ret;
543 }
544
545 /******************************************************************************
546  *  NtFlushKey  [NTDLL.@]
547  *  ZwFlushKey  [NTDLL.@]
548  */
549 NTSTATUS WINAPI NtFlushKey(HANDLE key)
550 {
551     NTSTATUS ret;
552
553     TRACE("key=%p\n", key);
554
555     SERVER_START_REQ( flush_key )
556     {
557         req->hkey = key;
558         ret = wine_server_call( req );
559     }
560     SERVER_END_REQ;
561     
562     return ret;
563 }
564
565 /******************************************************************************
566  *  NtLoadKey   [NTDLL.@]
567  *  ZwLoadKey   [NTDLL.@]
568  */
569 NTSTATUS WINAPI NtLoadKey( const OBJECT_ATTRIBUTES *attr, OBJECT_ATTRIBUTES *file )
570 {
571     NTSTATUS ret;
572     HANDLE hive;
573     IO_STATUS_BLOCK io;
574
575     TRACE("(%p,%p)\n", attr, file);
576
577     ret = NtCreateFile(&hive, GENERIC_READ, file, &io, NULL, FILE_ATTRIBUTE_NORMAL, 0,
578                        FILE_OPEN, 0, NULL, 0);
579     if (ret) return ret;
580
581     SERVER_START_REQ( load_registry )
582     {
583         req->hkey = attr->RootDirectory;
584         req->file = hive;
585         wine_server_add_data(req, attr->ObjectName->Buffer, attr->ObjectName->Length);
586         ret = wine_server_call( req );
587     }
588     SERVER_END_REQ;
589
590     NtClose(hive);
591    
592     return ret;
593 }
594
595 /******************************************************************************
596  *  NtNotifyChangeKey   [NTDLL.@]
597  *  ZwNotifyChangeKey   [NTDLL.@]
598  */
599 NTSTATUS WINAPI NtNotifyChangeKey(
600         IN HANDLE KeyHandle,
601         IN HANDLE Event,
602         IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
603         IN PVOID ApcContext OPTIONAL,
604         OUT PIO_STATUS_BLOCK IoStatusBlock,
605         IN ULONG CompletionFilter,
606         IN BOOLEAN Asynchronous,
607         OUT PVOID ChangeBuffer,
608         IN ULONG Length,
609         IN BOOLEAN WatchSubtree)
610 {
611     NTSTATUS ret;
612
613     TRACE("(%p,%p,%p,%p,%p,0x%08lx, 0x%08x,%p,0x%08lx,0x%08x)\n",
614         KeyHandle, Event, ApcRoutine, ApcContext, IoStatusBlock, CompletionFilter,
615         Asynchronous, ChangeBuffer, Length, WatchSubtree);
616
617     if (ApcRoutine || ApcContext || ChangeBuffer || Length)
618         FIXME("Unimplemented optional parameter\n");
619
620     if (!Asynchronous)
621     {
622         OBJECT_ATTRIBUTES attr;
623         InitializeObjectAttributes( &attr, NULL, 0, NULL, NULL );
624         ret = NtCreateEvent( &Event, EVENT_ALL_ACCESS, &attr, FALSE, FALSE );
625         if (ret != STATUS_SUCCESS)
626             return ret;
627     }
628
629     SERVER_START_REQ( set_registry_notification )
630     {
631         req->hkey    = KeyHandle;
632         req->event   = Event;
633         req->subtree = WatchSubtree;
634         req->filter  = CompletionFilter;
635         ret = wine_server_call( req );
636     }
637     SERVER_END_REQ;
638  
639     if (!Asynchronous)
640     {
641         if (ret == STATUS_SUCCESS)
642             NtWaitForSingleObject( Event, FALSE, NULL );
643         NtClose( Event );
644     }
645
646     return STATUS_SUCCESS;
647 }
648
649 /******************************************************************************
650  * NtQueryMultipleValueKey [NTDLL]
651  * ZwQueryMultipleValueKey
652  */
653
654 NTSTATUS WINAPI NtQueryMultipleValueKey(
655         HANDLE KeyHandle,
656         PKEY_MULTIPLE_VALUE_INFORMATION ListOfValuesToQuery,
657         ULONG NumberOfItems,
658         PVOID MultipleValueInformation,
659         ULONG Length,
660         PULONG  ReturnLength)
661 {
662         FIXME("(%p,%p,0x%08lx,%p,0x%08lx,%p) stub!\n",
663         KeyHandle, ListOfValuesToQuery, NumberOfItems, MultipleValueInformation,
664         Length,ReturnLength);
665         return STATUS_SUCCESS;
666 }
667
668 /******************************************************************************
669  * NtReplaceKey [NTDLL.@]
670  * ZwReplaceKey [NTDLL.@]
671  */
672 NTSTATUS WINAPI NtReplaceKey(
673         IN POBJECT_ATTRIBUTES ObjectAttributes,
674         IN HANDLE Key,
675         IN POBJECT_ATTRIBUTES ReplacedObjectAttributes)
676 {
677         FIXME("(%p),stub!\n", Key);
678         dump_ObjectAttributes(ObjectAttributes);
679         dump_ObjectAttributes(ReplacedObjectAttributes);
680         return STATUS_SUCCESS;
681 }
682 /******************************************************************************
683  * NtRestoreKey [NTDLL.@]
684  * ZwRestoreKey [NTDLL.@]
685  */
686 NTSTATUS WINAPI NtRestoreKey(
687         HANDLE KeyHandle,
688         HANDLE FileHandle,
689         ULONG RestoreFlags)
690 {
691         FIXME("(%p,%p,0x%08lx) stub\n",
692         KeyHandle, FileHandle, RestoreFlags);
693         return STATUS_SUCCESS;
694 }
695 /******************************************************************************
696  * NtSaveKey [NTDLL.@]
697  * ZwSaveKey [NTDLL.@]
698  */
699 NTSTATUS WINAPI NtSaveKey(IN HANDLE KeyHandle, IN HANDLE FileHandle)
700 {
701     NTSTATUS ret;
702
703     TRACE("(%p,%p)\n", KeyHandle, FileHandle);
704
705     SERVER_START_REQ( save_registry )
706     {
707         req->hkey = KeyHandle;
708         req->file = FileHandle;
709         ret = wine_server_call( req );
710     }
711     SERVER_END_REQ;
712
713     return ret;
714 }
715 /******************************************************************************
716  * NtSetInformationKey [NTDLL.@]
717  * ZwSetInformationKey [NTDLL.@]
718  */
719 NTSTATUS WINAPI NtSetInformationKey(
720         IN HANDLE KeyHandle,
721         IN const int KeyInformationClass,
722         IN PVOID KeyInformation,
723         IN ULONG KeyInformationLength)
724 {
725         FIXME("(%p,0x%08x,%p,0x%08lx) stub\n",
726         KeyHandle, KeyInformationClass, KeyInformation, KeyInformationLength);
727         return STATUS_SUCCESS;
728 }
729
730
731 /******************************************************************************
732  * NtSetValueKey [NTDLL.@]
733  * ZwSetValueKey [NTDLL.@]
734  *
735  * NOTES
736  *   win95 does not care about count for REG_SZ and finds out the len by itself (js)
737  *   NT does definitely care (aj)
738  */
739 NTSTATUS WINAPI NtSetValueKey( HANDLE hkey, const UNICODE_STRING *name, ULONG TitleIndex,
740                                ULONG type, const void *data, ULONG count )
741 {
742     NTSTATUS ret;
743
744     TRACE( "(%p,%s,%ld,%p,%ld)\n", hkey, debugstr_us(name), type, data, count );
745
746     if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
747
748     SERVER_START_REQ( set_key_value )
749     {
750         req->hkey    = hkey;
751         req->type    = type;
752         req->namelen = name->Length;
753         wine_server_add_data( req, name->Buffer, name->Length );
754         wine_server_add_data( req, data, count );
755         ret = wine_server_call( req );
756     }
757     SERVER_END_REQ;
758     return ret;
759 }
760
761 /******************************************************************************
762  * RtlpNtSetValueKey [NTDLL.@]
763  *
764  */
765 NTSTATUS WINAPI RtlpNtSetValueKey( HANDLE hkey, ULONG type, const void *data,
766                                    ULONG count )
767 {
768     UNICODE_STRING name;
769
770     name.Length = 0;
771     return NtSetValueKey( hkey, &name, 0, type, data, count );
772 }
773
774 /******************************************************************************
775  * NtUnloadKey [NTDLL.@]
776  * ZwUnloadKey [NTDLL.@]
777  */
778 NTSTATUS WINAPI NtUnloadKey(IN HANDLE KeyHandle)
779 {
780     NTSTATUS ret;
781
782     TRACE("(%p)\n", KeyHandle);
783
784     SERVER_START_REQ( unload_registry )
785     {
786         req->hkey  = KeyHandle;
787         ret = wine_server_call(req);
788     }
789     SERVER_END_REQ;
790
791     return ret;
792 }
793
794 /******************************************************************************
795  *  RtlFormatCurrentUserKeyPath         [NTDLL.@]
796  *
797  */
798 NTSTATUS WINAPI RtlFormatCurrentUserKeyPath( IN OUT PUNICODE_STRING KeyPath)
799 {
800     static const WCHAR pathW[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\'};
801     HANDLE token;
802     NTSTATUS status;
803
804     status = NtOpenThreadToken(GetCurrentThread(), TOKEN_READ, TRUE, &token);
805     if (status == STATUS_NO_TOKEN)
806         status = NtOpenProcessToken(GetCurrentProcess(), TOKEN_READ, &token);
807     if (status == STATUS_SUCCESS)
808     {
809         char buffer[sizeof(TOKEN_USER) + sizeof(SID) + sizeof(DWORD)*SID_MAX_SUB_AUTHORITIES];
810         DWORD len = sizeof(buffer);
811
812         status = NtQueryInformationToken(token, TokenUser, buffer, len, &len);
813         if (status == STATUS_SUCCESS)
814         {
815             KeyPath->MaximumLength = 0;
816             status = RtlConvertSidToUnicodeString(KeyPath, ((TOKEN_USER *)buffer)->User.Sid, FALSE);
817             if (status == STATUS_BUFFER_OVERFLOW)
818             {
819                 PWCHAR buf = RtlAllocateHeap(GetProcessHeap(), 0,
820                                              sizeof(pathW) + KeyPath->Length + sizeof(WCHAR));
821                 if (buf)
822                 {
823                     memcpy(buf, pathW, sizeof(pathW));
824                     KeyPath->MaximumLength = KeyPath->Length + sizeof(WCHAR);
825                     KeyPath->Buffer = (PWCHAR)((LPBYTE)buf + sizeof(pathW));
826                     status = RtlConvertSidToUnicodeString(KeyPath,
827                                                           ((TOKEN_USER *)buffer)->User.Sid, FALSE);
828                     KeyPath->Buffer = (PWCHAR)buf;
829                     KeyPath->Length += sizeof(pathW);
830                     KeyPath->MaximumLength += sizeof(pathW);
831                 }
832                 else
833                     status = STATUS_NO_MEMORY;
834             }
835         }
836         NtClose(token);
837     }
838     return status;
839 }
840
841 /******************************************************************************
842  *  RtlOpenCurrentUser          [NTDLL.@]
843  *
844  * NOTES
845  *  If we return just HKEY_CURRENT_USER the advapi tries to find a remote
846  *  registry (odd handle) and fails.
847  */
848 NTSTATUS WINAPI RtlOpenCurrentUser(
849         IN ACCESS_MASK DesiredAccess, /* [in] */
850         OUT PHANDLE KeyHandle)        /* [out] handle of HKEY_CURRENT_USER */
851 {
852         OBJECT_ATTRIBUTES ObjectAttributes;
853         UNICODE_STRING ObjectName;
854         NTSTATUS ret;
855
856         TRACE("(0x%08lx, %p)\n",DesiredAccess, KeyHandle);
857
858         if ((ret = RtlFormatCurrentUserKeyPath(&ObjectName))) return ret;
859         InitializeObjectAttributes(&ObjectAttributes,&ObjectName,OBJ_CASE_INSENSITIVE,0, NULL);
860         ret = NtCreateKey(KeyHandle, DesiredAccess, &ObjectAttributes, 0, NULL, 0, NULL);
861         RtlFreeUnicodeString(&ObjectName);
862         return ret;
863 }
864
865
866 static NTSTATUS RTL_ReportRegistryValue(PKEY_VALUE_FULL_INFORMATION pInfo,
867                                         PRTL_QUERY_REGISTRY_TABLE pQuery, PVOID pContext, PVOID pEnvironment)
868 {
869     PUNICODE_STRING str;
870     UNICODE_STRING src, dst;
871     LONG *bin;
872     ULONG offset;
873     PWSTR wstr;
874     DWORD res;
875     NTSTATUS status = STATUS_SUCCESS;
876     ULONG len;
877     LPWSTR String;
878     INT count = 0;
879
880     if (pInfo == NULL)
881     {
882         if (pQuery->Flags & RTL_QUERY_REGISTRY_DIRECT)
883             return STATUS_INVALID_PARAMETER;
884         else
885         {
886             status = pQuery->QueryRoutine(pQuery->Name, pQuery->DefaultType, pQuery->DefaultData,
887                                           pQuery->DefaultLength, pContext, pQuery->EntryContext);
888         }
889         return status;
890     }
891     len = pInfo->DataLength;
892
893     if (pQuery->Flags & RTL_QUERY_REGISTRY_DIRECT)
894     {
895         str = (PUNICODE_STRING)pQuery->EntryContext;
896  
897         switch(pInfo->Type)
898         {
899         case REG_EXPAND_SZ:
900             if (!(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND))
901             {
902                 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
903                 res = 0;
904                 dst.MaximumLength = 0;
905                 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
906                 dst.Length = 0;
907                 dst.MaximumLength = res;
908                 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
909                 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
910                 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
911                                      dst.Length, pContext, pQuery->EntryContext);
912                 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
913             }
914
915         case REG_SZ:
916         case REG_LINK:
917             if (str->Buffer == NULL)
918                 RtlCreateUnicodeString(str, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
919             else
920                 RtlAppendUnicodeToString(str, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
921             break;
922
923         case REG_MULTI_SZ:
924             if (!(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND))
925                 return STATUS_INVALID_PARAMETER;
926
927             if (str->Buffer == NULL)
928             {
929                 str->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, len);
930                 str->MaximumLength = len;
931             }
932             len = min(len, str->MaximumLength);
933             memcpy(str->Buffer, ((CHAR*)pInfo) + pInfo->DataOffset, len);
934             str->Length = len;
935             break;
936
937         default:
938             bin = (LONG*)pQuery->EntryContext;
939             if (pInfo->DataLength <= sizeof(ULONG))
940                 memcpy(bin, ((CHAR*)pInfo) + pInfo->DataOffset,
941                     pInfo->DataLength);
942             else
943             {
944                 if (bin[0] <= sizeof(ULONG))
945                 {
946                     memcpy(&bin[1], ((CHAR*)pInfo) + pInfo->DataOffset,
947                     min(-bin[0], pInfo->DataLength));
948                 }
949                 else
950                 {
951                    len = min(bin[0], pInfo->DataLength);
952                     bin[1] = len;
953                     bin[2] = pInfo->Type;
954                     memcpy(&bin[3], ((CHAR*)pInfo) + pInfo->DataOffset, len);
955                 }
956            }
957            break;
958         }
959     }
960     else
961     {
962         if((pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND) ||
963            (pInfo->Type != REG_EXPAND_SZ && pInfo->Type != REG_MULTI_SZ))
964         {
965             status = pQuery->QueryRoutine(pInfo->Name, pInfo->Type,
966                 ((CHAR*)pInfo) + pInfo->DataOffset, pInfo->DataLength,
967                 pContext, pQuery->EntryContext);
968         }
969         else if (pInfo->Type == REG_EXPAND_SZ)
970         {
971             RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
972             res = 0;
973             dst.MaximumLength = 0;
974             RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
975             dst.Length = 0;
976             dst.MaximumLength = res;
977             dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
978             RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
979             status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
980                                           dst.Length, pContext, pQuery->EntryContext);
981             RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
982         }
983         else /* REG_MULTI_SZ */
984         {
985             if(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND)
986             {
987                 for (offset = 0; offset <= pInfo->DataLength; offset += len + sizeof(WCHAR))
988                     {
989                     wstr = (WCHAR*)(((CHAR*)pInfo) + offset);
990                     len = strlenW(wstr) * sizeof(WCHAR);
991                     status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, wstr, len,
992                         pContext, pQuery->EntryContext);
993                     if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
994                         return status;
995                     }
996             }
997             else
998             {
999                 while(count<=pInfo->DataLength)
1000                 {
1001                     String = (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset)+count;
1002                     count+=strlenW(String)+1;
1003                     RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
1004                     res = 0;
1005                     dst.MaximumLength = 0;
1006                     RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
1007                     dst.Length = 0;
1008                     dst.MaximumLength = res;
1009                     dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
1010                     RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
1011                     status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
1012                                                   dst.Length, pContext, pQuery->EntryContext);
1013                     RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
1014                     if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1015                         return status;
1016                 }
1017             }
1018         }
1019     }
1020     return status;
1021 }
1022
1023
1024 static NTSTATUS RTL_GetKeyHandle(ULONG RelativeTo, PCWSTR Path, PHANDLE handle)
1025 {
1026     UNICODE_STRING KeyString;
1027     OBJECT_ATTRIBUTES regkey;
1028     PCWSTR base;
1029     INT len;
1030     NTSTATUS status;
1031
1032     static const WCHAR empty[] = {0};
1033     static const WCHAR control[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e',
1034     '\\','S','y','s','t','e','m','\\','C','u','r','r','e','n','t',' ','C','o','n','t','r','o','l','S','e','t','\\',
1035     'C','o','n','t','r','o','l','\\',0};
1036
1037     static const WCHAR devicemap[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1038     'H','a','r','d','w','a','r','e','\\','D','e','v','i','c','e','M','a','p','\\',0};
1039
1040     static const WCHAR services[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1041     'S','y','s','t','e','m','\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1042     'S','e','r','v','i','c','e','s','\\',0};
1043
1044     static const WCHAR user[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\',
1045     'C','u','r','r','e','n','t','U','s','e','r','\\',0};
1046
1047     static const WCHAR windows_nt[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1048     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1049     'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',0};
1050
1051     switch (RelativeTo & 0xff)
1052     {
1053     case RTL_REGISTRY_ABSOLUTE:
1054         base = empty;
1055         break;
1056
1057     case RTL_REGISTRY_CONTROL:
1058         base = control;
1059         break;
1060
1061     case RTL_REGISTRY_DEVICEMAP:
1062         base = devicemap;
1063         break;
1064
1065     case RTL_REGISTRY_SERVICES:
1066         base = services;
1067         break;
1068
1069     case RTL_REGISTRY_USER:
1070         base = user;
1071         break;
1072
1073     case RTL_REGISTRY_WINDOWS_NT:
1074         base = windows_nt;
1075         break;
1076
1077     default:
1078         return STATUS_INVALID_PARAMETER;
1079     }
1080
1081     len = (strlenW(base) + strlenW(Path) + 1) * sizeof(WCHAR);
1082     KeyString.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, len);
1083     if (KeyString.Buffer == NULL)
1084         return STATUS_NO_MEMORY;
1085
1086     strcpyW(KeyString.Buffer, base);
1087     strcatW(KeyString.Buffer, Path);
1088     KeyString.Length = len - sizeof(WCHAR);
1089     KeyString.MaximumLength = len;
1090     InitializeObjectAttributes(&regkey, &KeyString, OBJ_CASE_INSENSITIVE, NULL, NULL);
1091     status = NtOpenKey(handle, KEY_ALL_ACCESS, &regkey);
1092     RtlFreeHeap(GetProcessHeap(), 0, KeyString.Buffer);
1093     return status;
1094 }
1095
1096 /*************************************************************************
1097  * RtlQueryRegistryValues   [NTDLL.@]
1098  *
1099  * Query multiple registry values with a signle call.
1100  *
1101  * PARAMS
1102  *  RelativeTo  [I] Registry path that Path refers to
1103  *  Path        [I] Path to key
1104  *  QueryTable  [I] Table of key values to query
1105  *  Context     [I] Paremeter to pass to the application defined QueryRoutine function
1106  *  Environment [I] Optional parameter to use when performing expantion
1107  *
1108  * RETURNS
1109  *  STATUS_SUCCESS or an appropriate NTSTATUS error code.
1110  */
1111 NTSTATUS WINAPI RtlQueryRegistryValues(IN ULONG RelativeTo, IN PCWSTR Path,
1112                                        IN PRTL_QUERY_REGISTRY_TABLE QueryTable, IN PVOID Context,
1113                                        IN PVOID Environment OPTIONAL)
1114 {
1115     UNICODE_STRING Value;
1116     HANDLE handle, topkey;
1117     PKEY_VALUE_FULL_INFORMATION pInfo = NULL;
1118     ULONG len, buflen = 0;
1119     NTSTATUS status=STATUS_SUCCESS, ret = STATUS_SUCCESS;
1120     INT i;
1121
1122     TRACE("(%ld, %s, %p, %p, %p)\n", RelativeTo, debugstr_w(Path), QueryTable, Context, Environment);
1123
1124     if(Path == NULL)
1125         return STATUS_INVALID_PARAMETER;
1126
1127     /* get a valid handle */
1128     if (RelativeTo & RTL_REGISTRY_HANDLE)
1129         topkey = handle = (HANDLE)Path;
1130     else
1131     {
1132         status = RTL_GetKeyHandle(RelativeTo, Path, &topkey);
1133         handle = topkey;
1134     }
1135     if(status != STATUS_SUCCESS)
1136         return status;
1137
1138     /* Process query table entries */
1139     for (; QueryTable->QueryRoutine != NULL || QueryTable->Name != NULL; ++QueryTable)
1140     {
1141         if (QueryTable->Flags &
1142             (RTL_QUERY_REGISTRY_SUBKEY | RTL_QUERY_REGISTRY_TOPKEY))
1143         {
1144             /* topkey must be kept open just in case we will reuse it later */
1145             if (handle != topkey)
1146                 NtClose(handle);
1147
1148             if (QueryTable->Flags & RTL_QUERY_REGISTRY_SUBKEY)
1149             {
1150                 handle = 0;
1151                 status = RTL_GetKeyHandle((ULONG)QueryTable->Name, Path, &handle);
1152                 if(status != STATUS_SUCCESS)
1153                 {
1154                     ret = status;
1155                     goto out;
1156                 }
1157             }
1158             else
1159                 handle = topkey;
1160         }
1161
1162         if (QueryTable->Flags & RTL_QUERY_REGISTRY_NOVALUE)
1163         {
1164             QueryTable->QueryRoutine(QueryTable->Name, REG_NONE, NULL, 0,
1165                 Context, QueryTable->EntryContext);
1166             continue;
1167         }
1168
1169         if (!handle)
1170         {
1171             if (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED)
1172             {
1173                 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1174                 goto out;
1175             }
1176             continue;
1177         }
1178
1179         if (QueryTable->Name == NULL)
1180         {
1181             if (QueryTable->Flags & RTL_QUERY_REGISTRY_DIRECT)
1182             {
1183                 ret = STATUS_INVALID_PARAMETER;
1184                 goto out;
1185             }
1186
1187             /* Report all subkeys */
1188             for (i = 0;; ++i)
1189             {
1190                 status = NtEnumerateValueKey(handle, i,
1191                     KeyValueFullInformation, pInfo, buflen, &len);
1192                 if (status == STATUS_NO_MORE_ENTRIES)
1193                     break;
1194                 if (status == STATUS_BUFFER_OVERFLOW ||
1195                     status == STATUS_BUFFER_TOO_SMALL)
1196                 {
1197                     buflen = len;
1198                     RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1199                     pInfo = (KEY_VALUE_FULL_INFORMATION*)RtlAllocateHeap(
1200                         GetProcessHeap(), 0, buflen);
1201                     NtEnumerateValueKey(handle, i, KeyValueFullInformation,
1202                         pInfo, buflen, &len);
1203                 }
1204
1205                 status = RTL_ReportRegistryValue(pInfo, QueryTable, Context, Environment);
1206                 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1207                 {
1208                     ret = status;
1209                     goto out;
1210                 }
1211                 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DELETE)
1212                 {
1213                     RtlInitUnicodeString(&Value, pInfo->Name);
1214                     NtDeleteValueKey(handle, &Value);
1215                 }
1216             }
1217
1218             if (i == 0  && (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED))
1219             {
1220                 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1221                 goto out;
1222             }
1223         }
1224         else
1225         {
1226             RtlInitUnicodeString(&Value, QueryTable->Name);
1227             status = NtQueryValueKey(handle, &Value, KeyValueFullInformation,
1228                 pInfo, buflen, &len);
1229             if (status == STATUS_BUFFER_OVERFLOW ||
1230                 status == STATUS_BUFFER_TOO_SMALL)
1231             {
1232                 buflen = len;
1233                 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1234                 pInfo = (KEY_VALUE_FULL_INFORMATION*)RtlAllocateHeap(
1235                     GetProcessHeap(), 0, buflen);
1236                 status = NtQueryValueKey(handle, &Value,
1237                     KeyValueFullInformation, pInfo, buflen, &len);
1238             }
1239             if (status != STATUS_SUCCESS)
1240             {
1241                 if (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED)
1242                 {
1243                     ret = STATUS_OBJECT_NAME_NOT_FOUND;
1244                     goto out;
1245                 }
1246                 status = RTL_ReportRegistryValue(NULL, QueryTable, Context, Environment);
1247                 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1248                 {
1249                     ret = status;
1250                     goto out;
1251                 }
1252             }
1253             else
1254             {
1255                 status = RTL_ReportRegistryValue(pInfo, QueryTable, Context, Environment);
1256                 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1257                 {
1258                     ret = status;
1259                     goto out;
1260                 }
1261                 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DELETE)
1262                     NtDeleteValueKey(handle, &Value);
1263             }
1264         }
1265     }
1266
1267 out:
1268     RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1269     if (handle != topkey)
1270         NtClose(handle);
1271     NtClose(topkey);
1272     return ret;
1273 }
1274
1275 /*************************************************************************
1276  * RtlCheckRegistryKey   [NTDLL.@]
1277  *
1278  * Query multiple registry values with a signle call.
1279  *
1280  * PARAMS
1281  *  RelativeTo [I] Registry path that Path refers to
1282  *  Path       [I] Path to key
1283  *
1284  * RETURNS
1285  *  STATUS_SUCCESS if the specified key exists, or an NTSTATUS error code.
1286  */
1287 NTSTATUS WINAPI RtlCheckRegistryKey(IN ULONG RelativeTo, IN PWSTR Path)
1288 {
1289     HANDLE handle;
1290     NTSTATUS status;
1291
1292     TRACE("(%ld, %s)\n", RelativeTo, debugstr_w(Path));
1293
1294     if((!RelativeTo) && Path == NULL)
1295         return STATUS_OBJECT_PATH_SYNTAX_BAD;
1296     if(RelativeTo & RTL_REGISTRY_HANDLE)
1297         return STATUS_SUCCESS;
1298
1299     status = RTL_GetKeyHandle(RelativeTo, Path, &handle);
1300     if (handle) NtClose(handle);
1301     if (status == STATUS_INVALID_HANDLE) status = STATUS_OBJECT_NAME_NOT_FOUND;
1302     return status;
1303 }
1304
1305 /*************************************************************************
1306  * RtlDeleteRegistryValue   [NTDLL.@]
1307  *
1308  * Query multiple registry values with a signle call.
1309  *
1310  * PARAMS
1311  *  RelativeTo [I] Registry path that Path refers to
1312  *  Path       [I] Path to key
1313  *  ValueName  [I] Name of the value to delete
1314  *
1315  * RETURNS
1316  *  STATUS_SUCCESS if the specified key is successfully deleted, or an NTSTATUS error code.
1317  */
1318 NTSTATUS WINAPI RtlDeleteRegistryValue(IN ULONG RelativeTo, IN PCWSTR Path, IN PCWSTR ValueName)
1319 {
1320     NTSTATUS status;
1321     HANDLE handle;
1322     UNICODE_STRING Value;
1323
1324     TRACE("(%ld, %s, %s)\n", RelativeTo, debugstr_w(Path), debugstr_w(ValueName));
1325
1326     RtlInitUnicodeString(&Value, ValueName);
1327     if(RelativeTo == RTL_REGISTRY_HANDLE)
1328     {
1329         return NtDeleteValueKey((HANDLE)Path, &Value);
1330     }
1331     status = RTL_GetKeyHandle(RelativeTo, Path, &handle);
1332     if (status) return status;
1333     status = NtDeleteValueKey(handle, &Value);
1334     NtClose(handle);
1335     return status;
1336 }