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, const OBJECT_ATTRIBUTES *file )
564 dump_ObjectAttributes(attr);
565 dump_ObjectAttributes(file);
566 return STATUS_SUCCESS;
569 /******************************************************************************
570 * NtNotifyChangeKey [NTDLL.@]
571 * ZwNotifyChangeKey [NTDLL.@]
573 NTSTATUS WINAPI NtNotifyChangeKey(
576 IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
577 IN PVOID ApcContext OPTIONAL,
578 OUT PIO_STATUS_BLOCK IoStatusBlock,
579 IN ULONG CompletionFilter,
580 IN BOOLEAN Asynchroneous,
581 OUT PVOID ChangeBuffer,
583 IN BOOLEAN WatchSubtree)
585 FIXME("(%p,%p,%p,%p,%p,0x%08lx, 0x%08x,%p,0x%08lx,0x%08x) stub!\n",
586 KeyHandle, Event, ApcRoutine, ApcContext, IoStatusBlock, CompletionFilter,
587 Asynchroneous, ChangeBuffer, Length, WatchSubtree);
588 return STATUS_SUCCESS;
591 /******************************************************************************
592 * NtQueryMultipleValueKey [NTDLL]
593 * ZwQueryMultipleValueKey
596 NTSTATUS WINAPI NtQueryMultipleValueKey(
598 PVALENTW ListOfValuesToQuery,
600 PVOID MultipleValueInformation,
604 FIXME("(%p,%p,0x%08lx,%p,0x%08lx,%p) stub!\n",
605 KeyHandle, ListOfValuesToQuery, NumberOfItems, MultipleValueInformation,
606 Length,ReturnLength);
607 return STATUS_SUCCESS;
610 /******************************************************************************
611 * NtReplaceKey [NTDLL.@]
612 * ZwReplaceKey [NTDLL.@]
614 NTSTATUS WINAPI NtReplaceKey(
615 IN POBJECT_ATTRIBUTES ObjectAttributes,
617 IN POBJECT_ATTRIBUTES ReplacedObjectAttributes)
619 FIXME("(%p),stub!\n", Key);
620 dump_ObjectAttributes(ObjectAttributes);
621 dump_ObjectAttributes(ReplacedObjectAttributes);
622 return STATUS_SUCCESS;
624 /******************************************************************************
625 * NtRestoreKey [NTDLL.@]
626 * ZwRestoreKey [NTDLL.@]
628 NTSTATUS WINAPI NtRestoreKey(
633 FIXME("(%p,%p,0x%08lx) stub\n",
634 KeyHandle, FileHandle, RestoreFlags);
635 return STATUS_SUCCESS;
637 /******************************************************************************
638 * NtSaveKey [NTDLL.@]
639 * ZwSaveKey [NTDLL.@]
641 NTSTATUS WINAPI NtSaveKey(
643 IN HANDLE FileHandle)
645 FIXME("(%p,%p) stub\n",
646 KeyHandle, FileHandle);
647 return STATUS_SUCCESS;
649 /******************************************************************************
650 * NtSetInformationKey [NTDLL.@]
651 * ZwSetInformationKey [NTDLL.@]
653 NTSTATUS WINAPI NtSetInformationKey(
655 IN const int KeyInformationClass,
656 IN PVOID KeyInformation,
657 IN ULONG KeyInformationLength)
659 FIXME("(%p,0x%08x,%p,0x%08lx) stub\n",
660 KeyHandle, KeyInformationClass, KeyInformation, KeyInformationLength);
661 return STATUS_SUCCESS;
665 /******************************************************************************
666 * NtSetValueKey [NTDLL.@]
667 * ZwSetValueKey [NTDLL.@]
670 * win95 does not care about count for REG_SZ and finds out the len by itself (js)
671 * NT does definitely care (aj)
673 NTSTATUS WINAPI NtSetValueKey( HKEY hkey, const UNICODE_STRING *name, ULONG TitleIndex,
674 ULONG type, const void *data, ULONG count )
678 TRACE( "(%p,%s,%ld,%p,%ld)\n", hkey, debugstr_us(name), type, data, count );
680 if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
682 SERVER_START_REQ( set_key_value )
686 req->namelen = name->Length;
687 wine_server_add_data( req, name->Buffer, name->Length );
688 wine_server_add_data( req, data, count );
689 ret = wine_server_call( req );
695 /******************************************************************************
696 * RtlpNtSetValueKey [NTDLL.@]
699 NTSTATUS WINAPI RtlpNtSetValueKey( HKEY hkey, ULONG type, const void *data,
705 return NtSetValueKey( hkey, &name, 0, type, data, count );
708 /******************************************************************************
709 * NtUnloadKey [NTDLL.@]
710 * ZwUnloadKey [NTDLL.@]
712 NTSTATUS WINAPI NtUnloadKey(
717 return STATUS_SUCCESS;
720 /******************************************************************************
721 * RtlFormatCurrentUserKeyPath [NTDLL.@]
723 * NOTE: under NT the user name part of the path is an SID.
725 NTSTATUS WINAPI RtlFormatCurrentUserKeyPath( IN OUT PUNICODE_STRING KeyPath)
727 static const WCHAR pathW[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\'};
728 const char *user = wine_get_user_name();
729 int len = ntdll_umbstowcs( 0, user, strlen(user)+1, NULL, 0 );
731 KeyPath->MaximumLength = sizeof(pathW) + len * sizeof(WCHAR);
732 KeyPath->Length = KeyPath->MaximumLength - sizeof(WCHAR);
733 if (!(KeyPath->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, KeyPath->MaximumLength )))
734 return STATUS_NO_MEMORY;
735 memcpy( KeyPath->Buffer, pathW, sizeof(pathW) );
736 ntdll_umbstowcs( 0, user, strlen(user)+1, KeyPath->Buffer + sizeof(pathW)/sizeof(WCHAR), len );
737 return STATUS_SUCCESS;
740 /******************************************************************************
741 * RtlOpenCurrentUser [NTDLL.@]
743 * if we return just HKEY_CURRENT_USER the advapi tries to find a remote
744 * registry (odd handle) and fails
747 DWORD WINAPI RtlOpenCurrentUser(
748 IN ACCESS_MASK DesiredAccess, /* [in] */
749 OUT PHKEY KeyHandle) /* [out] handle of HKEY_CURRENT_USER */
751 OBJECT_ATTRIBUTES ObjectAttributes;
752 UNICODE_STRING ObjectName;
755 TRACE("(0x%08lx, %p) stub\n",DesiredAccess, KeyHandle);
757 RtlFormatCurrentUserKeyPath(&ObjectName);
758 InitializeObjectAttributes(&ObjectAttributes,&ObjectName,OBJ_CASE_INSENSITIVE,0, NULL);
759 ret = NtCreateKey(KeyHandle, DesiredAccess, &ObjectAttributes, 0, NULL, 0, NULL);
760 RtlFreeUnicodeString(&ObjectName);
765 static NTSTATUS RTL_ReportRegistryValue(PKEY_VALUE_FULL_INFORMATION pInfo,
766 PRTL_QUERY_REGISTRY_TABLE pQuery, PVOID pContext, PVOID pEnvironment)
769 UNICODE_STRING src, dst;
774 NTSTATUS status = STATUS_SUCCESS;
781 if (pQuery->Flags & RTL_QUERY_REGISTRY_DIRECT)
782 return STATUS_INVALID_PARAMETER;
785 status = pQuery->QueryRoutine(pQuery->Name, pQuery->DefaultType, pQuery->DefaultData,
786 pQuery->DefaultLength, pContext, pQuery->EntryContext);
790 len = pInfo->DataLength;
792 if (pQuery->Flags & RTL_QUERY_REGISTRY_DIRECT)
794 str = (PUNICODE_STRING)pQuery->EntryContext;
799 if (!(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND))
801 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
803 dst.MaximumLength = 0;
804 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
806 dst.MaximumLength = res;
807 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
808 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
809 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
810 dst.Length, pContext, pQuery->EntryContext);
811 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
816 if (str->Buffer == NULL)
817 RtlCreateUnicodeString(str, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
819 RtlAppendUnicodeToString(str, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
823 if (!(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND))
824 return STATUS_INVALID_PARAMETER;
826 if (str->Buffer == NULL)
828 str->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, len);
829 str->MaximumLength = len;
831 len = min(len, str->MaximumLength);
832 memcpy(str->Buffer, ((CHAR*)pInfo) + pInfo->DataOffset, len);
837 bin = (LONG*)pQuery->EntryContext;
838 if (pInfo->DataLength <= sizeof(ULONG))
839 memcpy(bin, ((CHAR*)pInfo) + pInfo->DataOffset,
843 if (bin[0] <= sizeof(ULONG))
845 memcpy(&bin[1], ((CHAR*)pInfo) + pInfo->DataOffset,
846 min(-bin[0], pInfo->DataLength));
850 len = min(bin[0], pInfo->DataLength);
852 bin[2] = pInfo->Type;
853 memcpy(&bin[3], ((CHAR*)pInfo) + pInfo->DataOffset, len);
861 if((pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND) ||
862 (pInfo->Type != REG_EXPAND_SZ && pInfo->Type != REG_MULTI_SZ))
864 status = pQuery->QueryRoutine(pInfo->Name, pInfo->Type,
865 ((CHAR*)pInfo) + pInfo->DataOffset, pInfo->DataLength,
866 pContext, pQuery->EntryContext);
868 else if (pInfo->Type == REG_EXPAND_SZ)
870 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
872 dst.MaximumLength = 0;
873 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
875 dst.MaximumLength = res;
876 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
877 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
878 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
879 dst.Length, pContext, pQuery->EntryContext);
880 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
882 else /* REG_MULTI_SZ */
884 if(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND)
886 for (offset = 0; offset <= pInfo->DataLength; offset += len + sizeof(WCHAR))
888 wstr = (WCHAR*)(((CHAR*)pInfo) + offset);
889 len = strlenW(wstr) * sizeof(WCHAR);
890 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, wstr, len,
891 pContext, pQuery->EntryContext);
892 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
898 while(count<=pInfo->DataLength)
900 String = (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset)+count;
901 count+=strlenW(String)+1;
902 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
904 dst.MaximumLength = 0;
905 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
907 dst.MaximumLength = res;
908 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
909 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
910 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
911 dst.Length, pContext, pQuery->EntryContext);
912 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
913 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
923 static NTSTATUS RTL_GetKeyHandle(ULONG RelativeTo, PCWSTR Path, PHKEY handle)
925 UNICODE_STRING KeyString;
926 OBJECT_ATTRIBUTES regkey;
931 static const WCHAR empty[] = {0};
932 static const WCHAR control[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e',
933 '\\','S','y','s','t','e','m','\\','C','u','r','r','e','n','t',' ','C','o','n','t','r','o','l','S','e','t','\\',
934 'C','o','n','t','r','o','l','\\',0};
936 static const WCHAR devicemap[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
937 'H','a','r','d','w','a','r','e','\\','D','e','v','i','c','e','M','a','p','\\',0};
939 static const WCHAR services[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
940 'S','y','s','t','e','m','\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
941 'S','e','r','v','i','c','e','s','\\',0};
943 static const WCHAR user[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\',
944 'C','u','r','r','e','n','t','U','s','e','r','\\',0};
946 static const WCHAR windows_nt[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
947 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
948 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',0};
950 switch (RelativeTo & 0xff)
952 case RTL_REGISTRY_ABSOLUTE:
956 case RTL_REGISTRY_CONTROL:
960 case RTL_REGISTRY_DEVICEMAP:
964 case RTL_REGISTRY_SERVICES:
968 case RTL_REGISTRY_USER:
972 case RTL_REGISTRY_WINDOWS_NT:
977 return STATUS_INVALID_PARAMETER;
980 len = (strlenW(base) + strlenW(Path) + 1) * sizeof(WCHAR);
981 KeyString.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, len);
982 if (KeyString.Buffer == NULL)
983 return STATUS_NO_MEMORY;
985 strcpyW(KeyString.Buffer, base);
986 strcatW(KeyString.Buffer, Path);
987 KeyString.Length = len - sizeof(WCHAR);
988 KeyString.MaximumLength = len;
989 InitializeObjectAttributes(®key, &KeyString, OBJ_CASE_INSENSITIVE, NULL, NULL);
990 status = NtOpenKey(handle, KEY_ALL_ACCESS, ®key);
991 RtlFreeHeap(GetProcessHeap(), 0, KeyString.Buffer);
995 /*************************************************************************
996 * RtlQueryRegistryValues [NTDLL.@]
998 * Query multiple registry values with a signle call.
1001 * RelativeTo [I] Registry path that Path refers to
1002 * Path [I] Path to key
1003 * QueryTable [I] Table of key values to query
1004 * Context [I] Paremeter to pass to the application defined QueryRoutine function
1005 * Environment [I] Optional parameter to use when performing expantion
1008 * STATUS_SUCCESS or an appropriate NTSTATUS error code.
1010 NTSTATUS WINAPI RtlQueryRegistryValues(IN ULONG RelativeTo, IN PCWSTR Path,
1011 IN PRTL_QUERY_REGISTRY_TABLE QueryTable, IN PVOID Context,
1012 IN PVOID Environment OPTIONAL)
1014 UNICODE_STRING Value;
1015 HKEY handle, topkey;
1016 PKEY_VALUE_FULL_INFORMATION pInfo = NULL;
1017 ULONG len, buflen = 0;
1018 NTSTATUS status=STATUS_SUCCESS, ret = STATUS_SUCCESS;
1021 TRACE("(%ld, %s, %p, %p, %p)\n", RelativeTo, debugstr_w(Path), QueryTable, Context, Environment);
1024 return STATUS_INVALID_PARAMETER;
1026 /* get a valid handle */
1027 if (RelativeTo & RTL_REGISTRY_HANDLE)
1028 topkey = handle = (HANDLE)Path;
1031 status = RTL_GetKeyHandle(RelativeTo, Path, &topkey);
1034 if(status != STATUS_SUCCESS)
1037 /* Process query table entries */
1038 for (; QueryTable->QueryRoutine != NULL || QueryTable->Name != NULL; ++QueryTable)
1040 if (QueryTable->Flags &
1041 (RTL_QUERY_REGISTRY_SUBKEY | RTL_QUERY_REGISTRY_TOPKEY))
1043 /* topkey must be kept open just in case we will reuse it later */
1044 if (handle != topkey)
1047 if (QueryTable->Flags & RTL_QUERY_REGISTRY_SUBKEY)
1050 status = RTL_GetKeyHandle((ULONG)QueryTable->Name, Path, &handle);
1051 if(status != STATUS_SUCCESS)
1061 if (QueryTable->Flags & RTL_QUERY_REGISTRY_NOVALUE)
1063 QueryTable->QueryRoutine(QueryTable->Name, REG_NONE, NULL, 0,
1064 Context, QueryTable->EntryContext);
1070 if (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED)
1072 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1078 if (QueryTable->Name == NULL)
1080 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DIRECT)
1082 ret = STATUS_INVALID_PARAMETER;
1086 /* Report all subkeys */
1089 status = NtEnumerateValueKey(handle, i,
1090 KeyValueFullInformation, pInfo, buflen, &len);
1091 if (status == STATUS_NO_MORE_ENTRIES)
1093 if (status == STATUS_BUFFER_OVERFLOW ||
1094 status == STATUS_BUFFER_TOO_SMALL)
1097 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1098 pInfo = (KEY_VALUE_FULL_INFORMATION*)RtlAllocateHeap(
1099 GetProcessHeap(), 0, buflen);
1100 NtEnumerateValueKey(handle, i, KeyValueFullInformation,
1101 pInfo, buflen, &len);
1104 status = RTL_ReportRegistryValue(pInfo, QueryTable, Context, Environment);
1105 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1110 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DELETE)
1112 RtlInitUnicodeString(&Value, pInfo->Name);
1113 NtDeleteValueKey(handle, &Value);
1117 if (i == 0 && (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED))
1119 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1125 RtlInitUnicodeString(&Value, QueryTable->Name);
1126 status = NtQueryValueKey(handle, &Value, KeyValueFullInformation,
1127 pInfo, buflen, &len);
1128 if (status == STATUS_BUFFER_OVERFLOW ||
1129 status == STATUS_BUFFER_TOO_SMALL)
1132 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1133 pInfo = (KEY_VALUE_FULL_INFORMATION*)RtlAllocateHeap(
1134 GetProcessHeap(), 0, buflen);
1135 status = NtQueryValueKey(handle, &Value,
1136 KeyValueFullInformation, pInfo, buflen, &len);
1138 if (status != STATUS_SUCCESS)
1140 if (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED)
1142 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1145 status = RTL_ReportRegistryValue(NULL, QueryTable, Context, Environment);
1146 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1154 status = RTL_ReportRegistryValue(pInfo, QueryTable, Context, Environment);
1155 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1160 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DELETE)
1161 NtDeleteValueKey(handle, &Value);
1167 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1168 if (handle != topkey)
1174 /*************************************************************************
1175 * RtlCheckRegistryKey [NTDLL.@]
1177 * Query multiple registry values with a signle call.
1180 * RelativeTo [I] Registry path that Path refers to
1181 * Path [I] Path to key
1184 * STATUS_SUCCESS if the specified key exists, or an NTSTATUS error code.
1186 NTSTATUS WINAPI RtlCheckRegistryKey(IN ULONG RelativeTo, IN PWSTR Path)
1191 TRACE("(%ld, %s)\n", RelativeTo, debugstr_w(Path));
1193 if((!RelativeTo) && Path == NULL)
1194 return STATUS_OBJECT_PATH_SYNTAX_BAD;
1195 if(RelativeTo & RTL_REGISTRY_HANDLE)
1196 return STATUS_SUCCESS;
1198 status = RTL_GetKeyHandle(RelativeTo, Path, &handle);
1199 if (handle) NtClose(handle);
1200 if (status == STATUS_INVALID_HANDLE) status = STATUS_OBJECT_NAME_NOT_FOUND;
1204 /*************************************************************************
1205 * RtlDeleteRegistryValue [NTDLL.@]
1207 * Query multiple registry values with a signle call.
1210 * RelativeTo [I] Registry path that Path refers to
1211 * Path [I] Path to key
1212 * ValueName [I] Name of the value to delete
1215 * STATUS_SUCCESS if the specified key is successfully deleted, or an NTSTATUS error code.
1217 NTSTATUS WINAPI RtlDeleteRegistryValue(IN ULONG RelativeTo, IN PCWSTR Path, IN PCWSTR ValueName)
1221 UNICODE_STRING Value;
1223 TRACE("(%ld, %s, %s)\n", RelativeTo, debugstr_w(Path), debugstr_w(ValueName));
1225 RtlInitUnicodeString(&Value, ValueName);
1226 if(RelativeTo == RTL_REGISTRY_HANDLE)
1228 return NtDeleteValueKey((HKEY)Path, &Value);
1230 status = RTL_GetKeyHandle(RelativeTo, Path, &handle);
1231 if (status) return status;
1232 status = NtDeleteValueKey(handle, &Value);