4 * Copyright (C) 1999 Juergen Schmied
5 * Copyright (C) 2000 Alexandre Julliard
6 * Copyright 2005 Ivan Leo Puoti, Laurent Pinchart
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.
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.
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
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
30 #include "wine/port.h"
36 #include "wine/library.h"
37 #include "ntdll_misc.h"
38 #include "wine/debug.h"
39 #include "wine/unicode.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(reg);
43 /* maximum length of a key/value name in bytes (without terminating null) */
44 #define MAX_NAME_LENGTH ((MAX_PATH-1) * sizeof(WCHAR))
46 /******************************************************************************
47 * NtCreateKey [NTDLL.@]
48 * ZwCreateKey [NTDLL.@]
50 NTSTATUS WINAPI NtCreateKey( PHANDLE retkey, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr,
51 ULONG TitleIndex, const UNICODE_STRING *class, ULONG options,
56 TRACE( "(%p,%s,%s,%lx,%lx,%p)\n", attr->RootDirectory, debugstr_us(attr->ObjectName),
57 debugstr_us(class), options, access, retkey );
59 if (attr->ObjectName->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
60 if (!retkey) return STATUS_INVALID_PARAMETER;
62 SERVER_START_REQ( create_key )
64 req->parent = attr->RootDirectory;
66 req->options = options;
68 req->namelen = attr->ObjectName->Length;
69 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
70 if (class) wine_server_add_data( req, class->Buffer, class->Length );
71 if (!(ret = wine_server_call( req )))
73 *retkey = reply->hkey;
74 if (dispos) *dispos = reply->created ? REG_CREATED_NEW_KEY : REG_OPENED_EXISTING_KEY;
78 TRACE("<- %p\n", *retkey);
82 /******************************************************************************
83 * RtlpNtCreateKey [NTDLL.@]
87 NTSTATUS WINAPI RtlpNtCreateKey( PHANDLE retkey, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr,
88 ULONG TitleIndex, const UNICODE_STRING *class, ULONG options,
95 memcpy( &oa, attr, sizeof oa );
96 oa.Attributes &= ~(OBJ_PERMANENT|OBJ_EXCLUSIVE);
100 return NtCreateKey(retkey, access, attr, 0, NULL, 0, dispos);
103 /******************************************************************************
104 * NtOpenKey [NTDLL.@]
105 * ZwOpenKey [NTDLL.@]
107 * OUT HANDLE retkey (returns 0 when failure)
108 * IN ACCESS_MASK access
109 * IN POBJECT_ATTRIBUTES attr
111 NTSTATUS WINAPI NtOpenKey( PHANDLE retkey, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr )
114 DWORD len = attr->ObjectName->Length;
116 TRACE( "(%p,%s,%lx,%p)\n", attr->RootDirectory,
117 debugstr_us(attr->ObjectName), access, retkey );
119 if (len > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
120 if (!retkey) return STATUS_INVALID_PARAMETER;
122 SERVER_START_REQ( open_key )
124 req->parent = attr->RootDirectory;
125 req->access = access;
126 wine_server_add_data( req, attr->ObjectName->Buffer, len );
127 ret = wine_server_call( req );
128 *retkey = reply->hkey;
131 TRACE("<- %p\n", *retkey);
135 /******************************************************************************
136 * RtlpNtOpenKey [NTDLL.@]
140 NTSTATUS WINAPI RtlpNtOpenKey( PHANDLE retkey, ACCESS_MASK access, OBJECT_ATTRIBUTES *attr )
143 attr->Attributes &= ~(OBJ_PERMANENT|OBJ_EXCLUSIVE);
144 return NtOpenKey(retkey, access, attr);
147 /******************************************************************************
148 * NtDeleteKey [NTDLL.@]
149 * ZwDeleteKey [NTDLL.@]
151 NTSTATUS WINAPI NtDeleteKey( HANDLE hkey )
155 TRACE( "(%p)\n", hkey );
157 SERVER_START_REQ( delete_key )
160 ret = wine_server_call( req );
166 /******************************************************************************
167 * RtlpNtMakeTemporaryKey [NTDLL.@]
171 NTSTATUS WINAPI RtlpNtMakeTemporaryKey( HANDLE hkey )
173 return NtDeleteKey(hkey);
176 /******************************************************************************
177 * NtDeleteValueKey [NTDLL.@]
178 * ZwDeleteValueKey [NTDLL.@]
180 NTSTATUS WINAPI NtDeleteValueKey( HANDLE hkey, const UNICODE_STRING *name )
184 TRACE( "(%p,%s)\n", hkey, debugstr_us(name) );
185 if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
187 SERVER_START_REQ( delete_key_value )
190 wine_server_add_data( req, name->Buffer, name->Length );
191 ret = wine_server_call( req );
198 /******************************************************************************
201 * Implementation of NtQueryKey and NtEnumerateKey
203 static NTSTATUS enumerate_key( HANDLE handle, int index, KEY_INFORMATION_CLASS info_class,
204 void *info, DWORD length, DWORD *result_len )
213 case KeyBasicInformation: data_ptr = ((KEY_BASIC_INFORMATION *)info)->Name; break;
214 case KeyFullInformation: data_ptr = ((KEY_FULL_INFORMATION *)info)->Class; break;
215 case KeyNodeInformation: data_ptr = ((KEY_NODE_INFORMATION *)info)->Name; break;
217 FIXME( "Information class %d not implemented\n", info_class );
218 return STATUS_INVALID_PARAMETER;
220 fixed_size = (char *)data_ptr - (char *)info;
222 SERVER_START_REQ( enum_key )
226 req->info_class = info_class;
227 if (length > fixed_size) wine_server_set_reply( req, data_ptr, length - fixed_size );
228 if (!(ret = wine_server_call( req )))
232 RtlSecondsSince1970ToTime( reply->modif, &modif );
236 case KeyBasicInformation:
238 KEY_BASIC_INFORMATION keyinfo;
239 fixed_size = (char *)keyinfo.Name - (char *)&keyinfo;
240 keyinfo.LastWriteTime = modif;
241 keyinfo.TitleIndex = 0;
242 keyinfo.NameLength = reply->namelen;
243 memcpy( info, &keyinfo, min( length, fixed_size ) );
246 case KeyFullInformation:
248 KEY_FULL_INFORMATION keyinfo;
249 fixed_size = (char *)keyinfo.Class - (char *)&keyinfo;
250 keyinfo.LastWriteTime = modif;
251 keyinfo.TitleIndex = 0;
252 keyinfo.ClassLength = wine_server_reply_size(reply);
253 keyinfo.ClassOffset = keyinfo.ClassLength ? fixed_size : -1;
254 keyinfo.SubKeys = reply->subkeys;
255 keyinfo.MaxNameLen = reply->max_subkey;
256 keyinfo.MaxClassLen = reply->max_class;
257 keyinfo.Values = reply->values;
258 keyinfo.MaxValueNameLen = reply->max_value;
259 keyinfo.MaxValueDataLen = reply->max_data;
260 memcpy( info, &keyinfo, min( length, fixed_size ) );
263 case KeyNodeInformation:
265 KEY_NODE_INFORMATION keyinfo;
266 fixed_size = (char *)keyinfo.Name - (char *)&keyinfo;
267 keyinfo.LastWriteTime = modif;
268 keyinfo.TitleIndex = 0;
269 keyinfo.ClassLength = max( 0, wine_server_reply_size(reply) - reply->namelen );
270 keyinfo.ClassOffset = keyinfo.ClassLength ? fixed_size + reply->namelen : -1;
271 keyinfo.NameLength = reply->namelen;
272 memcpy( info, &keyinfo, min( length, fixed_size ) );
276 *result_len = fixed_size + reply->total;
277 if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
286 /******************************************************************************
287 * NtEnumerateKey [NTDLL.@]
288 * ZwEnumerateKey [NTDLL.@]
291 * the name copied into the buffer is NOT 0-terminated
293 NTSTATUS WINAPI NtEnumerateKey( HANDLE handle, ULONG index, KEY_INFORMATION_CLASS info_class,
294 void *info, DWORD length, DWORD *result_len )
296 /* -1 means query key, so avoid it here */
297 if (index == (ULONG)-1) return STATUS_NO_MORE_ENTRIES;
298 return enumerate_key( handle, index, info_class, info, length, result_len );
302 /******************************************************************************
303 * RtlpNtEnumerateSubKey [NTDLL.@]
306 NTSTATUS WINAPI RtlpNtEnumerateSubKey( HANDLE handle, UNICODE_STRING *out, ULONG index )
308 KEY_BASIC_INFORMATION *info;
309 DWORD dwLen, dwResultLen;
314 dwLen = out->Length + sizeof(KEY_BASIC_INFORMATION);
315 info = (KEY_BASIC_INFORMATION*)RtlAllocateHeap( GetProcessHeap(), 0, dwLen );
317 return STATUS_NO_MEMORY;
325 ret = NtEnumerateKey( handle, index, KeyBasicInformation, info, dwLen, &dwResultLen );
326 dwResultLen -= sizeof(KEY_BASIC_INFORMATION);
328 if (ret == STATUS_BUFFER_OVERFLOW)
329 out->Length = dwResultLen;
332 if (out->Length < info->NameLength)
334 out->Length = dwResultLen;
335 ret = STATUS_BUFFER_OVERFLOW;
339 out->Length = info->NameLength;
340 memcpy(out->Buffer, info->Name, info->NameLength);
345 RtlFreeHeap( GetProcessHeap(), 0, info );
349 /******************************************************************************
350 * NtQueryKey [NTDLL.@]
351 * ZwQueryKey [NTDLL.@]
353 NTSTATUS WINAPI NtQueryKey( HANDLE handle, KEY_INFORMATION_CLASS info_class,
354 void *info, DWORD length, DWORD *result_len )
356 return enumerate_key( handle, -1, info_class, info, length, result_len );
360 /* fill the key value info structure for a specific info class */
361 static void copy_key_value_info( KEY_VALUE_INFORMATION_CLASS info_class, void *info,
362 DWORD length, int type, int name_len, int data_len )
366 case KeyValueBasicInformation:
368 KEY_VALUE_BASIC_INFORMATION keyinfo;
369 keyinfo.TitleIndex = 0;
371 keyinfo.NameLength = name_len;
372 length = min( length, (char *)keyinfo.Name - (char *)&keyinfo );
373 memcpy( info, &keyinfo, length );
376 case KeyValueFullInformation:
378 KEY_VALUE_FULL_INFORMATION keyinfo;
379 keyinfo.TitleIndex = 0;
381 keyinfo.DataOffset = (char *)keyinfo.Name - (char *)&keyinfo + name_len;
382 keyinfo.DataLength = data_len;
383 keyinfo.NameLength = name_len;
384 length = min( length, (char *)keyinfo.Name - (char *)&keyinfo );
385 memcpy( info, &keyinfo, length );
388 case KeyValuePartialInformation:
390 KEY_VALUE_PARTIAL_INFORMATION keyinfo;
391 keyinfo.TitleIndex = 0;
393 keyinfo.DataLength = data_len;
394 length = min( length, (char *)keyinfo.Data - (char *)&keyinfo );
395 memcpy( info, &keyinfo, length );
404 /******************************************************************************
405 * NtEnumerateValueKey [NTDLL.@]
406 * ZwEnumerateValueKey [NTDLL.@]
408 NTSTATUS WINAPI NtEnumerateValueKey( HANDLE handle, ULONG index,
409 KEY_VALUE_INFORMATION_CLASS info_class,
410 void *info, DWORD length, DWORD *result_len )
416 TRACE( "(%p,%lu,%d,%p,%ld)\n", handle, index, info_class, info, length );
418 /* compute the length we want to retrieve */
421 case KeyValueBasicInformation: ptr = ((KEY_VALUE_BASIC_INFORMATION *)info)->Name; break;
422 case KeyValueFullInformation: ptr = ((KEY_VALUE_FULL_INFORMATION *)info)->Name; break;
423 case KeyValuePartialInformation: ptr = ((KEY_VALUE_PARTIAL_INFORMATION *)info)->Data; break;
425 FIXME( "Information class %d not implemented\n", info_class );
426 return STATUS_INVALID_PARAMETER;
428 fixed_size = (char *)ptr - (char *)info;
430 SERVER_START_REQ( enum_key_value )
434 req->info_class = info_class;
435 if (length > fixed_size) wine_server_set_reply( req, ptr, length - fixed_size );
436 if (!(ret = wine_server_call( req )))
438 copy_key_value_info( info_class, info, length, reply->type, reply->namelen,
439 wine_server_reply_size(reply) - reply->namelen );
440 *result_len = fixed_size + reply->total;
441 if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
449 /******************************************************************************
450 * NtQueryValueKey [NTDLL.@]
451 * ZwQueryValueKey [NTDLL.@]
454 * the name in the KeyValueInformation is never set
456 NTSTATUS WINAPI NtQueryValueKey( HANDLE handle, const UNICODE_STRING *name,
457 KEY_VALUE_INFORMATION_CLASS info_class,
458 void *info, DWORD length, DWORD *result_len )
462 unsigned int fixed_size = 0;
464 TRACE( "(%p,%s,%d,%p,%ld)\n", handle, debugstr_us(name), info_class, info, length );
466 if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
468 /* compute the length we want to retrieve */
471 case KeyValueBasicInformation:
472 fixed_size = (char *)((KEY_VALUE_BASIC_INFORMATION *)info)->Name - (char *)info;
475 case KeyValueFullInformation:
476 data_ptr = (UCHAR *)((KEY_VALUE_FULL_INFORMATION *)info)->Name;
477 fixed_size = (char *)data_ptr - (char *)info;
479 case KeyValuePartialInformation:
480 data_ptr = ((KEY_VALUE_PARTIAL_INFORMATION *)info)->Data;
481 fixed_size = (char *)data_ptr - (char *)info;
484 FIXME( "Information class %d not implemented\n", info_class );
485 return STATUS_INVALID_PARAMETER;
488 SERVER_START_REQ( get_key_value )
491 wine_server_add_data( req, name->Buffer, name->Length );
492 if (length > fixed_size) wine_server_set_reply( req, data_ptr, length - fixed_size );
493 if (!(ret = wine_server_call( req )))
495 copy_key_value_info( info_class, info, length, reply->type,
496 0, wine_server_reply_size(reply) );
497 *result_len = fixed_size + reply->total;
498 if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
505 /******************************************************************************
506 * RtlpNtQueryValueKey [NTDLL.@]
509 NTSTATUS WINAPI RtlpNtQueryValueKey( HANDLE handle, ULONG *result_type, PBYTE dest,
512 KEY_VALUE_PARTIAL_INFORMATION *info;
516 DWORD dwLen = sizeof (KEY_VALUE_PARTIAL_INFORMATION) + result_len ? *result_len : 0;
518 info = (KEY_VALUE_PARTIAL_INFORMATION*)RtlAllocateHeap( GetProcessHeap(), 0, dwLen );
520 return STATUS_NO_MEMORY;
523 ret = NtQueryValueKey( handle, &name, KeyValuePartialInformation, info, dwLen, &dwResultLen );
525 if (!ret || ret == STATUS_BUFFER_OVERFLOW)
528 *result_len = info->DataLength;
531 *result_type = info->Type;
533 if (ret != STATUS_BUFFER_OVERFLOW)
534 memcpy( dest, info->Data, info->DataLength );
537 RtlFreeHeap( GetProcessHeap(), 0, info );
541 /******************************************************************************
542 * NtFlushKey [NTDLL.@]
543 * ZwFlushKey [NTDLL.@]
545 NTSTATUS WINAPI NtFlushKey(HANDLE key)
549 TRACE("key=%p\n", key);
551 SERVER_START_REQ( flush_key )
554 ret = wine_server_call( req );
561 /******************************************************************************
562 * NtLoadKey [NTDLL.@]
563 * ZwLoadKey [NTDLL.@]
565 NTSTATUS WINAPI NtLoadKey( const OBJECT_ATTRIBUTES *attr, OBJECT_ATTRIBUTES *file )
571 TRACE("(%p,%p)\n", attr, file);
573 ret = NtCreateFile(&hive, GENERIC_READ, file, &io, NULL, FILE_ATTRIBUTE_NORMAL, 0,
574 OPEN_EXISTING, 0, NULL, 0);
577 SERVER_START_REQ( load_registry )
579 req->hkey = attr->RootDirectory;
581 wine_server_add_data(req, attr->ObjectName->Buffer, attr->ObjectName->Length);
582 ret = wine_server_call( req );
591 /******************************************************************************
592 * NtNotifyChangeKey [NTDLL.@]
593 * ZwNotifyChangeKey [NTDLL.@]
595 NTSTATUS WINAPI NtNotifyChangeKey(
598 IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
599 IN PVOID ApcContext OPTIONAL,
600 OUT PIO_STATUS_BLOCK IoStatusBlock,
601 IN ULONG CompletionFilter,
602 IN BOOLEAN Asynchronous,
603 OUT PVOID ChangeBuffer,
605 IN BOOLEAN WatchSubtree)
609 TRACE("(%p,%p,%p,%p,%p,0x%08lx, 0x%08x,%p,0x%08lx,0x%08x)\n",
610 KeyHandle, Event, ApcRoutine, ApcContext, IoStatusBlock, CompletionFilter,
611 Asynchronous, ChangeBuffer, Length, WatchSubtree);
613 if (ApcRoutine || ApcContext || ChangeBuffer || Length)
614 FIXME("Unimplemented optional parameter\n");
618 OBJECT_ATTRIBUTES attr;
619 InitializeObjectAttributes( &attr, NULL, 0, NULL, NULL );
620 ret = NtCreateEvent( &Event, EVENT_ALL_ACCESS, &attr, FALSE, FALSE );
621 if (ret != STATUS_SUCCESS)
625 SERVER_START_REQ( set_registry_notification )
627 req->hkey = KeyHandle;
629 req->subtree = WatchSubtree;
630 req->filter = CompletionFilter;
631 ret = wine_server_call( req );
637 if (ret == STATUS_SUCCESS)
638 NtWaitForSingleObject( Event, FALSE, NULL );
642 return STATUS_SUCCESS;
645 /******************************************************************************
646 * NtQueryMultipleValueKey [NTDLL]
647 * ZwQueryMultipleValueKey
650 NTSTATUS WINAPI NtQueryMultipleValueKey(
652 PKEY_MULTIPLE_VALUE_INFORMATION ListOfValuesToQuery,
654 PVOID MultipleValueInformation,
658 FIXME("(%p,%p,0x%08lx,%p,0x%08lx,%p) stub!\n",
659 KeyHandle, ListOfValuesToQuery, NumberOfItems, MultipleValueInformation,
660 Length,ReturnLength);
661 return STATUS_SUCCESS;
664 /******************************************************************************
665 * NtReplaceKey [NTDLL.@]
666 * ZwReplaceKey [NTDLL.@]
668 NTSTATUS WINAPI NtReplaceKey(
669 IN POBJECT_ATTRIBUTES ObjectAttributes,
671 IN POBJECT_ATTRIBUTES ReplacedObjectAttributes)
673 FIXME("(%p),stub!\n", Key);
674 dump_ObjectAttributes(ObjectAttributes);
675 dump_ObjectAttributes(ReplacedObjectAttributes);
676 return STATUS_SUCCESS;
678 /******************************************************************************
679 * NtRestoreKey [NTDLL.@]
680 * ZwRestoreKey [NTDLL.@]
682 NTSTATUS WINAPI NtRestoreKey(
687 FIXME("(%p,%p,0x%08lx) stub\n",
688 KeyHandle, FileHandle, RestoreFlags);
689 return STATUS_SUCCESS;
691 /******************************************************************************
692 * NtSaveKey [NTDLL.@]
693 * ZwSaveKey [NTDLL.@]
695 NTSTATUS WINAPI NtSaveKey(IN HANDLE KeyHandle, IN HANDLE FileHandle)
699 TRACE("(%p,%p)\n", KeyHandle, FileHandle);
701 SERVER_START_REQ( save_registry )
703 req->hkey = KeyHandle;
704 req->file = FileHandle;
705 ret = wine_server_call( req );
711 /******************************************************************************
712 * NtSetInformationKey [NTDLL.@]
713 * ZwSetInformationKey [NTDLL.@]
715 NTSTATUS WINAPI NtSetInformationKey(
717 IN const int KeyInformationClass,
718 IN PVOID KeyInformation,
719 IN ULONG KeyInformationLength)
721 FIXME("(%p,0x%08x,%p,0x%08lx) stub\n",
722 KeyHandle, KeyInformationClass, KeyInformation, KeyInformationLength);
723 return STATUS_SUCCESS;
727 /******************************************************************************
728 * NtSetValueKey [NTDLL.@]
729 * ZwSetValueKey [NTDLL.@]
732 * win95 does not care about count for REG_SZ and finds out the len by itself (js)
733 * NT does definitely care (aj)
735 NTSTATUS WINAPI NtSetValueKey( HANDLE hkey, const UNICODE_STRING *name, ULONG TitleIndex,
736 ULONG type, const void *data, ULONG count )
740 TRACE( "(%p,%s,%ld,%p,%ld)\n", hkey, debugstr_us(name), type, data, count );
742 if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
744 SERVER_START_REQ( set_key_value )
748 req->namelen = name->Length;
749 wine_server_add_data( req, name->Buffer, name->Length );
750 wine_server_add_data( req, data, count );
751 ret = wine_server_call( req );
757 /******************************************************************************
758 * RtlpNtSetValueKey [NTDLL.@]
761 NTSTATUS WINAPI RtlpNtSetValueKey( HANDLE hkey, ULONG type, const void *data,
767 return NtSetValueKey( hkey, &name, 0, type, data, count );
770 /******************************************************************************
771 * NtUnloadKey [NTDLL.@]
772 * ZwUnloadKey [NTDLL.@]
774 NTSTATUS WINAPI NtUnloadKey(IN HANDLE KeyHandle)
778 TRACE("(%p)\n", KeyHandle);
780 SERVER_START_REQ( unload_registry )
782 req->hkey = KeyHandle;
783 ret = wine_server_call(req);
790 /******************************************************************************
791 * RtlFormatCurrentUserKeyPath [NTDLL.@]
793 * NOTE: under NT the user name part of the path is an SID.
795 NTSTATUS WINAPI RtlFormatCurrentUserKeyPath( IN OUT PUNICODE_STRING KeyPath)
797 static const WCHAR pathW[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\'};
798 const char *user = wine_get_user_name();
799 int len = ntdll_umbstowcs( 0, user, strlen(user)+1, NULL, 0 );
801 KeyPath->MaximumLength = sizeof(pathW) + len * sizeof(WCHAR);
802 KeyPath->Length = KeyPath->MaximumLength - sizeof(WCHAR);
803 if (!(KeyPath->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, KeyPath->MaximumLength )))
804 return STATUS_NO_MEMORY;
805 memcpy( KeyPath->Buffer, pathW, sizeof(pathW) );
806 ntdll_umbstowcs( 0, user, strlen(user)+1, KeyPath->Buffer + sizeof(pathW)/sizeof(WCHAR), len );
807 return STATUS_SUCCESS;
810 /******************************************************************************
811 * RtlOpenCurrentUser [NTDLL.@]
813 * if we return just HKEY_CURRENT_USER the advapi tries to find a remote
814 * registry (odd handle) and fails
817 DWORD WINAPI RtlOpenCurrentUser(
818 IN ACCESS_MASK DesiredAccess, /* [in] */
819 OUT PHANDLE KeyHandle) /* [out] handle of HKEY_CURRENT_USER */
821 OBJECT_ATTRIBUTES ObjectAttributes;
822 UNICODE_STRING ObjectName;
825 TRACE("(0x%08lx, %p) stub\n",DesiredAccess, KeyHandle);
827 RtlFormatCurrentUserKeyPath(&ObjectName);
828 InitializeObjectAttributes(&ObjectAttributes,&ObjectName,OBJ_CASE_INSENSITIVE,0, NULL);
829 ret = NtCreateKey(KeyHandle, DesiredAccess, &ObjectAttributes, 0, NULL, 0, NULL);
830 RtlFreeUnicodeString(&ObjectName);
835 static NTSTATUS RTL_ReportRegistryValue(PKEY_VALUE_FULL_INFORMATION pInfo,
836 PRTL_QUERY_REGISTRY_TABLE pQuery, PVOID pContext, PVOID pEnvironment)
839 UNICODE_STRING src, dst;
844 NTSTATUS status = STATUS_SUCCESS;
851 if (pQuery->Flags & RTL_QUERY_REGISTRY_DIRECT)
852 return STATUS_INVALID_PARAMETER;
855 status = pQuery->QueryRoutine(pQuery->Name, pQuery->DefaultType, pQuery->DefaultData,
856 pQuery->DefaultLength, pContext, pQuery->EntryContext);
860 len = pInfo->DataLength;
862 if (pQuery->Flags & RTL_QUERY_REGISTRY_DIRECT)
864 str = (PUNICODE_STRING)pQuery->EntryContext;
869 if (!(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND))
871 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
873 dst.MaximumLength = 0;
874 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
876 dst.MaximumLength = res;
877 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
878 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
879 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
880 dst.Length, pContext, pQuery->EntryContext);
881 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
886 if (str->Buffer == NULL)
887 RtlCreateUnicodeString(str, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
889 RtlAppendUnicodeToString(str, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
893 if (!(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND))
894 return STATUS_INVALID_PARAMETER;
896 if (str->Buffer == NULL)
898 str->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, len);
899 str->MaximumLength = len;
901 len = min(len, str->MaximumLength);
902 memcpy(str->Buffer, ((CHAR*)pInfo) + pInfo->DataOffset, len);
907 bin = (LONG*)pQuery->EntryContext;
908 if (pInfo->DataLength <= sizeof(ULONG))
909 memcpy(bin, ((CHAR*)pInfo) + pInfo->DataOffset,
913 if (bin[0] <= sizeof(ULONG))
915 memcpy(&bin[1], ((CHAR*)pInfo) + pInfo->DataOffset,
916 min(-bin[0], pInfo->DataLength));
920 len = min(bin[0], pInfo->DataLength);
922 bin[2] = pInfo->Type;
923 memcpy(&bin[3], ((CHAR*)pInfo) + pInfo->DataOffset, len);
931 if((pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND) ||
932 (pInfo->Type != REG_EXPAND_SZ && pInfo->Type != REG_MULTI_SZ))
934 status = pQuery->QueryRoutine(pInfo->Name, pInfo->Type,
935 ((CHAR*)pInfo) + pInfo->DataOffset, pInfo->DataLength,
936 pContext, pQuery->EntryContext);
938 else if (pInfo->Type == REG_EXPAND_SZ)
940 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
942 dst.MaximumLength = 0;
943 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
945 dst.MaximumLength = res;
946 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
947 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
948 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
949 dst.Length, pContext, pQuery->EntryContext);
950 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
952 else /* REG_MULTI_SZ */
954 if(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND)
956 for (offset = 0; offset <= pInfo->DataLength; offset += len + sizeof(WCHAR))
958 wstr = (WCHAR*)(((CHAR*)pInfo) + offset);
959 len = strlenW(wstr) * sizeof(WCHAR);
960 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, wstr, len,
961 pContext, pQuery->EntryContext);
962 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
968 while(count<=pInfo->DataLength)
970 String = (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset)+count;
971 count+=strlenW(String)+1;
972 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
974 dst.MaximumLength = 0;
975 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
977 dst.MaximumLength = res;
978 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
979 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
980 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
981 dst.Length, pContext, pQuery->EntryContext);
982 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
983 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
993 static NTSTATUS RTL_GetKeyHandle(ULONG RelativeTo, PCWSTR Path, PHANDLE handle)
995 UNICODE_STRING KeyString;
996 OBJECT_ATTRIBUTES regkey;
1001 static const WCHAR empty[] = {0};
1002 static const WCHAR control[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e',
1003 '\\','S','y','s','t','e','m','\\','C','u','r','r','e','n','t',' ','C','o','n','t','r','o','l','S','e','t','\\',
1004 'C','o','n','t','r','o','l','\\',0};
1006 static const WCHAR devicemap[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1007 'H','a','r','d','w','a','r','e','\\','D','e','v','i','c','e','M','a','p','\\',0};
1009 static const WCHAR services[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1010 'S','y','s','t','e','m','\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1011 'S','e','r','v','i','c','e','s','\\',0};
1013 static const WCHAR user[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\',
1014 'C','u','r','r','e','n','t','U','s','e','r','\\',0};
1016 static const WCHAR windows_nt[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1017 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1018 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',0};
1020 switch (RelativeTo & 0xff)
1022 case RTL_REGISTRY_ABSOLUTE:
1026 case RTL_REGISTRY_CONTROL:
1030 case RTL_REGISTRY_DEVICEMAP:
1034 case RTL_REGISTRY_SERVICES:
1038 case RTL_REGISTRY_USER:
1042 case RTL_REGISTRY_WINDOWS_NT:
1047 return STATUS_INVALID_PARAMETER;
1050 len = (strlenW(base) + strlenW(Path) + 1) * sizeof(WCHAR);
1051 KeyString.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, len);
1052 if (KeyString.Buffer == NULL)
1053 return STATUS_NO_MEMORY;
1055 strcpyW(KeyString.Buffer, base);
1056 strcatW(KeyString.Buffer, Path);
1057 KeyString.Length = len - sizeof(WCHAR);
1058 KeyString.MaximumLength = len;
1059 InitializeObjectAttributes(®key, &KeyString, OBJ_CASE_INSENSITIVE, NULL, NULL);
1060 status = NtOpenKey(handle, KEY_ALL_ACCESS, ®key);
1061 RtlFreeHeap(GetProcessHeap(), 0, KeyString.Buffer);
1065 /*************************************************************************
1066 * RtlQueryRegistryValues [NTDLL.@]
1068 * Query multiple registry values with a signle call.
1071 * RelativeTo [I] Registry path that Path refers to
1072 * Path [I] Path to key
1073 * QueryTable [I] Table of key values to query
1074 * Context [I] Paremeter to pass to the application defined QueryRoutine function
1075 * Environment [I] Optional parameter to use when performing expantion
1078 * STATUS_SUCCESS or an appropriate NTSTATUS error code.
1080 NTSTATUS WINAPI RtlQueryRegistryValues(IN ULONG RelativeTo, IN PCWSTR Path,
1081 IN PRTL_QUERY_REGISTRY_TABLE QueryTable, IN PVOID Context,
1082 IN PVOID Environment OPTIONAL)
1084 UNICODE_STRING Value;
1085 HANDLE handle, topkey;
1086 PKEY_VALUE_FULL_INFORMATION pInfo = NULL;
1087 ULONG len, buflen = 0;
1088 NTSTATUS status=STATUS_SUCCESS, ret = STATUS_SUCCESS;
1091 TRACE("(%ld, %s, %p, %p, %p)\n", RelativeTo, debugstr_w(Path), QueryTable, Context, Environment);
1094 return STATUS_INVALID_PARAMETER;
1096 /* get a valid handle */
1097 if (RelativeTo & RTL_REGISTRY_HANDLE)
1098 topkey = handle = (HANDLE)Path;
1101 status = RTL_GetKeyHandle(RelativeTo, Path, &topkey);
1104 if(status != STATUS_SUCCESS)
1107 /* Process query table entries */
1108 for (; QueryTable->QueryRoutine != NULL || QueryTable->Name != NULL; ++QueryTable)
1110 if (QueryTable->Flags &
1111 (RTL_QUERY_REGISTRY_SUBKEY | RTL_QUERY_REGISTRY_TOPKEY))
1113 /* topkey must be kept open just in case we will reuse it later */
1114 if (handle != topkey)
1117 if (QueryTable->Flags & RTL_QUERY_REGISTRY_SUBKEY)
1120 status = RTL_GetKeyHandle((ULONG)QueryTable->Name, Path, &handle);
1121 if(status != STATUS_SUCCESS)
1131 if (QueryTable->Flags & RTL_QUERY_REGISTRY_NOVALUE)
1133 QueryTable->QueryRoutine(QueryTable->Name, REG_NONE, NULL, 0,
1134 Context, QueryTable->EntryContext);
1140 if (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED)
1142 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1148 if (QueryTable->Name == NULL)
1150 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DIRECT)
1152 ret = STATUS_INVALID_PARAMETER;
1156 /* Report all subkeys */
1159 status = NtEnumerateValueKey(handle, i,
1160 KeyValueFullInformation, pInfo, buflen, &len);
1161 if (status == STATUS_NO_MORE_ENTRIES)
1163 if (status == STATUS_BUFFER_OVERFLOW ||
1164 status == STATUS_BUFFER_TOO_SMALL)
1167 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1168 pInfo = (KEY_VALUE_FULL_INFORMATION*)RtlAllocateHeap(
1169 GetProcessHeap(), 0, buflen);
1170 NtEnumerateValueKey(handle, i, KeyValueFullInformation,
1171 pInfo, buflen, &len);
1174 status = RTL_ReportRegistryValue(pInfo, QueryTable, Context, Environment);
1175 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1180 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DELETE)
1182 RtlInitUnicodeString(&Value, pInfo->Name);
1183 NtDeleteValueKey(handle, &Value);
1187 if (i == 0 && (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED))
1189 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1195 RtlInitUnicodeString(&Value, QueryTable->Name);
1196 status = NtQueryValueKey(handle, &Value, KeyValueFullInformation,
1197 pInfo, buflen, &len);
1198 if (status == STATUS_BUFFER_OVERFLOW ||
1199 status == STATUS_BUFFER_TOO_SMALL)
1202 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1203 pInfo = (KEY_VALUE_FULL_INFORMATION*)RtlAllocateHeap(
1204 GetProcessHeap(), 0, buflen);
1205 status = NtQueryValueKey(handle, &Value,
1206 KeyValueFullInformation, pInfo, buflen, &len);
1208 if (status != STATUS_SUCCESS)
1210 if (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED)
1212 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1215 status = RTL_ReportRegistryValue(NULL, QueryTable, Context, Environment);
1216 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1224 status = RTL_ReportRegistryValue(pInfo, QueryTable, Context, Environment);
1225 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1230 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DELETE)
1231 NtDeleteValueKey(handle, &Value);
1237 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1238 if (handle != topkey)
1244 /*************************************************************************
1245 * RtlCheckRegistryKey [NTDLL.@]
1247 * Query multiple registry values with a signle call.
1250 * RelativeTo [I] Registry path that Path refers to
1251 * Path [I] Path to key
1254 * STATUS_SUCCESS if the specified key exists, or an NTSTATUS error code.
1256 NTSTATUS WINAPI RtlCheckRegistryKey(IN ULONG RelativeTo, IN PWSTR Path)
1261 TRACE("(%ld, %s)\n", RelativeTo, debugstr_w(Path));
1263 if((!RelativeTo) && Path == NULL)
1264 return STATUS_OBJECT_PATH_SYNTAX_BAD;
1265 if(RelativeTo & RTL_REGISTRY_HANDLE)
1266 return STATUS_SUCCESS;
1268 status = RTL_GetKeyHandle(RelativeTo, Path, &handle);
1269 if (handle) NtClose(handle);
1270 if (status == STATUS_INVALID_HANDLE) status = STATUS_OBJECT_NAME_NOT_FOUND;
1274 /*************************************************************************
1275 * RtlDeleteRegistryValue [NTDLL.@]
1277 * Query multiple registry values with a signle call.
1280 * RelativeTo [I] Registry path that Path refers to
1281 * Path [I] Path to key
1282 * ValueName [I] Name of the value to delete
1285 * STATUS_SUCCESS if the specified key is successfully deleted, or an NTSTATUS error code.
1287 NTSTATUS WINAPI RtlDeleteRegistryValue(IN ULONG RelativeTo, IN PCWSTR Path, IN PCWSTR ValueName)
1291 UNICODE_STRING Value;
1293 TRACE("(%ld, %s, %s)\n", RelativeTo, debugstr_w(Path), debugstr_w(ValueName));
1295 RtlInitUnicodeString(&Value, ValueName);
1296 if(RelativeTo == RTL_REGISTRY_HANDLE)
1298 return NtDeleteValueKey((HANDLE)Path, &Value);
1300 status = RTL_GetKeyHandle(RelativeTo, Path, &handle);
1301 if (status) return status;
1302 status = NtDeleteValueKey(handle, &Value);