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 = wine_server_obj_handle( attr->RootDirectory );
69 req->attributes = attr->Attributes;
70 req->options = options;
71 req->namelen = attr->ObjectName->Length;
72 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
73 if (class) wine_server_add_data( req, class->Buffer, class->Length );
74 if (!(ret = wine_server_call( req )))
76 *retkey = wine_server_ptr_handle( reply->hkey );
77 if (dispos) *dispos = reply->created ? REG_CREATED_NEW_KEY : REG_OPENED_EXISTING_KEY;
81 TRACE("<- %p\n", *retkey);
85 /******************************************************************************
86 * RtlpNtCreateKey [NTDLL.@]
90 NTSTATUS WINAPI RtlpNtCreateKey( PHANDLE retkey, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr,
91 ULONG TitleIndex, const UNICODE_STRING *class, ULONG options,
99 oa.Attributes &= ~(OBJ_PERMANENT|OBJ_EXCLUSIVE);
103 return NtCreateKey(retkey, access, attr, 0, NULL, 0, dispos);
106 /******************************************************************************
107 * NtOpenKey [NTDLL.@]
108 * ZwOpenKey [NTDLL.@]
110 * OUT HANDLE retkey (returns 0 when failure)
111 * IN ACCESS_MASK access
112 * IN POBJECT_ATTRIBUTES attr
114 NTSTATUS WINAPI NtOpenKey( PHANDLE retkey, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr )
117 DWORD len = attr->ObjectName->Length;
119 TRACE( "(%p,%s,%x,%p)\n", attr->RootDirectory,
120 debugstr_us(attr->ObjectName), access, retkey );
122 if (len > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
123 if (!retkey) return STATUS_INVALID_PARAMETER;
125 SERVER_START_REQ( open_key )
127 req->parent = wine_server_obj_handle( attr->RootDirectory );
128 req->access = access;
129 req->attributes = attr->Attributes;
130 wine_server_add_data( req, attr->ObjectName->Buffer, len );
131 ret = wine_server_call( req );
132 *retkey = wine_server_ptr_handle( reply->hkey );
135 TRACE("<- %p\n", *retkey);
139 /******************************************************************************
140 * RtlpNtOpenKey [NTDLL.@]
144 NTSTATUS WINAPI RtlpNtOpenKey( PHANDLE retkey, ACCESS_MASK access, OBJECT_ATTRIBUTES *attr )
147 attr->Attributes &= ~(OBJ_PERMANENT|OBJ_EXCLUSIVE);
148 return NtOpenKey(retkey, access, attr);
151 /******************************************************************************
152 * NtDeleteKey [NTDLL.@]
153 * ZwDeleteKey [NTDLL.@]
155 NTSTATUS WINAPI NtDeleteKey( HANDLE hkey )
159 TRACE( "(%p)\n", hkey );
161 SERVER_START_REQ( delete_key )
163 req->hkey = wine_server_obj_handle( hkey );
164 ret = wine_server_call( req );
170 /******************************************************************************
171 * RtlpNtMakeTemporaryKey [NTDLL.@]
175 NTSTATUS WINAPI RtlpNtMakeTemporaryKey( HANDLE hkey )
177 return NtDeleteKey(hkey);
180 /******************************************************************************
181 * NtDeleteValueKey [NTDLL.@]
182 * ZwDeleteValueKey [NTDLL.@]
184 NTSTATUS WINAPI NtDeleteValueKey( HANDLE hkey, const UNICODE_STRING *name )
188 TRACE( "(%p,%s)\n", hkey, debugstr_us(name) );
189 if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
191 SERVER_START_REQ( delete_key_value )
193 req->hkey = wine_server_obj_handle( hkey );
194 wine_server_add_data( req, name->Buffer, name->Length );
195 ret = wine_server_call( req );
202 /******************************************************************************
205 * Implementation of NtQueryKey and NtEnumerateKey
207 static NTSTATUS enumerate_key( HANDLE handle, int index, KEY_INFORMATION_CLASS info_class,
208 void *info, DWORD length, DWORD *result_len )
217 case KeyBasicInformation: data_ptr = ((KEY_BASIC_INFORMATION *)info)->Name; break;
218 case KeyFullInformation: data_ptr = ((KEY_FULL_INFORMATION *)info)->Class; break;
219 case KeyNodeInformation: data_ptr = ((KEY_NODE_INFORMATION *)info)->Name; break;
220 case KeyNameInformation: data_ptr = ((KEY_NAME_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 )
229 req->hkey = wine_server_obj_handle( handle );
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 case KeyBasicInformation:
239 KEY_BASIC_INFORMATION keyinfo;
240 fixed_size = (char *)keyinfo.Name - (char *)&keyinfo;
241 keyinfo.LastWriteTime.QuadPart = reply->modif;
242 keyinfo.TitleIndex = 0;
243 keyinfo.NameLength = reply->namelen;
244 memcpy( info, &keyinfo, min( length, fixed_size ) );
247 case KeyFullInformation:
249 KEY_FULL_INFORMATION keyinfo;
250 fixed_size = (char *)keyinfo.Class - (char *)&keyinfo;
251 keyinfo.LastWriteTime.QuadPart = reply->modif;
252 keyinfo.TitleIndex = 0;
253 keyinfo.ClassLength = wine_server_reply_size(reply);
254 keyinfo.ClassOffset = keyinfo.ClassLength ? fixed_size : -1;
255 keyinfo.SubKeys = reply->subkeys;
256 keyinfo.MaxNameLen = reply->max_subkey;
257 keyinfo.MaxClassLen = reply->max_class;
258 keyinfo.Values = reply->values;
259 keyinfo.MaxValueNameLen = reply->max_value;
260 keyinfo.MaxValueDataLen = reply->max_data;
261 memcpy( info, &keyinfo, min( length, fixed_size ) );
264 case KeyNodeInformation:
266 KEY_NODE_INFORMATION keyinfo;
267 fixed_size = (char *)keyinfo.Name - (char *)&keyinfo;
268 keyinfo.LastWriteTime.QuadPart = reply->modif;
269 keyinfo.TitleIndex = 0;
270 if (reply->namelen < wine_server_reply_size(reply))
272 keyinfo.ClassLength = wine_server_reply_size(reply) - reply->namelen;
273 keyinfo.ClassOffset = fixed_size + reply->namelen;
277 keyinfo.ClassLength = 0;
278 keyinfo.ClassOffset = -1;
280 keyinfo.NameLength = reply->namelen;
281 memcpy( info, &keyinfo, min( length, fixed_size ) );
284 case KeyNameInformation:
286 KEY_NAME_INFORMATION keyinfo;
287 fixed_size = (char *)keyinfo.Name - (char *)&keyinfo;
288 keyinfo.NameLength = reply->namelen;
289 memcpy( info, &keyinfo, min( length, fixed_size ) );
293 *result_len = fixed_size + reply->total;
294 if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
303 /******************************************************************************
304 * NtEnumerateKey [NTDLL.@]
305 * ZwEnumerateKey [NTDLL.@]
308 * the name copied into the buffer is NOT 0-terminated
310 NTSTATUS WINAPI NtEnumerateKey( HANDLE handle, ULONG index, KEY_INFORMATION_CLASS info_class,
311 void *info, DWORD length, DWORD *result_len )
313 /* -1 means query key, so avoid it here */
314 if (index == (ULONG)-1) return STATUS_NO_MORE_ENTRIES;
315 return enumerate_key( handle, index, info_class, info, length, result_len );
319 /******************************************************************************
320 * RtlpNtEnumerateSubKey [NTDLL.@]
323 NTSTATUS WINAPI RtlpNtEnumerateSubKey( HANDLE handle, UNICODE_STRING *out, ULONG index )
325 KEY_BASIC_INFORMATION *info;
326 DWORD dwLen, dwResultLen;
331 dwLen = out->Length + sizeof(KEY_BASIC_INFORMATION);
332 info = RtlAllocateHeap( GetProcessHeap(), 0, dwLen );
334 return STATUS_NO_MEMORY;
342 ret = NtEnumerateKey( handle, index, KeyBasicInformation, info, dwLen, &dwResultLen );
343 dwResultLen -= sizeof(KEY_BASIC_INFORMATION);
345 if (ret == STATUS_BUFFER_OVERFLOW)
346 out->Length = dwResultLen;
349 if (out->Length < info->NameLength)
351 out->Length = dwResultLen;
352 ret = STATUS_BUFFER_OVERFLOW;
356 out->Length = info->NameLength;
357 memcpy(out->Buffer, info->Name, info->NameLength);
361 RtlFreeHeap( GetProcessHeap(), 0, info );
365 /******************************************************************************
366 * NtQueryKey [NTDLL.@]
367 * ZwQueryKey [NTDLL.@]
369 NTSTATUS WINAPI NtQueryKey( HANDLE handle, KEY_INFORMATION_CLASS info_class,
370 void *info, DWORD length, DWORD *result_len )
372 return enumerate_key( handle, -1, info_class, info, length, result_len );
376 /* fill the key value info structure for a specific info class */
377 static void copy_key_value_info( KEY_VALUE_INFORMATION_CLASS info_class, void *info,
378 DWORD length, int type, int name_len, int data_len )
382 case KeyValueBasicInformation:
384 KEY_VALUE_BASIC_INFORMATION keyinfo;
385 keyinfo.TitleIndex = 0;
387 keyinfo.NameLength = name_len;
388 length = min( length, (char *)keyinfo.Name - (char *)&keyinfo );
389 memcpy( info, &keyinfo, length );
392 case KeyValueFullInformation:
394 KEY_VALUE_FULL_INFORMATION keyinfo;
395 keyinfo.TitleIndex = 0;
397 keyinfo.DataOffset = (char *)keyinfo.Name - (char *)&keyinfo + name_len;
398 keyinfo.DataLength = data_len;
399 keyinfo.NameLength = name_len;
400 length = min( length, (char *)keyinfo.Name - (char *)&keyinfo );
401 memcpy( info, &keyinfo, length );
404 case KeyValuePartialInformation:
406 KEY_VALUE_PARTIAL_INFORMATION keyinfo;
407 keyinfo.TitleIndex = 0;
409 keyinfo.DataLength = data_len;
410 length = min( length, (char *)keyinfo.Data - (char *)&keyinfo );
411 memcpy( info, &keyinfo, length );
420 /******************************************************************************
421 * NtEnumerateValueKey [NTDLL.@]
422 * ZwEnumerateValueKey [NTDLL.@]
424 NTSTATUS WINAPI NtEnumerateValueKey( HANDLE handle, ULONG index,
425 KEY_VALUE_INFORMATION_CLASS info_class,
426 void *info, DWORD length, DWORD *result_len )
432 TRACE( "(%p,%u,%d,%p,%d)\n", handle, index, info_class, info, length );
434 /* compute the length we want to retrieve */
437 case KeyValueBasicInformation: ptr = ((KEY_VALUE_BASIC_INFORMATION *)info)->Name; break;
438 case KeyValueFullInformation: ptr = ((KEY_VALUE_FULL_INFORMATION *)info)->Name; break;
439 case KeyValuePartialInformation: ptr = ((KEY_VALUE_PARTIAL_INFORMATION *)info)->Data; break;
441 FIXME( "Information class %d not implemented\n", info_class );
442 return STATUS_INVALID_PARAMETER;
444 fixed_size = (char *)ptr - (char *)info;
446 SERVER_START_REQ( enum_key_value )
448 req->hkey = wine_server_obj_handle( handle );
450 req->info_class = info_class;
451 if (length > fixed_size) wine_server_set_reply( req, ptr, length - fixed_size );
452 if (!(ret = wine_server_call( req )))
454 copy_key_value_info( info_class, info, length, reply->type, reply->namelen,
455 wine_server_reply_size(reply) - reply->namelen );
456 *result_len = fixed_size + reply->total;
457 if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
465 /******************************************************************************
466 * NtQueryValueKey [NTDLL.@]
467 * ZwQueryValueKey [NTDLL.@]
470 * the name in the KeyValueInformation is never set
472 NTSTATUS WINAPI NtQueryValueKey( HANDLE handle, const UNICODE_STRING *name,
473 KEY_VALUE_INFORMATION_CLASS info_class,
474 void *info, DWORD length, DWORD *result_len )
478 unsigned int fixed_size = 0;
480 TRACE( "(%p,%s,%d,%p,%d)\n", handle, debugstr_us(name), info_class, info, length );
482 if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
484 /* compute the length we want to retrieve */
487 case KeyValueBasicInformation:
489 KEY_VALUE_BASIC_INFORMATION *basic_info = info;
490 if (FIELD_OFFSET(KEY_VALUE_BASIC_INFORMATION, Name) < length)
492 memcpy(basic_info->Name, name->Buffer,
493 min(length - FIELD_OFFSET(KEY_VALUE_BASIC_INFORMATION, Name), name->Length));
495 fixed_size = FIELD_OFFSET(KEY_VALUE_BASIC_INFORMATION, Name) + name->Length;
499 case KeyValueFullInformation:
501 KEY_VALUE_FULL_INFORMATION *full_info = info;
502 if (FIELD_OFFSET(KEY_VALUE_FULL_INFORMATION, Name) < length)
504 memcpy(full_info->Name, name->Buffer,
505 min(length - FIELD_OFFSET(KEY_VALUE_FULL_INFORMATION, Name), name->Length));
507 data_ptr = (UCHAR *)full_info->Name + name->Length;
508 fixed_size = (char *)data_ptr - (char *)info;
511 case KeyValuePartialInformation:
512 data_ptr = ((KEY_VALUE_PARTIAL_INFORMATION *)info)->Data;
513 fixed_size = (char *)data_ptr - (char *)info;
516 FIXME( "Information class %d not implemented\n", info_class );
517 return STATUS_INVALID_PARAMETER;
520 SERVER_START_REQ( get_key_value )
522 req->hkey = wine_server_obj_handle( handle );
523 wine_server_add_data( req, name->Buffer, name->Length );
524 if (length > fixed_size && data_ptr) wine_server_set_reply( req, data_ptr, length - fixed_size );
525 if (!(ret = wine_server_call( req )))
527 copy_key_value_info( info_class, info, length, reply->type,
528 name->Length, reply->total );
529 *result_len = fixed_size + (info_class == KeyValueBasicInformation ? 0 : reply->total);
530 if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
537 /******************************************************************************
538 * RtlpNtQueryValueKey [NTDLL.@]
541 NTSTATUS WINAPI RtlpNtQueryValueKey( HANDLE handle, ULONG *result_type, PBYTE dest,
542 DWORD *result_len, void *unknown )
544 KEY_VALUE_PARTIAL_INFORMATION *info;
548 DWORD dwLen = sizeof (KEY_VALUE_PARTIAL_INFORMATION) + (result_len ? *result_len : 0);
550 info = RtlAllocateHeap( GetProcessHeap(), 0, dwLen );
552 return STATUS_NO_MEMORY;
555 ret = NtQueryValueKey( handle, &name, KeyValuePartialInformation, info, dwLen, &dwResultLen );
557 if (!ret || ret == STATUS_BUFFER_OVERFLOW)
560 *result_len = info->DataLength;
563 *result_type = info->Type;
565 if (ret != STATUS_BUFFER_OVERFLOW)
566 memcpy( dest, info->Data, info->DataLength );
569 RtlFreeHeap( GetProcessHeap(), 0, info );
573 /******************************************************************************
574 * NtFlushKey [NTDLL.@]
575 * ZwFlushKey [NTDLL.@]
577 NTSTATUS WINAPI NtFlushKey(HANDLE key)
581 TRACE("key=%p\n", key);
583 SERVER_START_REQ( flush_key )
585 req->hkey = wine_server_obj_handle( key );
586 ret = wine_server_call( req );
593 /******************************************************************************
594 * NtLoadKey [NTDLL.@]
595 * ZwLoadKey [NTDLL.@]
597 NTSTATUS WINAPI NtLoadKey( const OBJECT_ATTRIBUTES *attr, OBJECT_ATTRIBUTES *file )
603 TRACE("(%p,%p)\n", attr, file);
605 ret = NtCreateFile(&hive, GENERIC_READ, file, &io, NULL, FILE_ATTRIBUTE_NORMAL, 0,
606 FILE_OPEN, 0, NULL, 0);
609 SERVER_START_REQ( load_registry )
611 req->hkey = wine_server_obj_handle( attr->RootDirectory );
612 req->file = wine_server_obj_handle( hive );
613 wine_server_add_data(req, attr->ObjectName->Buffer, attr->ObjectName->Length);
614 ret = wine_server_call( req );
623 /******************************************************************************
624 * NtNotifyChangeKey [NTDLL.@]
625 * ZwNotifyChangeKey [NTDLL.@]
627 NTSTATUS WINAPI NtNotifyChangeKey(
630 IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
631 IN PVOID ApcContext OPTIONAL,
632 OUT PIO_STATUS_BLOCK IoStatusBlock,
633 IN ULONG CompletionFilter,
634 IN BOOLEAN Asynchronous,
635 OUT PVOID ChangeBuffer,
637 IN BOOLEAN WatchSubtree)
641 TRACE("(%p,%p,%p,%p,%p,0x%08x, 0x%08x,%p,0x%08x,0x%08x)\n",
642 KeyHandle, Event, ApcRoutine, ApcContext, IoStatusBlock, CompletionFilter,
643 Asynchronous, ChangeBuffer, Length, WatchSubtree);
645 if (ApcRoutine || ApcContext || ChangeBuffer || Length)
646 FIXME("Unimplemented optional parameter\n");
650 OBJECT_ATTRIBUTES attr;
651 InitializeObjectAttributes( &attr, NULL, 0, NULL, NULL );
652 ret = NtCreateEvent( &Event, EVENT_ALL_ACCESS, &attr, FALSE, FALSE );
653 if (ret != STATUS_SUCCESS)
657 SERVER_START_REQ( set_registry_notification )
659 req->hkey = wine_server_obj_handle( KeyHandle );
660 req->event = wine_server_obj_handle( Event );
661 req->subtree = WatchSubtree;
662 req->filter = CompletionFilter;
663 ret = wine_server_call( req );
669 if (ret == STATUS_SUCCESS)
670 NtWaitForSingleObject( Event, FALSE, NULL );
674 return STATUS_SUCCESS;
677 /******************************************************************************
678 * NtQueryMultipleValueKey [NTDLL]
679 * ZwQueryMultipleValueKey
682 NTSTATUS WINAPI NtQueryMultipleValueKey(
684 PKEY_MULTIPLE_VALUE_INFORMATION ListOfValuesToQuery,
686 PVOID MultipleValueInformation,
690 FIXME("(%p,%p,0x%08x,%p,0x%08x,%p) stub!\n",
691 KeyHandle, ListOfValuesToQuery, NumberOfItems, MultipleValueInformation,
692 Length,ReturnLength);
693 return STATUS_SUCCESS;
696 /******************************************************************************
697 * NtReplaceKey [NTDLL.@]
698 * ZwReplaceKey [NTDLL.@]
700 NTSTATUS WINAPI NtReplaceKey(
701 IN POBJECT_ATTRIBUTES ObjectAttributes,
703 IN POBJECT_ATTRIBUTES ReplacedObjectAttributes)
705 FIXME("(%s,%p,%s),stub!\n", debugstr_ObjectAttributes(ObjectAttributes), Key,
706 debugstr_ObjectAttributes(ReplacedObjectAttributes) );
707 return STATUS_SUCCESS;
709 /******************************************************************************
710 * NtRestoreKey [NTDLL.@]
711 * ZwRestoreKey [NTDLL.@]
713 NTSTATUS WINAPI NtRestoreKey(
718 FIXME("(%p,%p,0x%08x) stub\n",
719 KeyHandle, FileHandle, RestoreFlags);
720 return STATUS_SUCCESS;
722 /******************************************************************************
723 * NtSaveKey [NTDLL.@]
724 * ZwSaveKey [NTDLL.@]
726 NTSTATUS WINAPI NtSaveKey(IN HANDLE KeyHandle, IN HANDLE FileHandle)
730 TRACE("(%p,%p)\n", KeyHandle, FileHandle);
732 SERVER_START_REQ( save_registry )
734 req->hkey = wine_server_obj_handle( KeyHandle );
735 req->file = wine_server_obj_handle( FileHandle );
736 ret = wine_server_call( req );
742 /******************************************************************************
743 * NtSetInformationKey [NTDLL.@]
744 * ZwSetInformationKey [NTDLL.@]
746 NTSTATUS WINAPI NtSetInformationKey(
748 IN const int KeyInformationClass,
749 IN PVOID KeyInformation,
750 IN ULONG KeyInformationLength)
752 FIXME("(%p,0x%08x,%p,0x%08x) stub\n",
753 KeyHandle, KeyInformationClass, KeyInformation, KeyInformationLength);
754 return STATUS_SUCCESS;
758 /******************************************************************************
759 * NtSetValueKey [NTDLL.@]
760 * ZwSetValueKey [NTDLL.@]
763 * win95 does not care about count for REG_SZ and finds out the len by itself (js)
764 * NT does definitely care (aj)
766 NTSTATUS WINAPI NtSetValueKey( HANDLE hkey, const UNICODE_STRING *name, ULONG TitleIndex,
767 ULONG type, const void *data, ULONG count )
771 TRACE( "(%p,%s,%d,%p,%d)\n", hkey, debugstr_us(name), type, data, count );
773 if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
775 SERVER_START_REQ( set_key_value )
777 req->hkey = wine_server_obj_handle( hkey );
779 req->namelen = name->Length;
780 wine_server_add_data( req, name->Buffer, name->Length );
781 wine_server_add_data( req, data, count );
782 ret = wine_server_call( req );
788 /******************************************************************************
789 * RtlpNtSetValueKey [NTDLL.@]
792 NTSTATUS WINAPI RtlpNtSetValueKey( HANDLE hkey, ULONG type, const void *data,
798 return NtSetValueKey( hkey, &name, 0, type, data, count );
801 /******************************************************************************
802 * NtUnloadKey [NTDLL.@]
803 * ZwUnloadKey [NTDLL.@]
805 NTSTATUS WINAPI NtUnloadKey(IN POBJECT_ATTRIBUTES attr)
809 TRACE("(%p)\n", attr);
811 SERVER_START_REQ( unload_registry )
813 req->hkey = wine_server_obj_handle( attr->RootDirectory );
814 ret = wine_server_call(req);
821 /******************************************************************************
822 * RtlFormatCurrentUserKeyPath [NTDLL.@]
825 NTSTATUS WINAPI RtlFormatCurrentUserKeyPath( IN OUT PUNICODE_STRING KeyPath)
827 static const WCHAR pathW[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\'};
831 status = NtOpenThreadToken(GetCurrentThread(), TOKEN_READ, TRUE, &token);
832 if (status == STATUS_NO_TOKEN)
833 status = NtOpenProcessToken(GetCurrentProcess(), TOKEN_READ, &token);
834 if (status == STATUS_SUCCESS)
836 char buffer[sizeof(TOKEN_USER) + sizeof(SID) + sizeof(DWORD)*SID_MAX_SUB_AUTHORITIES];
837 DWORD len = sizeof(buffer);
839 status = NtQueryInformationToken(token, TokenUser, buffer, len, &len);
840 if (status == STATUS_SUCCESS)
842 KeyPath->MaximumLength = 0;
843 status = RtlConvertSidToUnicodeString(KeyPath, ((TOKEN_USER *)buffer)->User.Sid, FALSE);
844 if (status == STATUS_BUFFER_OVERFLOW)
846 PWCHAR buf = RtlAllocateHeap(GetProcessHeap(), 0,
847 sizeof(pathW) + KeyPath->Length + sizeof(WCHAR));
850 memcpy(buf, pathW, sizeof(pathW));
851 KeyPath->MaximumLength = KeyPath->Length + sizeof(WCHAR);
852 KeyPath->Buffer = (PWCHAR)((LPBYTE)buf + sizeof(pathW));
853 status = RtlConvertSidToUnicodeString(KeyPath,
854 ((TOKEN_USER *)buffer)->User.Sid, FALSE);
855 KeyPath->Buffer = buf;
856 KeyPath->Length += sizeof(pathW);
857 KeyPath->MaximumLength += sizeof(pathW);
860 status = STATUS_NO_MEMORY;
868 /******************************************************************************
869 * RtlOpenCurrentUser [NTDLL.@]
872 * If we return just HKEY_CURRENT_USER the advapi tries to find a remote
873 * registry (odd handle) and fails.
875 NTSTATUS WINAPI RtlOpenCurrentUser(
876 IN ACCESS_MASK DesiredAccess, /* [in] */
877 OUT PHANDLE KeyHandle) /* [out] handle of HKEY_CURRENT_USER */
879 OBJECT_ATTRIBUTES ObjectAttributes;
880 UNICODE_STRING ObjectName;
883 TRACE("(0x%08x, %p)\n",DesiredAccess, KeyHandle);
885 if ((ret = RtlFormatCurrentUserKeyPath(&ObjectName))) return ret;
886 InitializeObjectAttributes(&ObjectAttributes,&ObjectName,OBJ_CASE_INSENSITIVE,0, NULL);
887 ret = NtCreateKey(KeyHandle, DesiredAccess, &ObjectAttributes, 0, NULL, 0, NULL);
888 RtlFreeUnicodeString(&ObjectName);
893 static NTSTATUS RTL_ReportRegistryValue(PKEY_VALUE_FULL_INFORMATION pInfo,
894 PRTL_QUERY_REGISTRY_TABLE pQuery, PVOID pContext, PVOID pEnvironment)
897 UNICODE_STRING src, dst;
902 NTSTATUS status = STATUS_SUCCESS;
909 if (pQuery->Flags & RTL_QUERY_REGISTRY_DIRECT)
910 return STATUS_INVALID_PARAMETER;
913 status = pQuery->QueryRoutine(pQuery->Name, pQuery->DefaultType, pQuery->DefaultData,
914 pQuery->DefaultLength, pContext, pQuery->EntryContext);
918 len = pInfo->DataLength;
920 if (pQuery->Flags & RTL_QUERY_REGISTRY_DIRECT)
922 str = pQuery->EntryContext;
927 if (!(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND))
929 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
931 dst.MaximumLength = 0;
932 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
934 dst.MaximumLength = res;
935 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
936 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
937 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
938 dst.Length, pContext, pQuery->EntryContext);
939 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
944 if (str->Buffer == NULL)
945 RtlCreateUnicodeString(str, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
947 RtlAppendUnicodeToString(str, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
951 if (!(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND))
952 return STATUS_INVALID_PARAMETER;
954 if (str->Buffer == NULL)
956 str->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, len);
957 str->MaximumLength = len;
959 len = min(len, str->MaximumLength);
960 memcpy(str->Buffer, ((CHAR*)pInfo) + pInfo->DataOffset, len);
965 bin = pQuery->EntryContext;
966 if (pInfo->DataLength <= sizeof(ULONG))
967 memcpy(bin, ((CHAR*)pInfo) + pInfo->DataOffset,
971 if (bin[0] <= sizeof(ULONG))
973 memcpy(&bin[1], ((CHAR*)pInfo) + pInfo->DataOffset,
974 min(-bin[0], pInfo->DataLength));
978 len = min(bin[0], pInfo->DataLength);
980 bin[2] = pInfo->Type;
981 memcpy(&bin[3], ((CHAR*)pInfo) + pInfo->DataOffset, len);
989 if((pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND) ||
990 (pInfo->Type != REG_EXPAND_SZ && pInfo->Type != REG_MULTI_SZ))
992 status = pQuery->QueryRoutine(pInfo->Name, pInfo->Type,
993 ((CHAR*)pInfo) + pInfo->DataOffset, pInfo->DataLength,
994 pContext, pQuery->EntryContext);
996 else if (pInfo->Type == REG_EXPAND_SZ)
998 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
1000 dst.MaximumLength = 0;
1001 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
1003 dst.MaximumLength = res;
1004 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
1005 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
1006 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
1007 dst.Length, pContext, pQuery->EntryContext);
1008 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
1010 else /* REG_MULTI_SZ */
1012 if(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND)
1014 for (offset = 0; offset <= pInfo->DataLength; offset += len + sizeof(WCHAR))
1016 wstr = (WCHAR*)(((CHAR*)pInfo) + offset);
1017 len = strlenW(wstr) * sizeof(WCHAR);
1018 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, wstr, len,
1019 pContext, pQuery->EntryContext);
1020 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1026 while(count<=pInfo->DataLength)
1028 String = (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset)+count;
1029 count+=strlenW(String)+1;
1030 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
1032 dst.MaximumLength = 0;
1033 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
1035 dst.MaximumLength = res;
1036 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
1037 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
1038 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
1039 dst.Length, pContext, pQuery->EntryContext);
1040 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
1041 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1051 static NTSTATUS RTL_GetKeyHandle(ULONG RelativeTo, PCWSTR Path, PHANDLE handle)
1053 UNICODE_STRING KeyString;
1054 OBJECT_ATTRIBUTES regkey;
1059 static const WCHAR empty[] = {0};
1060 static const WCHAR control[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e',
1061 '\\','S','y','s','t','e','m','\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1062 'C','o','n','t','r','o','l','\\',0};
1064 static const WCHAR devicemap[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1065 'H','a','r','d','w','a','r','e','\\','D','e','v','i','c','e','M','a','p','\\',0};
1067 static const WCHAR services[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1068 'S','y','s','t','e','m','\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1069 'S','e','r','v','i','c','e','s','\\',0};
1071 static const WCHAR user[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\',
1072 'C','u','r','r','e','n','t','U','s','e','r','\\',0};
1074 static const WCHAR windows_nt[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1075 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1076 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',0};
1078 switch (RelativeTo & 0xff)
1080 case RTL_REGISTRY_ABSOLUTE:
1084 case RTL_REGISTRY_CONTROL:
1088 case RTL_REGISTRY_DEVICEMAP:
1092 case RTL_REGISTRY_SERVICES:
1096 case RTL_REGISTRY_USER:
1100 case RTL_REGISTRY_WINDOWS_NT:
1105 return STATUS_INVALID_PARAMETER;
1108 len = (strlenW(base) + strlenW(Path) + 1) * sizeof(WCHAR);
1109 KeyString.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, len);
1110 if (KeyString.Buffer == NULL)
1111 return STATUS_NO_MEMORY;
1113 strcpyW(KeyString.Buffer, base);
1114 strcatW(KeyString.Buffer, Path);
1115 KeyString.Length = len - sizeof(WCHAR);
1116 KeyString.MaximumLength = len;
1117 InitializeObjectAttributes(®key, &KeyString, OBJ_CASE_INSENSITIVE, NULL, NULL);
1118 status = NtOpenKey(handle, KEY_ALL_ACCESS, ®key);
1119 RtlFreeHeap(GetProcessHeap(), 0, KeyString.Buffer);
1123 /*************************************************************************
1124 * RtlQueryRegistryValues [NTDLL.@]
1126 * Query multiple registry values with a signle call.
1129 * RelativeTo [I] Registry path that Path refers to
1130 * Path [I] Path to key
1131 * QueryTable [I] Table of key values to query
1132 * Context [I] Parameter to pass to the application defined QueryRoutine function
1133 * Environment [I] Optional parameter to use when performing expansion
1136 * STATUS_SUCCESS or an appropriate NTSTATUS error code.
1138 NTSTATUS WINAPI RtlQueryRegistryValues(IN ULONG RelativeTo, IN PCWSTR Path,
1139 IN PRTL_QUERY_REGISTRY_TABLE QueryTable, IN PVOID Context,
1140 IN PVOID Environment OPTIONAL)
1142 UNICODE_STRING Value;
1143 HANDLE handle, topkey;
1144 PKEY_VALUE_FULL_INFORMATION pInfo = NULL;
1145 ULONG len, buflen = 0;
1146 NTSTATUS status=STATUS_SUCCESS, ret = STATUS_SUCCESS;
1149 TRACE("(%d, %s, %p, %p, %p)\n", RelativeTo, debugstr_w(Path), QueryTable, Context, Environment);
1152 return STATUS_INVALID_PARAMETER;
1154 /* get a valid handle */
1155 if (RelativeTo & RTL_REGISTRY_HANDLE)
1156 topkey = handle = (HANDLE)Path;
1159 status = RTL_GetKeyHandle(RelativeTo, Path, &topkey);
1162 if(status != STATUS_SUCCESS)
1165 /* Process query table entries */
1166 for (; QueryTable->QueryRoutine != NULL || QueryTable->Name != NULL; ++QueryTable)
1168 if (QueryTable->Flags &
1169 (RTL_QUERY_REGISTRY_SUBKEY | RTL_QUERY_REGISTRY_TOPKEY))
1171 /* topkey must be kept open just in case we will reuse it later */
1172 if (handle != topkey)
1175 if (QueryTable->Flags & RTL_QUERY_REGISTRY_SUBKEY)
1178 status = RTL_GetKeyHandle(PtrToUlong(QueryTable->Name), Path, &handle);
1179 if(status != STATUS_SUCCESS)
1189 if (QueryTable->Flags & RTL_QUERY_REGISTRY_NOVALUE)
1191 QueryTable->QueryRoutine(QueryTable->Name, REG_NONE, NULL, 0,
1192 Context, QueryTable->EntryContext);
1198 if (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED)
1200 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1206 if (QueryTable->Name == NULL)
1208 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DIRECT)
1210 ret = STATUS_INVALID_PARAMETER;
1214 /* Report all subkeys */
1217 status = NtEnumerateValueKey(handle, i,
1218 KeyValueFullInformation, pInfo, buflen, &len);
1219 if (status == STATUS_NO_MORE_ENTRIES)
1221 if (status == STATUS_BUFFER_OVERFLOW ||
1222 status == STATUS_BUFFER_TOO_SMALL)
1225 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1226 pInfo = RtlAllocateHeap(GetProcessHeap(), 0, buflen);
1227 NtEnumerateValueKey(handle, i, KeyValueFullInformation,
1228 pInfo, buflen, &len);
1231 status = RTL_ReportRegistryValue(pInfo, QueryTable, Context, Environment);
1232 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1237 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DELETE)
1239 RtlInitUnicodeString(&Value, pInfo->Name);
1240 NtDeleteValueKey(handle, &Value);
1244 if (i == 0 && (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED))
1246 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1252 RtlInitUnicodeString(&Value, QueryTable->Name);
1253 status = NtQueryValueKey(handle, &Value, KeyValueFullInformation,
1254 pInfo, buflen, &len);
1255 if (status == STATUS_BUFFER_OVERFLOW ||
1256 status == STATUS_BUFFER_TOO_SMALL)
1259 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1260 pInfo = RtlAllocateHeap(GetProcessHeap(), 0, buflen);
1261 status = NtQueryValueKey(handle, &Value,
1262 KeyValueFullInformation, pInfo, buflen, &len);
1264 if (status != STATUS_SUCCESS)
1266 if (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED)
1268 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1271 status = RTL_ReportRegistryValue(NULL, QueryTable, Context, Environment);
1272 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1280 status = RTL_ReportRegistryValue(pInfo, QueryTable, Context, Environment);
1281 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1286 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DELETE)
1287 NtDeleteValueKey(handle, &Value);
1293 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1294 if (handle != topkey)
1300 /*************************************************************************
1301 * RtlCheckRegistryKey [NTDLL.@]
1303 * Query multiple registry values with a signle call.
1306 * RelativeTo [I] Registry path that Path refers to
1307 * Path [I] Path to key
1310 * STATUS_SUCCESS if the specified key exists, or an NTSTATUS error code.
1312 NTSTATUS WINAPI RtlCheckRegistryKey(IN ULONG RelativeTo, IN PWSTR Path)
1317 TRACE("(%d, %s)\n", RelativeTo, debugstr_w(Path));
1319 if((!RelativeTo) && Path == NULL)
1320 return STATUS_OBJECT_PATH_SYNTAX_BAD;
1321 if(RelativeTo & RTL_REGISTRY_HANDLE)
1322 return STATUS_SUCCESS;
1324 status = RTL_GetKeyHandle(RelativeTo, Path, &handle);
1325 if (handle) NtClose(handle);
1326 if (status == STATUS_INVALID_HANDLE) status = STATUS_OBJECT_NAME_NOT_FOUND;
1330 /*************************************************************************
1331 * RtlDeleteRegistryValue [NTDLL.@]
1333 * Query multiple registry values with a signle call.
1336 * RelativeTo [I] Registry path that Path refers to
1337 * Path [I] Path to key
1338 * ValueName [I] Name of the value to delete
1341 * STATUS_SUCCESS if the specified key is successfully deleted, or an NTSTATUS error code.
1343 NTSTATUS WINAPI RtlDeleteRegistryValue(IN ULONG RelativeTo, IN PCWSTR Path, IN PCWSTR ValueName)
1347 UNICODE_STRING Value;
1349 TRACE("(%d, %s, %s)\n", RelativeTo, debugstr_w(Path), debugstr_w(ValueName));
1351 RtlInitUnicodeString(&Value, ValueName);
1352 if(RelativeTo == RTL_REGISTRY_HANDLE)
1354 return NtDeleteValueKey((HANDLE)Path, &Value);
1356 status = RTL_GetKeyHandle(RelativeTo, Path, &handle);
1357 if (status) return status;
1358 status = NtDeleteValueKey(handle, &Value);
1363 /*************************************************************************
1364 * RtlWriteRegistryValue [NTDLL.@]
1366 * Sets the registry value with provided data.
1369 * RelativeTo [I] Registry path that path parameter refers to
1370 * path [I] Path to the key (or handle - see RTL_GetKeyHandle)
1371 * name [I] Name of the registry value to set
1372 * type [I] Type of the registry key to set
1373 * data [I] Pointer to the user data to be set
1374 * length [I] Length of the user data pointed by data
1377 * STATUS_SUCCESS if the specified key is successfully set,
1378 * or an NTSTATUS error code.
1380 NTSTATUS WINAPI RtlWriteRegistryValue( ULONG RelativeTo, PCWSTR path, PCWSTR name,
1381 ULONG type, PVOID data, ULONG length )
1387 TRACE( "(%d, %s, %s) -> %d: %p [%d]\n", RelativeTo, debugstr_w(path), debugstr_w(name),
1388 type, data, length );
1390 RtlInitUnicodeString( &str, name );
1392 if (RelativeTo == RTL_REGISTRY_HANDLE)
1393 return NtSetValueKey( (HANDLE)path, &str, 0, type, data, length );
1395 status = RTL_GetKeyHandle( RelativeTo, path, &hkey );
1396 if (status != STATUS_SUCCESS) return status;
1398 status = NtSetValueKey( hkey, &str, 0, type, data, length );