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->attributes = attr->Attributes;
69 req->options = options;
71 req->namelen = attr->ObjectName->Length;
72 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
73 if (class) wine_server_add_data( req, class->Buffer, class->Length );
74 if (!(ret = wine_server_call( req )))
76 *retkey = reply->hkey;
77 if (dispos) *dispos = reply->created ? REG_CREATED_NEW_KEY : REG_OPENED_EXISTING_KEY;
81 TRACE("<- %p\n", *retkey);
85 /******************************************************************************
86 * RtlpNtCreateKey [NTDLL.@]
90 NTSTATUS WINAPI RtlpNtCreateKey( PHANDLE retkey, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr,
91 ULONG TitleIndex, const UNICODE_STRING *class, ULONG options,
98 memcpy( &oa, attr, sizeof oa );
99 oa.Attributes &= ~(OBJ_PERMANENT|OBJ_EXCLUSIVE);
103 return NtCreateKey(retkey, access, attr, 0, NULL, 0, dispos);
106 /******************************************************************************
107 * NtOpenKey [NTDLL.@]
108 * ZwOpenKey [NTDLL.@]
110 * OUT HANDLE retkey (returns 0 when failure)
111 * IN ACCESS_MASK access
112 * IN POBJECT_ATTRIBUTES attr
114 NTSTATUS WINAPI NtOpenKey( PHANDLE retkey, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr )
117 DWORD len = attr->ObjectName->Length;
119 TRACE( "(%p,%s,%lx,%p)\n", attr->RootDirectory,
120 debugstr_us(attr->ObjectName), access, retkey );
122 if (len > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
123 if (!retkey) return STATUS_INVALID_PARAMETER;
125 SERVER_START_REQ( open_key )
127 req->parent = attr->RootDirectory;
128 req->access = access;
129 req->attributes = attr->Attributes;
130 wine_server_add_data( req, attr->ObjectName->Buffer, len );
131 ret = wine_server_call( req );
132 *retkey = reply->hkey;
135 TRACE("<- %p\n", *retkey);
139 /******************************************************************************
140 * RtlpNtOpenKey [NTDLL.@]
144 NTSTATUS WINAPI RtlpNtOpenKey( PHANDLE retkey, ACCESS_MASK access, OBJECT_ATTRIBUTES *attr )
147 attr->Attributes &= ~(OBJ_PERMANENT|OBJ_EXCLUSIVE);
148 return NtOpenKey(retkey, access, attr);
151 /******************************************************************************
152 * NtDeleteKey [NTDLL.@]
153 * ZwDeleteKey [NTDLL.@]
155 NTSTATUS WINAPI NtDeleteKey( HANDLE hkey )
159 TRACE( "(%p)\n", hkey );
161 SERVER_START_REQ( delete_key )
164 ret = wine_server_call( req );
170 /******************************************************************************
171 * RtlpNtMakeTemporaryKey [NTDLL.@]
175 NTSTATUS WINAPI RtlpNtMakeTemporaryKey( HANDLE hkey )
177 return NtDeleteKey(hkey);
180 /******************************************************************************
181 * NtDeleteValueKey [NTDLL.@]
182 * ZwDeleteValueKey [NTDLL.@]
184 NTSTATUS WINAPI NtDeleteValueKey( HANDLE hkey, const UNICODE_STRING *name )
188 TRACE( "(%p,%s)\n", hkey, debugstr_us(name) );
189 if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
191 SERVER_START_REQ( delete_key_value )
194 wine_server_add_data( req, name->Buffer, name->Length );
195 ret = wine_server_call( req );
202 /******************************************************************************
205 * Implementation of NtQueryKey and NtEnumerateKey
207 static NTSTATUS enumerate_key( HANDLE handle, int index, KEY_INFORMATION_CLASS info_class,
208 void *info, DWORD length, DWORD *result_len )
217 case KeyBasicInformation: data_ptr = ((KEY_BASIC_INFORMATION *)info)->Name; break;
218 case KeyFullInformation: data_ptr = ((KEY_FULL_INFORMATION *)info)->Class; break;
219 case KeyNodeInformation: data_ptr = ((KEY_NODE_INFORMATION *)info)->Name; break;
221 FIXME( "Information class %d not implemented\n", info_class );
222 return STATUS_INVALID_PARAMETER;
224 fixed_size = (char *)data_ptr - (char *)info;
226 SERVER_START_REQ( enum_key )
230 req->info_class = info_class;
231 if (length > fixed_size) wine_server_set_reply( req, data_ptr, length - fixed_size );
232 if (!(ret = wine_server_call( req )))
236 RtlSecondsSince1970ToTime( reply->modif, &modif );
240 case KeyBasicInformation:
242 KEY_BASIC_INFORMATION keyinfo;
243 fixed_size = (char *)keyinfo.Name - (char *)&keyinfo;
244 keyinfo.LastWriteTime = modif;
245 keyinfo.TitleIndex = 0;
246 keyinfo.NameLength = reply->namelen;
247 memcpy( info, &keyinfo, min( length, fixed_size ) );
250 case KeyFullInformation:
252 KEY_FULL_INFORMATION keyinfo;
253 fixed_size = (char *)keyinfo.Class - (char *)&keyinfo;
254 keyinfo.LastWriteTime = modif;
255 keyinfo.TitleIndex = 0;
256 keyinfo.ClassLength = wine_server_reply_size(reply);
257 keyinfo.ClassOffset = keyinfo.ClassLength ? fixed_size : -1;
258 keyinfo.SubKeys = reply->subkeys;
259 keyinfo.MaxNameLen = reply->max_subkey;
260 keyinfo.MaxClassLen = reply->max_class;
261 keyinfo.Values = reply->values;
262 keyinfo.MaxValueNameLen = reply->max_value;
263 keyinfo.MaxValueDataLen = reply->max_data;
264 memcpy( info, &keyinfo, min( length, fixed_size ) );
267 case KeyNodeInformation:
269 KEY_NODE_INFORMATION keyinfo;
270 fixed_size = (char *)keyinfo.Name - (char *)&keyinfo;
271 keyinfo.LastWriteTime = modif;
272 keyinfo.TitleIndex = 0;
273 keyinfo.ClassLength = max( 0, wine_server_reply_size(reply) - reply->namelen );
274 keyinfo.ClassOffset = keyinfo.ClassLength ? fixed_size + reply->namelen : -1;
275 keyinfo.NameLength = reply->namelen;
276 memcpy( info, &keyinfo, min( length, fixed_size ) );
280 *result_len = fixed_size + reply->total;
281 if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
290 /******************************************************************************
291 * NtEnumerateKey [NTDLL.@]
292 * ZwEnumerateKey [NTDLL.@]
295 * the name copied into the buffer is NOT 0-terminated
297 NTSTATUS WINAPI NtEnumerateKey( HANDLE handle, ULONG index, KEY_INFORMATION_CLASS info_class,
298 void *info, DWORD length, DWORD *result_len )
300 /* -1 means query key, so avoid it here */
301 if (index == (ULONG)-1) return STATUS_NO_MORE_ENTRIES;
302 return enumerate_key( handle, index, info_class, info, length, result_len );
306 /******************************************************************************
307 * RtlpNtEnumerateSubKey [NTDLL.@]
310 NTSTATUS WINAPI RtlpNtEnumerateSubKey( HANDLE handle, UNICODE_STRING *out, ULONG index )
312 KEY_BASIC_INFORMATION *info;
313 DWORD dwLen, dwResultLen;
318 dwLen = out->Length + sizeof(KEY_BASIC_INFORMATION);
319 info = (KEY_BASIC_INFORMATION*)RtlAllocateHeap( GetProcessHeap(), 0, dwLen );
321 return STATUS_NO_MEMORY;
329 ret = NtEnumerateKey( handle, index, KeyBasicInformation, info, dwLen, &dwResultLen );
330 dwResultLen -= sizeof(KEY_BASIC_INFORMATION);
332 if (ret == STATUS_BUFFER_OVERFLOW)
333 out->Length = dwResultLen;
336 if (out->Length < info->NameLength)
338 out->Length = dwResultLen;
339 ret = STATUS_BUFFER_OVERFLOW;
343 out->Length = info->NameLength;
344 memcpy(out->Buffer, info->Name, info->NameLength);
348 RtlFreeHeap( GetProcessHeap(), 0, info );
352 /******************************************************************************
353 * NtQueryKey [NTDLL.@]
354 * ZwQueryKey [NTDLL.@]
356 NTSTATUS WINAPI NtQueryKey( HANDLE handle, KEY_INFORMATION_CLASS info_class,
357 void *info, DWORD length, DWORD *result_len )
359 return enumerate_key( handle, -1, info_class, info, length, result_len );
363 /* fill the key value info structure for a specific info class */
364 static void copy_key_value_info( KEY_VALUE_INFORMATION_CLASS info_class, void *info,
365 DWORD length, int type, int name_len, int data_len )
369 case KeyValueBasicInformation:
371 KEY_VALUE_BASIC_INFORMATION keyinfo;
372 keyinfo.TitleIndex = 0;
374 keyinfo.NameLength = name_len;
375 length = min( length, (char *)keyinfo.Name - (char *)&keyinfo );
376 memcpy( info, &keyinfo, length );
379 case KeyValueFullInformation:
381 KEY_VALUE_FULL_INFORMATION keyinfo;
382 keyinfo.TitleIndex = 0;
384 keyinfo.DataOffset = (char *)keyinfo.Name - (char *)&keyinfo + name_len;
385 keyinfo.DataLength = data_len;
386 keyinfo.NameLength = name_len;
387 length = min( length, (char *)keyinfo.Name - (char *)&keyinfo );
388 memcpy( info, &keyinfo, length );
391 case KeyValuePartialInformation:
393 KEY_VALUE_PARTIAL_INFORMATION keyinfo;
394 keyinfo.TitleIndex = 0;
396 keyinfo.DataLength = data_len;
397 length = min( length, (char *)keyinfo.Data - (char *)&keyinfo );
398 memcpy( info, &keyinfo, length );
407 /******************************************************************************
408 * NtEnumerateValueKey [NTDLL.@]
409 * ZwEnumerateValueKey [NTDLL.@]
411 NTSTATUS WINAPI NtEnumerateValueKey( HANDLE handle, ULONG index,
412 KEY_VALUE_INFORMATION_CLASS info_class,
413 void *info, DWORD length, DWORD *result_len )
419 TRACE( "(%p,%lu,%d,%p,%ld)\n", handle, index, info_class, info, length );
421 /* compute the length we want to retrieve */
424 case KeyValueBasicInformation: ptr = ((KEY_VALUE_BASIC_INFORMATION *)info)->Name; break;
425 case KeyValueFullInformation: ptr = ((KEY_VALUE_FULL_INFORMATION *)info)->Name; break;
426 case KeyValuePartialInformation: ptr = ((KEY_VALUE_PARTIAL_INFORMATION *)info)->Data; break;
428 FIXME( "Information class %d not implemented\n", info_class );
429 return STATUS_INVALID_PARAMETER;
431 fixed_size = (char *)ptr - (char *)info;
433 SERVER_START_REQ( enum_key_value )
437 req->info_class = info_class;
438 if (length > fixed_size) wine_server_set_reply( req, ptr, length - fixed_size );
439 if (!(ret = wine_server_call( req )))
441 copy_key_value_info( info_class, info, length, reply->type, reply->namelen,
442 wine_server_reply_size(reply) - reply->namelen );
443 *result_len = fixed_size + reply->total;
444 if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
452 /******************************************************************************
453 * NtQueryValueKey [NTDLL.@]
454 * ZwQueryValueKey [NTDLL.@]
457 * the name in the KeyValueInformation is never set
459 NTSTATUS WINAPI NtQueryValueKey( HANDLE handle, const UNICODE_STRING *name,
460 KEY_VALUE_INFORMATION_CLASS info_class,
461 void *info, DWORD length, DWORD *result_len )
465 unsigned int fixed_size = 0;
467 TRACE( "(%p,%s,%d,%p,%ld)\n", handle, debugstr_us(name), info_class, info, length );
469 if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
471 /* compute the length we want to retrieve */
474 case KeyValueBasicInformation:
475 fixed_size = (char *)((KEY_VALUE_BASIC_INFORMATION *)info)->Name - (char *)info;
478 case KeyValueFullInformation:
479 data_ptr = (UCHAR *)((KEY_VALUE_FULL_INFORMATION *)info)->Name;
480 fixed_size = (char *)data_ptr - (char *)info;
482 case KeyValuePartialInformation:
483 data_ptr = ((KEY_VALUE_PARTIAL_INFORMATION *)info)->Data;
484 fixed_size = (char *)data_ptr - (char *)info;
487 FIXME( "Information class %d not implemented\n", info_class );
488 return STATUS_INVALID_PARAMETER;
491 SERVER_START_REQ( get_key_value )
494 wine_server_add_data( req, name->Buffer, name->Length );
495 if (length > fixed_size) wine_server_set_reply( req, data_ptr, length - fixed_size );
496 if (!(ret = wine_server_call( req )))
498 copy_key_value_info( info_class, info, length, reply->type,
499 0, wine_server_reply_size(reply) );
500 *result_len = fixed_size + reply->total;
501 if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
508 /******************************************************************************
509 * RtlpNtQueryValueKey [NTDLL.@]
512 NTSTATUS WINAPI RtlpNtQueryValueKey( HANDLE handle, ULONG *result_type, PBYTE dest,
515 KEY_VALUE_PARTIAL_INFORMATION *info;
519 DWORD dwLen = sizeof (KEY_VALUE_PARTIAL_INFORMATION) + result_len ? *result_len : 0;
521 info = (KEY_VALUE_PARTIAL_INFORMATION*)RtlAllocateHeap( GetProcessHeap(), 0, dwLen );
523 return STATUS_NO_MEMORY;
526 ret = NtQueryValueKey( handle, &name, KeyValuePartialInformation, info, dwLen, &dwResultLen );
528 if (!ret || ret == STATUS_BUFFER_OVERFLOW)
531 *result_len = info->DataLength;
534 *result_type = info->Type;
536 if (ret != STATUS_BUFFER_OVERFLOW)
537 memcpy( dest, info->Data, info->DataLength );
540 RtlFreeHeap( GetProcessHeap(), 0, info );
544 /******************************************************************************
545 * NtFlushKey [NTDLL.@]
546 * ZwFlushKey [NTDLL.@]
548 NTSTATUS WINAPI NtFlushKey(HANDLE key)
552 TRACE("key=%p\n", key);
554 SERVER_START_REQ( flush_key )
557 ret = wine_server_call( req );
564 /******************************************************************************
565 * NtLoadKey [NTDLL.@]
566 * ZwLoadKey [NTDLL.@]
568 NTSTATUS WINAPI NtLoadKey( const OBJECT_ATTRIBUTES *attr, OBJECT_ATTRIBUTES *file )
574 TRACE("(%p,%p)\n", attr, file);
576 ret = NtCreateFile(&hive, GENERIC_READ, file, &io, NULL, FILE_ATTRIBUTE_NORMAL, 0,
577 FILE_OPEN, 0, NULL, 0);
580 SERVER_START_REQ( load_registry )
582 req->hkey = attr->RootDirectory;
584 wine_server_add_data(req, attr->ObjectName->Buffer, attr->ObjectName->Length);
585 ret = wine_server_call( req );
594 /******************************************************************************
595 * NtNotifyChangeKey [NTDLL.@]
596 * ZwNotifyChangeKey [NTDLL.@]
598 NTSTATUS WINAPI NtNotifyChangeKey(
601 IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
602 IN PVOID ApcContext OPTIONAL,
603 OUT PIO_STATUS_BLOCK IoStatusBlock,
604 IN ULONG CompletionFilter,
605 IN BOOLEAN Asynchronous,
606 OUT PVOID ChangeBuffer,
608 IN BOOLEAN WatchSubtree)
612 TRACE("(%p,%p,%p,%p,%p,0x%08lx, 0x%08x,%p,0x%08lx,0x%08x)\n",
613 KeyHandle, Event, ApcRoutine, ApcContext, IoStatusBlock, CompletionFilter,
614 Asynchronous, ChangeBuffer, Length, WatchSubtree);
616 if (ApcRoutine || ApcContext || ChangeBuffer || Length)
617 FIXME("Unimplemented optional parameter\n");
621 OBJECT_ATTRIBUTES attr;
622 InitializeObjectAttributes( &attr, NULL, 0, NULL, NULL );
623 ret = NtCreateEvent( &Event, EVENT_ALL_ACCESS, &attr, FALSE, FALSE );
624 if (ret != STATUS_SUCCESS)
628 SERVER_START_REQ( set_registry_notification )
630 req->hkey = KeyHandle;
632 req->subtree = WatchSubtree;
633 req->filter = CompletionFilter;
634 ret = wine_server_call( req );
640 if (ret == STATUS_SUCCESS)
641 NtWaitForSingleObject( Event, FALSE, NULL );
645 return STATUS_SUCCESS;
648 /******************************************************************************
649 * NtQueryMultipleValueKey [NTDLL]
650 * ZwQueryMultipleValueKey
653 NTSTATUS WINAPI NtQueryMultipleValueKey(
655 PKEY_MULTIPLE_VALUE_INFORMATION ListOfValuesToQuery,
657 PVOID MultipleValueInformation,
661 FIXME("(%p,%p,0x%08lx,%p,0x%08lx,%p) stub!\n",
662 KeyHandle, ListOfValuesToQuery, NumberOfItems, MultipleValueInformation,
663 Length,ReturnLength);
664 return STATUS_SUCCESS;
667 /******************************************************************************
668 * NtReplaceKey [NTDLL.@]
669 * ZwReplaceKey [NTDLL.@]
671 NTSTATUS WINAPI NtReplaceKey(
672 IN POBJECT_ATTRIBUTES ObjectAttributes,
674 IN POBJECT_ATTRIBUTES ReplacedObjectAttributes)
676 FIXME("(%p),stub!\n", Key);
677 dump_ObjectAttributes(ObjectAttributes);
678 dump_ObjectAttributes(ReplacedObjectAttributes);
679 return STATUS_SUCCESS;
681 /******************************************************************************
682 * NtRestoreKey [NTDLL.@]
683 * ZwRestoreKey [NTDLL.@]
685 NTSTATUS WINAPI NtRestoreKey(
690 FIXME("(%p,%p,0x%08lx) stub\n",
691 KeyHandle, FileHandle, RestoreFlags);
692 return STATUS_SUCCESS;
694 /******************************************************************************
695 * NtSaveKey [NTDLL.@]
696 * ZwSaveKey [NTDLL.@]
698 NTSTATUS WINAPI NtSaveKey(IN HANDLE KeyHandle, IN HANDLE FileHandle)
702 TRACE("(%p,%p)\n", KeyHandle, FileHandle);
704 SERVER_START_REQ( save_registry )
706 req->hkey = KeyHandle;
707 req->file = FileHandle;
708 ret = wine_server_call( req );
714 /******************************************************************************
715 * NtSetInformationKey [NTDLL.@]
716 * ZwSetInformationKey [NTDLL.@]
718 NTSTATUS WINAPI NtSetInformationKey(
720 IN const int KeyInformationClass,
721 IN PVOID KeyInformation,
722 IN ULONG KeyInformationLength)
724 FIXME("(%p,0x%08x,%p,0x%08lx) stub\n",
725 KeyHandle, KeyInformationClass, KeyInformation, KeyInformationLength);
726 return STATUS_SUCCESS;
730 /******************************************************************************
731 * NtSetValueKey [NTDLL.@]
732 * ZwSetValueKey [NTDLL.@]
735 * win95 does not care about count for REG_SZ and finds out the len by itself (js)
736 * NT does definitely care (aj)
738 NTSTATUS WINAPI NtSetValueKey( HANDLE hkey, const UNICODE_STRING *name, ULONG TitleIndex,
739 ULONG type, const void *data, ULONG count )
743 TRACE( "(%p,%s,%ld,%p,%ld)\n", hkey, debugstr_us(name), type, data, count );
745 if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
747 SERVER_START_REQ( set_key_value )
751 req->namelen = name->Length;
752 wine_server_add_data( req, name->Buffer, name->Length );
753 wine_server_add_data( req, data, count );
754 ret = wine_server_call( req );
760 /******************************************************************************
761 * RtlpNtSetValueKey [NTDLL.@]
764 NTSTATUS WINAPI RtlpNtSetValueKey( HANDLE hkey, ULONG type, const void *data,
770 return NtSetValueKey( hkey, &name, 0, type, data, count );
773 /******************************************************************************
774 * NtUnloadKey [NTDLL.@]
775 * ZwUnloadKey [NTDLL.@]
777 NTSTATUS WINAPI NtUnloadKey(IN HANDLE KeyHandle)
781 TRACE("(%p)\n", KeyHandle);
783 SERVER_START_REQ( unload_registry )
785 req->hkey = KeyHandle;
786 ret = wine_server_call(req);
793 /******************************************************************************
794 * RtlFormatCurrentUserKeyPath [NTDLL.@]
797 NTSTATUS WINAPI RtlFormatCurrentUserKeyPath( IN OUT PUNICODE_STRING KeyPath)
799 static const WCHAR pathW[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\'};
803 status = NtOpenThreadToken(GetCurrentThread(), TOKEN_READ, TRUE, &token);
804 if (status == STATUS_NO_TOKEN)
805 status = NtOpenProcessToken(GetCurrentProcess(), TOKEN_READ, &token);
806 if (status == STATUS_SUCCESS)
808 char buffer[sizeof(TOKEN_USER) + sizeof(SID) + sizeof(DWORD)*SID_MAX_SUB_AUTHORITIES];
809 DWORD len = sizeof(buffer);
811 status = NtQueryInformationToken(token, TokenUser, buffer, len, &len);
812 if (status == STATUS_SUCCESS)
814 KeyPath->MaximumLength = 0;
815 status = RtlConvertSidToUnicodeString(KeyPath, ((TOKEN_USER *)buffer)->User.Sid, FALSE);
816 if (status == STATUS_BUFFER_OVERFLOW)
818 PWCHAR buf = RtlAllocateHeap(GetProcessHeap(), 0,
819 sizeof(pathW) + KeyPath->Length + sizeof(WCHAR));
822 memcpy(buf, pathW, sizeof(pathW));
823 KeyPath->MaximumLength = KeyPath->Length + sizeof(WCHAR);
824 KeyPath->Buffer = (PWCHAR)((LPBYTE)buf + sizeof(pathW));
825 status = RtlConvertSidToUnicodeString(KeyPath,
826 ((TOKEN_USER *)buffer)->User.Sid, FALSE);
827 KeyPath->Buffer = (PWCHAR)buf;
828 KeyPath->Length += sizeof(pathW);
829 KeyPath->MaximumLength += sizeof(pathW);
832 status = STATUS_NO_MEMORY;
840 /******************************************************************************
841 * RtlOpenCurrentUser [NTDLL.@]
844 * If we return just HKEY_CURRENT_USER the advapi tries to find a remote
845 * registry (odd handle) and fails.
847 NTSTATUS WINAPI RtlOpenCurrentUser(
848 IN ACCESS_MASK DesiredAccess, /* [in] */
849 OUT PHANDLE KeyHandle) /* [out] handle of HKEY_CURRENT_USER */
851 OBJECT_ATTRIBUTES ObjectAttributes;
852 UNICODE_STRING ObjectName;
855 TRACE("(0x%08lx, %p)\n",DesiredAccess, KeyHandle);
857 if ((ret = RtlFormatCurrentUserKeyPath(&ObjectName))) return ret;
858 InitializeObjectAttributes(&ObjectAttributes,&ObjectName,OBJ_CASE_INSENSITIVE,0, NULL);
859 ret = NtCreateKey(KeyHandle, DesiredAccess, &ObjectAttributes, 0, NULL, 0, NULL);
860 RtlFreeUnicodeString(&ObjectName);
865 static NTSTATUS RTL_ReportRegistryValue(PKEY_VALUE_FULL_INFORMATION pInfo,
866 PRTL_QUERY_REGISTRY_TABLE pQuery, PVOID pContext, PVOID pEnvironment)
869 UNICODE_STRING src, dst;
874 NTSTATUS status = STATUS_SUCCESS;
881 if (pQuery->Flags & RTL_QUERY_REGISTRY_DIRECT)
882 return STATUS_INVALID_PARAMETER;
885 status = pQuery->QueryRoutine(pQuery->Name, pQuery->DefaultType, pQuery->DefaultData,
886 pQuery->DefaultLength, pContext, pQuery->EntryContext);
890 len = pInfo->DataLength;
892 if (pQuery->Flags & RTL_QUERY_REGISTRY_DIRECT)
894 str = (PUNICODE_STRING)pQuery->EntryContext;
899 if (!(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND))
901 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
903 dst.MaximumLength = 0;
904 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
906 dst.MaximumLength = res;
907 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
908 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
909 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
910 dst.Length, pContext, pQuery->EntryContext);
911 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
916 if (str->Buffer == NULL)
917 RtlCreateUnicodeString(str, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
919 RtlAppendUnicodeToString(str, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
923 if (!(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND))
924 return STATUS_INVALID_PARAMETER;
926 if (str->Buffer == NULL)
928 str->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, len);
929 str->MaximumLength = len;
931 len = min(len, str->MaximumLength);
932 memcpy(str->Buffer, ((CHAR*)pInfo) + pInfo->DataOffset, len);
937 bin = (LONG*)pQuery->EntryContext;
938 if (pInfo->DataLength <= sizeof(ULONG))
939 memcpy(bin, ((CHAR*)pInfo) + pInfo->DataOffset,
943 if (bin[0] <= sizeof(ULONG))
945 memcpy(&bin[1], ((CHAR*)pInfo) + pInfo->DataOffset,
946 min(-bin[0], pInfo->DataLength));
950 len = min(bin[0], pInfo->DataLength);
952 bin[2] = pInfo->Type;
953 memcpy(&bin[3], ((CHAR*)pInfo) + pInfo->DataOffset, len);
961 if((pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND) ||
962 (pInfo->Type != REG_EXPAND_SZ && pInfo->Type != REG_MULTI_SZ))
964 status = pQuery->QueryRoutine(pInfo->Name, pInfo->Type,
965 ((CHAR*)pInfo) + pInfo->DataOffset, pInfo->DataLength,
966 pContext, pQuery->EntryContext);
968 else if (pInfo->Type == REG_EXPAND_SZ)
970 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
972 dst.MaximumLength = 0;
973 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
975 dst.MaximumLength = res;
976 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
977 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
978 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
979 dst.Length, pContext, pQuery->EntryContext);
980 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
982 else /* REG_MULTI_SZ */
984 if(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND)
986 for (offset = 0; offset <= pInfo->DataLength; offset += len + sizeof(WCHAR))
988 wstr = (WCHAR*)(((CHAR*)pInfo) + offset);
989 len = strlenW(wstr) * sizeof(WCHAR);
990 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, wstr, len,
991 pContext, pQuery->EntryContext);
992 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
998 while(count<=pInfo->DataLength)
1000 String = (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset)+count;
1001 count+=strlenW(String)+1;
1002 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
1004 dst.MaximumLength = 0;
1005 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
1007 dst.MaximumLength = res;
1008 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
1009 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
1010 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
1011 dst.Length, pContext, pQuery->EntryContext);
1012 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
1013 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1023 static NTSTATUS RTL_GetKeyHandle(ULONG RelativeTo, PCWSTR Path, PHANDLE handle)
1025 UNICODE_STRING KeyString;
1026 OBJECT_ATTRIBUTES regkey;
1031 static const WCHAR empty[] = {0};
1032 static const WCHAR control[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e',
1033 '\\','S','y','s','t','e','m','\\','C','u','r','r','e','n','t',' ','C','o','n','t','r','o','l','S','e','t','\\',
1034 'C','o','n','t','r','o','l','\\',0};
1036 static const WCHAR devicemap[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1037 'H','a','r','d','w','a','r','e','\\','D','e','v','i','c','e','M','a','p','\\',0};
1039 static const WCHAR services[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1040 'S','y','s','t','e','m','\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1041 'S','e','r','v','i','c','e','s','\\',0};
1043 static const WCHAR user[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\',
1044 'C','u','r','r','e','n','t','U','s','e','r','\\',0};
1046 static const WCHAR windows_nt[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1047 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1048 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',0};
1050 switch (RelativeTo & 0xff)
1052 case RTL_REGISTRY_ABSOLUTE:
1056 case RTL_REGISTRY_CONTROL:
1060 case RTL_REGISTRY_DEVICEMAP:
1064 case RTL_REGISTRY_SERVICES:
1068 case RTL_REGISTRY_USER:
1072 case RTL_REGISTRY_WINDOWS_NT:
1077 return STATUS_INVALID_PARAMETER;
1080 len = (strlenW(base) + strlenW(Path) + 1) * sizeof(WCHAR);
1081 KeyString.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, len);
1082 if (KeyString.Buffer == NULL)
1083 return STATUS_NO_MEMORY;
1085 strcpyW(KeyString.Buffer, base);
1086 strcatW(KeyString.Buffer, Path);
1087 KeyString.Length = len - sizeof(WCHAR);
1088 KeyString.MaximumLength = len;
1089 InitializeObjectAttributes(®key, &KeyString, OBJ_CASE_INSENSITIVE, NULL, NULL);
1090 status = NtOpenKey(handle, KEY_ALL_ACCESS, ®key);
1091 RtlFreeHeap(GetProcessHeap(), 0, KeyString.Buffer);
1095 /*************************************************************************
1096 * RtlQueryRegistryValues [NTDLL.@]
1098 * Query multiple registry values with a signle call.
1101 * RelativeTo [I] Registry path that Path refers to
1102 * Path [I] Path to key
1103 * QueryTable [I] Table of key values to query
1104 * Context [I] Paremeter to pass to the application defined QueryRoutine function
1105 * Environment [I] Optional parameter to use when performing expantion
1108 * STATUS_SUCCESS or an appropriate NTSTATUS error code.
1110 NTSTATUS WINAPI RtlQueryRegistryValues(IN ULONG RelativeTo, IN PCWSTR Path,
1111 IN PRTL_QUERY_REGISTRY_TABLE QueryTable, IN PVOID Context,
1112 IN PVOID Environment OPTIONAL)
1114 UNICODE_STRING Value;
1115 HANDLE handle, topkey;
1116 PKEY_VALUE_FULL_INFORMATION pInfo = NULL;
1117 ULONG len, buflen = 0;
1118 NTSTATUS status=STATUS_SUCCESS, ret = STATUS_SUCCESS;
1121 TRACE("(%ld, %s, %p, %p, %p)\n", RelativeTo, debugstr_w(Path), QueryTable, Context, Environment);
1124 return STATUS_INVALID_PARAMETER;
1126 /* get a valid handle */
1127 if (RelativeTo & RTL_REGISTRY_HANDLE)
1128 topkey = handle = (HANDLE)Path;
1131 status = RTL_GetKeyHandle(RelativeTo, Path, &topkey);
1134 if(status != STATUS_SUCCESS)
1137 /* Process query table entries */
1138 for (; QueryTable->QueryRoutine != NULL || QueryTable->Name != NULL; ++QueryTable)
1140 if (QueryTable->Flags &
1141 (RTL_QUERY_REGISTRY_SUBKEY | RTL_QUERY_REGISTRY_TOPKEY))
1143 /* topkey must be kept open just in case we will reuse it later */
1144 if (handle != topkey)
1147 if (QueryTable->Flags & RTL_QUERY_REGISTRY_SUBKEY)
1150 status = RTL_GetKeyHandle((ULONG)QueryTable->Name, Path, &handle);
1151 if(status != STATUS_SUCCESS)
1161 if (QueryTable->Flags & RTL_QUERY_REGISTRY_NOVALUE)
1163 QueryTable->QueryRoutine(QueryTable->Name, REG_NONE, NULL, 0,
1164 Context, QueryTable->EntryContext);
1170 if (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED)
1172 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1178 if (QueryTable->Name == NULL)
1180 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DIRECT)
1182 ret = STATUS_INVALID_PARAMETER;
1186 /* Report all subkeys */
1189 status = NtEnumerateValueKey(handle, i,
1190 KeyValueFullInformation, pInfo, buflen, &len);
1191 if (status == STATUS_NO_MORE_ENTRIES)
1193 if (status == STATUS_BUFFER_OVERFLOW ||
1194 status == STATUS_BUFFER_TOO_SMALL)
1197 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1198 pInfo = (KEY_VALUE_FULL_INFORMATION*)RtlAllocateHeap(
1199 GetProcessHeap(), 0, buflen);
1200 NtEnumerateValueKey(handle, i, KeyValueFullInformation,
1201 pInfo, buflen, &len);
1204 status = RTL_ReportRegistryValue(pInfo, QueryTable, Context, Environment);
1205 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1210 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DELETE)
1212 RtlInitUnicodeString(&Value, pInfo->Name);
1213 NtDeleteValueKey(handle, &Value);
1217 if (i == 0 && (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED))
1219 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1225 RtlInitUnicodeString(&Value, QueryTable->Name);
1226 status = NtQueryValueKey(handle, &Value, KeyValueFullInformation,
1227 pInfo, buflen, &len);
1228 if (status == STATUS_BUFFER_OVERFLOW ||
1229 status == STATUS_BUFFER_TOO_SMALL)
1232 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1233 pInfo = (KEY_VALUE_FULL_INFORMATION*)RtlAllocateHeap(
1234 GetProcessHeap(), 0, buflen);
1235 status = NtQueryValueKey(handle, &Value,
1236 KeyValueFullInformation, pInfo, buflen, &len);
1238 if (status != STATUS_SUCCESS)
1240 if (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED)
1242 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1245 status = RTL_ReportRegistryValue(NULL, QueryTable, Context, Environment);
1246 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1254 status = RTL_ReportRegistryValue(pInfo, QueryTable, Context, Environment);
1255 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1260 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DELETE)
1261 NtDeleteValueKey(handle, &Value);
1267 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1268 if (handle != topkey)
1274 /*************************************************************************
1275 * RtlCheckRegistryKey [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
1284 * STATUS_SUCCESS if the specified key exists, or an NTSTATUS error code.
1286 NTSTATUS WINAPI RtlCheckRegistryKey(IN ULONG RelativeTo, IN PWSTR Path)
1291 TRACE("(%ld, %s)\n", RelativeTo, debugstr_w(Path));
1293 if((!RelativeTo) && Path == NULL)
1294 return STATUS_OBJECT_PATH_SYNTAX_BAD;
1295 if(RelativeTo & RTL_REGISTRY_HANDLE)
1296 return STATUS_SUCCESS;
1298 status = RTL_GetKeyHandle(RelativeTo, Path, &handle);
1299 if (handle) NtClose(handle);
1300 if (status == STATUS_INVALID_HANDLE) status = STATUS_OBJECT_NAME_NOT_FOUND;
1304 /*************************************************************************
1305 * RtlDeleteRegistryValue [NTDLL.@]
1307 * Query multiple registry values with a signle call.
1310 * RelativeTo [I] Registry path that Path refers to
1311 * Path [I] Path to key
1312 * ValueName [I] Name of the value to delete
1315 * STATUS_SUCCESS if the specified key is successfully deleted, or an NTSTATUS error code.
1317 NTSTATUS WINAPI RtlDeleteRegistryValue(IN ULONG RelativeTo, IN PCWSTR Path, IN PCWSTR ValueName)
1321 UNICODE_STRING Value;
1323 TRACE("(%ld, %s, %s)\n", RelativeTo, debugstr_w(Path), debugstr_w(ValueName));
1325 RtlInitUnicodeString(&Value, ValueName);
1326 if(RelativeTo == RTL_REGISTRY_HANDLE)
1328 return NtDeleteValueKey((HANDLE)Path, &Value);
1330 status = RTL_GetKeyHandle(RelativeTo, Path, &handle);
1331 if (status) return status;
1332 status = NtDeleteValueKey(handle, &Value);