4 * Copyright (C) 1999 Juergen Schmied
5 * Copyright (C) 2000 Alexandre Julliard
6 * Copyright 2005 Ivan Leo Puoti, Laurent Pinchart
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * HKEY_LOCAL_MACHINE \\REGISTRY\\MACHINE
24 * HKEY_USERS \\REGISTRY\\USER
25 * HKEY_CURRENT_CONFIG \\REGISTRY\\MACHINE\\SYSTEM\\CURRENTCONTROLSET\\HARDWARE PROFILES\\CURRENT
26 * HKEY_CLASSES \\REGISTRY\\MACHINE\\SOFTWARE\\CLASSES
30 #include "wine/port.h"
36 #include "wine/library.h"
37 #include "ntdll_misc.h"
38 #include "wine/debug.h"
39 #include "wine/unicode.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(reg);
43 /* maximum length of a key/value name in bytes (without terminating null) */
44 #define MAX_NAME_LENGTH ((MAX_PATH-1) * sizeof(WCHAR))
46 /******************************************************************************
47 * NtCreateKey [NTDLL.@]
48 * ZwCreateKey [NTDLL.@]
50 NTSTATUS WINAPI NtCreateKey( PHANDLE retkey, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr,
51 ULONG TitleIndex, const UNICODE_STRING *class, ULONG options,
56 TRACE( "(%p,%s,%s,%lx,%lx,%p)\n", attr->RootDirectory, debugstr_us(attr->ObjectName),
57 debugstr_us(class), options, access, retkey );
59 if (attr->ObjectName->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
60 if (!retkey) return STATUS_INVALID_PARAMETER;
62 SERVER_START_REQ( create_key )
64 req->parent = attr->RootDirectory;
66 req->options = options;
68 req->namelen = attr->ObjectName->Length;
69 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
70 if (class) wine_server_add_data( req, class->Buffer, class->Length );
71 if (!(ret = wine_server_call( req )))
73 *retkey = reply->hkey;
74 if (dispos) *dispos = reply->created ? REG_CREATED_NEW_KEY : REG_OPENED_EXISTING_KEY;
78 TRACE("<- %p\n", *retkey);
82 /******************************************************************************
83 * RtlpNtCreateKey [NTDLL.@]
87 NTSTATUS WINAPI RtlpNtCreateKey( PHANDLE retkey, ACCESS_MASK access, OBJECT_ATTRIBUTES *attr,
88 ULONG TitleIndex, const UNICODE_STRING *class, ULONG options,
92 attr->Attributes &= ~(OBJ_PERMANENT|OBJ_EXCLUSIVE);
94 return NtCreateKey(retkey, access, attr, 0, NULL, 0, dispos);
97 /******************************************************************************
101 * OUT HANDLE retkey (returns 0 when failure)
102 * IN ACCESS_MASK access
103 * IN POBJECT_ATTRIBUTES attr
105 NTSTATUS WINAPI NtOpenKey( PHANDLE retkey, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr )
108 DWORD len = attr->ObjectName->Length;
110 TRACE( "(%p,%s,%lx,%p)\n", attr->RootDirectory,
111 debugstr_us(attr->ObjectName), access, retkey );
113 if (len > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
114 if (!retkey) return STATUS_INVALID_PARAMETER;
116 SERVER_START_REQ( open_key )
118 req->parent = attr->RootDirectory;
119 req->access = access;
120 wine_server_add_data( req, attr->ObjectName->Buffer, len );
121 ret = wine_server_call( req );
122 *retkey = reply->hkey;
125 TRACE("<- %p\n", *retkey);
129 /******************************************************************************
130 * RtlpNtOpenKey [NTDLL.@]
134 NTSTATUS WINAPI RtlpNtOpenKey( PHANDLE retkey, ACCESS_MASK access, OBJECT_ATTRIBUTES *attr )
137 attr->Attributes &= ~(OBJ_PERMANENT|OBJ_EXCLUSIVE);
138 return NtOpenKey(retkey, access, attr);
141 /******************************************************************************
142 * NtDeleteKey [NTDLL.@]
143 * ZwDeleteKey [NTDLL.@]
145 NTSTATUS WINAPI NtDeleteKey( HANDLE hkey )
149 TRACE( "(%p)\n", hkey );
151 SERVER_START_REQ( delete_key )
154 ret = wine_server_call( req );
160 /******************************************************************************
161 * RtlpNtMakeTemporaryKey [NTDLL.@]
165 NTSTATUS WINAPI RtlpNtMakeTemporaryKey( HANDLE hkey )
167 return NtDeleteKey(hkey);
170 /******************************************************************************
171 * NtDeleteValueKey [NTDLL.@]
172 * ZwDeleteValueKey [NTDLL.@]
174 NTSTATUS WINAPI NtDeleteValueKey( HANDLE hkey, const UNICODE_STRING *name )
178 TRACE( "(%p,%s)\n", hkey, debugstr_us(name) );
179 if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
181 SERVER_START_REQ( delete_key_value )
184 wine_server_add_data( req, name->Buffer, name->Length );
185 ret = wine_server_call( req );
192 /******************************************************************************
195 * Implementation of NtQueryKey and NtEnumerateKey
197 static NTSTATUS enumerate_key( HANDLE handle, int index, KEY_INFORMATION_CLASS info_class,
198 void *info, DWORD length, DWORD *result_len )
207 case KeyBasicInformation: data_ptr = ((KEY_BASIC_INFORMATION *)info)->Name; break;
208 case KeyFullInformation: data_ptr = ((KEY_FULL_INFORMATION *)info)->Class; break;
209 case KeyNodeInformation: data_ptr = ((KEY_NODE_INFORMATION *)info)->Name; break;
211 FIXME( "Information class %d not implemented\n", info_class );
212 return STATUS_INVALID_PARAMETER;
214 fixed_size = (char *)data_ptr - (char *)info;
216 SERVER_START_REQ( enum_key )
220 req->info_class = info_class;
221 if (length > fixed_size) wine_server_set_reply( req, data_ptr, length - fixed_size );
222 if (!(ret = wine_server_call( req )))
226 RtlSecondsSince1970ToTime( reply->modif, &modif );
230 case KeyBasicInformation:
232 KEY_BASIC_INFORMATION keyinfo;
233 fixed_size = (char *)keyinfo.Name - (char *)&keyinfo;
234 keyinfo.LastWriteTime = modif;
235 keyinfo.TitleIndex = 0;
236 keyinfo.NameLength = reply->namelen;
237 memcpy( info, &keyinfo, min( length, fixed_size ) );
240 case KeyFullInformation:
242 KEY_FULL_INFORMATION keyinfo;
243 fixed_size = (char *)keyinfo.Class - (char *)&keyinfo;
244 keyinfo.LastWriteTime = modif;
245 keyinfo.TitleIndex = 0;
246 keyinfo.ClassLength = wine_server_reply_size(reply);
247 keyinfo.ClassOffset = keyinfo.ClassLength ? fixed_size : -1;
248 keyinfo.SubKeys = reply->subkeys;
249 keyinfo.MaxNameLen = reply->max_subkey;
250 keyinfo.MaxClassLen = reply->max_class;
251 keyinfo.Values = reply->values;
252 keyinfo.MaxValueNameLen = reply->max_value;
253 keyinfo.MaxValueDataLen = reply->max_data;
254 memcpy( info, &keyinfo, min( length, fixed_size ) );
257 case KeyNodeInformation:
259 KEY_NODE_INFORMATION keyinfo;
260 fixed_size = (char *)keyinfo.Name - (char *)&keyinfo;
261 keyinfo.LastWriteTime = modif;
262 keyinfo.TitleIndex = 0;
263 keyinfo.ClassLength = max( 0, wine_server_reply_size(reply) - reply->namelen );
264 keyinfo.ClassOffset = keyinfo.ClassLength ? fixed_size + reply->namelen : -1;
265 keyinfo.NameLength = reply->namelen;
266 memcpy( info, &keyinfo, min( length, fixed_size ) );
270 *result_len = fixed_size + reply->total;
271 if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
280 /******************************************************************************
281 * NtEnumerateKey [NTDLL.@]
282 * ZwEnumerateKey [NTDLL.@]
285 * the name copied into the buffer is NOT 0-terminated
287 NTSTATUS WINAPI NtEnumerateKey( HANDLE handle, ULONG index, KEY_INFORMATION_CLASS info_class,
288 void *info, DWORD length, DWORD *result_len )
290 /* -1 means query key, so avoid it here */
291 if (index == (ULONG)-1) return STATUS_NO_MORE_ENTRIES;
292 return enumerate_key( handle, index, info_class, info, length, result_len );
296 /******************************************************************************
297 * RtlpNtEnumerateSubKey [NTDLL.@]
300 NTSTATUS WINAPI RtlpNtEnumerateSubKey( HANDLE handle, UNICODE_STRING *out, ULONG index )
302 KEY_BASIC_INFORMATION *info;
303 DWORD dwLen, dwResultLen;
308 dwLen = out->Length + sizeof(KEY_BASIC_INFORMATION);
309 info = (KEY_BASIC_INFORMATION*)RtlAllocateHeap( GetProcessHeap(), 0, dwLen );
311 return STATUS_NO_MEMORY;
319 ret = NtEnumerateKey( handle, index, KeyBasicInformation, info, dwLen, &dwResultLen );
320 dwResultLen -= sizeof(KEY_BASIC_INFORMATION);
322 if (ret == STATUS_BUFFER_OVERFLOW)
323 out->Length = dwResultLen;
326 if (out->Length < info->NameLength)
328 out->Length = dwResultLen;
329 ret = STATUS_BUFFER_OVERFLOW;
333 out->Length = info->NameLength;
334 memcpy(out->Buffer, info->Name, info->NameLength);
339 RtlFreeHeap( GetProcessHeap(), 0, info );
343 /******************************************************************************
344 * NtQueryKey [NTDLL.@]
345 * ZwQueryKey [NTDLL.@]
347 NTSTATUS WINAPI NtQueryKey( HANDLE handle, KEY_INFORMATION_CLASS info_class,
348 void *info, DWORD length, DWORD *result_len )
350 return enumerate_key( handle, -1, info_class, info, length, result_len );
354 /* fill the key value info structure for a specific info class */
355 static void copy_key_value_info( KEY_VALUE_INFORMATION_CLASS info_class, void *info,
356 DWORD length, int type, int name_len, int data_len )
360 case KeyValueBasicInformation:
362 KEY_VALUE_BASIC_INFORMATION keyinfo;
363 keyinfo.TitleIndex = 0;
365 keyinfo.NameLength = name_len;
366 length = min( length, (char *)keyinfo.Name - (char *)&keyinfo );
367 memcpy( info, &keyinfo, length );
370 case KeyValueFullInformation:
372 KEY_VALUE_FULL_INFORMATION keyinfo;
373 keyinfo.TitleIndex = 0;
375 keyinfo.DataOffset = (char *)keyinfo.Name - (char *)&keyinfo + name_len;
376 keyinfo.DataLength = data_len;
377 keyinfo.NameLength = name_len;
378 length = min( length, (char *)keyinfo.Name - (char *)&keyinfo );
379 memcpy( info, &keyinfo, length );
382 case KeyValuePartialInformation:
384 KEY_VALUE_PARTIAL_INFORMATION keyinfo;
385 keyinfo.TitleIndex = 0;
387 keyinfo.DataLength = data_len;
388 length = min( length, (char *)keyinfo.Data - (char *)&keyinfo );
389 memcpy( info, &keyinfo, length );
398 /******************************************************************************
399 * NtEnumerateValueKey [NTDLL.@]
400 * ZwEnumerateValueKey [NTDLL.@]
402 NTSTATUS WINAPI NtEnumerateValueKey( HANDLE handle, ULONG index,
403 KEY_VALUE_INFORMATION_CLASS info_class,
404 void *info, DWORD length, DWORD *result_len )
410 TRACE( "(%p,%lu,%d,%p,%ld)\n", handle, index, info_class, info, length );
412 /* compute the length we want to retrieve */
415 case KeyValueBasicInformation: ptr = ((KEY_VALUE_BASIC_INFORMATION *)info)->Name; break;
416 case KeyValueFullInformation: ptr = ((KEY_VALUE_FULL_INFORMATION *)info)->Name; break;
417 case KeyValuePartialInformation: ptr = ((KEY_VALUE_PARTIAL_INFORMATION *)info)->Data; break;
419 FIXME( "Information class %d not implemented\n", info_class );
420 return STATUS_INVALID_PARAMETER;
422 fixed_size = (char *)ptr - (char *)info;
424 SERVER_START_REQ( enum_key_value )
428 req->info_class = info_class;
429 if (length > fixed_size) wine_server_set_reply( req, ptr, length - fixed_size );
430 if (!(ret = wine_server_call( req )))
432 copy_key_value_info( info_class, info, length, reply->type, reply->namelen,
433 wine_server_reply_size(reply) - reply->namelen );
434 *result_len = fixed_size + reply->total;
435 if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
443 /******************************************************************************
444 * NtQueryValueKey [NTDLL.@]
445 * ZwQueryValueKey [NTDLL.@]
448 * the name in the KeyValueInformation is never set
450 NTSTATUS WINAPI NtQueryValueKey( HANDLE handle, const UNICODE_STRING *name,
451 KEY_VALUE_INFORMATION_CLASS info_class,
452 void *info, DWORD length, DWORD *result_len )
456 unsigned int fixed_size = 0;
458 TRACE( "(%p,%s,%d,%p,%ld)\n", handle, debugstr_us(name), info_class, info, length );
460 if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
462 /* compute the length we want to retrieve */
465 case KeyValueBasicInformation:
466 fixed_size = (char *)((KEY_VALUE_BASIC_INFORMATION *)info)->Name - (char *)info;
469 case KeyValueFullInformation:
470 data_ptr = (UCHAR *)((KEY_VALUE_FULL_INFORMATION *)info)->Name;
471 fixed_size = (char *)data_ptr - (char *)info;
473 case KeyValuePartialInformation:
474 data_ptr = ((KEY_VALUE_PARTIAL_INFORMATION *)info)->Data;
475 fixed_size = (char *)data_ptr - (char *)info;
478 FIXME( "Information class %d not implemented\n", info_class );
479 return STATUS_INVALID_PARAMETER;
482 SERVER_START_REQ( get_key_value )
485 wine_server_add_data( req, name->Buffer, name->Length );
486 if (length > fixed_size) wine_server_set_reply( req, data_ptr, length - fixed_size );
487 if (!(ret = wine_server_call( req )))
489 copy_key_value_info( info_class, info, length, reply->type,
490 0, wine_server_reply_size(reply) );
491 *result_len = fixed_size + reply->total;
492 if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
499 /******************************************************************************
500 * RtlpNtQueryValueKey [NTDLL.@]
503 NTSTATUS WINAPI RtlpNtQueryValueKey( HANDLE handle, ULONG *result_type, PBYTE dest,
506 KEY_VALUE_PARTIAL_INFORMATION *info;
510 DWORD dwLen = sizeof (KEY_VALUE_PARTIAL_INFORMATION) + result_len ? *result_len : 0;
512 info = (KEY_VALUE_PARTIAL_INFORMATION*)RtlAllocateHeap( GetProcessHeap(), 0, dwLen );
514 return STATUS_NO_MEMORY;
517 ret = NtQueryValueKey( handle, &name, KeyValuePartialInformation, info, dwLen, &dwResultLen );
519 if (!ret || ret == STATUS_BUFFER_OVERFLOW)
522 *result_len = info->DataLength;
525 *result_type = info->Type;
527 if (ret != STATUS_BUFFER_OVERFLOW)
528 memcpy( dest, info->Data, info->DataLength );
531 RtlFreeHeap( GetProcessHeap(), 0, info );
535 /******************************************************************************
536 * NtFlushKey [NTDLL.@]
537 * ZwFlushKey [NTDLL.@]
539 NTSTATUS WINAPI NtFlushKey(HANDLE key)
543 TRACE("key=%p\n", key);
545 SERVER_START_REQ( flush_key )
548 ret = wine_server_call( req );
555 /******************************************************************************
556 * NtLoadKey [NTDLL.@]
557 * ZwLoadKey [NTDLL.@]
559 NTSTATUS WINAPI NtLoadKey( const OBJECT_ATTRIBUTES *attr, OBJECT_ATTRIBUTES *file )
565 TRACE("(%p,%p)\n", attr, file);
567 ret = NtCreateFile(&hive, GENERIC_READ, file, &io, NULL, FILE_ATTRIBUTE_NORMAL, 0,
568 OPEN_EXISTING, 0, NULL, 0);
571 SERVER_START_REQ( load_registry )
573 req->hkey = attr->RootDirectory;
575 wine_server_add_data(req, attr->ObjectName->Buffer, attr->ObjectName->Length);
576 ret = wine_server_call( req );
585 /******************************************************************************
586 * NtNotifyChangeKey [NTDLL.@]
587 * ZwNotifyChangeKey [NTDLL.@]
589 NTSTATUS WINAPI NtNotifyChangeKey(
592 IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
593 IN PVOID ApcContext OPTIONAL,
594 OUT PIO_STATUS_BLOCK IoStatusBlock,
595 IN ULONG CompletionFilter,
596 IN BOOLEAN Asynchronous,
597 OUT PVOID ChangeBuffer,
599 IN BOOLEAN WatchSubtree)
603 TRACE("(%p,%p,%p,%p,%p,0x%08lx, 0x%08x,%p,0x%08lx,0x%08x)\n",
604 KeyHandle, Event, ApcRoutine, ApcContext, IoStatusBlock, CompletionFilter,
605 Asynchronous, ChangeBuffer, Length, WatchSubtree);
607 if (ApcRoutine || ApcContext || ChangeBuffer || Length)
608 FIXME("Unimplemented optional parameter\n");
612 OBJECT_ATTRIBUTES attr;
613 InitializeObjectAttributes( &attr, NULL, 0, NULL, NULL );
614 ret = NtCreateEvent( &Event, EVENT_ALL_ACCESS, &attr, FALSE, FALSE );
615 if (ret != STATUS_SUCCESS)
619 SERVER_START_REQ( set_registry_notification )
621 req->hkey = KeyHandle;
623 req->subtree = WatchSubtree;
624 req->filter = CompletionFilter;
625 ret = wine_server_call( req );
631 if (ret == STATUS_SUCCESS)
632 NtWaitForSingleObject( Event, FALSE, NULL );
636 return STATUS_SUCCESS;
639 /******************************************************************************
640 * NtQueryMultipleValueKey [NTDLL]
641 * ZwQueryMultipleValueKey
644 NTSTATUS WINAPI NtQueryMultipleValueKey(
646 PKEY_MULTIPLE_VALUE_INFORMATION ListOfValuesToQuery,
648 PVOID MultipleValueInformation,
652 FIXME("(%p,%p,0x%08lx,%p,0x%08lx,%p) stub!\n",
653 KeyHandle, ListOfValuesToQuery, NumberOfItems, MultipleValueInformation,
654 Length,ReturnLength);
655 return STATUS_SUCCESS;
658 /******************************************************************************
659 * NtReplaceKey [NTDLL.@]
660 * ZwReplaceKey [NTDLL.@]
662 NTSTATUS WINAPI NtReplaceKey(
663 IN POBJECT_ATTRIBUTES ObjectAttributes,
665 IN POBJECT_ATTRIBUTES ReplacedObjectAttributes)
667 FIXME("(%p),stub!\n", Key);
668 dump_ObjectAttributes(ObjectAttributes);
669 dump_ObjectAttributes(ReplacedObjectAttributes);
670 return STATUS_SUCCESS;
672 /******************************************************************************
673 * NtRestoreKey [NTDLL.@]
674 * ZwRestoreKey [NTDLL.@]
676 NTSTATUS WINAPI NtRestoreKey(
681 FIXME("(%p,%p,0x%08lx) stub\n",
682 KeyHandle, FileHandle, RestoreFlags);
683 return STATUS_SUCCESS;
685 /******************************************************************************
686 * NtSaveKey [NTDLL.@]
687 * ZwSaveKey [NTDLL.@]
689 NTSTATUS WINAPI NtSaveKey(IN HANDLE KeyHandle, IN HANDLE FileHandle)
693 TRACE("(%p,%p)\n", KeyHandle, FileHandle);
695 SERVER_START_REQ( save_registry )
697 req->hkey = KeyHandle;
698 req->file = FileHandle;
699 ret = wine_server_call( req );
705 /******************************************************************************
706 * NtSetInformationKey [NTDLL.@]
707 * ZwSetInformationKey [NTDLL.@]
709 NTSTATUS WINAPI NtSetInformationKey(
711 IN const int KeyInformationClass,
712 IN PVOID KeyInformation,
713 IN ULONG KeyInformationLength)
715 FIXME("(%p,0x%08x,%p,0x%08lx) stub\n",
716 KeyHandle, KeyInformationClass, KeyInformation, KeyInformationLength);
717 return STATUS_SUCCESS;
721 /******************************************************************************
722 * NtSetValueKey [NTDLL.@]
723 * ZwSetValueKey [NTDLL.@]
726 * win95 does not care about count for REG_SZ and finds out the len by itself (js)
727 * NT does definitely care (aj)
729 NTSTATUS WINAPI NtSetValueKey( HANDLE hkey, const UNICODE_STRING *name, ULONG TitleIndex,
730 ULONG type, const void *data, ULONG count )
734 TRACE( "(%p,%s,%ld,%p,%ld)\n", hkey, debugstr_us(name), type, data, count );
736 if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
738 SERVER_START_REQ( set_key_value )
742 req->namelen = name->Length;
743 wine_server_add_data( req, name->Buffer, name->Length );
744 wine_server_add_data( req, data, count );
745 ret = wine_server_call( req );
751 /******************************************************************************
752 * RtlpNtSetValueKey [NTDLL.@]
755 NTSTATUS WINAPI RtlpNtSetValueKey( HANDLE hkey, ULONG type, const void *data,
761 return NtSetValueKey( hkey, &name, 0, type, data, count );
764 /******************************************************************************
765 * NtUnloadKey [NTDLL.@]
766 * ZwUnloadKey [NTDLL.@]
768 NTSTATUS WINAPI NtUnloadKey(IN HANDLE KeyHandle)
772 TRACE("(%p)\n", KeyHandle);
774 SERVER_START_REQ( unload_registry )
776 req->hkey = KeyHandle;
777 ret = wine_server_call(req);
784 /******************************************************************************
785 * RtlFormatCurrentUserKeyPath [NTDLL.@]
787 * NOTE: under NT the user name part of the path is an SID.
789 NTSTATUS WINAPI RtlFormatCurrentUserKeyPath( IN OUT PUNICODE_STRING KeyPath)
791 static const WCHAR pathW[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\'};
792 const char *user = wine_get_user_name();
793 int len = ntdll_umbstowcs( 0, user, strlen(user)+1, NULL, 0 );
795 KeyPath->MaximumLength = sizeof(pathW) + len * sizeof(WCHAR);
796 KeyPath->Length = KeyPath->MaximumLength - sizeof(WCHAR);
797 if (!(KeyPath->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, KeyPath->MaximumLength )))
798 return STATUS_NO_MEMORY;
799 memcpy( KeyPath->Buffer, pathW, sizeof(pathW) );
800 ntdll_umbstowcs( 0, user, strlen(user)+1, KeyPath->Buffer + sizeof(pathW)/sizeof(WCHAR), len );
801 return STATUS_SUCCESS;
804 /******************************************************************************
805 * RtlOpenCurrentUser [NTDLL.@]
807 * if we return just HKEY_CURRENT_USER the advapi tries to find a remote
808 * registry (odd handle) and fails
811 DWORD WINAPI RtlOpenCurrentUser(
812 IN ACCESS_MASK DesiredAccess, /* [in] */
813 OUT PHANDLE KeyHandle) /* [out] handle of HKEY_CURRENT_USER */
815 OBJECT_ATTRIBUTES ObjectAttributes;
816 UNICODE_STRING ObjectName;
819 TRACE("(0x%08lx, %p) stub\n",DesiredAccess, KeyHandle);
821 RtlFormatCurrentUserKeyPath(&ObjectName);
822 InitializeObjectAttributes(&ObjectAttributes,&ObjectName,OBJ_CASE_INSENSITIVE,0, NULL);
823 ret = NtCreateKey(KeyHandle, DesiredAccess, &ObjectAttributes, 0, NULL, 0, NULL);
824 RtlFreeUnicodeString(&ObjectName);
829 static NTSTATUS RTL_ReportRegistryValue(PKEY_VALUE_FULL_INFORMATION pInfo,
830 PRTL_QUERY_REGISTRY_TABLE pQuery, PVOID pContext, PVOID pEnvironment)
833 UNICODE_STRING src, dst;
838 NTSTATUS status = STATUS_SUCCESS;
845 if (pQuery->Flags & RTL_QUERY_REGISTRY_DIRECT)
846 return STATUS_INVALID_PARAMETER;
849 status = pQuery->QueryRoutine(pQuery->Name, pQuery->DefaultType, pQuery->DefaultData,
850 pQuery->DefaultLength, pContext, pQuery->EntryContext);
854 len = pInfo->DataLength;
856 if (pQuery->Flags & RTL_QUERY_REGISTRY_DIRECT)
858 str = (PUNICODE_STRING)pQuery->EntryContext;
863 if (!(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND))
865 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
867 dst.MaximumLength = 0;
868 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
870 dst.MaximumLength = res;
871 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
872 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
873 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
874 dst.Length, pContext, pQuery->EntryContext);
875 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
880 if (str->Buffer == NULL)
881 RtlCreateUnicodeString(str, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
883 RtlAppendUnicodeToString(str, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
887 if (!(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND))
888 return STATUS_INVALID_PARAMETER;
890 if (str->Buffer == NULL)
892 str->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, len);
893 str->MaximumLength = len;
895 len = min(len, str->MaximumLength);
896 memcpy(str->Buffer, ((CHAR*)pInfo) + pInfo->DataOffset, len);
901 bin = (LONG*)pQuery->EntryContext;
902 if (pInfo->DataLength <= sizeof(ULONG))
903 memcpy(bin, ((CHAR*)pInfo) + pInfo->DataOffset,
907 if (bin[0] <= sizeof(ULONG))
909 memcpy(&bin[1], ((CHAR*)pInfo) + pInfo->DataOffset,
910 min(-bin[0], pInfo->DataLength));
914 len = min(bin[0], pInfo->DataLength);
916 bin[2] = pInfo->Type;
917 memcpy(&bin[3], ((CHAR*)pInfo) + pInfo->DataOffset, len);
925 if((pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND) ||
926 (pInfo->Type != REG_EXPAND_SZ && pInfo->Type != REG_MULTI_SZ))
928 status = pQuery->QueryRoutine(pInfo->Name, pInfo->Type,
929 ((CHAR*)pInfo) + pInfo->DataOffset, pInfo->DataLength,
930 pContext, pQuery->EntryContext);
932 else if (pInfo->Type == REG_EXPAND_SZ)
934 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
936 dst.MaximumLength = 0;
937 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
939 dst.MaximumLength = res;
940 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
941 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
942 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
943 dst.Length, pContext, pQuery->EntryContext);
944 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
946 else /* REG_MULTI_SZ */
948 if(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND)
950 for (offset = 0; offset <= pInfo->DataLength; offset += len + sizeof(WCHAR))
952 wstr = (WCHAR*)(((CHAR*)pInfo) + offset);
953 len = strlenW(wstr) * sizeof(WCHAR);
954 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, wstr, len,
955 pContext, pQuery->EntryContext);
956 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
962 while(count<=pInfo->DataLength)
964 String = (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset)+count;
965 count+=strlenW(String)+1;
966 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
968 dst.MaximumLength = 0;
969 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
971 dst.MaximumLength = res;
972 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
973 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
974 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
975 dst.Length, pContext, pQuery->EntryContext);
976 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
977 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
987 static NTSTATUS RTL_GetKeyHandle(ULONG RelativeTo, PCWSTR Path, PHANDLE handle)
989 UNICODE_STRING KeyString;
990 OBJECT_ATTRIBUTES regkey;
995 static const WCHAR empty[] = {0};
996 static const WCHAR control[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e',
997 '\\','S','y','s','t','e','m','\\','C','u','r','r','e','n','t',' ','C','o','n','t','r','o','l','S','e','t','\\',
998 'C','o','n','t','r','o','l','\\',0};
1000 static const WCHAR devicemap[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1001 'H','a','r','d','w','a','r','e','\\','D','e','v','i','c','e','M','a','p','\\',0};
1003 static const WCHAR services[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1004 'S','y','s','t','e','m','\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1005 'S','e','r','v','i','c','e','s','\\',0};
1007 static const WCHAR user[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\',
1008 'C','u','r','r','e','n','t','U','s','e','r','\\',0};
1010 static const WCHAR windows_nt[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1011 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1012 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',0};
1014 switch (RelativeTo & 0xff)
1016 case RTL_REGISTRY_ABSOLUTE:
1020 case RTL_REGISTRY_CONTROL:
1024 case RTL_REGISTRY_DEVICEMAP:
1028 case RTL_REGISTRY_SERVICES:
1032 case RTL_REGISTRY_USER:
1036 case RTL_REGISTRY_WINDOWS_NT:
1041 return STATUS_INVALID_PARAMETER;
1044 len = (strlenW(base) + strlenW(Path) + 1) * sizeof(WCHAR);
1045 KeyString.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, len);
1046 if (KeyString.Buffer == NULL)
1047 return STATUS_NO_MEMORY;
1049 strcpyW(KeyString.Buffer, base);
1050 strcatW(KeyString.Buffer, Path);
1051 KeyString.Length = len - sizeof(WCHAR);
1052 KeyString.MaximumLength = len;
1053 InitializeObjectAttributes(®key, &KeyString, OBJ_CASE_INSENSITIVE, NULL, NULL);
1054 status = NtOpenKey(handle, KEY_ALL_ACCESS, ®key);
1055 RtlFreeHeap(GetProcessHeap(), 0, KeyString.Buffer);
1059 /*************************************************************************
1060 * RtlQueryRegistryValues [NTDLL.@]
1062 * Query multiple registry values with a signle call.
1065 * RelativeTo [I] Registry path that Path refers to
1066 * Path [I] Path to key
1067 * QueryTable [I] Table of key values to query
1068 * Context [I] Paremeter to pass to the application defined QueryRoutine function
1069 * Environment [I] Optional parameter to use when performing expantion
1072 * STATUS_SUCCESS or an appropriate NTSTATUS error code.
1074 NTSTATUS WINAPI RtlQueryRegistryValues(IN ULONG RelativeTo, IN PCWSTR Path,
1075 IN PRTL_QUERY_REGISTRY_TABLE QueryTable, IN PVOID Context,
1076 IN PVOID Environment OPTIONAL)
1078 UNICODE_STRING Value;
1079 HANDLE handle, topkey;
1080 PKEY_VALUE_FULL_INFORMATION pInfo = NULL;
1081 ULONG len, buflen = 0;
1082 NTSTATUS status=STATUS_SUCCESS, ret = STATUS_SUCCESS;
1085 TRACE("(%ld, %s, %p, %p, %p)\n", RelativeTo, debugstr_w(Path), QueryTable, Context, Environment);
1088 return STATUS_INVALID_PARAMETER;
1090 /* get a valid handle */
1091 if (RelativeTo & RTL_REGISTRY_HANDLE)
1092 topkey = handle = (HANDLE)Path;
1095 status = RTL_GetKeyHandle(RelativeTo, Path, &topkey);
1098 if(status != STATUS_SUCCESS)
1101 /* Process query table entries */
1102 for (; QueryTable->QueryRoutine != NULL || QueryTable->Name != NULL; ++QueryTable)
1104 if (QueryTable->Flags &
1105 (RTL_QUERY_REGISTRY_SUBKEY | RTL_QUERY_REGISTRY_TOPKEY))
1107 /* topkey must be kept open just in case we will reuse it later */
1108 if (handle != topkey)
1111 if (QueryTable->Flags & RTL_QUERY_REGISTRY_SUBKEY)
1114 status = RTL_GetKeyHandle((ULONG)QueryTable->Name, Path, &handle);
1115 if(status != STATUS_SUCCESS)
1125 if (QueryTable->Flags & RTL_QUERY_REGISTRY_NOVALUE)
1127 QueryTable->QueryRoutine(QueryTable->Name, REG_NONE, NULL, 0,
1128 Context, QueryTable->EntryContext);
1134 if (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED)
1136 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1142 if (QueryTable->Name == NULL)
1144 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DIRECT)
1146 ret = STATUS_INVALID_PARAMETER;
1150 /* Report all subkeys */
1153 status = NtEnumerateValueKey(handle, i,
1154 KeyValueFullInformation, pInfo, buflen, &len);
1155 if (status == STATUS_NO_MORE_ENTRIES)
1157 if (status == STATUS_BUFFER_OVERFLOW ||
1158 status == STATUS_BUFFER_TOO_SMALL)
1161 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1162 pInfo = (KEY_VALUE_FULL_INFORMATION*)RtlAllocateHeap(
1163 GetProcessHeap(), 0, buflen);
1164 NtEnumerateValueKey(handle, i, KeyValueFullInformation,
1165 pInfo, buflen, &len);
1168 status = RTL_ReportRegistryValue(pInfo, QueryTable, Context, Environment);
1169 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1174 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DELETE)
1176 RtlInitUnicodeString(&Value, pInfo->Name);
1177 NtDeleteValueKey(handle, &Value);
1181 if (i == 0 && (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED))
1183 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1189 RtlInitUnicodeString(&Value, QueryTable->Name);
1190 status = NtQueryValueKey(handle, &Value, KeyValueFullInformation,
1191 pInfo, buflen, &len);
1192 if (status == STATUS_BUFFER_OVERFLOW ||
1193 status == STATUS_BUFFER_TOO_SMALL)
1196 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1197 pInfo = (KEY_VALUE_FULL_INFORMATION*)RtlAllocateHeap(
1198 GetProcessHeap(), 0, buflen);
1199 status = NtQueryValueKey(handle, &Value,
1200 KeyValueFullInformation, pInfo, buflen, &len);
1202 if (status != STATUS_SUCCESS)
1204 if (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED)
1206 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1209 status = RTL_ReportRegistryValue(NULL, QueryTable, Context, Environment);
1210 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1218 status = RTL_ReportRegistryValue(pInfo, QueryTable, Context, Environment);
1219 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1224 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DELETE)
1225 NtDeleteValueKey(handle, &Value);
1231 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1232 if (handle != topkey)
1238 /*************************************************************************
1239 * RtlCheckRegistryKey [NTDLL.@]
1241 * Query multiple registry values with a signle call.
1244 * RelativeTo [I] Registry path that Path refers to
1245 * Path [I] Path to key
1248 * STATUS_SUCCESS if the specified key exists, or an NTSTATUS error code.
1250 NTSTATUS WINAPI RtlCheckRegistryKey(IN ULONG RelativeTo, IN PWSTR Path)
1255 TRACE("(%ld, %s)\n", RelativeTo, debugstr_w(Path));
1257 if((!RelativeTo) && Path == NULL)
1258 return STATUS_OBJECT_PATH_SYNTAX_BAD;
1259 if(RelativeTo & RTL_REGISTRY_HANDLE)
1260 return STATUS_SUCCESS;
1262 status = RTL_GetKeyHandle(RelativeTo, Path, &handle);
1263 if (handle) NtClose(handle);
1264 if (status == STATUS_INVALID_HANDLE) status = STATUS_OBJECT_NAME_NOT_FOUND;
1268 /*************************************************************************
1269 * RtlDeleteRegistryValue [NTDLL.@]
1271 * Query multiple registry values with a signle call.
1274 * RelativeTo [I] Registry path that Path refers to
1275 * Path [I] Path to key
1276 * ValueName [I] Name of the value to delete
1279 * STATUS_SUCCESS if the specified key is successfully deleted, or an NTSTATUS error code.
1281 NTSTATUS WINAPI RtlDeleteRegistryValue(IN ULONG RelativeTo, IN PCWSTR Path, IN PCWSTR ValueName)
1285 UNICODE_STRING Value;
1287 TRACE("(%ld, %s, %s)\n", RelativeTo, debugstr_w(Path), debugstr_w(ValueName));
1289 RtlInitUnicodeString(&Value, ValueName);
1290 if(RelativeTo == RTL_REGISTRY_HANDLE)
1292 return NtDeleteValueKey((HANDLE)Path, &Value);
1294 status = RTL_GetKeyHandle(RelativeTo, Path, &handle);
1295 if (status) return status;
1296 status = NtDeleteValueKey(handle, &Value);