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 #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( PHKEY 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( PHKEY retkey, ACCESS_MASK access, OBJECT_ATTRIBUTES *attr,
90 ULONG TitleIndex, const UNICODE_STRING *class, ULONG options,
94 attr->Attributes &= ~(OBJ_PERMANENT|OBJ_EXCLUSIVE);
96 return NtCreateKey(retkey, access, attr, 0, NULL, 0, dispos);
99 /******************************************************************************
100 * NtOpenKey [NTDLL.@]
101 * ZwOpenKey [NTDLL.@]
103 * OUT PHKEY retkey (returns 0 when failure)
104 * IN ACCESS_MASK access
105 * IN POBJECT_ATTRIBUTES attr
107 NTSTATUS WINAPI NtOpenKey( PHKEY retkey, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr )
110 DWORD len = attr->ObjectName->Length;
112 TRACE( "(%p,%s,%lx,%p)\n", attr->RootDirectory,
113 debugstr_us(attr->ObjectName), access, retkey );
115 if (len > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
116 if (!retkey) return STATUS_INVALID_PARAMETER;
118 SERVER_START_REQ( open_key )
120 req->parent = attr->RootDirectory;
121 req->access = access;
122 wine_server_add_data( req, attr->ObjectName->Buffer, len );
123 ret = wine_server_call( req );
124 *retkey = reply->hkey;
127 TRACE("<- %p\n", *retkey);
131 /******************************************************************************
132 * RtlpNtOpenKey [NTDLL.@]
136 NTSTATUS WINAPI RtlpNtOpenKey( PHKEY retkey, ACCESS_MASK access, OBJECT_ATTRIBUTES *attr )
139 attr->Attributes &= ~(OBJ_PERMANENT|OBJ_EXCLUSIVE);
140 return NtOpenKey(retkey, access, attr);
143 /******************************************************************************
144 * NtDeleteKey [NTDLL.@]
145 * ZwDeleteKey [NTDLL.@]
147 NTSTATUS WINAPI NtDeleteKey( HKEY hkey )
151 TRACE( "(%p)\n", hkey );
153 SERVER_START_REQ( delete_key )
156 ret = wine_server_call( req );
162 /******************************************************************************
163 * RtlpNtMakeTemporaryKey [NTDLL.@]
167 NTSTATUS WINAPI RtlpNtMakeTemporaryKey( HKEY hkey )
169 return NtDeleteKey(hkey);
172 /******************************************************************************
173 * NtDeleteValueKey [NTDLL.@]
174 * ZwDeleteValueKey [NTDLL.@]
176 NTSTATUS WINAPI NtDeleteValueKey( HKEY hkey, const UNICODE_STRING *name )
180 TRACE( "(%p,%s)\n", hkey, debugstr_us(name) );
181 if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
183 SERVER_START_REQ( delete_key_value )
186 wine_server_add_data( req, name->Buffer, name->Length );
187 ret = wine_server_call( req );
194 /******************************************************************************
197 * Implementation of NtQueryKey and NtEnumerateKey
199 static NTSTATUS enumerate_key( HKEY handle, int index, KEY_INFORMATION_CLASS info_class,
200 void *info, DWORD length, DWORD *result_len )
209 case KeyBasicInformation: data_ptr = ((KEY_BASIC_INFORMATION *)info)->Name; break;
210 case KeyFullInformation: data_ptr = ((KEY_FULL_INFORMATION *)info)->Class; break;
211 case KeyNodeInformation: data_ptr = ((KEY_NODE_INFORMATION *)info)->Name; break;
213 FIXME( "Information class %d not implemented\n", info_class );
214 return STATUS_INVALID_PARAMETER;
216 fixed_size = (char *)data_ptr - (char *)info;
218 SERVER_START_REQ( enum_key )
222 req->info_class = info_class;
223 if (length > fixed_size) wine_server_set_reply( req, data_ptr, length - fixed_size );
224 if (!(ret = wine_server_call( req )))
228 RtlSecondsSince1970ToTime( reply->modif, &modif );
232 case KeyBasicInformation:
234 KEY_BASIC_INFORMATION keyinfo;
235 fixed_size = (char *)keyinfo.Name - (char *)&keyinfo;
236 keyinfo.LastWriteTime = modif;
237 keyinfo.TitleIndex = 0;
238 keyinfo.NameLength = reply->namelen;
239 memcpy( info, &keyinfo, min( length, fixed_size ) );
242 case KeyFullInformation:
244 KEY_FULL_INFORMATION keyinfo;
245 fixed_size = (char *)keyinfo.Class - (char *)&keyinfo;
246 keyinfo.LastWriteTime = modif;
247 keyinfo.TitleIndex = 0;
248 keyinfo.ClassLength = wine_server_reply_size(reply);
249 keyinfo.ClassOffset = keyinfo.ClassLength ? fixed_size : -1;
250 keyinfo.SubKeys = reply->subkeys;
251 keyinfo.MaxNameLen = reply->max_subkey;
252 keyinfo.MaxClassLen = reply->max_class;
253 keyinfo.Values = reply->values;
254 keyinfo.MaxValueNameLen = reply->max_value;
255 keyinfo.MaxValueDataLen = reply->max_data;
256 memcpy( info, &keyinfo, min( length, fixed_size ) );
259 case KeyNodeInformation:
261 KEY_NODE_INFORMATION keyinfo;
262 fixed_size = (char *)keyinfo.Name - (char *)&keyinfo;
263 keyinfo.LastWriteTime = modif;
264 keyinfo.TitleIndex = 0;
265 keyinfo.ClassLength = max( 0, wine_server_reply_size(reply) - reply->namelen );
266 keyinfo.ClassOffset = keyinfo.ClassLength ? fixed_size + reply->namelen : -1;
267 keyinfo.NameLength = reply->namelen;
268 memcpy( info, &keyinfo, min( length, fixed_size ) );
272 *result_len = fixed_size + reply->total;
273 if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
282 /******************************************************************************
283 * NtEnumerateKey [NTDLL.@]
284 * ZwEnumerateKey [NTDLL.@]
287 * the name copied into the buffer is NOT 0-terminated
289 NTSTATUS WINAPI NtEnumerateKey( HKEY handle, ULONG index, KEY_INFORMATION_CLASS info_class,
290 void *info, DWORD length, DWORD *result_len )
292 /* -1 means query key, so avoid it here */
293 if (index == (ULONG)-1) return STATUS_NO_MORE_ENTRIES;
294 return enumerate_key( handle, index, info_class, info, length, result_len );
298 /******************************************************************************
299 * RtlpNtEnumerateSubKey [NTDLL.@]
302 NTSTATUS WINAPI RtlpNtEnumerateSubKey( HKEY handle, UNICODE_STRING *out, ULONG index )
304 KEY_BASIC_INFORMATION *info;
305 DWORD dwLen, dwResultLen;
310 dwLen = out->Length + sizeof(KEY_BASIC_INFORMATION);
311 info = (KEY_BASIC_INFORMATION*)RtlAllocateHeap( GetProcessHeap(), 0, dwLen );
313 return STATUS_NO_MEMORY;
321 ret = NtEnumerateKey( handle, index, KeyBasicInformation, info, dwLen, &dwResultLen );
322 dwResultLen -= sizeof(KEY_BASIC_INFORMATION);
324 if (ret == STATUS_BUFFER_OVERFLOW)
325 out->Length = dwResultLen;
328 if (out->Length < info->NameLength)
330 out->Length = dwResultLen;
331 ret = STATUS_BUFFER_OVERFLOW;
335 out->Length = info->NameLength;
336 memcpy(out->Buffer, info->Name, info->NameLength);
341 RtlFreeHeap( GetProcessHeap(), 0, info );
345 /******************************************************************************
346 * NtQueryKey [NTDLL.@]
347 * ZwQueryKey [NTDLL.@]
349 NTSTATUS WINAPI NtQueryKey( HKEY handle, KEY_INFORMATION_CLASS info_class,
350 void *info, DWORD length, DWORD *result_len )
352 return enumerate_key( handle, -1, info_class, info, length, result_len );
356 /* fill the key value info structure for a specific info class */
357 static void copy_key_value_info( KEY_VALUE_INFORMATION_CLASS info_class, void *info,
358 DWORD length, int type, int name_len, int data_len )
362 case KeyValueBasicInformation:
364 KEY_VALUE_BASIC_INFORMATION keyinfo;
365 keyinfo.TitleIndex = 0;
367 keyinfo.NameLength = name_len;
368 length = min( length, (char *)keyinfo.Name - (char *)&keyinfo );
369 memcpy( info, &keyinfo, length );
372 case KeyValueFullInformation:
374 KEY_VALUE_FULL_INFORMATION keyinfo;
375 keyinfo.TitleIndex = 0;
377 keyinfo.DataOffset = (char *)keyinfo.Name - (char *)&keyinfo + name_len;
378 keyinfo.DataLength = data_len;
379 keyinfo.NameLength = name_len;
380 length = min( length, (char *)keyinfo.Name - (char *)&keyinfo );
381 memcpy( info, &keyinfo, length );
384 case KeyValuePartialInformation:
386 KEY_VALUE_PARTIAL_INFORMATION keyinfo;
387 keyinfo.TitleIndex = 0;
389 keyinfo.DataLength = data_len;
390 length = min( length, (char *)keyinfo.Data - (char *)&keyinfo );
391 memcpy( info, &keyinfo, length );
400 /******************************************************************************
401 * NtEnumerateValueKey [NTDLL.@]
402 * ZwEnumerateValueKey [NTDLL.@]
404 NTSTATUS WINAPI NtEnumerateValueKey( HKEY handle, ULONG index,
405 KEY_VALUE_INFORMATION_CLASS info_class,
406 void *info, DWORD length, DWORD *result_len )
412 TRACE( "(%p,%lu,%d,%p,%ld)\n", handle, index, info_class, info, length );
414 /* compute the length we want to retrieve */
417 case KeyValueBasicInformation: ptr = ((KEY_VALUE_BASIC_INFORMATION *)info)->Name; break;
418 case KeyValueFullInformation: ptr = ((KEY_VALUE_FULL_INFORMATION *)info)->Name; break;
419 case KeyValuePartialInformation: ptr = ((KEY_VALUE_PARTIAL_INFORMATION *)info)->Data; break;
421 FIXME( "Information class %d not implemented\n", info_class );
422 return STATUS_INVALID_PARAMETER;
424 fixed_size = (char *)ptr - (char *)info;
426 SERVER_START_REQ( enum_key_value )
430 req->info_class = info_class;
431 if (length > fixed_size) wine_server_set_reply( req, ptr, length - fixed_size );
432 if (!(ret = wine_server_call( req )))
434 copy_key_value_info( info_class, info, length, reply->type, reply->namelen,
435 wine_server_reply_size(reply) - reply->namelen );
436 *result_len = fixed_size + reply->total;
437 if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
445 /******************************************************************************
446 * NtQueryValueKey [NTDLL.@]
447 * ZwQueryValueKey [NTDLL.@]
450 * the name in the KeyValueInformation is never set
452 NTSTATUS WINAPI NtQueryValueKey( HKEY handle, const UNICODE_STRING *name,
453 KEY_VALUE_INFORMATION_CLASS info_class,
454 void *info, DWORD length, DWORD *result_len )
458 unsigned int fixed_size = 0;
460 TRACE( "(%p,%s,%d,%p,%ld)\n", handle, debugstr_us(name), info_class, info, length );
462 if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
464 /* compute the length we want to retrieve */
467 case KeyValueBasicInformation:
468 fixed_size = (char *)((KEY_VALUE_BASIC_INFORMATION *)info)->Name - (char *)info;
471 case KeyValueFullInformation:
472 data_ptr = (UCHAR *)((KEY_VALUE_FULL_INFORMATION *)info)->Name;
473 fixed_size = (char *)data_ptr - (char *)info;
475 case KeyValuePartialInformation:
476 data_ptr = ((KEY_VALUE_PARTIAL_INFORMATION *)info)->Data;
477 fixed_size = (char *)data_ptr - (char *)info;
480 FIXME( "Information class %d not implemented\n", info_class );
481 return STATUS_INVALID_PARAMETER;
484 SERVER_START_REQ( get_key_value )
487 wine_server_add_data( req, name->Buffer, name->Length );
488 if (length > fixed_size) wine_server_set_reply( req, data_ptr, length - fixed_size );
489 if (!(ret = wine_server_call( req )))
491 copy_key_value_info( info_class, info, length, reply->type,
492 0, wine_server_reply_size(reply) );
493 *result_len = fixed_size + reply->total;
494 if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
501 /******************************************************************************
502 * RtlpNtQueryValueKey [NTDLL.@]
505 NTSTATUS WINAPI RtlpNtQueryValueKey( HKEY handle, ULONG *result_type, PBYTE dest,
508 KEY_VALUE_PARTIAL_INFORMATION *info;
512 DWORD dwLen = sizeof (KEY_VALUE_PARTIAL_INFORMATION) + result_len ? *result_len : 0;
514 info = (KEY_VALUE_PARTIAL_INFORMATION*)RtlAllocateHeap( GetProcessHeap(), 0, dwLen );
516 return STATUS_NO_MEMORY;
519 ret = NtQueryValueKey( handle, &name, KeyValuePartialInformation, info, dwLen, &dwResultLen );
521 if (!ret || ret == STATUS_BUFFER_OVERFLOW)
524 *result_len = info->DataLength;
527 *result_type = info->Type;
529 if (ret != STATUS_BUFFER_OVERFLOW)
530 memcpy( dest, info->Data, info->DataLength );
533 RtlFreeHeap( GetProcessHeap(), 0, info );
537 /******************************************************************************
538 * NtFlushKey [NTDLL.@]
539 * ZwFlushKey [NTDLL.@]
541 NTSTATUS WINAPI NtFlushKey(HKEY key)
545 TRACE("key=%p\n", key);
547 SERVER_START_REQ( flush_key )
550 ret = wine_server_call( req );
557 /******************************************************************************
558 * NtLoadKey [NTDLL.@]
559 * ZwLoadKey [NTDLL.@]
561 NTSTATUS WINAPI NtLoadKey( const OBJECT_ATTRIBUTES *attr, OBJECT_ATTRIBUTES *file )
567 TRACE("(%p,%p)\n", attr, file);
569 ret = NtCreateFile(&hive, GENERIC_READ, file, &io, NULL, FILE_ATTRIBUTE_NORMAL, 0,
570 OPEN_EXISTING, 0, NULL, 0);
573 SERVER_START_REQ( load_registry )
575 req->hkey = attr->RootDirectory;
577 wine_server_add_data(req, attr->ObjectName->Buffer, attr->ObjectName->Length);
578 ret = wine_server_call( req );
587 /******************************************************************************
588 * NtNotifyChangeKey [NTDLL.@]
589 * ZwNotifyChangeKey [NTDLL.@]
591 NTSTATUS WINAPI NtNotifyChangeKey(
594 IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
595 IN PVOID ApcContext OPTIONAL,
596 OUT PIO_STATUS_BLOCK IoStatusBlock,
597 IN ULONG CompletionFilter,
598 IN BOOLEAN Asynchronous,
599 OUT PVOID ChangeBuffer,
601 IN BOOLEAN WatchSubtree)
605 TRACE("(%p,%p,%p,%p,%p,0x%08lx, 0x%08x,%p,0x%08lx,0x%08x)\n",
606 KeyHandle, Event, ApcRoutine, ApcContext, IoStatusBlock, CompletionFilter,
607 Asynchronous, ChangeBuffer, Length, WatchSubtree);
609 if (ApcRoutine || ApcContext || ChangeBuffer || Length)
610 FIXME("Unimplemented optional parameter\n");
614 OBJECT_ATTRIBUTES attr;
615 InitializeObjectAttributes( &attr, NULL, 0, NULL, NULL );
616 ret = NtCreateEvent( &Event, EVENT_ALL_ACCESS, &attr, FALSE, FALSE );
617 if (ret != STATUS_SUCCESS)
621 SERVER_START_REQ( set_registry_notification )
623 req->hkey = KeyHandle;
625 req->subtree = WatchSubtree;
626 req->filter = CompletionFilter;
627 ret = wine_server_call( req );
633 if (ret == STATUS_SUCCESS)
634 NtWaitForSingleObject( Event, FALSE, NULL );
638 return STATUS_SUCCESS;
641 /******************************************************************************
642 * NtQueryMultipleValueKey [NTDLL]
643 * ZwQueryMultipleValueKey
646 NTSTATUS WINAPI NtQueryMultipleValueKey(
648 PVALENTW ListOfValuesToQuery,
650 PVOID MultipleValueInformation,
654 FIXME("(%p,%p,0x%08lx,%p,0x%08lx,%p) stub!\n",
655 KeyHandle, ListOfValuesToQuery, NumberOfItems, MultipleValueInformation,
656 Length,ReturnLength);
657 return STATUS_SUCCESS;
660 /******************************************************************************
661 * NtReplaceKey [NTDLL.@]
662 * ZwReplaceKey [NTDLL.@]
664 NTSTATUS WINAPI NtReplaceKey(
665 IN POBJECT_ATTRIBUTES ObjectAttributes,
667 IN POBJECT_ATTRIBUTES ReplacedObjectAttributes)
669 FIXME("(%p),stub!\n", Key);
670 dump_ObjectAttributes(ObjectAttributes);
671 dump_ObjectAttributes(ReplacedObjectAttributes);
672 return STATUS_SUCCESS;
674 /******************************************************************************
675 * NtRestoreKey [NTDLL.@]
676 * ZwRestoreKey [NTDLL.@]
678 NTSTATUS WINAPI NtRestoreKey(
683 FIXME("(%p,%p,0x%08lx) stub\n",
684 KeyHandle, FileHandle, RestoreFlags);
685 return STATUS_SUCCESS;
687 /******************************************************************************
688 * NtSaveKey [NTDLL.@]
689 * ZwSaveKey [NTDLL.@]
691 NTSTATUS WINAPI NtSaveKey(IN HKEY KeyHandle, IN HANDLE FileHandle)
695 TRACE("(%p,%p)\n", KeyHandle, FileHandle);
697 SERVER_START_REQ( save_registry )
699 req->hkey = KeyHandle;
700 req->file = FileHandle;
701 ret = wine_server_call( req );
707 /******************************************************************************
708 * NtSetInformationKey [NTDLL.@]
709 * ZwSetInformationKey [NTDLL.@]
711 NTSTATUS WINAPI NtSetInformationKey(
713 IN const int KeyInformationClass,
714 IN PVOID KeyInformation,
715 IN ULONG KeyInformationLength)
717 FIXME("(%p,0x%08x,%p,0x%08lx) stub\n",
718 KeyHandle, KeyInformationClass, KeyInformation, KeyInformationLength);
719 return STATUS_SUCCESS;
723 /******************************************************************************
724 * NtSetValueKey [NTDLL.@]
725 * ZwSetValueKey [NTDLL.@]
728 * win95 does not care about count for REG_SZ and finds out the len by itself (js)
729 * NT does definitely care (aj)
731 NTSTATUS WINAPI NtSetValueKey( HKEY hkey, const UNICODE_STRING *name, ULONG TitleIndex,
732 ULONG type, const void *data, ULONG count )
736 TRACE( "(%p,%s,%ld,%p,%ld)\n", hkey, debugstr_us(name), type, data, count );
738 if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
740 SERVER_START_REQ( set_key_value )
744 req->namelen = name->Length;
745 wine_server_add_data( req, name->Buffer, name->Length );
746 wine_server_add_data( req, data, count );
747 ret = wine_server_call( req );
753 /******************************************************************************
754 * RtlpNtSetValueKey [NTDLL.@]
757 NTSTATUS WINAPI RtlpNtSetValueKey( HKEY hkey, ULONG type, const void *data,
763 return NtSetValueKey( hkey, &name, 0, type, data, count );
766 /******************************************************************************
767 * NtUnloadKey [NTDLL.@]
768 * ZwUnloadKey [NTDLL.@]
770 NTSTATUS WINAPI NtUnloadKey(IN HKEY KeyHandle)
774 TRACE("(%p)\n", KeyHandle);
776 SERVER_START_REQ( unload_registry )
778 req->hkey = KeyHandle;
779 ret = wine_server_call(req);
786 /******************************************************************************
787 * RtlFormatCurrentUserKeyPath [NTDLL.@]
789 * NOTE: under NT the user name part of the path is an SID.
791 NTSTATUS WINAPI RtlFormatCurrentUserKeyPath( IN OUT PUNICODE_STRING KeyPath)
793 static const WCHAR pathW[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\'};
794 const char *user = wine_get_user_name();
795 int len = ntdll_umbstowcs( 0, user, strlen(user)+1, NULL, 0 );
797 KeyPath->MaximumLength = sizeof(pathW) + len * sizeof(WCHAR);
798 KeyPath->Length = KeyPath->MaximumLength - sizeof(WCHAR);
799 if (!(KeyPath->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, KeyPath->MaximumLength )))
800 return STATUS_NO_MEMORY;
801 memcpy( KeyPath->Buffer, pathW, sizeof(pathW) );
802 ntdll_umbstowcs( 0, user, strlen(user)+1, KeyPath->Buffer + sizeof(pathW)/sizeof(WCHAR), len );
803 return STATUS_SUCCESS;
806 /******************************************************************************
807 * RtlOpenCurrentUser [NTDLL.@]
809 * if we return just HKEY_CURRENT_USER the advapi tries to find a remote
810 * registry (odd handle) and fails
813 DWORD WINAPI RtlOpenCurrentUser(
814 IN ACCESS_MASK DesiredAccess, /* [in] */
815 OUT PHKEY KeyHandle) /* [out] handle of HKEY_CURRENT_USER */
817 OBJECT_ATTRIBUTES ObjectAttributes;
818 UNICODE_STRING ObjectName;
821 TRACE("(0x%08lx, %p) stub\n",DesiredAccess, KeyHandle);
823 RtlFormatCurrentUserKeyPath(&ObjectName);
824 InitializeObjectAttributes(&ObjectAttributes,&ObjectName,OBJ_CASE_INSENSITIVE,0, NULL);
825 ret = NtCreateKey(KeyHandle, DesiredAccess, &ObjectAttributes, 0, NULL, 0, NULL);
826 RtlFreeUnicodeString(&ObjectName);
831 static NTSTATUS RTL_ReportRegistryValue(PKEY_VALUE_FULL_INFORMATION pInfo,
832 PRTL_QUERY_REGISTRY_TABLE pQuery, PVOID pContext, PVOID pEnvironment)
835 UNICODE_STRING src, dst;
840 NTSTATUS status = STATUS_SUCCESS;
847 if (pQuery->Flags & RTL_QUERY_REGISTRY_DIRECT)
848 return STATUS_INVALID_PARAMETER;
851 status = pQuery->QueryRoutine(pQuery->Name, pQuery->DefaultType, pQuery->DefaultData,
852 pQuery->DefaultLength, pContext, pQuery->EntryContext);
856 len = pInfo->DataLength;
858 if (pQuery->Flags & RTL_QUERY_REGISTRY_DIRECT)
860 str = (PUNICODE_STRING)pQuery->EntryContext;
865 if (!(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND))
867 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
869 dst.MaximumLength = 0;
870 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
872 dst.MaximumLength = res;
873 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
874 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
875 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
876 dst.Length, pContext, pQuery->EntryContext);
877 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
882 if (str->Buffer == NULL)
883 RtlCreateUnicodeString(str, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
885 RtlAppendUnicodeToString(str, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
889 if (!(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND))
890 return STATUS_INVALID_PARAMETER;
892 if (str->Buffer == NULL)
894 str->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, len);
895 str->MaximumLength = len;
897 len = min(len, str->MaximumLength);
898 memcpy(str->Buffer, ((CHAR*)pInfo) + pInfo->DataOffset, len);
903 bin = (LONG*)pQuery->EntryContext;
904 if (pInfo->DataLength <= sizeof(ULONG))
905 memcpy(bin, ((CHAR*)pInfo) + pInfo->DataOffset,
909 if (bin[0] <= sizeof(ULONG))
911 memcpy(&bin[1], ((CHAR*)pInfo) + pInfo->DataOffset,
912 min(-bin[0], pInfo->DataLength));
916 len = min(bin[0], pInfo->DataLength);
918 bin[2] = pInfo->Type;
919 memcpy(&bin[3], ((CHAR*)pInfo) + pInfo->DataOffset, len);
927 if((pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND) ||
928 (pInfo->Type != REG_EXPAND_SZ && pInfo->Type != REG_MULTI_SZ))
930 status = pQuery->QueryRoutine(pInfo->Name, pInfo->Type,
931 ((CHAR*)pInfo) + pInfo->DataOffset, pInfo->DataLength,
932 pContext, pQuery->EntryContext);
934 else if (pInfo->Type == REG_EXPAND_SZ)
936 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
938 dst.MaximumLength = 0;
939 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
941 dst.MaximumLength = res;
942 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
943 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
944 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
945 dst.Length, pContext, pQuery->EntryContext);
946 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
948 else /* REG_MULTI_SZ */
950 if(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND)
952 for (offset = 0; offset <= pInfo->DataLength; offset += len + sizeof(WCHAR))
954 wstr = (WCHAR*)(((CHAR*)pInfo) + offset);
955 len = strlenW(wstr) * sizeof(WCHAR);
956 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, wstr, len,
957 pContext, pQuery->EntryContext);
958 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
964 while(count<=pInfo->DataLength)
966 String = (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset)+count;
967 count+=strlenW(String)+1;
968 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
970 dst.MaximumLength = 0;
971 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
973 dst.MaximumLength = res;
974 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
975 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
976 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
977 dst.Length, pContext, pQuery->EntryContext);
978 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
979 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
989 static NTSTATUS RTL_GetKeyHandle(ULONG RelativeTo, PCWSTR Path, PHKEY handle)
991 UNICODE_STRING KeyString;
992 OBJECT_ATTRIBUTES regkey;
997 static const WCHAR empty[] = {0};
998 static const WCHAR control[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e',
999 '\\','S','y','s','t','e','m','\\','C','u','r','r','e','n','t',' ','C','o','n','t','r','o','l','S','e','t','\\',
1000 'C','o','n','t','r','o','l','\\',0};
1002 static const WCHAR devicemap[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1003 'H','a','r','d','w','a','r','e','\\','D','e','v','i','c','e','M','a','p','\\',0};
1005 static const WCHAR services[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1006 'S','y','s','t','e','m','\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1007 'S','e','r','v','i','c','e','s','\\',0};
1009 static const WCHAR user[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\',
1010 'C','u','r','r','e','n','t','U','s','e','r','\\',0};
1012 static const WCHAR windows_nt[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1013 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1014 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',0};
1016 switch (RelativeTo & 0xff)
1018 case RTL_REGISTRY_ABSOLUTE:
1022 case RTL_REGISTRY_CONTROL:
1026 case RTL_REGISTRY_DEVICEMAP:
1030 case RTL_REGISTRY_SERVICES:
1034 case RTL_REGISTRY_USER:
1038 case RTL_REGISTRY_WINDOWS_NT:
1043 return STATUS_INVALID_PARAMETER;
1046 len = (strlenW(base) + strlenW(Path) + 1) * sizeof(WCHAR);
1047 KeyString.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, len);
1048 if (KeyString.Buffer == NULL)
1049 return STATUS_NO_MEMORY;
1051 strcpyW(KeyString.Buffer, base);
1052 strcatW(KeyString.Buffer, Path);
1053 KeyString.Length = len - sizeof(WCHAR);
1054 KeyString.MaximumLength = len;
1055 InitializeObjectAttributes(®key, &KeyString, OBJ_CASE_INSENSITIVE, NULL, NULL);
1056 status = NtOpenKey(handle, KEY_ALL_ACCESS, ®key);
1057 RtlFreeHeap(GetProcessHeap(), 0, KeyString.Buffer);
1061 /*************************************************************************
1062 * RtlQueryRegistryValues [NTDLL.@]
1064 * Query multiple registry values with a signle call.
1067 * RelativeTo [I] Registry path that Path refers to
1068 * Path [I] Path to key
1069 * QueryTable [I] Table of key values to query
1070 * Context [I] Paremeter to pass to the application defined QueryRoutine function
1071 * Environment [I] Optional parameter to use when performing expantion
1074 * STATUS_SUCCESS or an appropriate NTSTATUS error code.
1076 NTSTATUS WINAPI RtlQueryRegistryValues(IN ULONG RelativeTo, IN PCWSTR Path,
1077 IN PRTL_QUERY_REGISTRY_TABLE QueryTable, IN PVOID Context,
1078 IN PVOID Environment OPTIONAL)
1080 UNICODE_STRING Value;
1081 HKEY handle, topkey;
1082 PKEY_VALUE_FULL_INFORMATION pInfo = NULL;
1083 ULONG len, buflen = 0;
1084 NTSTATUS status=STATUS_SUCCESS, ret = STATUS_SUCCESS;
1087 TRACE("(%ld, %s, %p, %p, %p)\n", RelativeTo, debugstr_w(Path), QueryTable, Context, Environment);
1090 return STATUS_INVALID_PARAMETER;
1092 /* get a valid handle */
1093 if (RelativeTo & RTL_REGISTRY_HANDLE)
1094 topkey = handle = (HANDLE)Path;
1097 status = RTL_GetKeyHandle(RelativeTo, Path, &topkey);
1100 if(status != STATUS_SUCCESS)
1103 /* Process query table entries */
1104 for (; QueryTable->QueryRoutine != NULL || QueryTable->Name != NULL; ++QueryTable)
1106 if (QueryTable->Flags &
1107 (RTL_QUERY_REGISTRY_SUBKEY | RTL_QUERY_REGISTRY_TOPKEY))
1109 /* topkey must be kept open just in case we will reuse it later */
1110 if (handle != topkey)
1113 if (QueryTable->Flags & RTL_QUERY_REGISTRY_SUBKEY)
1116 status = RTL_GetKeyHandle((ULONG)QueryTable->Name, Path, &handle);
1117 if(status != STATUS_SUCCESS)
1127 if (QueryTable->Flags & RTL_QUERY_REGISTRY_NOVALUE)
1129 QueryTable->QueryRoutine(QueryTable->Name, REG_NONE, NULL, 0,
1130 Context, QueryTable->EntryContext);
1136 if (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED)
1138 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1144 if (QueryTable->Name == NULL)
1146 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DIRECT)
1148 ret = STATUS_INVALID_PARAMETER;
1152 /* Report all subkeys */
1155 status = NtEnumerateValueKey(handle, i,
1156 KeyValueFullInformation, pInfo, buflen, &len);
1157 if (status == STATUS_NO_MORE_ENTRIES)
1159 if (status == STATUS_BUFFER_OVERFLOW ||
1160 status == STATUS_BUFFER_TOO_SMALL)
1163 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1164 pInfo = (KEY_VALUE_FULL_INFORMATION*)RtlAllocateHeap(
1165 GetProcessHeap(), 0, buflen);
1166 NtEnumerateValueKey(handle, i, KeyValueFullInformation,
1167 pInfo, buflen, &len);
1170 status = RTL_ReportRegistryValue(pInfo, QueryTable, Context, Environment);
1171 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1176 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DELETE)
1178 RtlInitUnicodeString(&Value, pInfo->Name);
1179 NtDeleteValueKey(handle, &Value);
1183 if (i == 0 && (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED))
1185 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1191 RtlInitUnicodeString(&Value, QueryTable->Name);
1192 status = NtQueryValueKey(handle, &Value, KeyValueFullInformation,
1193 pInfo, buflen, &len);
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 status = NtQueryValueKey(handle, &Value,
1202 KeyValueFullInformation, pInfo, buflen, &len);
1204 if (status != STATUS_SUCCESS)
1206 if (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED)
1208 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1211 status = RTL_ReportRegistryValue(NULL, QueryTable, Context, Environment);
1212 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1220 status = RTL_ReportRegistryValue(pInfo, QueryTable, Context, Environment);
1221 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1226 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DELETE)
1227 NtDeleteValueKey(handle, &Value);
1233 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1234 if (handle != topkey)
1240 /*************************************************************************
1241 * RtlCheckRegistryKey [NTDLL.@]
1243 * Query multiple registry values with a signle call.
1246 * RelativeTo [I] Registry path that Path refers to
1247 * Path [I] Path to key
1250 * STATUS_SUCCESS if the specified key exists, or an NTSTATUS error code.
1252 NTSTATUS WINAPI RtlCheckRegistryKey(IN ULONG RelativeTo, IN PWSTR Path)
1257 TRACE("(%ld, %s)\n", RelativeTo, debugstr_w(Path));
1259 if((!RelativeTo) && Path == NULL)
1260 return STATUS_OBJECT_PATH_SYNTAX_BAD;
1261 if(RelativeTo & RTL_REGISTRY_HANDLE)
1262 return STATUS_SUCCESS;
1264 status = RTL_GetKeyHandle(RelativeTo, Path, &handle);
1265 if (handle) NtClose(handle);
1266 if (status == STATUS_INVALID_HANDLE) status = STATUS_OBJECT_NAME_NOT_FOUND;
1270 /*************************************************************************
1271 * RtlDeleteRegistryValue [NTDLL.@]
1273 * Query multiple registry values with a signle call.
1276 * RelativeTo [I] Registry path that Path refers to
1277 * Path [I] Path to key
1278 * ValueName [I] Name of the value to delete
1281 * STATUS_SUCCESS if the specified key is successfully deleted, or an NTSTATUS error code.
1283 NTSTATUS WINAPI RtlDeleteRegistryValue(IN ULONG RelativeTo, IN PCWSTR Path, IN PCWSTR ValueName)
1287 UNICODE_STRING Value;
1289 TRACE("(%ld, %s, %s)\n", RelativeTo, debugstr_w(Path), debugstr_w(ValueName));
1291 RtlInitUnicodeString(&Value, ValueName);
1292 if(RelativeTo == RTL_REGISTRY_HANDLE)
1294 return NtDeleteValueKey((HKEY)Path, &Value);
1296 status = RTL_GetKeyHandle(RelativeTo, Path, &handle);
1297 if (status) return status;
1298 status = NtDeleteValueKey(handle, &Value);