Implement NtAccessCheck.
[wine/gsoc-2012-control.git] / dlls / ntdll / reg.c
blob44d0736fd591c1577cee1c778120e7ebf029b49c
1 /*
2 * Registry functions
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
22 * NOTES:
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
29 #include "config.h"
30 #include "wine/port.h"
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <string.h>
36 #include "winerror.h"
37 #include "wine/library.h"
38 #include "winreg.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,
54 PULONG dispos )
56 NTSTATUS ret;
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;
67 req->access = access;
68 req->options = options;
69 req->modif = 0;
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;
79 SERVER_END_REQ;
80 TRACE("<- %p\n", *retkey);
81 return ret;
84 /******************************************************************************
85 * RtlpNtCreateKey [NTDLL.@]
87 * See NtCreateKey.
89 NTSTATUS WINAPI RtlpNtCreateKey( PHKEY retkey, ACCESS_MASK access, OBJECT_ATTRIBUTES *attr,
90 ULONG TitleIndex, const UNICODE_STRING *class, ULONG options,
91 PULONG dispos )
93 if (attr)
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 )
109 NTSTATUS ret;
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;
126 SERVER_END_REQ;
127 TRACE("<- %p\n", *retkey);
128 return ret;
131 /******************************************************************************
132 * RtlpNtOpenKey [NTDLL.@]
134 * See NtOpenKey.
136 NTSTATUS WINAPI RtlpNtOpenKey( PHKEY retkey, ACCESS_MASK access, OBJECT_ATTRIBUTES *attr )
138 if (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 )
149 NTSTATUS ret;
151 TRACE( "(%p)\n", hkey );
153 SERVER_START_REQ( delete_key )
155 req->hkey = hkey;
156 ret = wine_server_call( req );
158 SERVER_END_REQ;
159 return ret;
162 /******************************************************************************
163 * RtlpNtMakeTemporaryKey [NTDLL.@]
165 * See NtDeleteKey.
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 )
178 NTSTATUS ret;
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 )
185 req->hkey = hkey;
186 wine_server_add_data( req, name->Buffer, name->Length );
187 ret = wine_server_call( req );
189 SERVER_END_REQ;
190 return ret;
194 /******************************************************************************
195 * enumerate_key
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 )
203 NTSTATUS ret;
204 void *data_ptr;
205 size_t fixed_size;
207 switch(info_class)
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;
212 default:
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 )
220 req->hkey = handle;
221 req->index = index;
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 )))
226 LARGE_INTEGER modif;
228 RtlSecondsSince1970ToTime( reply->modif, &modif );
230 switch(info_class)
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 ) );
241 break;
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 ) );
258 break;
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 ) );
270 break;
272 *result_len = fixed_size + reply->total;
273 if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
276 SERVER_END_REQ;
277 return ret;
282 /******************************************************************************
283 * NtEnumerateKey [NTDLL.@]
284 * ZwEnumerateKey [NTDLL.@]
286 * NOTES
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;
306 NTSTATUS ret;
308 if (out->Length)
310 dwLen = out->Length + sizeof(KEY_BASIC_INFORMATION);
311 info = (KEY_BASIC_INFORMATION*)RtlAllocateHeap( GetProcessHeap(), 0, dwLen );
312 if (!info)
313 return STATUS_NO_MEMORY;
315 else
317 dwLen = 0;
318 info = NULL;
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;
326 else if (!ret)
328 if (out->Length < info->NameLength)
330 out->Length = dwResultLen;
331 ret = STATUS_BUFFER_OVERFLOW;
333 else
335 out->Length = info->NameLength;
336 memcpy(out->Buffer, info->Name, info->NameLength);
340 if (info)
341 RtlFreeHeap( GetProcessHeap(), 0, info );
342 return ret;
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 )
360 switch(info_class)
362 case KeyValueBasicInformation:
364 KEY_VALUE_BASIC_INFORMATION keyinfo;
365 keyinfo.TitleIndex = 0;
366 keyinfo.Type = type;
367 keyinfo.NameLength = name_len;
368 length = min( length, (char *)keyinfo.Name - (char *)&keyinfo );
369 memcpy( info, &keyinfo, length );
370 break;
372 case KeyValueFullInformation:
374 KEY_VALUE_FULL_INFORMATION keyinfo;
375 keyinfo.TitleIndex = 0;
376 keyinfo.Type = type;
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 );
382 break;
384 case KeyValuePartialInformation:
386 KEY_VALUE_PARTIAL_INFORMATION keyinfo;
387 keyinfo.TitleIndex = 0;
388 keyinfo.Type = type;
389 keyinfo.DataLength = data_len;
390 length = min( length, (char *)keyinfo.Data - (char *)&keyinfo );
391 memcpy( info, &keyinfo, length );
392 break;
394 default:
395 break;
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 )
408 NTSTATUS ret;
409 void *ptr;
410 size_t fixed_size;
412 TRACE( "(%p,%lu,%d,%p,%ld)\n", handle, index, info_class, info, length );
414 /* compute the length we want to retrieve */
415 switch(info_class)
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;
420 default:
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 )
428 req->hkey = handle;
429 req->index = index;
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;
440 SERVER_END_REQ;
441 return ret;
445 /******************************************************************************
446 * NtQueryValueKey [NTDLL.@]
447 * ZwQueryValueKey [NTDLL.@]
449 * NOTES
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 )
456 NTSTATUS ret;
457 UCHAR *data_ptr;
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 */
465 switch(info_class)
467 case KeyValueBasicInformation:
468 fixed_size = (char *)((KEY_VALUE_BASIC_INFORMATION *)info)->Name - (char *)info;
469 data_ptr = NULL;
470 break;
471 case KeyValueFullInformation:
472 data_ptr = (UCHAR *)((KEY_VALUE_FULL_INFORMATION *)info)->Name;
473 fixed_size = (char *)data_ptr - (char *)info;
474 break;
475 case KeyValuePartialInformation:
476 data_ptr = ((KEY_VALUE_PARTIAL_INFORMATION *)info)->Data;
477 fixed_size = (char *)data_ptr - (char *)info;
478 break;
479 default:
480 FIXME( "Information class %d not implemented\n", info_class );
481 return STATUS_INVALID_PARAMETER;
484 SERVER_START_REQ( get_key_value )
486 req->hkey = handle;
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;
497 SERVER_END_REQ;
498 return ret;
501 /******************************************************************************
502 * RtlpNtQueryValueKey [NTDLL.@]
505 NTSTATUS WINAPI RtlpNtQueryValueKey( HKEY handle, ULONG *result_type, PBYTE dest,
506 DWORD *result_len )
508 KEY_VALUE_PARTIAL_INFORMATION *info;
509 UNICODE_STRING name;
510 NTSTATUS ret;
511 DWORD dwResultLen;
512 DWORD dwLen = sizeof (KEY_VALUE_PARTIAL_INFORMATION) + result_len ? *result_len : 0;
514 info = (KEY_VALUE_PARTIAL_INFORMATION*)RtlAllocateHeap( GetProcessHeap(), 0, dwLen );
515 if (!info)
516 return STATUS_NO_MEMORY;
518 name.Length = 0;
519 ret = NtQueryValueKey( handle, &name, KeyValuePartialInformation, info, dwLen, &dwResultLen );
521 if (!ret || ret == STATUS_BUFFER_OVERFLOW)
523 if (result_len)
524 *result_len = info->DataLength;
526 if (result_type)
527 *result_type = info->Type;
529 if (ret != STATUS_BUFFER_OVERFLOW)
530 memcpy( dest, info->Data, info->DataLength );
533 RtlFreeHeap( GetProcessHeap(), 0, info );
534 return ret;
537 /******************************************************************************
538 * NtFlushKey [NTDLL.@]
539 * ZwFlushKey [NTDLL.@]
541 NTSTATUS WINAPI NtFlushKey(HKEY key)
543 NTSTATUS ret;
545 TRACE("key=%p\n", key);
547 SERVER_START_REQ( flush_key )
549 req->hkey = key;
550 ret = wine_server_call( req );
552 SERVER_END_REQ;
554 return ret;
557 /******************************************************************************
558 * NtLoadKey [NTDLL.@]
559 * ZwLoadKey [NTDLL.@]
561 NTSTATUS WINAPI NtLoadKey( const OBJECT_ATTRIBUTES *attr, OBJECT_ATTRIBUTES *file )
563 NTSTATUS ret;
564 HANDLE hive;
565 IO_STATUS_BLOCK io;
567 TRACE("(%p,%p)\n", attr, file);
569 ret = NtCreateFile(&hive, GENERIC_READ, file, &io, NULL, FILE_ATTRIBUTE_NORMAL, 0,
570 OPEN_EXISTING, 0, NULL, 0);
571 if (ret) return ret;
573 SERVER_START_REQ( load_registry )
575 req->hkey = attr->RootDirectory;
576 req->file = hive;
577 wine_server_add_data(req, attr->ObjectName->Buffer, attr->ObjectName->Length);
578 ret = wine_server_call( req );
580 SERVER_END_REQ;
582 NtClose(hive);
584 return ret;
587 /******************************************************************************
588 * NtNotifyChangeKey [NTDLL.@]
589 * ZwNotifyChangeKey [NTDLL.@]
591 NTSTATUS WINAPI NtNotifyChangeKey(
592 IN HKEY KeyHandle,
593 IN HANDLE Event,
594 IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
595 IN PVOID ApcContext OPTIONAL,
596 OUT PIO_STATUS_BLOCK IoStatusBlock,
597 IN ULONG CompletionFilter,
598 IN BOOLEAN Asynchronous,
599 OUT PVOID ChangeBuffer,
600 IN ULONG Length,
601 IN BOOLEAN WatchSubtree)
603 NTSTATUS ret;
605 TRACE("(%p,%p,%p,%p,%p,0x%08lx, 0x%08x,%p,0x%08lx,0x%08x)\n",
606 KeyHandle, Event, ApcRoutine, ApcContext, IoStatusBlock, CompletionFilter,
607 Asynchronous, ChangeBuffer, Length, WatchSubtree);
609 if (ApcRoutine || ApcContext || ChangeBuffer || Length)
610 FIXME("Unimplemented optional parameter\n");
612 if (!Asynchronous)
614 OBJECT_ATTRIBUTES attr;
615 InitializeObjectAttributes( &attr, NULL, 0, NULL, NULL );
616 ret = NtCreateEvent( &Event, EVENT_ALL_ACCESS, &attr, FALSE, FALSE );
617 if (ret != STATUS_SUCCESS)
618 return ret;
621 SERVER_START_REQ( set_registry_notification )
623 req->hkey = KeyHandle;
624 req->event = Event;
625 req->subtree = WatchSubtree;
626 req->filter = CompletionFilter;
627 ret = wine_server_call( req );
629 SERVER_END_REQ;
631 if (!Asynchronous)
633 if (ret == STATUS_SUCCESS)
634 NtWaitForSingleObject( Event, FALSE, NULL );
635 NtClose( Event );
638 return STATUS_SUCCESS;
641 /******************************************************************************
642 * NtQueryMultipleValueKey [NTDLL]
643 * ZwQueryMultipleValueKey
646 NTSTATUS WINAPI NtQueryMultipleValueKey(
647 HKEY KeyHandle,
648 PVALENTW ListOfValuesToQuery,
649 ULONG NumberOfItems,
650 PVOID MultipleValueInformation,
651 ULONG Length,
652 PULONG ReturnLength)
654 FIXME("(%p,%p,0x%08lx,%p,0x%08lx,%p) stub!\n",
655 KeyHandle, ListOfValuesToQuery, NumberOfItems, MultipleValueInformation,
656 Length,ReturnLength);
657 return STATUS_SUCCESS;
660 /******************************************************************************
661 * NtReplaceKey [NTDLL.@]
662 * ZwReplaceKey [NTDLL.@]
664 NTSTATUS WINAPI NtReplaceKey(
665 IN POBJECT_ATTRIBUTES ObjectAttributes,
666 IN HKEY Key,
667 IN POBJECT_ATTRIBUTES ReplacedObjectAttributes)
669 FIXME("(%p),stub!\n", Key);
670 dump_ObjectAttributes(ObjectAttributes);
671 dump_ObjectAttributes(ReplacedObjectAttributes);
672 return STATUS_SUCCESS;
674 /******************************************************************************
675 * NtRestoreKey [NTDLL.@]
676 * ZwRestoreKey [NTDLL.@]
678 NTSTATUS WINAPI NtRestoreKey(
679 HKEY KeyHandle,
680 HANDLE FileHandle,
681 ULONG RestoreFlags)
683 FIXME("(%p,%p,0x%08lx) stub\n",
684 KeyHandle, FileHandle, RestoreFlags);
685 return STATUS_SUCCESS;
687 /******************************************************************************
688 * NtSaveKey [NTDLL.@]
689 * ZwSaveKey [NTDLL.@]
691 NTSTATUS WINAPI NtSaveKey(IN HKEY KeyHandle, IN HANDLE FileHandle)
693 NTSTATUS ret;
695 TRACE("(%p,%p)\n", KeyHandle, FileHandle);
697 SERVER_START_REQ( save_registry )
699 req->hkey = KeyHandle;
700 req->file = FileHandle;
701 ret = wine_server_call( req );
703 SERVER_END_REQ;
705 return ret;
707 /******************************************************************************
708 * NtSetInformationKey [NTDLL.@]
709 * ZwSetInformationKey [NTDLL.@]
711 NTSTATUS WINAPI NtSetInformationKey(
712 IN HKEY KeyHandle,
713 IN const int KeyInformationClass,
714 IN PVOID KeyInformation,
715 IN ULONG KeyInformationLength)
717 FIXME("(%p,0x%08x,%p,0x%08lx) stub\n",
718 KeyHandle, KeyInformationClass, KeyInformation, KeyInformationLength);
719 return STATUS_SUCCESS;
723 /******************************************************************************
724 * NtSetValueKey [NTDLL.@]
725 * ZwSetValueKey [NTDLL.@]
727 * NOTES
728 * win95 does not care about count for REG_SZ and finds out the len by itself (js)
729 * NT does definitely care (aj)
731 NTSTATUS WINAPI NtSetValueKey( HKEY hkey, const UNICODE_STRING *name, ULONG TitleIndex,
732 ULONG type, const void *data, ULONG count )
734 NTSTATUS ret;
736 TRACE( "(%p,%s,%ld,%p,%ld)\n", hkey, debugstr_us(name), type, data, count );
738 if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
740 SERVER_START_REQ( set_key_value )
742 req->hkey = hkey;
743 req->type = type;
744 req->namelen = name->Length;
745 wine_server_add_data( req, name->Buffer, name->Length );
746 wine_server_add_data( req, data, count );
747 ret = wine_server_call( req );
749 SERVER_END_REQ;
750 return ret;
753 /******************************************************************************
754 * RtlpNtSetValueKey [NTDLL.@]
757 NTSTATUS WINAPI RtlpNtSetValueKey( HKEY hkey, ULONG type, const void *data,
758 ULONG count )
760 UNICODE_STRING name;
762 name.Length = 0;
763 return NtSetValueKey( hkey, &name, 0, type, data, count );
766 /******************************************************************************
767 * NtUnloadKey [NTDLL.@]
768 * ZwUnloadKey [NTDLL.@]
770 NTSTATUS WINAPI NtUnloadKey(IN HKEY KeyHandle)
772 NTSTATUS ret;
774 TRACE("(%p)\n", KeyHandle);
776 SERVER_START_REQ( unload_registry )
778 req->hkey = KeyHandle;
779 ret = wine_server_call(req);
781 SERVER_END_REQ;
783 return ret;
786 /******************************************************************************
787 * RtlFormatCurrentUserKeyPath [NTDLL.@]
789 * NOTE: under NT the user name part of the path is an SID.
791 NTSTATUS WINAPI RtlFormatCurrentUserKeyPath( IN OUT PUNICODE_STRING KeyPath)
793 static const WCHAR pathW[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\'};
794 const char *user = wine_get_user_name();
795 int len = ntdll_umbstowcs( 0, user, strlen(user)+1, NULL, 0 );
797 KeyPath->MaximumLength = sizeof(pathW) + len * sizeof(WCHAR);
798 KeyPath->Length = KeyPath->MaximumLength - sizeof(WCHAR);
799 if (!(KeyPath->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, KeyPath->MaximumLength )))
800 return STATUS_NO_MEMORY;
801 memcpy( KeyPath->Buffer, pathW, sizeof(pathW) );
802 ntdll_umbstowcs( 0, user, strlen(user)+1, KeyPath->Buffer + sizeof(pathW)/sizeof(WCHAR), len );
803 return STATUS_SUCCESS;
806 /******************************************************************************
807 * RtlOpenCurrentUser [NTDLL.@]
809 * if we return just HKEY_CURRENT_USER the advapi tries to find a remote
810 * registry (odd handle) and fails
813 DWORD WINAPI RtlOpenCurrentUser(
814 IN ACCESS_MASK DesiredAccess, /* [in] */
815 OUT PHKEY KeyHandle) /* [out] handle of HKEY_CURRENT_USER */
817 OBJECT_ATTRIBUTES ObjectAttributes;
818 UNICODE_STRING ObjectName;
819 NTSTATUS ret;
821 TRACE("(0x%08lx, %p) stub\n",DesiredAccess, KeyHandle);
823 RtlFormatCurrentUserKeyPath(&ObjectName);
824 InitializeObjectAttributes(&ObjectAttributes,&ObjectName,OBJ_CASE_INSENSITIVE,0, NULL);
825 ret = NtCreateKey(KeyHandle, DesiredAccess, &ObjectAttributes, 0, NULL, 0, NULL);
826 RtlFreeUnicodeString(&ObjectName);
827 return ret;
831 static NTSTATUS RTL_ReportRegistryValue(PKEY_VALUE_FULL_INFORMATION pInfo,
832 PRTL_QUERY_REGISTRY_TABLE pQuery, PVOID pContext, PVOID pEnvironment)
834 PUNICODE_STRING str;
835 UNICODE_STRING src, dst;
836 LONG *bin;
837 ULONG offset;
838 PWSTR wstr;
839 DWORD res;
840 NTSTATUS status = STATUS_SUCCESS;
841 ULONG len;
842 LPWSTR String;
843 INT count = 0;
845 if (pInfo == NULL)
847 if (pQuery->Flags & RTL_QUERY_REGISTRY_DIRECT)
848 return STATUS_INVALID_PARAMETER;
849 else
851 status = pQuery->QueryRoutine(pQuery->Name, pQuery->DefaultType, pQuery->DefaultData,
852 pQuery->DefaultLength, pContext, pQuery->EntryContext);
854 return status;
856 len = pInfo->DataLength;
858 if (pQuery->Flags & RTL_QUERY_REGISTRY_DIRECT)
860 str = (PUNICODE_STRING)pQuery->EntryContext;
862 switch(pInfo->Type)
864 case REG_EXPAND_SZ:
865 if (!(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND))
867 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
868 res = 0;
869 dst.MaximumLength = 0;
870 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
871 dst.Length = 0;
872 dst.MaximumLength = res;
873 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
874 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
875 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
876 dst.Length, pContext, pQuery->EntryContext);
877 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
880 case REG_SZ:
881 case REG_LINK:
882 if (str->Buffer == NULL)
883 RtlCreateUnicodeString(str, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
884 else
885 RtlAppendUnicodeToString(str, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
886 break;
888 case REG_MULTI_SZ:
889 if (!(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND))
890 return STATUS_INVALID_PARAMETER;
892 if (str->Buffer == NULL)
894 str->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, len);
895 str->MaximumLength = len;
897 len = min(len, str->MaximumLength);
898 memcpy(str->Buffer, ((CHAR*)pInfo) + pInfo->DataOffset, len);
899 str->Length = len;
900 break;
902 default:
903 bin = (LONG*)pQuery->EntryContext;
904 if (pInfo->DataLength <= sizeof(ULONG))
905 memcpy(bin, ((CHAR*)pInfo) + pInfo->DataOffset,
906 pInfo->DataLength);
907 else
909 if (bin[0] <= sizeof(ULONG))
911 memcpy(&bin[1], ((CHAR*)pInfo) + pInfo->DataOffset,
912 min(-bin[0], pInfo->DataLength));
914 else
916 len = min(bin[0], pInfo->DataLength);
917 bin[1] = len;
918 bin[2] = pInfo->Type;
919 memcpy(&bin[3], ((CHAR*)pInfo) + pInfo->DataOffset, len);
922 break;
925 else
927 if((pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND) ||
928 (pInfo->Type != REG_EXPAND_SZ && pInfo->Type != REG_MULTI_SZ))
930 status = pQuery->QueryRoutine(pInfo->Name, pInfo->Type,
931 ((CHAR*)pInfo) + pInfo->DataOffset, pInfo->DataLength,
932 pContext, pQuery->EntryContext);
934 else if (pInfo->Type == REG_EXPAND_SZ)
936 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
937 res = 0;
938 dst.MaximumLength = 0;
939 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
940 dst.Length = 0;
941 dst.MaximumLength = res;
942 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
943 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
944 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
945 dst.Length, pContext, pQuery->EntryContext);
946 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
948 else /* REG_MULTI_SZ */
950 if(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND)
952 for (offset = 0; offset <= pInfo->DataLength; offset += len + sizeof(WCHAR))
954 wstr = (WCHAR*)(((CHAR*)pInfo) + offset);
955 len = strlenW(wstr) * sizeof(WCHAR);
956 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, wstr, len,
957 pContext, pQuery->EntryContext);
958 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
959 return status;
962 else
964 while(count<=pInfo->DataLength)
966 String = (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset)+count;
967 count+=strlenW(String)+1;
968 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
969 res = 0;
970 dst.MaximumLength = 0;
971 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
972 dst.Length = 0;
973 dst.MaximumLength = res;
974 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
975 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
976 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
977 dst.Length, pContext, pQuery->EntryContext);
978 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
979 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
980 return status;
985 return status;
989 static NTSTATUS RTL_GetKeyHandle(ULONG RelativeTo, PCWSTR Path, PHKEY handle)
991 UNICODE_STRING KeyString;
992 OBJECT_ATTRIBUTES regkey;
993 PCWSTR base;
994 INT len;
995 NTSTATUS status;
997 static const WCHAR empty[] = {0};
998 static const WCHAR control[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e',
999 '\\','S','y','s','t','e','m','\\','C','u','r','r','e','n','t',' ','C','o','n','t','r','o','l','S','e','t','\\',
1000 'C','o','n','t','r','o','l','\\',0};
1002 static const WCHAR devicemap[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1003 'H','a','r','d','w','a','r','e','\\','D','e','v','i','c','e','M','a','p','\\',0};
1005 static const WCHAR services[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1006 'S','y','s','t','e','m','\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1007 'S','e','r','v','i','c','e','s','\\',0};
1009 static const WCHAR user[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\',
1010 'C','u','r','r','e','n','t','U','s','e','r','\\',0};
1012 static const WCHAR windows_nt[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1013 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1014 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',0};
1016 switch (RelativeTo & 0xff)
1018 case RTL_REGISTRY_ABSOLUTE:
1019 base = empty;
1020 break;
1022 case RTL_REGISTRY_CONTROL:
1023 base = control;
1024 break;
1026 case RTL_REGISTRY_DEVICEMAP:
1027 base = devicemap;
1028 break;
1030 case RTL_REGISTRY_SERVICES:
1031 base = services;
1032 break;
1034 case RTL_REGISTRY_USER:
1035 base = user;
1036 break;
1038 case RTL_REGISTRY_WINDOWS_NT:
1039 base = windows_nt;
1040 break;
1042 default:
1043 return STATUS_INVALID_PARAMETER;
1046 len = (strlenW(base) + strlenW(Path) + 1) * sizeof(WCHAR);
1047 KeyString.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, len);
1048 if (KeyString.Buffer == NULL)
1049 return STATUS_NO_MEMORY;
1051 strcpyW(KeyString.Buffer, base);
1052 strcatW(KeyString.Buffer, Path);
1053 KeyString.Length = len - sizeof(WCHAR);
1054 KeyString.MaximumLength = len;
1055 InitializeObjectAttributes(&regkey, &KeyString, OBJ_CASE_INSENSITIVE, NULL, NULL);
1056 status = NtOpenKey(handle, KEY_ALL_ACCESS, &regkey);
1057 RtlFreeHeap(GetProcessHeap(), 0, KeyString.Buffer);
1058 return status;
1061 /*************************************************************************
1062 * RtlQueryRegistryValues [NTDLL.@]
1064 * Query multiple registry values with a signle call.
1066 * PARAMS
1067 * RelativeTo [I] Registry path that Path refers to
1068 * Path [I] Path to key
1069 * QueryTable [I] Table of key values to query
1070 * Context [I] Paremeter to pass to the application defined QueryRoutine function
1071 * Environment [I] Optional parameter to use when performing expantion
1073 * RETURNS
1074 * STATUS_SUCCESS or an appropriate NTSTATUS error code.
1076 NTSTATUS WINAPI RtlQueryRegistryValues(IN ULONG RelativeTo, IN PCWSTR Path,
1077 IN PRTL_QUERY_REGISTRY_TABLE QueryTable, IN PVOID Context,
1078 IN PVOID Environment OPTIONAL)
1080 UNICODE_STRING Value;
1081 HKEY handle, topkey;
1082 PKEY_VALUE_FULL_INFORMATION pInfo = NULL;
1083 ULONG len, buflen = 0;
1084 NTSTATUS status=STATUS_SUCCESS, ret = STATUS_SUCCESS;
1085 INT i;
1087 TRACE("(%ld, %s, %p, %p, %p)\n", RelativeTo, debugstr_w(Path), QueryTable, Context, Environment);
1089 if(Path == NULL)
1090 return STATUS_INVALID_PARAMETER;
1092 /* get a valid handle */
1093 if (RelativeTo & RTL_REGISTRY_HANDLE)
1094 topkey = handle = (HANDLE)Path;
1095 else
1097 status = RTL_GetKeyHandle(RelativeTo, Path, &topkey);
1098 handle = topkey;
1100 if(status != STATUS_SUCCESS)
1101 return status;
1103 /* Process query table entries */
1104 for (; QueryTable->QueryRoutine != NULL || QueryTable->Name != NULL; ++QueryTable)
1106 if (QueryTable->Flags &
1107 (RTL_QUERY_REGISTRY_SUBKEY | RTL_QUERY_REGISTRY_TOPKEY))
1109 /* topkey must be kept open just in case we will reuse it later */
1110 if (handle != topkey)
1111 NtClose(handle);
1113 if (QueryTable->Flags & RTL_QUERY_REGISTRY_SUBKEY)
1115 handle = 0;
1116 status = RTL_GetKeyHandle((ULONG)QueryTable->Name, Path, &handle);
1117 if(status != STATUS_SUCCESS)
1119 ret = status;
1120 goto out;
1123 else
1124 handle = topkey;
1127 if (QueryTable->Flags & RTL_QUERY_REGISTRY_NOVALUE)
1129 QueryTable->QueryRoutine(QueryTable->Name, REG_NONE, NULL, 0,
1130 Context, QueryTable->EntryContext);
1131 continue;
1134 if (!handle)
1136 if (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED)
1138 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1139 goto out;
1141 continue;
1144 if (QueryTable->Name == NULL)
1146 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DIRECT)
1148 ret = STATUS_INVALID_PARAMETER;
1149 goto out;
1152 /* Report all subkeys */
1153 for (i = 0;; ++i)
1155 status = NtEnumerateValueKey(handle, i,
1156 KeyValueFullInformation, pInfo, buflen, &len);
1157 if (status == STATUS_NO_MORE_ENTRIES)
1158 break;
1159 if (status == STATUS_BUFFER_OVERFLOW ||
1160 status == STATUS_BUFFER_TOO_SMALL)
1162 buflen = len;
1163 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1164 pInfo = (KEY_VALUE_FULL_INFORMATION*)RtlAllocateHeap(
1165 GetProcessHeap(), 0, buflen);
1166 NtEnumerateValueKey(handle, i, KeyValueFullInformation,
1167 pInfo, buflen, &len);
1170 status = RTL_ReportRegistryValue(pInfo, QueryTable, Context, Environment);
1171 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1173 ret = status;
1174 goto out;
1176 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DELETE)
1178 RtlInitUnicodeString(&Value, pInfo->Name);
1179 NtDeleteValueKey(handle, &Value);
1183 if (i == 0 && (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED))
1185 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1186 goto out;
1189 else
1191 RtlInitUnicodeString(&Value, QueryTable->Name);
1192 status = NtQueryValueKey(handle, &Value, KeyValueFullInformation,
1193 pInfo, buflen, &len);
1194 if (status == STATUS_BUFFER_OVERFLOW ||
1195 status == STATUS_BUFFER_TOO_SMALL)
1197 buflen = len;
1198 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1199 pInfo = (KEY_VALUE_FULL_INFORMATION*)RtlAllocateHeap(
1200 GetProcessHeap(), 0, buflen);
1201 status = NtQueryValueKey(handle, &Value,
1202 KeyValueFullInformation, pInfo, buflen, &len);
1204 if (status != STATUS_SUCCESS)
1206 if (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED)
1208 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1209 goto out;
1211 status = RTL_ReportRegistryValue(NULL, QueryTable, Context, Environment);
1212 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1214 ret = status;
1215 goto out;
1218 else
1220 status = RTL_ReportRegistryValue(pInfo, QueryTable, Context, Environment);
1221 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1223 ret = status;
1224 goto out;
1226 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DELETE)
1227 NtDeleteValueKey(handle, &Value);
1232 out:
1233 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1234 if (handle != topkey)
1235 NtClose(handle);
1236 NtClose(topkey);
1237 return ret;
1240 /*************************************************************************
1241 * RtlCheckRegistryKey [NTDLL.@]
1243 * Query multiple registry values with a signle call.
1245 * PARAMS
1246 * RelativeTo [I] Registry path that Path refers to
1247 * Path [I] Path to key
1249 * RETURNS
1250 * STATUS_SUCCESS if the specified key exists, or an NTSTATUS error code.
1252 NTSTATUS WINAPI RtlCheckRegistryKey(IN ULONG RelativeTo, IN PWSTR Path)
1254 HKEY handle;
1255 NTSTATUS status;
1257 TRACE("(%ld, %s)\n", RelativeTo, debugstr_w(Path));
1259 if((!RelativeTo) && Path == NULL)
1260 return STATUS_OBJECT_PATH_SYNTAX_BAD;
1261 if(RelativeTo & RTL_REGISTRY_HANDLE)
1262 return STATUS_SUCCESS;
1264 status = RTL_GetKeyHandle(RelativeTo, Path, &handle);
1265 if (handle) NtClose(handle);
1266 if (status == STATUS_INVALID_HANDLE) status = STATUS_OBJECT_NAME_NOT_FOUND;
1267 return status;
1270 /*************************************************************************
1271 * RtlDeleteRegistryValue [NTDLL.@]
1273 * Query multiple registry values with a signle call.
1275 * PARAMS
1276 * RelativeTo [I] Registry path that Path refers to
1277 * Path [I] Path to key
1278 * ValueName [I] Name of the value to delete
1280 * RETURNS
1281 * STATUS_SUCCESS if the specified key is successfully deleted, or an NTSTATUS error code.
1283 NTSTATUS WINAPI RtlDeleteRegistryValue(IN ULONG RelativeTo, IN PCWSTR Path, IN PCWSTR ValueName)
1285 NTSTATUS status;
1286 HKEY handle;
1287 UNICODE_STRING Value;
1289 TRACE("(%ld, %s, %s)\n", RelativeTo, debugstr_w(Path), debugstr_w(ValueName));
1291 RtlInitUnicodeString(&Value, ValueName);
1292 if(RelativeTo == RTL_REGISTRY_HANDLE)
1294 return NtDeleteValueKey((HKEY)Path, &Value);
1296 status = RTL_GetKeyHandle(RelativeTo, Path, &handle);
1297 if (status) return status;
1298 status = NtDeleteValueKey(handle, &Value);
1299 NtClose(handle);
1300 return status;