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