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