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