4 * Copyright (C) 1999 Juergen Schmied
5 * Copyright (C) 2000 Alexandre Julliard
6 * Copyright 2005 Ivan Leo Puoti, Laurent Pinchart
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * HKEY_LOCAL_MACHINE \\REGISTRY\\MACHINE
24 * HKEY_USERS \\REGISTRY\\USER
25 * HKEY_CURRENT_CONFIG \\REGISTRY\\MACHINE\\SYSTEM\\CURRENTCONTROLSET\\HARDWARE PROFILES\\CURRENT
26 * HKEY_CLASSES \\REGISTRY\\MACHINE\\SOFTWARE\\CLASSES
30 #include "wine/port.h"
37 #define WIN32_NO_STATUS
38 #include "wine/library.h"
39 #include "ntdll_misc.h"
40 #include "wine/debug.h"
41 #include "wine/unicode.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(reg
);
45 /* maximum length of a key/value name in bytes (without terminating null) */
46 #define MAX_NAME_LENGTH ((MAX_PATH-1) * sizeof(WCHAR))
48 /******************************************************************************
49 * NtCreateKey [NTDLL.@]
50 * ZwCreateKey [NTDLL.@]
52 NTSTATUS WINAPI
NtCreateKey( PHANDLE retkey
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
,
53 ULONG TitleIndex
, const UNICODE_STRING
*class, ULONG options
,
58 if (!retkey
|| !attr
) return STATUS_ACCESS_VIOLATION
;
59 if (attr
->Length
> sizeof(OBJECT_ATTRIBUTES
)) return STATUS_INVALID_PARAMETER
;
60 if (attr
->ObjectName
->Length
> MAX_NAME_LENGTH
) return STATUS_BUFFER_OVERFLOW
;
62 TRACE( "(%p,%s,%s,%x,%x,%p)\n", attr
->RootDirectory
, debugstr_us(attr
->ObjectName
),
63 debugstr_us(class), options
, access
, retkey
);
65 SERVER_START_REQ( create_key
)
67 req
->parent
= attr
->RootDirectory
;
69 req
->attributes
= attr
->Attributes
;
70 req
->options
= options
;
72 req
->namelen
= attr
->ObjectName
->Length
;
73 wine_server_add_data( req
, attr
->ObjectName
->Buffer
, attr
->ObjectName
->Length
);
74 if (class) wine_server_add_data( req
, class->Buffer
, class->Length
);
75 if (!(ret
= wine_server_call( req
)))
77 *retkey
= reply
->hkey
;
78 if (dispos
) *dispos
= reply
->created
? REG_CREATED_NEW_KEY
: REG_OPENED_EXISTING_KEY
;
82 TRACE("<- %p\n", *retkey
);
86 /******************************************************************************
87 * RtlpNtCreateKey [NTDLL.@]
91 NTSTATUS WINAPI
RtlpNtCreateKey( PHANDLE retkey
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
,
92 ULONG TitleIndex
, const UNICODE_STRING
*class, ULONG options
,
99 memcpy( &oa
, attr
, sizeof oa
);
100 oa
.Attributes
&= ~(OBJ_PERMANENT
|OBJ_EXCLUSIVE
);
104 return NtCreateKey(retkey
, access
, attr
, 0, NULL
, 0, dispos
);
107 /******************************************************************************
108 * NtOpenKey [NTDLL.@]
109 * ZwOpenKey [NTDLL.@]
111 * OUT HANDLE retkey (returns 0 when failure)
112 * IN ACCESS_MASK access
113 * IN POBJECT_ATTRIBUTES attr
115 NTSTATUS WINAPI
NtOpenKey( PHANDLE retkey
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
)
118 DWORD len
= attr
->ObjectName
->Length
;
120 TRACE( "(%p,%s,%x,%p)\n", attr
->RootDirectory
,
121 debugstr_us(attr
->ObjectName
), access
, retkey
);
123 if (len
> MAX_NAME_LENGTH
) return STATUS_BUFFER_OVERFLOW
;
124 if (!retkey
) return STATUS_INVALID_PARAMETER
;
126 SERVER_START_REQ( open_key
)
128 req
->parent
= attr
->RootDirectory
;
129 req
->access
= access
;
130 req
->attributes
= attr
->Attributes
;
131 wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
132 ret
= wine_server_call( req
);
133 *retkey
= reply
->hkey
;
136 TRACE("<- %p\n", *retkey
);
140 /******************************************************************************
141 * RtlpNtOpenKey [NTDLL.@]
145 NTSTATUS WINAPI
RtlpNtOpenKey( PHANDLE retkey
, ACCESS_MASK access
, OBJECT_ATTRIBUTES
*attr
)
148 attr
->Attributes
&= ~(OBJ_PERMANENT
|OBJ_EXCLUSIVE
);
149 return NtOpenKey(retkey
, access
, attr
);
152 /******************************************************************************
153 * NtDeleteKey [NTDLL.@]
154 * ZwDeleteKey [NTDLL.@]
156 NTSTATUS WINAPI
NtDeleteKey( HANDLE hkey
)
160 TRACE( "(%p)\n", hkey
);
162 SERVER_START_REQ( delete_key
)
165 ret
= wine_server_call( req
);
171 /******************************************************************************
172 * RtlpNtMakeTemporaryKey [NTDLL.@]
176 NTSTATUS WINAPI
RtlpNtMakeTemporaryKey( HANDLE hkey
)
178 return NtDeleteKey(hkey
);
181 /******************************************************************************
182 * NtDeleteValueKey [NTDLL.@]
183 * ZwDeleteValueKey [NTDLL.@]
185 NTSTATUS WINAPI
NtDeleteValueKey( HANDLE hkey
, const UNICODE_STRING
*name
)
189 TRACE( "(%p,%s)\n", hkey
, debugstr_us(name
) );
190 if (name
->Length
> MAX_NAME_LENGTH
) return STATUS_BUFFER_OVERFLOW
;
192 SERVER_START_REQ( delete_key_value
)
195 wine_server_add_data( req
, name
->Buffer
, name
->Length
);
196 ret
= wine_server_call( req
);
203 /******************************************************************************
206 * Implementation of NtQueryKey and NtEnumerateKey
208 static NTSTATUS
enumerate_key( HANDLE handle
, int index
, KEY_INFORMATION_CLASS info_class
,
209 void *info
, DWORD length
, DWORD
*result_len
)
218 case KeyBasicInformation
: data_ptr
= ((KEY_BASIC_INFORMATION
*)info
)->Name
; break;
219 case KeyFullInformation
: data_ptr
= ((KEY_FULL_INFORMATION
*)info
)->Class
; break;
220 case KeyNodeInformation
: data_ptr
= ((KEY_NODE_INFORMATION
*)info
)->Name
; break;
222 FIXME( "Information class %d not implemented\n", info_class
);
223 return STATUS_INVALID_PARAMETER
;
225 fixed_size
= (char *)data_ptr
- (char *)info
;
227 SERVER_START_REQ( enum_key
)
231 req
->info_class
= info_class
;
232 if (length
> fixed_size
) wine_server_set_reply( req
, data_ptr
, length
- fixed_size
);
233 if (!(ret
= wine_server_call( req
)))
237 RtlSecondsSince1970ToTime( reply
->modif
, &modif
);
241 case KeyBasicInformation
:
243 KEY_BASIC_INFORMATION keyinfo
;
244 fixed_size
= (char *)keyinfo
.Name
- (char *)&keyinfo
;
245 keyinfo
.LastWriteTime
= modif
;
246 keyinfo
.TitleIndex
= 0;
247 keyinfo
.NameLength
= reply
->namelen
;
248 memcpy( info
, &keyinfo
, min( length
, fixed_size
) );
251 case KeyFullInformation
:
253 KEY_FULL_INFORMATION keyinfo
;
254 fixed_size
= (char *)keyinfo
.Class
- (char *)&keyinfo
;
255 keyinfo
.LastWriteTime
= modif
;
256 keyinfo
.TitleIndex
= 0;
257 keyinfo
.ClassLength
= wine_server_reply_size(reply
);
258 keyinfo
.ClassOffset
= keyinfo
.ClassLength
? fixed_size
: -1;
259 keyinfo
.SubKeys
= reply
->subkeys
;
260 keyinfo
.MaxNameLen
= reply
->max_subkey
;
261 keyinfo
.MaxClassLen
= reply
->max_class
;
262 keyinfo
.Values
= reply
->values
;
263 keyinfo
.MaxValueNameLen
= reply
->max_value
;
264 keyinfo
.MaxValueDataLen
= reply
->max_data
;
265 memcpy( info
, &keyinfo
, min( length
, fixed_size
) );
268 case KeyNodeInformation
:
270 KEY_NODE_INFORMATION keyinfo
;
271 fixed_size
= (char *)keyinfo
.Name
- (char *)&keyinfo
;
272 keyinfo
.LastWriteTime
= modif
;
273 keyinfo
.TitleIndex
= 0;
274 if (reply
->namelen
< wine_server_reply_size(reply
))
276 keyinfo
.ClassLength
= wine_server_reply_size(reply
) - reply
->namelen
;
277 keyinfo
.ClassOffset
= fixed_size
+ reply
->namelen
;
281 keyinfo
.ClassLength
= 0;
282 keyinfo
.ClassOffset
= -1;
284 keyinfo
.NameLength
= reply
->namelen
;
285 memcpy( info
, &keyinfo
, min( length
, fixed_size
) );
289 *result_len
= fixed_size
+ reply
->total
;
290 if (length
< *result_len
) ret
= STATUS_BUFFER_OVERFLOW
;
299 /******************************************************************************
300 * NtEnumerateKey [NTDLL.@]
301 * ZwEnumerateKey [NTDLL.@]
304 * the name copied into the buffer is NOT 0-terminated
306 NTSTATUS WINAPI
NtEnumerateKey( HANDLE handle
, ULONG index
, KEY_INFORMATION_CLASS info_class
,
307 void *info
, DWORD length
, DWORD
*result_len
)
309 /* -1 means query key, so avoid it here */
310 if (index
== (ULONG
)-1) return STATUS_NO_MORE_ENTRIES
;
311 return enumerate_key( handle
, index
, info_class
, info
, length
, result_len
);
315 /******************************************************************************
316 * RtlpNtEnumerateSubKey [NTDLL.@]
319 NTSTATUS WINAPI
RtlpNtEnumerateSubKey( HANDLE handle
, UNICODE_STRING
*out
, ULONG index
)
321 KEY_BASIC_INFORMATION
*info
;
322 DWORD dwLen
, dwResultLen
;
327 dwLen
= out
->Length
+ sizeof(KEY_BASIC_INFORMATION
);
328 info
= (KEY_BASIC_INFORMATION
*)RtlAllocateHeap( GetProcessHeap(), 0, dwLen
);
330 return STATUS_NO_MEMORY
;
338 ret
= NtEnumerateKey( handle
, index
, KeyBasicInformation
, info
, dwLen
, &dwResultLen
);
339 dwResultLen
-= sizeof(KEY_BASIC_INFORMATION
);
341 if (ret
== STATUS_BUFFER_OVERFLOW
)
342 out
->Length
= dwResultLen
;
345 if (out
->Length
< info
->NameLength
)
347 out
->Length
= dwResultLen
;
348 ret
= STATUS_BUFFER_OVERFLOW
;
352 out
->Length
= info
->NameLength
;
353 memcpy(out
->Buffer
, info
->Name
, info
->NameLength
);
357 RtlFreeHeap( GetProcessHeap(), 0, info
);
361 /******************************************************************************
362 * NtQueryKey [NTDLL.@]
363 * ZwQueryKey [NTDLL.@]
365 NTSTATUS WINAPI
NtQueryKey( HANDLE handle
, KEY_INFORMATION_CLASS info_class
,
366 void *info
, DWORD length
, DWORD
*result_len
)
368 return enumerate_key( handle
, -1, info_class
, info
, length
, result_len
);
372 /* fill the key value info structure for a specific info class */
373 static void copy_key_value_info( KEY_VALUE_INFORMATION_CLASS info_class
, void *info
,
374 DWORD length
, int type
, int name_len
, int data_len
)
378 case KeyValueBasicInformation
:
380 KEY_VALUE_BASIC_INFORMATION keyinfo
;
381 keyinfo
.TitleIndex
= 0;
383 keyinfo
.NameLength
= name_len
;
384 length
= min( length
, (char *)keyinfo
.Name
- (char *)&keyinfo
);
385 memcpy( info
, &keyinfo
, length
);
388 case KeyValueFullInformation
:
390 KEY_VALUE_FULL_INFORMATION keyinfo
;
391 keyinfo
.TitleIndex
= 0;
393 keyinfo
.DataOffset
= (char *)keyinfo
.Name
- (char *)&keyinfo
+ name_len
;
394 keyinfo
.DataLength
= data_len
;
395 keyinfo
.NameLength
= name_len
;
396 length
= min( length
, (char *)keyinfo
.Name
- (char *)&keyinfo
);
397 memcpy( info
, &keyinfo
, length
);
400 case KeyValuePartialInformation
:
402 KEY_VALUE_PARTIAL_INFORMATION keyinfo
;
403 keyinfo
.TitleIndex
= 0;
405 keyinfo
.DataLength
= data_len
;
406 length
= min( length
, (char *)keyinfo
.Data
- (char *)&keyinfo
);
407 memcpy( info
, &keyinfo
, length
);
416 /******************************************************************************
417 * NtEnumerateValueKey [NTDLL.@]
418 * ZwEnumerateValueKey [NTDLL.@]
420 NTSTATUS WINAPI
NtEnumerateValueKey( HANDLE handle
, ULONG index
,
421 KEY_VALUE_INFORMATION_CLASS info_class
,
422 void *info
, DWORD length
, DWORD
*result_len
)
428 TRACE( "(%p,%u,%d,%p,%d)\n", handle
, index
, info_class
, info
, length
);
430 /* compute the length we want to retrieve */
433 case KeyValueBasicInformation
: ptr
= ((KEY_VALUE_BASIC_INFORMATION
*)info
)->Name
; break;
434 case KeyValueFullInformation
: ptr
= ((KEY_VALUE_FULL_INFORMATION
*)info
)->Name
; break;
435 case KeyValuePartialInformation
: ptr
= ((KEY_VALUE_PARTIAL_INFORMATION
*)info
)->Data
; break;
437 FIXME( "Information class %d not implemented\n", info_class
);
438 return STATUS_INVALID_PARAMETER
;
440 fixed_size
= (char *)ptr
- (char *)info
;
442 SERVER_START_REQ( enum_key_value
)
446 req
->info_class
= info_class
;
447 if (length
> fixed_size
) wine_server_set_reply( req
, ptr
, length
- fixed_size
);
448 if (!(ret
= wine_server_call( req
)))
450 copy_key_value_info( info_class
, info
, length
, reply
->type
, reply
->namelen
,
451 wine_server_reply_size(reply
) - reply
->namelen
);
452 *result_len
= fixed_size
+ reply
->total
;
453 if (length
< *result_len
) ret
= STATUS_BUFFER_OVERFLOW
;
461 /******************************************************************************
462 * NtQueryValueKey [NTDLL.@]
463 * ZwQueryValueKey [NTDLL.@]
466 * the name in the KeyValueInformation is never set
468 NTSTATUS WINAPI
NtQueryValueKey( HANDLE handle
, const UNICODE_STRING
*name
,
469 KEY_VALUE_INFORMATION_CLASS info_class
,
470 void *info
, DWORD length
, DWORD
*result_len
)
474 unsigned int fixed_size
= 0;
476 TRACE( "(%p,%s,%d,%p,%d)\n", handle
, debugstr_us(name
), info_class
, info
, length
);
478 if (name
->Length
> MAX_NAME_LENGTH
) return STATUS_BUFFER_OVERFLOW
;
480 /* compute the length we want to retrieve */
483 case KeyValueBasicInformation
:
484 fixed_size
= (char *)((KEY_VALUE_BASIC_INFORMATION
*)info
)->Name
- (char *)info
;
487 case KeyValueFullInformation
:
488 data_ptr
= (UCHAR
*)((KEY_VALUE_FULL_INFORMATION
*)info
)->Name
;
489 fixed_size
= (char *)data_ptr
- (char *)info
;
491 case KeyValuePartialInformation
:
492 data_ptr
= ((KEY_VALUE_PARTIAL_INFORMATION
*)info
)->Data
;
493 fixed_size
= (char *)data_ptr
- (char *)info
;
496 FIXME( "Information class %d not implemented\n", info_class
);
497 return STATUS_INVALID_PARAMETER
;
500 SERVER_START_REQ( get_key_value
)
503 wine_server_add_data( req
, name
->Buffer
, name
->Length
);
504 if (length
> fixed_size
) wine_server_set_reply( req
, data_ptr
, length
- fixed_size
);
505 if (!(ret
= wine_server_call( req
)))
507 copy_key_value_info( info_class
, info
, length
, reply
->type
,
508 0, wine_server_reply_size(reply
) );
509 *result_len
= fixed_size
+ reply
->total
;
510 if (length
< *result_len
) ret
= STATUS_BUFFER_OVERFLOW
;
517 /******************************************************************************
518 * RtlpNtQueryValueKey [NTDLL.@]
521 NTSTATUS WINAPI
RtlpNtQueryValueKey( HANDLE handle
, ULONG
*result_type
, PBYTE dest
,
524 KEY_VALUE_PARTIAL_INFORMATION
*info
;
528 DWORD dwLen
= sizeof (KEY_VALUE_PARTIAL_INFORMATION
) + (result_len
? *result_len
: 0);
530 info
= (KEY_VALUE_PARTIAL_INFORMATION
*)RtlAllocateHeap( GetProcessHeap(), 0, dwLen
);
532 return STATUS_NO_MEMORY
;
535 ret
= NtQueryValueKey( handle
, &name
, KeyValuePartialInformation
, info
, dwLen
, &dwResultLen
);
537 if (!ret
|| ret
== STATUS_BUFFER_OVERFLOW
)
540 *result_len
= info
->DataLength
;
543 *result_type
= info
->Type
;
545 if (ret
!= STATUS_BUFFER_OVERFLOW
)
546 memcpy( dest
, info
->Data
, info
->DataLength
);
549 RtlFreeHeap( GetProcessHeap(), 0, info
);
553 /******************************************************************************
554 * NtFlushKey [NTDLL.@]
555 * ZwFlushKey [NTDLL.@]
557 NTSTATUS WINAPI
NtFlushKey(HANDLE key
)
561 TRACE("key=%p\n", key
);
563 SERVER_START_REQ( flush_key
)
566 ret
= wine_server_call( req
);
573 /******************************************************************************
574 * NtLoadKey [NTDLL.@]
575 * ZwLoadKey [NTDLL.@]
577 NTSTATUS WINAPI
NtLoadKey( const OBJECT_ATTRIBUTES
*attr
, OBJECT_ATTRIBUTES
*file
)
583 TRACE("(%p,%p)\n", attr
, file
);
585 ret
= NtCreateFile(&hive
, GENERIC_READ
, file
, &io
, NULL
, FILE_ATTRIBUTE_NORMAL
, 0,
586 FILE_OPEN
, 0, NULL
, 0);
589 SERVER_START_REQ( load_registry
)
591 req
->hkey
= attr
->RootDirectory
;
593 wine_server_add_data(req
, attr
->ObjectName
->Buffer
, attr
->ObjectName
->Length
);
594 ret
= wine_server_call( req
);
603 /******************************************************************************
604 * NtNotifyChangeKey [NTDLL.@]
605 * ZwNotifyChangeKey [NTDLL.@]
607 NTSTATUS WINAPI
NtNotifyChangeKey(
610 IN PIO_APC_ROUTINE ApcRoutine OPTIONAL
,
611 IN PVOID ApcContext OPTIONAL
,
612 OUT PIO_STATUS_BLOCK IoStatusBlock
,
613 IN ULONG CompletionFilter
,
614 IN BOOLEAN Asynchronous
,
615 OUT PVOID ChangeBuffer
,
617 IN BOOLEAN WatchSubtree
)
621 TRACE("(%p,%p,%p,%p,%p,0x%08x, 0x%08x,%p,0x%08x,0x%08x)\n",
622 KeyHandle
, Event
, ApcRoutine
, ApcContext
, IoStatusBlock
, CompletionFilter
,
623 Asynchronous
, ChangeBuffer
, Length
, WatchSubtree
);
625 if (ApcRoutine
|| ApcContext
|| ChangeBuffer
|| Length
)
626 FIXME("Unimplemented optional parameter\n");
630 OBJECT_ATTRIBUTES attr
;
631 InitializeObjectAttributes( &attr
, NULL
, 0, NULL
, NULL
);
632 ret
= NtCreateEvent( &Event
, EVENT_ALL_ACCESS
, &attr
, FALSE
, FALSE
);
633 if (ret
!= STATUS_SUCCESS
)
637 SERVER_START_REQ( set_registry_notification
)
639 req
->hkey
= KeyHandle
;
641 req
->subtree
= WatchSubtree
;
642 req
->filter
= CompletionFilter
;
643 ret
= wine_server_call( req
);
649 if (ret
== STATUS_SUCCESS
)
650 NtWaitForSingleObject( Event
, FALSE
, NULL
);
654 return STATUS_SUCCESS
;
657 /******************************************************************************
658 * NtQueryMultipleValueKey [NTDLL]
659 * ZwQueryMultipleValueKey
662 NTSTATUS WINAPI
NtQueryMultipleValueKey(
664 PKEY_MULTIPLE_VALUE_INFORMATION ListOfValuesToQuery
,
666 PVOID MultipleValueInformation
,
670 FIXME("(%p,%p,0x%08x,%p,0x%08x,%p) stub!\n",
671 KeyHandle
, ListOfValuesToQuery
, NumberOfItems
, MultipleValueInformation
,
672 Length
,ReturnLength
);
673 return STATUS_SUCCESS
;
676 /******************************************************************************
677 * NtReplaceKey [NTDLL.@]
678 * ZwReplaceKey [NTDLL.@]
680 NTSTATUS WINAPI
NtReplaceKey(
681 IN POBJECT_ATTRIBUTES ObjectAttributes
,
683 IN POBJECT_ATTRIBUTES ReplacedObjectAttributes
)
685 FIXME("(%p),stub!\n", Key
);
686 dump_ObjectAttributes(ObjectAttributes
);
687 dump_ObjectAttributes(ReplacedObjectAttributes
);
688 return STATUS_SUCCESS
;
690 /******************************************************************************
691 * NtRestoreKey [NTDLL.@]
692 * ZwRestoreKey [NTDLL.@]
694 NTSTATUS WINAPI
NtRestoreKey(
699 FIXME("(%p,%p,0x%08x) stub\n",
700 KeyHandle
, FileHandle
, RestoreFlags
);
701 return STATUS_SUCCESS
;
703 /******************************************************************************
704 * NtSaveKey [NTDLL.@]
705 * ZwSaveKey [NTDLL.@]
707 NTSTATUS WINAPI
NtSaveKey(IN HANDLE KeyHandle
, IN HANDLE FileHandle
)
711 TRACE("(%p,%p)\n", KeyHandle
, FileHandle
);
713 SERVER_START_REQ( save_registry
)
715 req
->hkey
= KeyHandle
;
716 req
->file
= FileHandle
;
717 ret
= wine_server_call( req
);
723 /******************************************************************************
724 * NtSetInformationKey [NTDLL.@]
725 * ZwSetInformationKey [NTDLL.@]
727 NTSTATUS WINAPI
NtSetInformationKey(
729 IN
const int KeyInformationClass
,
730 IN PVOID KeyInformation
,
731 IN ULONG KeyInformationLength
)
733 FIXME("(%p,0x%08x,%p,0x%08x) stub\n",
734 KeyHandle
, KeyInformationClass
, KeyInformation
, KeyInformationLength
);
735 return STATUS_SUCCESS
;
739 /******************************************************************************
740 * NtSetValueKey [NTDLL.@]
741 * ZwSetValueKey [NTDLL.@]
744 * win95 does not care about count for REG_SZ and finds out the len by itself (js)
745 * NT does definitely care (aj)
747 NTSTATUS WINAPI
NtSetValueKey( HANDLE hkey
, const UNICODE_STRING
*name
, ULONG TitleIndex
,
748 ULONG type
, const void *data
, ULONG count
)
752 TRACE( "(%p,%s,%d,%p,%d)\n", hkey
, debugstr_us(name
), type
, data
, count
);
754 if (name
->Length
> MAX_NAME_LENGTH
) return STATUS_BUFFER_OVERFLOW
;
756 SERVER_START_REQ( set_key_value
)
760 req
->namelen
= name
->Length
;
761 wine_server_add_data( req
, name
->Buffer
, name
->Length
);
762 wine_server_add_data( req
, data
, count
);
763 ret
= wine_server_call( req
);
769 /******************************************************************************
770 * RtlpNtSetValueKey [NTDLL.@]
773 NTSTATUS WINAPI
RtlpNtSetValueKey( HANDLE hkey
, ULONG type
, const void *data
,
779 return NtSetValueKey( hkey
, &name
, 0, type
, data
, count
);
782 /******************************************************************************
783 * NtUnloadKey [NTDLL.@]
784 * ZwUnloadKey [NTDLL.@]
786 NTSTATUS WINAPI
NtUnloadKey(IN POBJECT_ATTRIBUTES attr
)
790 TRACE("(%p)\n", attr
);
792 SERVER_START_REQ( unload_registry
)
794 req
->hkey
= attr
->RootDirectory
;
795 ret
= wine_server_call(req
);
802 /******************************************************************************
803 * RtlFormatCurrentUserKeyPath [NTDLL.@]
806 NTSTATUS WINAPI
RtlFormatCurrentUserKeyPath( IN OUT PUNICODE_STRING KeyPath
)
808 static const WCHAR pathW
[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\'};
812 status
= NtOpenThreadToken(GetCurrentThread(), TOKEN_READ
, TRUE
, &token
);
813 if (status
== STATUS_NO_TOKEN
)
814 status
= NtOpenProcessToken(GetCurrentProcess(), TOKEN_READ
, &token
);
815 if (status
== STATUS_SUCCESS
)
817 char buffer
[sizeof(TOKEN_USER
) + sizeof(SID
) + sizeof(DWORD
)*SID_MAX_SUB_AUTHORITIES
];
818 DWORD len
= sizeof(buffer
);
820 status
= NtQueryInformationToken(token
, TokenUser
, buffer
, len
, &len
);
821 if (status
== STATUS_SUCCESS
)
823 KeyPath
->MaximumLength
= 0;
824 status
= RtlConvertSidToUnicodeString(KeyPath
, ((TOKEN_USER
*)buffer
)->User
.Sid
, FALSE
);
825 if (status
== STATUS_BUFFER_OVERFLOW
)
827 PWCHAR buf
= RtlAllocateHeap(GetProcessHeap(), 0,
828 sizeof(pathW
) + KeyPath
->Length
+ sizeof(WCHAR
));
831 memcpy(buf
, pathW
, sizeof(pathW
));
832 KeyPath
->MaximumLength
= KeyPath
->Length
+ sizeof(WCHAR
);
833 KeyPath
->Buffer
= (PWCHAR
)((LPBYTE
)buf
+ sizeof(pathW
));
834 status
= RtlConvertSidToUnicodeString(KeyPath
,
835 ((TOKEN_USER
*)buffer
)->User
.Sid
, FALSE
);
836 KeyPath
->Buffer
= (PWCHAR
)buf
;
837 KeyPath
->Length
+= sizeof(pathW
);
838 KeyPath
->MaximumLength
+= sizeof(pathW
);
841 status
= STATUS_NO_MEMORY
;
849 /******************************************************************************
850 * RtlOpenCurrentUser [NTDLL.@]
853 * If we return just HKEY_CURRENT_USER the advapi tries to find a remote
854 * registry (odd handle) and fails.
856 NTSTATUS WINAPI
RtlOpenCurrentUser(
857 IN ACCESS_MASK DesiredAccess
, /* [in] */
858 OUT PHANDLE KeyHandle
) /* [out] handle of HKEY_CURRENT_USER */
860 OBJECT_ATTRIBUTES ObjectAttributes
;
861 UNICODE_STRING ObjectName
;
864 TRACE("(0x%08x, %p)\n",DesiredAccess
, KeyHandle
);
866 if ((ret
= RtlFormatCurrentUserKeyPath(&ObjectName
))) return ret
;
867 InitializeObjectAttributes(&ObjectAttributes
,&ObjectName
,OBJ_CASE_INSENSITIVE
,0, NULL
);
868 ret
= NtCreateKey(KeyHandle
, DesiredAccess
, &ObjectAttributes
, 0, NULL
, 0, NULL
);
869 RtlFreeUnicodeString(&ObjectName
);
874 static NTSTATUS
RTL_ReportRegistryValue(PKEY_VALUE_FULL_INFORMATION pInfo
,
875 PRTL_QUERY_REGISTRY_TABLE pQuery
, PVOID pContext
, PVOID pEnvironment
)
878 UNICODE_STRING src
, dst
;
883 NTSTATUS status
= STATUS_SUCCESS
;
890 if (pQuery
->Flags
& RTL_QUERY_REGISTRY_DIRECT
)
891 return STATUS_INVALID_PARAMETER
;
894 status
= pQuery
->QueryRoutine(pQuery
->Name
, pQuery
->DefaultType
, pQuery
->DefaultData
,
895 pQuery
->DefaultLength
, pContext
, pQuery
->EntryContext
);
899 len
= pInfo
->DataLength
;
901 if (pQuery
->Flags
& RTL_QUERY_REGISTRY_DIRECT
)
903 str
= (PUNICODE_STRING
)pQuery
->EntryContext
;
908 if (!(pQuery
->Flags
& RTL_QUERY_REGISTRY_NOEXPAND
))
910 RtlInitUnicodeString(&src
, (WCHAR
*)(((CHAR
*)pInfo
) + pInfo
->DataOffset
));
912 dst
.MaximumLength
= 0;
913 RtlExpandEnvironmentStrings_U(pEnvironment
, &src
, &dst
, &res
);
915 dst
.MaximumLength
= res
;
916 dst
.Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, res
* sizeof(WCHAR
));
917 RtlExpandEnvironmentStrings_U(pEnvironment
, &src
, &dst
, &res
);
918 status
= pQuery
->QueryRoutine(pQuery
->Name
, pInfo
->Type
, dst
.Buffer
,
919 dst
.Length
, pContext
, pQuery
->EntryContext
);
920 RtlFreeHeap(GetProcessHeap(), 0, dst
.Buffer
);
925 if (str
->Buffer
== NULL
)
926 RtlCreateUnicodeString(str
, (WCHAR
*)(((CHAR
*)pInfo
) + pInfo
->DataOffset
));
928 RtlAppendUnicodeToString(str
, (WCHAR
*)(((CHAR
*)pInfo
) + pInfo
->DataOffset
));
932 if (!(pQuery
->Flags
& RTL_QUERY_REGISTRY_NOEXPAND
))
933 return STATUS_INVALID_PARAMETER
;
935 if (str
->Buffer
== NULL
)
937 str
->Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, len
);
938 str
->MaximumLength
= len
;
940 len
= min(len
, str
->MaximumLength
);
941 memcpy(str
->Buffer
, ((CHAR
*)pInfo
) + pInfo
->DataOffset
, len
);
946 bin
= (LONG
*)pQuery
->EntryContext
;
947 if (pInfo
->DataLength
<= sizeof(ULONG
))
948 memcpy(bin
, ((CHAR
*)pInfo
) + pInfo
->DataOffset
,
952 if (bin
[0] <= sizeof(ULONG
))
954 memcpy(&bin
[1], ((CHAR
*)pInfo
) + pInfo
->DataOffset
,
955 min(-bin
[0], pInfo
->DataLength
));
959 len
= min(bin
[0], pInfo
->DataLength
);
961 bin
[2] = pInfo
->Type
;
962 memcpy(&bin
[3], ((CHAR
*)pInfo
) + pInfo
->DataOffset
, len
);
970 if((pQuery
->Flags
& RTL_QUERY_REGISTRY_NOEXPAND
) ||
971 (pInfo
->Type
!= REG_EXPAND_SZ
&& pInfo
->Type
!= REG_MULTI_SZ
))
973 status
= pQuery
->QueryRoutine(pInfo
->Name
, pInfo
->Type
,
974 ((CHAR
*)pInfo
) + pInfo
->DataOffset
, pInfo
->DataLength
,
975 pContext
, pQuery
->EntryContext
);
977 else if (pInfo
->Type
== REG_EXPAND_SZ
)
979 RtlInitUnicodeString(&src
, (WCHAR
*)(((CHAR
*)pInfo
) + pInfo
->DataOffset
));
981 dst
.MaximumLength
= 0;
982 RtlExpandEnvironmentStrings_U(pEnvironment
, &src
, &dst
, &res
);
984 dst
.MaximumLength
= res
;
985 dst
.Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, res
* sizeof(WCHAR
));
986 RtlExpandEnvironmentStrings_U(pEnvironment
, &src
, &dst
, &res
);
987 status
= pQuery
->QueryRoutine(pQuery
->Name
, pInfo
->Type
, dst
.Buffer
,
988 dst
.Length
, pContext
, pQuery
->EntryContext
);
989 RtlFreeHeap(GetProcessHeap(), 0, dst
.Buffer
);
991 else /* REG_MULTI_SZ */
993 if(pQuery
->Flags
& RTL_QUERY_REGISTRY_NOEXPAND
)
995 for (offset
= 0; offset
<= pInfo
->DataLength
; offset
+= len
+ sizeof(WCHAR
))
997 wstr
= (WCHAR
*)(((CHAR
*)pInfo
) + offset
);
998 len
= strlenW(wstr
) * sizeof(WCHAR
);
999 status
= pQuery
->QueryRoutine(pQuery
->Name
, pInfo
->Type
, wstr
, len
,
1000 pContext
, pQuery
->EntryContext
);
1001 if(status
!= STATUS_SUCCESS
&& status
!= STATUS_BUFFER_TOO_SMALL
)
1007 while(count
<=pInfo
->DataLength
)
1009 String
= (WCHAR
*)(((CHAR
*)pInfo
) + pInfo
->DataOffset
)+count
;
1010 count
+=strlenW(String
)+1;
1011 RtlInitUnicodeString(&src
, (WCHAR
*)(((CHAR
*)pInfo
) + pInfo
->DataOffset
));
1013 dst
.MaximumLength
= 0;
1014 RtlExpandEnvironmentStrings_U(pEnvironment
, &src
, &dst
, &res
);
1016 dst
.MaximumLength
= res
;
1017 dst
.Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, res
* sizeof(WCHAR
));
1018 RtlExpandEnvironmentStrings_U(pEnvironment
, &src
, &dst
, &res
);
1019 status
= pQuery
->QueryRoutine(pQuery
->Name
, pInfo
->Type
, dst
.Buffer
,
1020 dst
.Length
, pContext
, pQuery
->EntryContext
);
1021 RtlFreeHeap(GetProcessHeap(), 0, dst
.Buffer
);
1022 if(status
!= STATUS_SUCCESS
&& status
!= STATUS_BUFFER_TOO_SMALL
)
1032 static NTSTATUS
RTL_GetKeyHandle(ULONG RelativeTo
, PCWSTR Path
, PHANDLE handle
)
1034 UNICODE_STRING KeyString
;
1035 OBJECT_ATTRIBUTES regkey
;
1040 static const WCHAR empty
[] = {0};
1041 static const WCHAR control
[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e',
1042 '\\','S','y','s','t','e','m','\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1043 'C','o','n','t','r','o','l','\\',0};
1045 static const WCHAR devicemap
[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1046 'H','a','r','d','w','a','r','e','\\','D','e','v','i','c','e','M','a','p','\\',0};
1048 static const WCHAR services
[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1049 'S','y','s','t','e','m','\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1050 'S','e','r','v','i','c','e','s','\\',0};
1052 static const WCHAR user
[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\',
1053 'C','u','r','r','e','n','t','U','s','e','r','\\',0};
1055 static const WCHAR windows_nt
[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1056 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1057 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',0};
1059 switch (RelativeTo
& 0xff)
1061 case RTL_REGISTRY_ABSOLUTE
:
1065 case RTL_REGISTRY_CONTROL
:
1069 case RTL_REGISTRY_DEVICEMAP
:
1073 case RTL_REGISTRY_SERVICES
:
1077 case RTL_REGISTRY_USER
:
1081 case RTL_REGISTRY_WINDOWS_NT
:
1086 return STATUS_INVALID_PARAMETER
;
1089 len
= (strlenW(base
) + strlenW(Path
) + 1) * sizeof(WCHAR
);
1090 KeyString
.Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, len
);
1091 if (KeyString
.Buffer
== NULL
)
1092 return STATUS_NO_MEMORY
;
1094 strcpyW(KeyString
.Buffer
, base
);
1095 strcatW(KeyString
.Buffer
, Path
);
1096 KeyString
.Length
= len
- sizeof(WCHAR
);
1097 KeyString
.MaximumLength
= len
;
1098 InitializeObjectAttributes(®key
, &KeyString
, OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
1099 status
= NtOpenKey(handle
, KEY_ALL_ACCESS
, ®key
);
1100 RtlFreeHeap(GetProcessHeap(), 0, KeyString
.Buffer
);
1104 /*************************************************************************
1105 * RtlQueryRegistryValues [NTDLL.@]
1107 * Query multiple registry values with a signle call.
1110 * RelativeTo [I] Registry path that Path refers to
1111 * Path [I] Path to key
1112 * QueryTable [I] Table of key values to query
1113 * Context [I] Paremeter to pass to the application defined QueryRoutine function
1114 * Environment [I] Optional parameter to use when performing expantion
1117 * STATUS_SUCCESS or an appropriate NTSTATUS error code.
1119 NTSTATUS WINAPI
RtlQueryRegistryValues(IN ULONG RelativeTo
, IN PCWSTR Path
,
1120 IN PRTL_QUERY_REGISTRY_TABLE QueryTable
, IN PVOID Context
,
1121 IN PVOID Environment OPTIONAL
)
1123 UNICODE_STRING Value
;
1124 HANDLE handle
, topkey
;
1125 PKEY_VALUE_FULL_INFORMATION pInfo
= NULL
;
1126 ULONG len
, buflen
= 0;
1127 NTSTATUS status
=STATUS_SUCCESS
, ret
= STATUS_SUCCESS
;
1130 TRACE("(%d, %s, %p, %p, %p)\n", RelativeTo
, debugstr_w(Path
), QueryTable
, Context
, Environment
);
1133 return STATUS_INVALID_PARAMETER
;
1135 /* get a valid handle */
1136 if (RelativeTo
& RTL_REGISTRY_HANDLE
)
1137 topkey
= handle
= (HANDLE
)Path
;
1140 status
= RTL_GetKeyHandle(RelativeTo
, Path
, &topkey
);
1143 if(status
!= STATUS_SUCCESS
)
1146 /* Process query table entries */
1147 for (; QueryTable
->QueryRoutine
!= NULL
|| QueryTable
->Name
!= NULL
; ++QueryTable
)
1149 if (QueryTable
->Flags
&
1150 (RTL_QUERY_REGISTRY_SUBKEY
| RTL_QUERY_REGISTRY_TOPKEY
))
1152 /* topkey must be kept open just in case we will reuse it later */
1153 if (handle
!= topkey
)
1156 if (QueryTable
->Flags
& RTL_QUERY_REGISTRY_SUBKEY
)
1159 status
= RTL_GetKeyHandle(PtrToUlong(QueryTable
->Name
), Path
, &handle
);
1160 if(status
!= STATUS_SUCCESS
)
1170 if (QueryTable
->Flags
& RTL_QUERY_REGISTRY_NOVALUE
)
1172 QueryTable
->QueryRoutine(QueryTable
->Name
, REG_NONE
, NULL
, 0,
1173 Context
, QueryTable
->EntryContext
);
1179 if (QueryTable
->Flags
& RTL_QUERY_REGISTRY_REQUIRED
)
1181 ret
= STATUS_OBJECT_NAME_NOT_FOUND
;
1187 if (QueryTable
->Name
== NULL
)
1189 if (QueryTable
->Flags
& RTL_QUERY_REGISTRY_DIRECT
)
1191 ret
= STATUS_INVALID_PARAMETER
;
1195 /* Report all subkeys */
1198 status
= NtEnumerateValueKey(handle
, i
,
1199 KeyValueFullInformation
, pInfo
, buflen
, &len
);
1200 if (status
== STATUS_NO_MORE_ENTRIES
)
1202 if (status
== STATUS_BUFFER_OVERFLOW
||
1203 status
== STATUS_BUFFER_TOO_SMALL
)
1206 RtlFreeHeap(GetProcessHeap(), 0, pInfo
);
1207 pInfo
= (KEY_VALUE_FULL_INFORMATION
*)RtlAllocateHeap(
1208 GetProcessHeap(), 0, buflen
);
1209 NtEnumerateValueKey(handle
, i
, KeyValueFullInformation
,
1210 pInfo
, buflen
, &len
);
1213 status
= RTL_ReportRegistryValue(pInfo
, QueryTable
, Context
, Environment
);
1214 if(status
!= STATUS_SUCCESS
&& status
!= STATUS_BUFFER_TOO_SMALL
)
1219 if (QueryTable
->Flags
& RTL_QUERY_REGISTRY_DELETE
)
1221 RtlInitUnicodeString(&Value
, pInfo
->Name
);
1222 NtDeleteValueKey(handle
, &Value
);
1226 if (i
== 0 && (QueryTable
->Flags
& RTL_QUERY_REGISTRY_REQUIRED
))
1228 ret
= STATUS_OBJECT_NAME_NOT_FOUND
;
1234 RtlInitUnicodeString(&Value
, QueryTable
->Name
);
1235 status
= NtQueryValueKey(handle
, &Value
, KeyValueFullInformation
,
1236 pInfo
, buflen
, &len
);
1237 if (status
== STATUS_BUFFER_OVERFLOW
||
1238 status
== STATUS_BUFFER_TOO_SMALL
)
1241 RtlFreeHeap(GetProcessHeap(), 0, pInfo
);
1242 pInfo
= (KEY_VALUE_FULL_INFORMATION
*)RtlAllocateHeap(
1243 GetProcessHeap(), 0, buflen
);
1244 status
= NtQueryValueKey(handle
, &Value
,
1245 KeyValueFullInformation
, pInfo
, buflen
, &len
);
1247 if (status
!= STATUS_SUCCESS
)
1249 if (QueryTable
->Flags
& RTL_QUERY_REGISTRY_REQUIRED
)
1251 ret
= STATUS_OBJECT_NAME_NOT_FOUND
;
1254 status
= RTL_ReportRegistryValue(NULL
, QueryTable
, Context
, Environment
);
1255 if(status
!= STATUS_SUCCESS
&& status
!= STATUS_BUFFER_TOO_SMALL
)
1263 status
= RTL_ReportRegistryValue(pInfo
, QueryTable
, Context
, Environment
);
1264 if(status
!= STATUS_SUCCESS
&& status
!= STATUS_BUFFER_TOO_SMALL
)
1269 if (QueryTable
->Flags
& RTL_QUERY_REGISTRY_DELETE
)
1270 NtDeleteValueKey(handle
, &Value
);
1276 RtlFreeHeap(GetProcessHeap(), 0, pInfo
);
1277 if (handle
!= topkey
)
1283 /*************************************************************************
1284 * RtlCheckRegistryKey [NTDLL.@]
1286 * Query multiple registry values with a signle call.
1289 * RelativeTo [I] Registry path that Path refers to
1290 * Path [I] Path to key
1293 * STATUS_SUCCESS if the specified key exists, or an NTSTATUS error code.
1295 NTSTATUS WINAPI
RtlCheckRegistryKey(IN ULONG RelativeTo
, IN PWSTR Path
)
1300 TRACE("(%d, %s)\n", RelativeTo
, debugstr_w(Path
));
1302 if((!RelativeTo
) && Path
== NULL
)
1303 return STATUS_OBJECT_PATH_SYNTAX_BAD
;
1304 if(RelativeTo
& RTL_REGISTRY_HANDLE
)
1305 return STATUS_SUCCESS
;
1307 status
= RTL_GetKeyHandle(RelativeTo
, Path
, &handle
);
1308 if (handle
) NtClose(handle
);
1309 if (status
== STATUS_INVALID_HANDLE
) status
= STATUS_OBJECT_NAME_NOT_FOUND
;
1313 /*************************************************************************
1314 * RtlDeleteRegistryValue [NTDLL.@]
1316 * Query multiple registry values with a signle call.
1319 * RelativeTo [I] Registry path that Path refers to
1320 * Path [I] Path to key
1321 * ValueName [I] Name of the value to delete
1324 * STATUS_SUCCESS if the specified key is successfully deleted, or an NTSTATUS error code.
1326 NTSTATUS WINAPI
RtlDeleteRegistryValue(IN ULONG RelativeTo
, IN PCWSTR Path
, IN PCWSTR ValueName
)
1330 UNICODE_STRING Value
;
1332 TRACE("(%d, %s, %s)\n", RelativeTo
, debugstr_w(Path
), debugstr_w(ValueName
));
1334 RtlInitUnicodeString(&Value
, ValueName
);
1335 if(RelativeTo
== RTL_REGISTRY_HANDLE
)
1337 return NtDeleteValueKey((HANDLE
)Path
, &Value
);
1339 status
= RTL_GetKeyHandle(RelativeTo
, Path
, &handle
);
1340 if (status
) return status
;
1341 status
= NtDeleteValueKey(handle
, &Value
);
1346 /*************************************************************************
1347 * RtlWriteRegistryValue [NTDLL.@]
1349 * Sets the registry value with provided data.
1352 * RelativeTo [I] Registry path that path parameter refers to
1353 * path [I] Path to the key (or handle - see RTL_GetKeyHandle)
1354 * name [I] Name of the registry value to set
1355 * type [I] Type of the registry key to set
1356 * data [I] Pointer to the user data to be set
1357 * length [I] Length of the user data pointed by data
1360 * STATUS_SUCCESS if the specified key is successfully set,
1361 * or an NTSTATUS error code.
1363 NTSTATUS WINAPI
RtlWriteRegistryValue( ULONG RelativeTo
, PCWSTR path
, PCWSTR name
,
1364 ULONG type
, PVOID data
, ULONG length
)
1370 TRACE( "(%d, %s, %s) -> %d: %p [%d]\n", RelativeTo
, debugstr_w(path
), debugstr_w(name
),
1371 type
, data
, length
);
1373 RtlInitUnicodeString( &str
, name
);
1375 if (RelativeTo
== RTL_REGISTRY_HANDLE
)
1376 return NtSetValueKey( (HANDLE
)path
, &str
, 0, type
, data
, length
);
1378 status
= RTL_GetKeyHandle( RelativeTo
, path
, &hkey
);
1379 if (status
!= STATUS_SUCCESS
) return status
;
1381 status
= NtSetValueKey( hkey
, &str
, 0, type
, data
, length
);