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