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"
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"
43 WINE_DEFAULT_DEBUG_CHANNEL(reg);
45 /* maximum length of a key/value name in bytes (without terminating null) */
46 #define MAX_NAME_LENGTH ((MAX_PATH-1) * sizeof(WCHAR))
48 /******************************************************************************
49 * NtCreateKey [NTDLL.@]
50 * ZwCreateKey [NTDLL.@]
52 NTSTATUS WINAPI NtCreateKey( PHANDLE retkey, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr,
53 ULONG TitleIndex, const UNICODE_STRING *class, ULONG options,
58 TRACE( "(%p,%s,%s,%lx,%lx,%p)\n", attr->RootDirectory, debugstr_us(attr->ObjectName),
59 debugstr_us(class), options, access, retkey );
61 if (attr->ObjectName->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
62 if (!retkey) return STATUS_INVALID_PARAMETER;
64 SERVER_START_REQ( create_key )
66 req->parent = attr->RootDirectory;
68 req->options = options;
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 )))
75 *retkey = reply->hkey;
76 if (dispos) *dispos = reply->created ? REG_CREATED_NEW_KEY : REG_OPENED_EXISTING_KEY;
80 TRACE("<- %p\n", *retkey);
84 /******************************************************************************
85 * RtlpNtCreateKey [NTDLL.@]
89 NTSTATUS WINAPI RtlpNtCreateKey( PHANDLE retkey, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr,
90 ULONG TitleIndex, const UNICODE_STRING *class, ULONG options,
97 memcpy( &oa, attr, sizeof oa );
98 oa.Attributes &= ~(OBJ_PERMANENT|OBJ_EXCLUSIVE);
102 return NtCreateKey(retkey, access, attr, 0, NULL, 0, dispos);
105 /******************************************************************************
106 * NtOpenKey [NTDLL.@]
107 * ZwOpenKey [NTDLL.@]
109 * OUT HANDLE retkey (returns 0 when failure)
110 * IN ACCESS_MASK access
111 * IN POBJECT_ATTRIBUTES attr
113 NTSTATUS WINAPI NtOpenKey( PHANDLE retkey, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr )
116 DWORD len = attr->ObjectName->Length;
118 TRACE( "(%p,%s,%lx,%p)\n", attr->RootDirectory,
119 debugstr_us(attr->ObjectName), access, retkey );
121 if (len > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
122 if (!retkey) return STATUS_INVALID_PARAMETER;
124 SERVER_START_REQ( open_key )
126 req->parent = attr->RootDirectory;
127 req->access = access;
128 wine_server_add_data( req, attr->ObjectName->Buffer, len );
129 ret = wine_server_call( req );
130 *retkey = reply->hkey;
133 TRACE("<- %p\n", *retkey);
137 /******************************************************************************
138 * RtlpNtOpenKey [NTDLL.@]
142 NTSTATUS WINAPI RtlpNtOpenKey( PHANDLE retkey, ACCESS_MASK access, OBJECT_ATTRIBUTES *attr )
145 attr->Attributes &= ~(OBJ_PERMANENT|OBJ_EXCLUSIVE);
146 return NtOpenKey(retkey, access, attr);
149 /******************************************************************************
150 * NtDeleteKey [NTDLL.@]
151 * ZwDeleteKey [NTDLL.@]
153 NTSTATUS WINAPI NtDeleteKey( HANDLE hkey )
157 TRACE( "(%p)\n", hkey );
159 SERVER_START_REQ( delete_key )
162 ret = wine_server_call( req );
168 /******************************************************************************
169 * RtlpNtMakeTemporaryKey [NTDLL.@]
173 NTSTATUS WINAPI RtlpNtMakeTemporaryKey( HANDLE hkey )
175 return NtDeleteKey(hkey);
178 /******************************************************************************
179 * NtDeleteValueKey [NTDLL.@]
180 * ZwDeleteValueKey [NTDLL.@]
182 NTSTATUS WINAPI NtDeleteValueKey( HANDLE hkey, const UNICODE_STRING *name )
186 TRACE( "(%p,%s)\n", hkey, debugstr_us(name) );
187 if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
189 SERVER_START_REQ( delete_key_value )
192 wine_server_add_data( req, name->Buffer, name->Length );
193 ret = wine_server_call( req );
200 /******************************************************************************
203 * Implementation of NtQueryKey and NtEnumerateKey
205 static NTSTATUS enumerate_key( HANDLE handle, int index, KEY_INFORMATION_CLASS info_class,
206 void *info, DWORD length, DWORD *result_len )
215 case KeyBasicInformation: data_ptr = ((KEY_BASIC_INFORMATION *)info)->Name; break;
216 case KeyFullInformation: data_ptr = ((KEY_FULL_INFORMATION *)info)->Class; break;
217 case KeyNodeInformation: data_ptr = ((KEY_NODE_INFORMATION *)info)->Name; break;
219 FIXME( "Information class %d not implemented\n", info_class );
220 return STATUS_INVALID_PARAMETER;
222 fixed_size = (char *)data_ptr - (char *)info;
224 SERVER_START_REQ( enum_key )
228 req->info_class = info_class;
229 if (length > fixed_size) wine_server_set_reply( req, data_ptr, length - fixed_size );
230 if (!(ret = wine_server_call( req )))
234 RtlSecondsSince1970ToTime( reply->modif, &modif );
238 case KeyBasicInformation:
240 KEY_BASIC_INFORMATION keyinfo;
241 fixed_size = (char *)keyinfo.Name - (char *)&keyinfo;
242 keyinfo.LastWriteTime = modif;
243 keyinfo.TitleIndex = 0;
244 keyinfo.NameLength = reply->namelen;
245 memcpy( info, &keyinfo, min( length, fixed_size ) );
248 case KeyFullInformation:
250 KEY_FULL_INFORMATION keyinfo;
251 fixed_size = (char *)keyinfo.Class - (char *)&keyinfo;
252 keyinfo.LastWriteTime = modif;
253 keyinfo.TitleIndex = 0;
254 keyinfo.ClassLength = wine_server_reply_size(reply);
255 keyinfo.ClassOffset = keyinfo.ClassLength ? fixed_size : -1;
256 keyinfo.SubKeys = reply->subkeys;
257 keyinfo.MaxNameLen = reply->max_subkey;
258 keyinfo.MaxClassLen = reply->max_class;
259 keyinfo.Values = reply->values;
260 keyinfo.MaxValueNameLen = reply->max_value;
261 keyinfo.MaxValueDataLen = reply->max_data;
262 memcpy( info, &keyinfo, min( length, fixed_size ) );
265 case KeyNodeInformation:
267 KEY_NODE_INFORMATION keyinfo;
268 fixed_size = (char *)keyinfo.Name - (char *)&keyinfo;
269 keyinfo.LastWriteTime = modif;
270 keyinfo.TitleIndex = 0;
271 keyinfo.ClassLength = max( 0, wine_server_reply_size(reply) - reply->namelen );
272 keyinfo.ClassOffset = keyinfo.ClassLength ? fixed_size + reply->namelen : -1;
273 keyinfo.NameLength = reply->namelen;
274 memcpy( info, &keyinfo, min( length, fixed_size ) );
278 *result_len = fixed_size + reply->total;
279 if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
288 /******************************************************************************
289 * NtEnumerateKey [NTDLL.@]
290 * ZwEnumerateKey [NTDLL.@]
293 * the name copied into the buffer is NOT 0-terminated
295 NTSTATUS WINAPI NtEnumerateKey( HANDLE handle, ULONG index, KEY_INFORMATION_CLASS info_class,
296 void *info, DWORD length, DWORD *result_len )
298 /* -1 means query key, so avoid it here */
299 if (index == (ULONG)-1) return STATUS_NO_MORE_ENTRIES;
300 return enumerate_key( handle, index, info_class, info, length, result_len );
304 /******************************************************************************
305 * RtlpNtEnumerateSubKey [NTDLL.@]
308 NTSTATUS WINAPI RtlpNtEnumerateSubKey( HANDLE handle, UNICODE_STRING *out, ULONG index )
310 KEY_BASIC_INFORMATION *info;
311 DWORD dwLen, dwResultLen;
316 dwLen = out->Length + sizeof(KEY_BASIC_INFORMATION);
317 info = (KEY_BASIC_INFORMATION*)RtlAllocateHeap( GetProcessHeap(), 0, dwLen );
319 return STATUS_NO_MEMORY;
327 ret = NtEnumerateKey( handle, index, KeyBasicInformation, info, dwLen, &dwResultLen );
328 dwResultLen -= sizeof(KEY_BASIC_INFORMATION);
330 if (ret == STATUS_BUFFER_OVERFLOW)
331 out->Length = dwResultLen;
334 if (out->Length < info->NameLength)
336 out->Length = dwResultLen;
337 ret = STATUS_BUFFER_OVERFLOW;
341 out->Length = info->NameLength;
342 memcpy(out->Buffer, info->Name, info->NameLength);
347 RtlFreeHeap( GetProcessHeap(), 0, info );
351 /******************************************************************************
352 * NtQueryKey [NTDLL.@]
353 * ZwQueryKey [NTDLL.@]
355 NTSTATUS WINAPI NtQueryKey( HANDLE handle, KEY_INFORMATION_CLASS info_class,
356 void *info, DWORD length, DWORD *result_len )
358 return enumerate_key( handle, -1, info_class, info, length, result_len );
362 /* fill the key value info structure for a specific info class */
363 static void copy_key_value_info( KEY_VALUE_INFORMATION_CLASS info_class, void *info,
364 DWORD length, int type, int name_len, int data_len )
368 case KeyValueBasicInformation:
370 KEY_VALUE_BASIC_INFORMATION keyinfo;
371 keyinfo.TitleIndex = 0;
373 keyinfo.NameLength = name_len;
374 length = min( length, (char *)keyinfo.Name - (char *)&keyinfo );
375 memcpy( info, &keyinfo, length );
378 case KeyValueFullInformation:
380 KEY_VALUE_FULL_INFORMATION keyinfo;
381 keyinfo.TitleIndex = 0;
383 keyinfo.DataOffset = (char *)keyinfo.Name - (char *)&keyinfo + name_len;
384 keyinfo.DataLength = data_len;
385 keyinfo.NameLength = name_len;
386 length = min( length, (char *)keyinfo.Name - (char *)&keyinfo );
387 memcpy( info, &keyinfo, length );
390 case KeyValuePartialInformation:
392 KEY_VALUE_PARTIAL_INFORMATION keyinfo;
393 keyinfo.TitleIndex = 0;
395 keyinfo.DataLength = data_len;
396 length = min( length, (char *)keyinfo.Data - (char *)&keyinfo );
397 memcpy( info, &keyinfo, length );
406 /******************************************************************************
407 * NtEnumerateValueKey [NTDLL.@]
408 * ZwEnumerateValueKey [NTDLL.@]
410 NTSTATUS WINAPI NtEnumerateValueKey( HANDLE handle, ULONG index,
411 KEY_VALUE_INFORMATION_CLASS info_class,
412 void *info, DWORD length, DWORD *result_len )
418 TRACE( "(%p,%lu,%d,%p,%ld)\n", handle, index, info_class, info, length );
420 /* compute the length we want to retrieve */
423 case KeyValueBasicInformation: ptr = ((KEY_VALUE_BASIC_INFORMATION *)info)->Name; break;
424 case KeyValueFullInformation: ptr = ((KEY_VALUE_FULL_INFORMATION *)info)->Name; break;
425 case KeyValuePartialInformation: ptr = ((KEY_VALUE_PARTIAL_INFORMATION *)info)->Data; break;
427 FIXME( "Information class %d not implemented\n", info_class );
428 return STATUS_INVALID_PARAMETER;
430 fixed_size = (char *)ptr - (char *)info;
432 SERVER_START_REQ( enum_key_value )
436 req->info_class = info_class;
437 if (length > fixed_size) wine_server_set_reply( req, ptr, length - fixed_size );
438 if (!(ret = wine_server_call( req )))
440 copy_key_value_info( info_class, info, length, reply->type, reply->namelen,
441 wine_server_reply_size(reply) - reply->namelen );
442 *result_len = fixed_size + reply->total;
443 if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
451 /******************************************************************************
452 * NtQueryValueKey [NTDLL.@]
453 * ZwQueryValueKey [NTDLL.@]
456 * the name in the KeyValueInformation is never set
458 NTSTATUS WINAPI NtQueryValueKey( HANDLE handle, const UNICODE_STRING *name,
459 KEY_VALUE_INFORMATION_CLASS info_class,
460 void *info, DWORD length, DWORD *result_len )
464 unsigned int fixed_size = 0;
466 TRACE( "(%p,%s,%d,%p,%ld)\n", handle, debugstr_us(name), info_class, info, length );
468 if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
470 /* compute the length we want to retrieve */
473 case KeyValueBasicInformation:
474 fixed_size = (char *)((KEY_VALUE_BASIC_INFORMATION *)info)->Name - (char *)info;
477 case KeyValueFullInformation:
478 data_ptr = (UCHAR *)((KEY_VALUE_FULL_INFORMATION *)info)->Name;
479 fixed_size = (char *)data_ptr - (char *)info;
481 case KeyValuePartialInformation:
482 data_ptr = ((KEY_VALUE_PARTIAL_INFORMATION *)info)->Data;
483 fixed_size = (char *)data_ptr - (char *)info;
486 FIXME( "Information class %d not implemented\n", info_class );
487 return STATUS_INVALID_PARAMETER;
490 SERVER_START_REQ( get_key_value )
493 wine_server_add_data( req, name->Buffer, name->Length );
494 if (length > fixed_size) wine_server_set_reply( req, data_ptr, length - fixed_size );
495 if (!(ret = wine_server_call( req )))
497 copy_key_value_info( info_class, info, length, reply->type,
498 0, wine_server_reply_size(reply) );
499 *result_len = fixed_size + reply->total;
500 if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
507 /******************************************************************************
508 * RtlpNtQueryValueKey [NTDLL.@]
511 NTSTATUS WINAPI RtlpNtQueryValueKey( HANDLE handle, ULONG *result_type, PBYTE dest,
514 KEY_VALUE_PARTIAL_INFORMATION *info;
518 DWORD dwLen = sizeof (KEY_VALUE_PARTIAL_INFORMATION) + result_len ? *result_len : 0;
520 info = (KEY_VALUE_PARTIAL_INFORMATION*)RtlAllocateHeap( GetProcessHeap(), 0, dwLen );
522 return STATUS_NO_MEMORY;
525 ret = NtQueryValueKey( handle, &name, KeyValuePartialInformation, info, dwLen, &dwResultLen );
527 if (!ret || ret == STATUS_BUFFER_OVERFLOW)
530 *result_len = info->DataLength;
533 *result_type = info->Type;
535 if (ret != STATUS_BUFFER_OVERFLOW)
536 memcpy( dest, info->Data, info->DataLength );
539 RtlFreeHeap( GetProcessHeap(), 0, info );
543 /******************************************************************************
544 * NtFlushKey [NTDLL.@]
545 * ZwFlushKey [NTDLL.@]
547 NTSTATUS WINAPI NtFlushKey(HANDLE key)
551 TRACE("key=%p\n", key);
553 SERVER_START_REQ( flush_key )
556 ret = wine_server_call( req );
563 /******************************************************************************
564 * NtLoadKey [NTDLL.@]
565 * ZwLoadKey [NTDLL.@]
567 NTSTATUS WINAPI NtLoadKey( const OBJECT_ATTRIBUTES *attr, OBJECT_ATTRIBUTES *file )
573 TRACE("(%p,%p)\n", attr, file);
575 ret = NtCreateFile(&hive, GENERIC_READ, file, &io, NULL, FILE_ATTRIBUTE_NORMAL, 0,
576 OPEN_EXISTING, 0, NULL, 0);
579 SERVER_START_REQ( load_registry )
581 req->hkey = attr->RootDirectory;
583 wine_server_add_data(req, attr->ObjectName->Buffer, attr->ObjectName->Length);
584 ret = wine_server_call( req );
593 /******************************************************************************
594 * NtNotifyChangeKey [NTDLL.@]
595 * ZwNotifyChangeKey [NTDLL.@]
597 NTSTATUS WINAPI NtNotifyChangeKey(
600 IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
601 IN PVOID ApcContext OPTIONAL,
602 OUT PIO_STATUS_BLOCK IoStatusBlock,
603 IN ULONG CompletionFilter,
604 IN BOOLEAN Asynchronous,
605 OUT PVOID ChangeBuffer,
607 IN BOOLEAN WatchSubtree)
611 TRACE("(%p,%p,%p,%p,%p,0x%08lx, 0x%08x,%p,0x%08lx,0x%08x)\n",
612 KeyHandle, Event, ApcRoutine, ApcContext, IoStatusBlock, CompletionFilter,
613 Asynchronous, ChangeBuffer, Length, WatchSubtree);
615 if (ApcRoutine || ApcContext || ChangeBuffer || Length)
616 FIXME("Unimplemented optional parameter\n");
620 OBJECT_ATTRIBUTES attr;
621 InitializeObjectAttributes( &attr, NULL, 0, NULL, NULL );
622 ret = NtCreateEvent( &Event, EVENT_ALL_ACCESS, &attr, FALSE, FALSE );
623 if (ret != STATUS_SUCCESS)
627 SERVER_START_REQ( set_registry_notification )
629 req->hkey = KeyHandle;
631 req->subtree = WatchSubtree;
632 req->filter = CompletionFilter;
633 ret = wine_server_call( req );
639 if (ret == STATUS_SUCCESS)
640 NtWaitForSingleObject( Event, FALSE, NULL );
644 return STATUS_SUCCESS;
647 /******************************************************************************
648 * NtQueryMultipleValueKey [NTDLL]
649 * ZwQueryMultipleValueKey
652 NTSTATUS WINAPI NtQueryMultipleValueKey(
654 PKEY_MULTIPLE_VALUE_INFORMATION ListOfValuesToQuery,
656 PVOID MultipleValueInformation,
660 FIXME("(%p,%p,0x%08lx,%p,0x%08lx,%p) stub!\n",
661 KeyHandle, ListOfValuesToQuery, NumberOfItems, MultipleValueInformation,
662 Length,ReturnLength);
663 return STATUS_SUCCESS;
666 /******************************************************************************
667 * NtReplaceKey [NTDLL.@]
668 * ZwReplaceKey [NTDLL.@]
670 NTSTATUS WINAPI NtReplaceKey(
671 IN POBJECT_ATTRIBUTES ObjectAttributes,
673 IN POBJECT_ATTRIBUTES ReplacedObjectAttributes)
675 FIXME("(%p),stub!\n", Key);
676 dump_ObjectAttributes(ObjectAttributes);
677 dump_ObjectAttributes(ReplacedObjectAttributes);
678 return STATUS_SUCCESS;
680 /******************************************************************************
681 * NtRestoreKey [NTDLL.@]
682 * ZwRestoreKey [NTDLL.@]
684 NTSTATUS WINAPI NtRestoreKey(
689 FIXME("(%p,%p,0x%08lx) stub\n",
690 KeyHandle, FileHandle, RestoreFlags);
691 return STATUS_SUCCESS;
693 /******************************************************************************
694 * NtSaveKey [NTDLL.@]
695 * ZwSaveKey [NTDLL.@]
697 NTSTATUS WINAPI NtSaveKey(IN HANDLE KeyHandle, IN HANDLE FileHandle)
701 TRACE("(%p,%p)\n", KeyHandle, FileHandle);
703 SERVER_START_REQ( save_registry )
705 req->hkey = KeyHandle;
706 req->file = FileHandle;
707 ret = wine_server_call( req );
713 /******************************************************************************
714 * NtSetInformationKey [NTDLL.@]
715 * ZwSetInformationKey [NTDLL.@]
717 NTSTATUS WINAPI NtSetInformationKey(
719 IN const int KeyInformationClass,
720 IN PVOID KeyInformation,
721 IN ULONG KeyInformationLength)
723 FIXME("(%p,0x%08x,%p,0x%08lx) stub\n",
724 KeyHandle, KeyInformationClass, KeyInformation, KeyInformationLength);
725 return STATUS_SUCCESS;
729 /******************************************************************************
730 * NtSetValueKey [NTDLL.@]
731 * ZwSetValueKey [NTDLL.@]
734 * win95 does not care about count for REG_SZ and finds out the len by itself (js)
735 * NT does definitely care (aj)
737 NTSTATUS WINAPI NtSetValueKey( HANDLE hkey, const UNICODE_STRING *name, ULONG TitleIndex,
738 ULONG type, const void *data, ULONG count )
742 TRACE( "(%p,%s,%ld,%p,%ld)\n", hkey, debugstr_us(name), type, data, count );
744 if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
746 SERVER_START_REQ( set_key_value )
750 req->namelen = name->Length;
751 wine_server_add_data( req, name->Buffer, name->Length );
752 wine_server_add_data( req, data, count );
753 ret = wine_server_call( req );
759 /******************************************************************************
760 * RtlpNtSetValueKey [NTDLL.@]
763 NTSTATUS WINAPI RtlpNtSetValueKey( HANDLE hkey, ULONG type, const void *data,
769 return NtSetValueKey( hkey, &name, 0, type, data, count );
772 /******************************************************************************
773 * NtUnloadKey [NTDLL.@]
774 * ZwUnloadKey [NTDLL.@]
776 NTSTATUS WINAPI NtUnloadKey(IN HANDLE KeyHandle)
780 TRACE("(%p)\n", KeyHandle);
782 SERVER_START_REQ( unload_registry )
784 req->hkey = KeyHandle;
785 ret = wine_server_call(req);
792 /******************************************************************************
793 * RtlFormatCurrentUserKeyPath [NTDLL.@]
796 NTSTATUS WINAPI RtlFormatCurrentUserKeyPath( IN OUT PUNICODE_STRING KeyPath)
798 static const WCHAR pathW[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\'};
802 status = NtOpenThreadToken(GetCurrentThread(), TOKEN_READ, TRUE, &token);
803 if (status == STATUS_NO_TOKEN)
804 status = NtOpenProcessToken(GetCurrentProcess(), TOKEN_READ, &token);
805 if (status == STATUS_SUCCESS)
807 char buffer[sizeof(TOKEN_USER) + sizeof(SID) + sizeof(DWORD)*SID_MAX_SUB_AUTHORITIES];
808 DWORD len = sizeof(buffer);
810 status = NtQueryInformationToken(token, TokenUser, buffer, len, &len);
811 if (status == STATUS_SUCCESS)
813 KeyPath->MaximumLength = 0;
814 status = RtlConvertSidToUnicodeString(KeyPath, ((TOKEN_USER *)buffer)->User.Sid, FALSE);
815 if (status == STATUS_BUFFER_OVERFLOW)
817 PWCHAR buf = RtlAllocateHeap(GetProcessHeap(), 0,
818 sizeof(pathW) + KeyPath->Length + sizeof(WCHAR));
821 memcpy(buf, pathW, sizeof(pathW));
822 KeyPath->MaximumLength = KeyPath->Length + sizeof(WCHAR);
823 KeyPath->Buffer = (PWCHAR)((LPBYTE)buf + sizeof(pathW));
824 status = RtlConvertSidToUnicodeString(KeyPath,
825 ((TOKEN_USER *)buffer)->User.Sid, FALSE);
826 KeyPath->Buffer = (PWCHAR)buf;
827 KeyPath->Length += sizeof(pathW);
828 KeyPath->MaximumLength += sizeof(pathW);
831 status = STATUS_NO_MEMORY;
839 /******************************************************************************
840 * RtlOpenCurrentUser [NTDLL.@]
843 * If we return just HKEY_CURRENT_USER the advapi tries to find a remote
844 * registry (odd handle) and fails.
846 DWORD WINAPI RtlOpenCurrentUser(
847 IN ACCESS_MASK DesiredAccess, /* [in] */
848 OUT PHANDLE KeyHandle) /* [out] handle of HKEY_CURRENT_USER */
850 OBJECT_ATTRIBUTES ObjectAttributes;
851 UNICODE_STRING ObjectName;
854 TRACE("(0x%08lx, %p)\n",DesiredAccess, KeyHandle);
856 RtlFormatCurrentUserKeyPath(&ObjectName);
857 InitializeObjectAttributes(&ObjectAttributes,&ObjectName,OBJ_CASE_INSENSITIVE,0, NULL);
858 ret = NtCreateKey(KeyHandle, DesiredAccess, &ObjectAttributes, 0, NULL, 0, NULL);
859 RtlFreeUnicodeString(&ObjectName);
864 static NTSTATUS RTL_ReportRegistryValue(PKEY_VALUE_FULL_INFORMATION pInfo,
865 PRTL_QUERY_REGISTRY_TABLE pQuery, PVOID pContext, PVOID pEnvironment)
868 UNICODE_STRING src, dst;
873 NTSTATUS status = STATUS_SUCCESS;
880 if (pQuery->Flags & RTL_QUERY_REGISTRY_DIRECT)
881 return STATUS_INVALID_PARAMETER;
884 status = pQuery->QueryRoutine(pQuery->Name, pQuery->DefaultType, pQuery->DefaultData,
885 pQuery->DefaultLength, pContext, pQuery->EntryContext);
889 len = pInfo->DataLength;
891 if (pQuery->Flags & RTL_QUERY_REGISTRY_DIRECT)
893 str = (PUNICODE_STRING)pQuery->EntryContext;
898 if (!(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND))
900 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
902 dst.MaximumLength = 0;
903 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
905 dst.MaximumLength = res;
906 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
907 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
908 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
909 dst.Length, pContext, pQuery->EntryContext);
910 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
915 if (str->Buffer == NULL)
916 RtlCreateUnicodeString(str, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
918 RtlAppendUnicodeToString(str, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
922 if (!(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND))
923 return STATUS_INVALID_PARAMETER;
925 if (str->Buffer == NULL)
927 str->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, len);
928 str->MaximumLength = len;
930 len = min(len, str->MaximumLength);
931 memcpy(str->Buffer, ((CHAR*)pInfo) + pInfo->DataOffset, len);
936 bin = (LONG*)pQuery->EntryContext;
937 if (pInfo->DataLength <= sizeof(ULONG))
938 memcpy(bin, ((CHAR*)pInfo) + pInfo->DataOffset,
942 if (bin[0] <= sizeof(ULONG))
944 memcpy(&bin[1], ((CHAR*)pInfo) + pInfo->DataOffset,
945 min(-bin[0], pInfo->DataLength));
949 len = min(bin[0], pInfo->DataLength);
951 bin[2] = pInfo->Type;
952 memcpy(&bin[3], ((CHAR*)pInfo) + pInfo->DataOffset, len);
960 if((pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND) ||
961 (pInfo->Type != REG_EXPAND_SZ && pInfo->Type != REG_MULTI_SZ))
963 status = pQuery->QueryRoutine(pInfo->Name, pInfo->Type,
964 ((CHAR*)pInfo) + pInfo->DataOffset, pInfo->DataLength,
965 pContext, pQuery->EntryContext);
967 else if (pInfo->Type == REG_EXPAND_SZ)
969 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
971 dst.MaximumLength = 0;
972 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
974 dst.MaximumLength = res;
975 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
976 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
977 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
978 dst.Length, pContext, pQuery->EntryContext);
979 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
981 else /* REG_MULTI_SZ */
983 if(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND)
985 for (offset = 0; offset <= pInfo->DataLength; offset += len + sizeof(WCHAR))
987 wstr = (WCHAR*)(((CHAR*)pInfo) + offset);
988 len = strlenW(wstr) * sizeof(WCHAR);
989 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, wstr, len,
990 pContext, pQuery->EntryContext);
991 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
997 while(count<=pInfo->DataLength)
999 String = (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset)+count;
1000 count+=strlenW(String)+1;
1001 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
1003 dst.MaximumLength = 0;
1004 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
1006 dst.MaximumLength = res;
1007 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
1008 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
1009 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
1010 dst.Length, pContext, pQuery->EntryContext);
1011 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
1012 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1022 static NTSTATUS RTL_GetKeyHandle(ULONG RelativeTo, PCWSTR Path, PHANDLE handle)
1024 UNICODE_STRING KeyString;
1025 OBJECT_ATTRIBUTES regkey;
1030 static const WCHAR empty[] = {0};
1031 static const WCHAR control[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e',
1032 '\\','S','y','s','t','e','m','\\','C','u','r','r','e','n','t',' ','C','o','n','t','r','o','l','S','e','t','\\',
1033 'C','o','n','t','r','o','l','\\',0};
1035 static const WCHAR devicemap[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1036 'H','a','r','d','w','a','r','e','\\','D','e','v','i','c','e','M','a','p','\\',0};
1038 static const WCHAR services[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1039 'S','y','s','t','e','m','\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1040 'S','e','r','v','i','c','e','s','\\',0};
1042 static const WCHAR user[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\',
1043 'C','u','r','r','e','n','t','U','s','e','r','\\',0};
1045 static const WCHAR windows_nt[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1046 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1047 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',0};
1049 switch (RelativeTo & 0xff)
1051 case RTL_REGISTRY_ABSOLUTE:
1055 case RTL_REGISTRY_CONTROL:
1059 case RTL_REGISTRY_DEVICEMAP:
1063 case RTL_REGISTRY_SERVICES:
1067 case RTL_REGISTRY_USER:
1071 case RTL_REGISTRY_WINDOWS_NT:
1076 return STATUS_INVALID_PARAMETER;
1079 len = (strlenW(base) + strlenW(Path) + 1) * sizeof(WCHAR);
1080 KeyString.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, len);
1081 if (KeyString.Buffer == NULL)
1082 return STATUS_NO_MEMORY;
1084 strcpyW(KeyString.Buffer, base);
1085 strcatW(KeyString.Buffer, Path);
1086 KeyString.Length = len - sizeof(WCHAR);
1087 KeyString.MaximumLength = len;
1088 InitializeObjectAttributes(®key, &KeyString, OBJ_CASE_INSENSITIVE, NULL, NULL);
1089 status = NtOpenKey(handle, KEY_ALL_ACCESS, ®key);
1090 RtlFreeHeap(GetProcessHeap(), 0, KeyString.Buffer);
1094 /*************************************************************************
1095 * RtlQueryRegistryValues [NTDLL.@]
1097 * Query multiple registry values with a signle call.
1100 * RelativeTo [I] Registry path that Path refers to
1101 * Path [I] Path to key
1102 * QueryTable [I] Table of key values to query
1103 * Context [I] Paremeter to pass to the application defined QueryRoutine function
1104 * Environment [I] Optional parameter to use when performing expantion
1107 * STATUS_SUCCESS or an appropriate NTSTATUS error code.
1109 NTSTATUS WINAPI RtlQueryRegistryValues(IN ULONG RelativeTo, IN PCWSTR Path,
1110 IN PRTL_QUERY_REGISTRY_TABLE QueryTable, IN PVOID Context,
1111 IN PVOID Environment OPTIONAL)
1113 UNICODE_STRING Value;
1114 HANDLE handle, topkey;
1115 PKEY_VALUE_FULL_INFORMATION pInfo = NULL;
1116 ULONG len, buflen = 0;
1117 NTSTATUS status=STATUS_SUCCESS, ret = STATUS_SUCCESS;
1120 TRACE("(%ld, %s, %p, %p, %p)\n", RelativeTo, debugstr_w(Path), QueryTable, Context, Environment);
1123 return STATUS_INVALID_PARAMETER;
1125 /* get a valid handle */
1126 if (RelativeTo & RTL_REGISTRY_HANDLE)
1127 topkey = handle = (HANDLE)Path;
1130 status = RTL_GetKeyHandle(RelativeTo, Path, &topkey);
1133 if(status != STATUS_SUCCESS)
1136 /* Process query table entries */
1137 for (; QueryTable->QueryRoutine != NULL || QueryTable->Name != NULL; ++QueryTable)
1139 if (QueryTable->Flags &
1140 (RTL_QUERY_REGISTRY_SUBKEY | RTL_QUERY_REGISTRY_TOPKEY))
1142 /* topkey must be kept open just in case we will reuse it later */
1143 if (handle != topkey)
1146 if (QueryTable->Flags & RTL_QUERY_REGISTRY_SUBKEY)
1149 status = RTL_GetKeyHandle((ULONG)QueryTable->Name, Path, &handle);
1150 if(status != STATUS_SUCCESS)
1160 if (QueryTable->Flags & RTL_QUERY_REGISTRY_NOVALUE)
1162 QueryTable->QueryRoutine(QueryTable->Name, REG_NONE, NULL, 0,
1163 Context, QueryTable->EntryContext);
1169 if (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED)
1171 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1177 if (QueryTable->Name == NULL)
1179 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DIRECT)
1181 ret = STATUS_INVALID_PARAMETER;
1185 /* Report all subkeys */
1188 status = NtEnumerateValueKey(handle, i,
1189 KeyValueFullInformation, pInfo, buflen, &len);
1190 if (status == STATUS_NO_MORE_ENTRIES)
1192 if (status == STATUS_BUFFER_OVERFLOW ||
1193 status == STATUS_BUFFER_TOO_SMALL)
1196 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1197 pInfo = (KEY_VALUE_FULL_INFORMATION*)RtlAllocateHeap(
1198 GetProcessHeap(), 0, buflen);
1199 NtEnumerateValueKey(handle, i, KeyValueFullInformation,
1200 pInfo, buflen, &len);
1203 status = RTL_ReportRegistryValue(pInfo, QueryTable, Context, Environment);
1204 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1209 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DELETE)
1211 RtlInitUnicodeString(&Value, pInfo->Name);
1212 NtDeleteValueKey(handle, &Value);
1216 if (i == 0 && (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED))
1218 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1224 RtlInitUnicodeString(&Value, QueryTable->Name);
1225 status = NtQueryValueKey(handle, &Value, KeyValueFullInformation,
1226 pInfo, buflen, &len);
1227 if (status == STATUS_BUFFER_OVERFLOW ||
1228 status == STATUS_BUFFER_TOO_SMALL)
1231 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1232 pInfo = (KEY_VALUE_FULL_INFORMATION*)RtlAllocateHeap(
1233 GetProcessHeap(), 0, buflen);
1234 status = NtQueryValueKey(handle, &Value,
1235 KeyValueFullInformation, pInfo, buflen, &len);
1237 if (status != STATUS_SUCCESS)
1239 if (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED)
1241 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1244 status = RTL_ReportRegistryValue(NULL, QueryTable, Context, Environment);
1245 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1253 status = RTL_ReportRegistryValue(pInfo, QueryTable, Context, Environment);
1254 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1259 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DELETE)
1260 NtDeleteValueKey(handle, &Value);
1266 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1267 if (handle != topkey)
1273 /*************************************************************************
1274 * RtlCheckRegistryKey [NTDLL.@]
1276 * Query multiple registry values with a signle call.
1279 * RelativeTo [I] Registry path that Path refers to
1280 * Path [I] Path to key
1283 * STATUS_SUCCESS if the specified key exists, or an NTSTATUS error code.
1285 NTSTATUS WINAPI RtlCheckRegistryKey(IN ULONG RelativeTo, IN PWSTR Path)
1290 TRACE("(%ld, %s)\n", RelativeTo, debugstr_w(Path));
1292 if((!RelativeTo) && Path == NULL)
1293 return STATUS_OBJECT_PATH_SYNTAX_BAD;
1294 if(RelativeTo & RTL_REGISTRY_HANDLE)
1295 return STATUS_SUCCESS;
1297 status = RTL_GetKeyHandle(RelativeTo, Path, &handle);
1298 if (handle) NtClose(handle);
1299 if (status == STATUS_INVALID_HANDLE) status = STATUS_OBJECT_NAME_NOT_FOUND;
1303 /*************************************************************************
1304 * RtlDeleteRegistryValue [NTDLL.@]
1306 * Query multiple registry values with a signle call.
1309 * RelativeTo [I] Registry path that Path refers to
1310 * Path [I] Path to key
1311 * ValueName [I] Name of the value to delete
1314 * STATUS_SUCCESS if the specified key is successfully deleted, or an NTSTATUS error code.
1316 NTSTATUS WINAPI RtlDeleteRegistryValue(IN ULONG RelativeTo, IN PCWSTR Path, IN PCWSTR ValueName)
1320 UNICODE_STRING Value;
1322 TRACE("(%ld, %s, %s)\n", RelativeTo, debugstr_w(Path), debugstr_w(ValueName));
1324 RtlInitUnicodeString(&Value, ValueName);
1325 if(RelativeTo == RTL_REGISTRY_HANDLE)
1327 return NtDeleteValueKey((HANDLE)Path, &Value);
1329 status = RTL_GetKeyHandle(RelativeTo, Path, &handle);
1330 if (status) return status;
1331 status = NtDeleteValueKey(handle, &Value);