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