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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 if (!retkey || !attr) return STATUS_ACCESS_VIOLATION;
59 if (attr->Length > sizeof(OBJECT_ATTRIBUTES)) return STATUS_INVALID_PARAMETER;
60 if (attr->ObjectName->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
62 TRACE( "(%p,%s,%s,%x,%x,%p)\n", attr->RootDirectory, debugstr_us(attr->ObjectName),
63 debugstr_us(class), options, access, retkey );
65 SERVER_START_REQ( create_key )
67 req->parent = attr->RootDirectory;
69 req->attributes = attr->Attributes;
70 req->options = options;
72 req->namelen = attr->ObjectName->Length;
73 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
74 if (class) wine_server_add_data( req, class->Buffer, class->Length );
75 if (!(ret = wine_server_call( req )))
77 *retkey = reply->hkey;
78 if (dispos) *dispos = reply->created ? REG_CREATED_NEW_KEY : REG_OPENED_EXISTING_KEY;
82 TRACE("<- %p\n", *retkey);
86 /******************************************************************************
87 * RtlpNtCreateKey [NTDLL.@]
91 NTSTATUS WINAPI RtlpNtCreateKey( PHANDLE retkey, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr,
92 ULONG TitleIndex, const UNICODE_STRING *class, ULONG options,
99 memcpy( &oa, attr, sizeof oa );
100 oa.Attributes &= ~(OBJ_PERMANENT|OBJ_EXCLUSIVE);
104 return NtCreateKey(retkey, access, attr, 0, NULL, 0, dispos);
107 /******************************************************************************
108 * NtOpenKey [NTDLL.@]
109 * ZwOpenKey [NTDLL.@]
111 * OUT HANDLE retkey (returns 0 when failure)
112 * IN ACCESS_MASK access
113 * IN POBJECT_ATTRIBUTES attr
115 NTSTATUS WINAPI NtOpenKey( PHANDLE retkey, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr )
118 DWORD len = attr->ObjectName->Length;
120 TRACE( "(%p,%s,%x,%p)\n", attr->RootDirectory,
121 debugstr_us(attr->ObjectName), access, retkey );
123 if (len > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
124 if (!retkey) return STATUS_INVALID_PARAMETER;
126 SERVER_START_REQ( open_key )
128 req->parent = attr->RootDirectory;
129 req->access = access;
130 req->attributes = attr->Attributes;
131 wine_server_add_data( req, attr->ObjectName->Buffer, len );
132 ret = wine_server_call( req );
133 *retkey = reply->hkey;
136 TRACE("<- %p\n", *retkey);
140 /******************************************************************************
141 * RtlpNtOpenKey [NTDLL.@]
145 NTSTATUS WINAPI RtlpNtOpenKey( PHANDLE retkey, ACCESS_MASK access, OBJECT_ATTRIBUTES *attr )
148 attr->Attributes &= ~(OBJ_PERMANENT|OBJ_EXCLUSIVE);
149 return NtOpenKey(retkey, access, attr);
152 /******************************************************************************
153 * NtDeleteKey [NTDLL.@]
154 * ZwDeleteKey [NTDLL.@]
156 NTSTATUS WINAPI NtDeleteKey( HANDLE hkey )
160 TRACE( "(%p)\n", hkey );
162 SERVER_START_REQ( delete_key )
165 ret = wine_server_call( req );
171 /******************************************************************************
172 * RtlpNtMakeTemporaryKey [NTDLL.@]
176 NTSTATUS WINAPI RtlpNtMakeTemporaryKey( HANDLE hkey )
178 return NtDeleteKey(hkey);
181 /******************************************************************************
182 * NtDeleteValueKey [NTDLL.@]
183 * ZwDeleteValueKey [NTDLL.@]
185 NTSTATUS WINAPI NtDeleteValueKey( HANDLE hkey, const UNICODE_STRING *name )
189 TRACE( "(%p,%s)\n", hkey, debugstr_us(name) );
190 if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
192 SERVER_START_REQ( delete_key_value )
195 wine_server_add_data( req, name->Buffer, name->Length );
196 ret = wine_server_call( req );
203 /******************************************************************************
206 * Implementation of NtQueryKey and NtEnumerateKey
208 static NTSTATUS enumerate_key( HANDLE handle, int index, KEY_INFORMATION_CLASS info_class,
209 void *info, DWORD length, DWORD *result_len )
218 case KeyBasicInformation: data_ptr = ((KEY_BASIC_INFORMATION *)info)->Name; break;
219 case KeyFullInformation: data_ptr = ((KEY_FULL_INFORMATION *)info)->Class; break;
220 case KeyNodeInformation: data_ptr = ((KEY_NODE_INFORMATION *)info)->Name; break;
222 FIXME( "Information class %d not implemented\n", info_class );
223 return STATUS_INVALID_PARAMETER;
225 fixed_size = (char *)data_ptr - (char *)info;
227 SERVER_START_REQ( enum_key )
231 req->info_class = info_class;
232 if (length > fixed_size) wine_server_set_reply( req, data_ptr, length - fixed_size );
233 if (!(ret = wine_server_call( req )))
237 RtlSecondsSince1970ToTime( reply->modif, &modif );
241 case KeyBasicInformation:
243 KEY_BASIC_INFORMATION keyinfo;
244 fixed_size = (char *)keyinfo.Name - (char *)&keyinfo;
245 keyinfo.LastWriteTime = modif;
246 keyinfo.TitleIndex = 0;
247 keyinfo.NameLength = reply->namelen;
248 memcpy( info, &keyinfo, min( length, fixed_size ) );
251 case KeyFullInformation:
253 KEY_FULL_INFORMATION keyinfo;
254 fixed_size = (char *)keyinfo.Class - (char *)&keyinfo;
255 keyinfo.LastWriteTime = modif;
256 keyinfo.TitleIndex = 0;
257 keyinfo.ClassLength = wine_server_reply_size(reply);
258 keyinfo.ClassOffset = keyinfo.ClassLength ? fixed_size : -1;
259 keyinfo.SubKeys = reply->subkeys;
260 keyinfo.MaxNameLen = reply->max_subkey;
261 keyinfo.MaxClassLen = reply->max_class;
262 keyinfo.Values = reply->values;
263 keyinfo.MaxValueNameLen = reply->max_value;
264 keyinfo.MaxValueDataLen = reply->max_data;
265 memcpy( info, &keyinfo, min( length, fixed_size ) );
268 case KeyNodeInformation:
270 KEY_NODE_INFORMATION keyinfo;
271 fixed_size = (char *)keyinfo.Name - (char *)&keyinfo;
272 keyinfo.LastWriteTime = modif;
273 keyinfo.TitleIndex = 0;
274 keyinfo.ClassLength = max( 0, wine_server_reply_size(reply) - reply->namelen );
275 keyinfo.ClassOffset = keyinfo.ClassLength ? fixed_size + reply->namelen : -1;
276 keyinfo.NameLength = reply->namelen;
277 memcpy( info, &keyinfo, min( length, fixed_size ) );
281 *result_len = fixed_size + reply->total;
282 if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
291 /******************************************************************************
292 * NtEnumerateKey [NTDLL.@]
293 * ZwEnumerateKey [NTDLL.@]
296 * the name copied into the buffer is NOT 0-terminated
298 NTSTATUS WINAPI NtEnumerateKey( HANDLE handle, ULONG index, KEY_INFORMATION_CLASS info_class,
299 void *info, DWORD length, DWORD *result_len )
301 /* -1 means query key, so avoid it here */
302 if (index == (ULONG)-1) return STATUS_NO_MORE_ENTRIES;
303 return enumerate_key( handle, index, info_class, info, length, result_len );
307 /******************************************************************************
308 * RtlpNtEnumerateSubKey [NTDLL.@]
311 NTSTATUS WINAPI RtlpNtEnumerateSubKey( HANDLE handle, UNICODE_STRING *out, ULONG index )
313 KEY_BASIC_INFORMATION *info;
314 DWORD dwLen, dwResultLen;
319 dwLen = out->Length + sizeof(KEY_BASIC_INFORMATION);
320 info = (KEY_BASIC_INFORMATION*)RtlAllocateHeap( GetProcessHeap(), 0, dwLen );
322 return STATUS_NO_MEMORY;
330 ret = NtEnumerateKey( handle, index, KeyBasicInformation, info, dwLen, &dwResultLen );
331 dwResultLen -= sizeof(KEY_BASIC_INFORMATION);
333 if (ret == STATUS_BUFFER_OVERFLOW)
334 out->Length = dwResultLen;
337 if (out->Length < info->NameLength)
339 out->Length = dwResultLen;
340 ret = STATUS_BUFFER_OVERFLOW;
344 out->Length = info->NameLength;
345 memcpy(out->Buffer, info->Name, info->NameLength);
349 RtlFreeHeap( GetProcessHeap(), 0, info );
353 /******************************************************************************
354 * NtQueryKey [NTDLL.@]
355 * ZwQueryKey [NTDLL.@]
357 NTSTATUS WINAPI NtQueryKey( HANDLE handle, KEY_INFORMATION_CLASS info_class,
358 void *info, DWORD length, DWORD *result_len )
360 return enumerate_key( handle, -1, info_class, info, length, result_len );
364 /* fill the key value info structure for a specific info class */
365 static void copy_key_value_info( KEY_VALUE_INFORMATION_CLASS info_class, void *info,
366 DWORD length, int type, int name_len, int data_len )
370 case KeyValueBasicInformation:
372 KEY_VALUE_BASIC_INFORMATION keyinfo;
373 keyinfo.TitleIndex = 0;
375 keyinfo.NameLength = name_len;
376 length = min( length, (char *)keyinfo.Name - (char *)&keyinfo );
377 memcpy( info, &keyinfo, length );
380 case KeyValueFullInformation:
382 KEY_VALUE_FULL_INFORMATION keyinfo;
383 keyinfo.TitleIndex = 0;
385 keyinfo.DataOffset = (char *)keyinfo.Name - (char *)&keyinfo + name_len;
386 keyinfo.DataLength = data_len;
387 keyinfo.NameLength = name_len;
388 length = min( length, (char *)keyinfo.Name - (char *)&keyinfo );
389 memcpy( info, &keyinfo, length );
392 case KeyValuePartialInformation:
394 KEY_VALUE_PARTIAL_INFORMATION keyinfo;
395 keyinfo.TitleIndex = 0;
397 keyinfo.DataLength = data_len;
398 length = min( length, (char *)keyinfo.Data - (char *)&keyinfo );
399 memcpy( info, &keyinfo, length );
408 /******************************************************************************
409 * NtEnumerateValueKey [NTDLL.@]
410 * ZwEnumerateValueKey [NTDLL.@]
412 NTSTATUS WINAPI NtEnumerateValueKey( HANDLE handle, ULONG index,
413 KEY_VALUE_INFORMATION_CLASS info_class,
414 void *info, DWORD length, DWORD *result_len )
420 TRACE( "(%p,%u,%d,%p,%d)\n", handle, index, info_class, info, length );
422 /* compute the length we want to retrieve */
425 case KeyValueBasicInformation: ptr = ((KEY_VALUE_BASIC_INFORMATION *)info)->Name; break;
426 case KeyValueFullInformation: ptr = ((KEY_VALUE_FULL_INFORMATION *)info)->Name; break;
427 case KeyValuePartialInformation: ptr = ((KEY_VALUE_PARTIAL_INFORMATION *)info)->Data; break;
429 FIXME( "Information class %d not implemented\n", info_class );
430 return STATUS_INVALID_PARAMETER;
432 fixed_size = (char *)ptr - (char *)info;
434 SERVER_START_REQ( enum_key_value )
438 req->info_class = info_class;
439 if (length > fixed_size) wine_server_set_reply( req, ptr, length - fixed_size );
440 if (!(ret = wine_server_call( req )))
442 copy_key_value_info( info_class, info, length, reply->type, reply->namelen,
443 wine_server_reply_size(reply) - reply->namelen );
444 *result_len = fixed_size + reply->total;
445 if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
453 /******************************************************************************
454 * NtQueryValueKey [NTDLL.@]
455 * ZwQueryValueKey [NTDLL.@]
458 * the name in the KeyValueInformation is never set
460 NTSTATUS WINAPI NtQueryValueKey( HANDLE handle, const UNICODE_STRING *name,
461 KEY_VALUE_INFORMATION_CLASS info_class,
462 void *info, DWORD length, DWORD *result_len )
466 unsigned int fixed_size = 0;
468 TRACE( "(%p,%s,%d,%p,%d)\n", handle, debugstr_us(name), info_class, info, length );
470 if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
472 /* compute the length we want to retrieve */
475 case KeyValueBasicInformation:
476 fixed_size = (char *)((KEY_VALUE_BASIC_INFORMATION *)info)->Name - (char *)info;
479 case KeyValueFullInformation:
480 data_ptr = (UCHAR *)((KEY_VALUE_FULL_INFORMATION *)info)->Name;
481 fixed_size = (char *)data_ptr - (char *)info;
483 case KeyValuePartialInformation:
484 data_ptr = ((KEY_VALUE_PARTIAL_INFORMATION *)info)->Data;
485 fixed_size = (char *)data_ptr - (char *)info;
488 FIXME( "Information class %d not implemented\n", info_class );
489 return STATUS_INVALID_PARAMETER;
492 SERVER_START_REQ( get_key_value )
495 wine_server_add_data( req, name->Buffer, name->Length );
496 if (length > fixed_size) wine_server_set_reply( req, data_ptr, length - fixed_size );
497 if (!(ret = wine_server_call( req )))
499 copy_key_value_info( info_class, info, length, reply->type,
500 0, wine_server_reply_size(reply) );
501 *result_len = fixed_size + reply->total;
502 if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
509 /******************************************************************************
510 * RtlpNtQueryValueKey [NTDLL.@]
513 NTSTATUS WINAPI RtlpNtQueryValueKey( HANDLE handle, ULONG *result_type, PBYTE dest,
516 KEY_VALUE_PARTIAL_INFORMATION *info;
520 DWORD dwLen = sizeof (KEY_VALUE_PARTIAL_INFORMATION) + (result_len ? *result_len : 0);
522 info = (KEY_VALUE_PARTIAL_INFORMATION*)RtlAllocateHeap( GetProcessHeap(), 0, dwLen );
524 return STATUS_NO_MEMORY;
527 ret = NtQueryValueKey( handle, &name, KeyValuePartialInformation, info, dwLen, &dwResultLen );
529 if (!ret || ret == STATUS_BUFFER_OVERFLOW)
532 *result_len = info->DataLength;
535 *result_type = info->Type;
537 if (ret != STATUS_BUFFER_OVERFLOW)
538 memcpy( dest, info->Data, info->DataLength );
541 RtlFreeHeap( GetProcessHeap(), 0, info );
545 /******************************************************************************
546 * NtFlushKey [NTDLL.@]
547 * ZwFlushKey [NTDLL.@]
549 NTSTATUS WINAPI NtFlushKey(HANDLE key)
553 TRACE("key=%p\n", key);
555 SERVER_START_REQ( flush_key )
558 ret = wine_server_call( req );
565 /******************************************************************************
566 * NtLoadKey [NTDLL.@]
567 * ZwLoadKey [NTDLL.@]
569 NTSTATUS WINAPI NtLoadKey( const OBJECT_ATTRIBUTES *attr, OBJECT_ATTRIBUTES *file )
575 TRACE("(%p,%p)\n", attr, file);
577 ret = NtCreateFile(&hive, GENERIC_READ, file, &io, NULL, FILE_ATTRIBUTE_NORMAL, 0,
578 FILE_OPEN, 0, NULL, 0);
581 SERVER_START_REQ( load_registry )
583 req->hkey = attr->RootDirectory;
585 wine_server_add_data(req, attr->ObjectName->Buffer, attr->ObjectName->Length);
586 ret = wine_server_call( req );
595 /******************************************************************************
596 * NtNotifyChangeKey [NTDLL.@]
597 * ZwNotifyChangeKey [NTDLL.@]
599 NTSTATUS WINAPI NtNotifyChangeKey(
602 IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
603 IN PVOID ApcContext OPTIONAL,
604 OUT PIO_STATUS_BLOCK IoStatusBlock,
605 IN ULONG CompletionFilter,
606 IN BOOLEAN Asynchronous,
607 OUT PVOID ChangeBuffer,
609 IN BOOLEAN WatchSubtree)
613 TRACE("(%p,%p,%p,%p,%p,0x%08x, 0x%08x,%p,0x%08x,0x%08x)\n",
614 KeyHandle, Event, ApcRoutine, ApcContext, IoStatusBlock, CompletionFilter,
615 Asynchronous, ChangeBuffer, Length, WatchSubtree);
617 if (ApcRoutine || ApcContext || ChangeBuffer || Length)
618 FIXME("Unimplemented optional parameter\n");
622 OBJECT_ATTRIBUTES attr;
623 InitializeObjectAttributes( &attr, NULL, 0, NULL, NULL );
624 ret = NtCreateEvent( &Event, EVENT_ALL_ACCESS, &attr, FALSE, FALSE );
625 if (ret != STATUS_SUCCESS)
629 SERVER_START_REQ( set_registry_notification )
631 req->hkey = KeyHandle;
633 req->subtree = WatchSubtree;
634 req->filter = CompletionFilter;
635 ret = wine_server_call( req );
641 if (ret == STATUS_SUCCESS)
642 NtWaitForSingleObject( Event, FALSE, NULL );
646 return STATUS_SUCCESS;
649 /******************************************************************************
650 * NtQueryMultipleValueKey [NTDLL]
651 * ZwQueryMultipleValueKey
654 NTSTATUS WINAPI NtQueryMultipleValueKey(
656 PKEY_MULTIPLE_VALUE_INFORMATION ListOfValuesToQuery,
658 PVOID MultipleValueInformation,
662 FIXME("(%p,%p,0x%08x,%p,0x%08x,%p) stub!\n",
663 KeyHandle, ListOfValuesToQuery, NumberOfItems, MultipleValueInformation,
664 Length,ReturnLength);
665 return STATUS_SUCCESS;
668 /******************************************************************************
669 * NtReplaceKey [NTDLL.@]
670 * ZwReplaceKey [NTDLL.@]
672 NTSTATUS WINAPI NtReplaceKey(
673 IN POBJECT_ATTRIBUTES ObjectAttributes,
675 IN POBJECT_ATTRIBUTES ReplacedObjectAttributes)
677 FIXME("(%p),stub!\n", Key);
678 dump_ObjectAttributes(ObjectAttributes);
679 dump_ObjectAttributes(ReplacedObjectAttributes);
680 return STATUS_SUCCESS;
682 /******************************************************************************
683 * NtRestoreKey [NTDLL.@]
684 * ZwRestoreKey [NTDLL.@]
686 NTSTATUS WINAPI NtRestoreKey(
691 FIXME("(%p,%p,0x%08x) stub\n",
692 KeyHandle, FileHandle, RestoreFlags);
693 return STATUS_SUCCESS;
695 /******************************************************************************
696 * NtSaveKey [NTDLL.@]
697 * ZwSaveKey [NTDLL.@]
699 NTSTATUS WINAPI NtSaveKey(IN HANDLE KeyHandle, IN HANDLE FileHandle)
703 TRACE("(%p,%p)\n", KeyHandle, FileHandle);
705 SERVER_START_REQ( save_registry )
707 req->hkey = KeyHandle;
708 req->file = FileHandle;
709 ret = wine_server_call( req );
715 /******************************************************************************
716 * NtSetInformationKey [NTDLL.@]
717 * ZwSetInformationKey [NTDLL.@]
719 NTSTATUS WINAPI NtSetInformationKey(
721 IN const int KeyInformationClass,
722 IN PVOID KeyInformation,
723 IN ULONG KeyInformationLength)
725 FIXME("(%p,0x%08x,%p,0x%08x) stub\n",
726 KeyHandle, KeyInformationClass, KeyInformation, KeyInformationLength);
727 return STATUS_SUCCESS;
731 /******************************************************************************
732 * NtSetValueKey [NTDLL.@]
733 * ZwSetValueKey [NTDLL.@]
736 * win95 does not care about count for REG_SZ and finds out the len by itself (js)
737 * NT does definitely care (aj)
739 NTSTATUS WINAPI NtSetValueKey( HANDLE hkey, const UNICODE_STRING *name, ULONG TitleIndex,
740 ULONG type, const void *data, ULONG count )
744 TRACE( "(%p,%s,%d,%p,%d)\n", hkey, debugstr_us(name), type, data, count );
746 if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
748 SERVER_START_REQ( set_key_value )
752 req->namelen = name->Length;
753 wine_server_add_data( req, name->Buffer, name->Length );
754 wine_server_add_data( req, data, count );
755 ret = wine_server_call( req );
761 /******************************************************************************
762 * RtlpNtSetValueKey [NTDLL.@]
765 NTSTATUS WINAPI RtlpNtSetValueKey( HANDLE hkey, ULONG type, const void *data,
771 return NtSetValueKey( hkey, &name, 0, type, data, count );
774 /******************************************************************************
775 * NtUnloadKey [NTDLL.@]
776 * ZwUnloadKey [NTDLL.@]
778 NTSTATUS WINAPI NtUnloadKey(IN POBJECT_ATTRIBUTES attr)
782 TRACE("(%p)\n", attr);
784 SERVER_START_REQ( unload_registry )
786 req->hkey = attr->RootDirectory;
787 ret = wine_server_call(req);
794 /******************************************************************************
795 * RtlFormatCurrentUserKeyPath [NTDLL.@]
798 NTSTATUS WINAPI RtlFormatCurrentUserKeyPath( IN OUT PUNICODE_STRING KeyPath)
800 static const WCHAR pathW[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\'};
804 status = NtOpenThreadToken(GetCurrentThread(), TOKEN_READ, TRUE, &token);
805 if (status == STATUS_NO_TOKEN)
806 status = NtOpenProcessToken(GetCurrentProcess(), TOKEN_READ, &token);
807 if (status == STATUS_SUCCESS)
809 char buffer[sizeof(TOKEN_USER) + sizeof(SID) + sizeof(DWORD)*SID_MAX_SUB_AUTHORITIES];
810 DWORD len = sizeof(buffer);
812 status = NtQueryInformationToken(token, TokenUser, buffer, len, &len);
813 if (status == STATUS_SUCCESS)
815 KeyPath->MaximumLength = 0;
816 status = RtlConvertSidToUnicodeString(KeyPath, ((TOKEN_USER *)buffer)->User.Sid, FALSE);
817 if (status == STATUS_BUFFER_OVERFLOW)
819 PWCHAR buf = RtlAllocateHeap(GetProcessHeap(), 0,
820 sizeof(pathW) + KeyPath->Length + sizeof(WCHAR));
823 memcpy(buf, pathW, sizeof(pathW));
824 KeyPath->MaximumLength = KeyPath->Length + sizeof(WCHAR);
825 KeyPath->Buffer = (PWCHAR)((LPBYTE)buf + sizeof(pathW));
826 status = RtlConvertSidToUnicodeString(KeyPath,
827 ((TOKEN_USER *)buffer)->User.Sid, FALSE);
828 KeyPath->Buffer = (PWCHAR)buf;
829 KeyPath->Length += sizeof(pathW);
830 KeyPath->MaximumLength += sizeof(pathW);
833 status = STATUS_NO_MEMORY;
841 /******************************************************************************
842 * RtlOpenCurrentUser [NTDLL.@]
845 * If we return just HKEY_CURRENT_USER the advapi tries to find a remote
846 * registry (odd handle) and fails.
848 NTSTATUS WINAPI RtlOpenCurrentUser(
849 IN ACCESS_MASK DesiredAccess, /* [in] */
850 OUT PHANDLE KeyHandle) /* [out] handle of HKEY_CURRENT_USER */
852 OBJECT_ATTRIBUTES ObjectAttributes;
853 UNICODE_STRING ObjectName;
856 TRACE("(0x%08x, %p)\n",DesiredAccess, KeyHandle);
858 if ((ret = RtlFormatCurrentUserKeyPath(&ObjectName))) return ret;
859 InitializeObjectAttributes(&ObjectAttributes,&ObjectName,OBJ_CASE_INSENSITIVE,0, NULL);
860 ret = NtCreateKey(KeyHandle, DesiredAccess, &ObjectAttributes, 0, NULL, 0, NULL);
861 RtlFreeUnicodeString(&ObjectName);
866 static NTSTATUS RTL_ReportRegistryValue(PKEY_VALUE_FULL_INFORMATION pInfo,
867 PRTL_QUERY_REGISTRY_TABLE pQuery, PVOID pContext, PVOID pEnvironment)
870 UNICODE_STRING src, dst;
875 NTSTATUS status = STATUS_SUCCESS;
882 if (pQuery->Flags & RTL_QUERY_REGISTRY_DIRECT)
883 return STATUS_INVALID_PARAMETER;
886 status = pQuery->QueryRoutine(pQuery->Name, pQuery->DefaultType, pQuery->DefaultData,
887 pQuery->DefaultLength, pContext, pQuery->EntryContext);
891 len = pInfo->DataLength;
893 if (pQuery->Flags & RTL_QUERY_REGISTRY_DIRECT)
895 str = (PUNICODE_STRING)pQuery->EntryContext;
900 if (!(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND))
902 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
904 dst.MaximumLength = 0;
905 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
907 dst.MaximumLength = res;
908 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
909 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
910 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
911 dst.Length, pContext, pQuery->EntryContext);
912 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
917 if (str->Buffer == NULL)
918 RtlCreateUnicodeString(str, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
920 RtlAppendUnicodeToString(str, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
924 if (!(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND))
925 return STATUS_INVALID_PARAMETER;
927 if (str->Buffer == NULL)
929 str->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, len);
930 str->MaximumLength = len;
932 len = min(len, str->MaximumLength);
933 memcpy(str->Buffer, ((CHAR*)pInfo) + pInfo->DataOffset, len);
938 bin = (LONG*)pQuery->EntryContext;
939 if (pInfo->DataLength <= sizeof(ULONG))
940 memcpy(bin, ((CHAR*)pInfo) + pInfo->DataOffset,
944 if (bin[0] <= sizeof(ULONG))
946 memcpy(&bin[1], ((CHAR*)pInfo) + pInfo->DataOffset,
947 min(-bin[0], pInfo->DataLength));
951 len = min(bin[0], pInfo->DataLength);
953 bin[2] = pInfo->Type;
954 memcpy(&bin[3], ((CHAR*)pInfo) + pInfo->DataOffset, len);
962 if((pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND) ||
963 (pInfo->Type != REG_EXPAND_SZ && pInfo->Type != REG_MULTI_SZ))
965 status = pQuery->QueryRoutine(pInfo->Name, pInfo->Type,
966 ((CHAR*)pInfo) + pInfo->DataOffset, pInfo->DataLength,
967 pContext, pQuery->EntryContext);
969 else if (pInfo->Type == REG_EXPAND_SZ)
971 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
973 dst.MaximumLength = 0;
974 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
976 dst.MaximumLength = res;
977 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
978 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
979 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
980 dst.Length, pContext, pQuery->EntryContext);
981 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
983 else /* REG_MULTI_SZ */
985 if(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND)
987 for (offset = 0; offset <= pInfo->DataLength; offset += len + sizeof(WCHAR))
989 wstr = (WCHAR*)(((CHAR*)pInfo) + offset);
990 len = strlenW(wstr) * sizeof(WCHAR);
991 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, wstr, len,
992 pContext, pQuery->EntryContext);
993 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
999 while(count<=pInfo->DataLength)
1001 String = (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset)+count;
1002 count+=strlenW(String)+1;
1003 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
1005 dst.MaximumLength = 0;
1006 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
1008 dst.MaximumLength = res;
1009 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
1010 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
1011 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
1012 dst.Length, pContext, pQuery->EntryContext);
1013 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
1014 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1024 static NTSTATUS RTL_GetKeyHandle(ULONG RelativeTo, PCWSTR Path, PHANDLE handle)
1026 UNICODE_STRING KeyString;
1027 OBJECT_ATTRIBUTES regkey;
1032 static const WCHAR empty[] = {0};
1033 static const WCHAR control[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e',
1034 '\\','S','y','s','t','e','m','\\','C','u','r','r','e','n','t',' ','C','o','n','t','r','o','l','S','e','t','\\',
1035 'C','o','n','t','r','o','l','\\',0};
1037 static const WCHAR devicemap[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1038 'H','a','r','d','w','a','r','e','\\','D','e','v','i','c','e','M','a','p','\\',0};
1040 static const WCHAR services[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1041 'S','y','s','t','e','m','\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1042 'S','e','r','v','i','c','e','s','\\',0};
1044 static const WCHAR user[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\',
1045 'C','u','r','r','e','n','t','U','s','e','r','\\',0};
1047 static const WCHAR windows_nt[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1048 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1049 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',0};
1051 switch (RelativeTo & 0xff)
1053 case RTL_REGISTRY_ABSOLUTE:
1057 case RTL_REGISTRY_CONTROL:
1061 case RTL_REGISTRY_DEVICEMAP:
1065 case RTL_REGISTRY_SERVICES:
1069 case RTL_REGISTRY_USER:
1073 case RTL_REGISTRY_WINDOWS_NT:
1078 return STATUS_INVALID_PARAMETER;
1081 len = (strlenW(base) + strlenW(Path) + 1) * sizeof(WCHAR);
1082 KeyString.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, len);
1083 if (KeyString.Buffer == NULL)
1084 return STATUS_NO_MEMORY;
1086 strcpyW(KeyString.Buffer, base);
1087 strcatW(KeyString.Buffer, Path);
1088 KeyString.Length = len - sizeof(WCHAR);
1089 KeyString.MaximumLength = len;
1090 InitializeObjectAttributes(®key, &KeyString, OBJ_CASE_INSENSITIVE, NULL, NULL);
1091 status = NtOpenKey(handle, KEY_ALL_ACCESS, ®key);
1092 RtlFreeHeap(GetProcessHeap(), 0, KeyString.Buffer);
1096 /*************************************************************************
1097 * RtlQueryRegistryValues [NTDLL.@]
1099 * Query multiple registry values with a signle call.
1102 * RelativeTo [I] Registry path that Path refers to
1103 * Path [I] Path to key
1104 * QueryTable [I] Table of key values to query
1105 * Context [I] Paremeter to pass to the application defined QueryRoutine function
1106 * Environment [I] Optional parameter to use when performing expantion
1109 * STATUS_SUCCESS or an appropriate NTSTATUS error code.
1111 NTSTATUS WINAPI RtlQueryRegistryValues(IN ULONG RelativeTo, IN PCWSTR Path,
1112 IN PRTL_QUERY_REGISTRY_TABLE QueryTable, IN PVOID Context,
1113 IN PVOID Environment OPTIONAL)
1115 UNICODE_STRING Value;
1116 HANDLE handle, topkey;
1117 PKEY_VALUE_FULL_INFORMATION pInfo = NULL;
1118 ULONG len, buflen = 0;
1119 NTSTATUS status=STATUS_SUCCESS, ret = STATUS_SUCCESS;
1122 TRACE("(%d, %s, %p, %p, %p)\n", RelativeTo, debugstr_w(Path), QueryTable, Context, Environment);
1125 return STATUS_INVALID_PARAMETER;
1127 /* get a valid handle */
1128 if (RelativeTo & RTL_REGISTRY_HANDLE)
1129 topkey = handle = (HANDLE)Path;
1132 status = RTL_GetKeyHandle(RelativeTo, Path, &topkey);
1135 if(status != STATUS_SUCCESS)
1138 /* Process query table entries */
1139 for (; QueryTable->QueryRoutine != NULL || QueryTable->Name != NULL; ++QueryTable)
1141 if (QueryTable->Flags &
1142 (RTL_QUERY_REGISTRY_SUBKEY | RTL_QUERY_REGISTRY_TOPKEY))
1144 /* topkey must be kept open just in case we will reuse it later */
1145 if (handle != topkey)
1148 if (QueryTable->Flags & RTL_QUERY_REGISTRY_SUBKEY)
1151 status = RTL_GetKeyHandle((ULONG)QueryTable->Name, Path, &handle);
1152 if(status != STATUS_SUCCESS)
1162 if (QueryTable->Flags & RTL_QUERY_REGISTRY_NOVALUE)
1164 QueryTable->QueryRoutine(QueryTable->Name, REG_NONE, NULL, 0,
1165 Context, QueryTable->EntryContext);
1171 if (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED)
1173 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1179 if (QueryTable->Name == NULL)
1181 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DIRECT)
1183 ret = STATUS_INVALID_PARAMETER;
1187 /* Report all subkeys */
1190 status = NtEnumerateValueKey(handle, i,
1191 KeyValueFullInformation, pInfo, buflen, &len);
1192 if (status == STATUS_NO_MORE_ENTRIES)
1194 if (status == STATUS_BUFFER_OVERFLOW ||
1195 status == STATUS_BUFFER_TOO_SMALL)
1198 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1199 pInfo = (KEY_VALUE_FULL_INFORMATION*)RtlAllocateHeap(
1200 GetProcessHeap(), 0, buflen);
1201 NtEnumerateValueKey(handle, i, KeyValueFullInformation,
1202 pInfo, buflen, &len);
1205 status = RTL_ReportRegistryValue(pInfo, QueryTable, Context, Environment);
1206 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1211 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DELETE)
1213 RtlInitUnicodeString(&Value, pInfo->Name);
1214 NtDeleteValueKey(handle, &Value);
1218 if (i == 0 && (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED))
1220 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1226 RtlInitUnicodeString(&Value, QueryTable->Name);
1227 status = NtQueryValueKey(handle, &Value, KeyValueFullInformation,
1228 pInfo, buflen, &len);
1229 if (status == STATUS_BUFFER_OVERFLOW ||
1230 status == STATUS_BUFFER_TOO_SMALL)
1233 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1234 pInfo = (KEY_VALUE_FULL_INFORMATION*)RtlAllocateHeap(
1235 GetProcessHeap(), 0, buflen);
1236 status = NtQueryValueKey(handle, &Value,
1237 KeyValueFullInformation, pInfo, buflen, &len);
1239 if (status != STATUS_SUCCESS)
1241 if (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED)
1243 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1246 status = RTL_ReportRegistryValue(NULL, QueryTable, Context, Environment);
1247 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1255 status = RTL_ReportRegistryValue(pInfo, QueryTable, Context, Environment);
1256 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1261 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DELETE)
1262 NtDeleteValueKey(handle, &Value);
1268 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1269 if (handle != topkey)
1275 /*************************************************************************
1276 * RtlCheckRegistryKey [NTDLL.@]
1278 * Query multiple registry values with a signle call.
1281 * RelativeTo [I] Registry path that Path refers to
1282 * Path [I] Path to key
1285 * STATUS_SUCCESS if the specified key exists, or an NTSTATUS error code.
1287 NTSTATUS WINAPI RtlCheckRegistryKey(IN ULONG RelativeTo, IN PWSTR Path)
1292 TRACE("(%d, %s)\n", RelativeTo, debugstr_w(Path));
1294 if((!RelativeTo) && Path == NULL)
1295 return STATUS_OBJECT_PATH_SYNTAX_BAD;
1296 if(RelativeTo & RTL_REGISTRY_HANDLE)
1297 return STATUS_SUCCESS;
1299 status = RTL_GetKeyHandle(RelativeTo, Path, &handle);
1300 if (handle) NtClose(handle);
1301 if (status == STATUS_INVALID_HANDLE) status = STATUS_OBJECT_NAME_NOT_FOUND;
1305 /*************************************************************************
1306 * RtlDeleteRegistryValue [NTDLL.@]
1308 * Query multiple registry values with a signle call.
1311 * RelativeTo [I] Registry path that Path refers to
1312 * Path [I] Path to key
1313 * ValueName [I] Name of the value to delete
1316 * STATUS_SUCCESS if the specified key is successfully deleted, or an NTSTATUS error code.
1318 NTSTATUS WINAPI RtlDeleteRegistryValue(IN ULONG RelativeTo, IN PCWSTR Path, IN PCWSTR ValueName)
1322 UNICODE_STRING Value;
1324 TRACE("(%d, %s, %s)\n", RelativeTo, debugstr_w(Path), debugstr_w(ValueName));
1326 RtlInitUnicodeString(&Value, ValueName);
1327 if(RelativeTo == RTL_REGISTRY_HANDLE)
1329 return NtDeleteValueKey((HANDLE)Path, &Value);
1331 status = RTL_GetKeyHandle(RelativeTo, Path, &handle);
1332 if (status) return status;
1333 status = NtDeleteValueKey(handle, &Value);