2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: lib/advapi32/reg/reg.c
5 * PURPOSE: Registry functions
6 * PROGRAMMER: Ariadne ( ariadne@xs4all.nl)
7 * Thomas Weidenmueller <w3seek@reactos.com>
11 * 20050502 Fireball imported some stuff from WINE
14 /* INCLUDES *****************************************************************/
18 #include <ndk/cmfuncs.h>
19 #include <pseh/pseh2.h>
23 WINE_DEFAULT_DEBUG_CHANNEL(reg
);
25 /* DEFINES ******************************************************************/
27 #define MAX_DEFAULT_HANDLES 6
28 #define REG_MAX_NAME_SIZE 256
29 #define REG_MAX_DATA_SIZE 2048
31 /* GLOBALS ******************************************************************/
33 static RTL_CRITICAL_SECTION HandleTableCS
;
34 static HANDLE DefaultHandleTable
[MAX_DEFAULT_HANDLES
];
35 static HANDLE ProcessHeap
;
36 static BOOLEAN DefaultHandlesDisabled
= FALSE
;
37 static BOOLEAN DefaultHandleHKUDisabled
= FALSE
;
38 static BOOLEAN DllInitialized
= FALSE
; /* HACK */
40 /* PROTOTYPES ***************************************************************/
42 static NTSTATUS
MapDefaultKey (PHANDLE ParentKey
, HKEY Key
);
43 static VOID
CloseDefaultKeys(VOID
);
44 #define ClosePredefKey(Handle) \
45 if ((ULONG_PTR)Handle & 0x1) { \
48 #define IsPredefKey(HKey) \
49 (((ULONG_PTR)(HKey) & 0xF0000000) == 0x80000000)
50 #define GetPredefKeyIndex(HKey) \
51 ((ULONG_PTR)(HKey) & 0x0FFFFFFF)
53 static NTSTATUS
OpenClassesRootKey(PHANDLE KeyHandle
);
54 static NTSTATUS
OpenLocalMachineKey (PHANDLE KeyHandle
);
55 static NTSTATUS
OpenUsersKey (PHANDLE KeyHandle
);
56 static NTSTATUS
OpenCurrentConfigKey(PHANDLE KeyHandle
);
59 /* FUNCTIONS ****************************************************************/
60 /* check if value type needs string conversion (Ansi<->Unicode) */
61 __inline
static int is_string( DWORD type
)
63 return (type
== REG_SZ
) || (type
== REG_EXPAND_SZ
) || (type
== REG_MULTI_SZ
);
66 /************************************************************************
67 * RegInitDefaultHandles
72 TRACE("RegInitialize()\n");
77 ProcessHeap
= RtlGetProcessHeap();
78 RtlZeroMemory(DefaultHandleTable
,
79 MAX_DEFAULT_HANDLES
* sizeof(HANDLE
));
80 RtlInitializeCriticalSection(&HandleTableCS
);
82 DllInitialized
= TRUE
;
89 /************************************************************************
95 TRACE("RegCleanup()\n");
98 RtlDeleteCriticalSection(&HandleTableCS
);
105 OpenPredefinedKey(IN ULONG Index
,
112 case 0: /* HKEY_CLASSES_ROOT */
113 Status
= OpenClassesRootKey (Handle
);
116 case 1: /* HKEY_CURRENT_USER */
117 Status
= RtlOpenCurrentUser (MAXIMUM_ALLOWED
,
121 case 2: /* HKEY_LOCAL_MACHINE */
122 Status
= OpenLocalMachineKey (Handle
);
125 case 3: /* HKEY_USERS */
126 Status
= OpenUsersKey (Handle
);
129 case 4: /* HKEY_PERFORMANCE_DATA */
130 Status
= OpenPerformanceDataKey (Handle
);
134 case 5: /* HKEY_CURRENT_CONFIG */
135 Status
= OpenCurrentConfigKey (Handle
);
138 case 6: /* HKEY_DYN_DATA */
139 Status
= STATUS_NOT_IMPLEMENTED
;
143 WARN("MapDefaultHandle() no handle creator\n");
144 Status
= STATUS_INVALID_PARAMETER
;
153 MapDefaultKey(OUT PHANDLE RealKey
,
158 BOOLEAN DoOpen
, DefDisabled
;
159 NTSTATUS Status
= STATUS_SUCCESS
;
161 TRACE("MapDefaultKey (Key %x)\n", Key
);
163 if (!IsPredefKey(Key
))
165 *RealKey
= (HANDLE
)((ULONG_PTR
)Key
& ~0x1);
166 return STATUS_SUCCESS
;
169 /* Handle special cases here */
170 Index
= GetPredefKeyIndex(Key
);
171 if (Index
>= MAX_DEFAULT_HANDLES
)
173 return STATUS_INVALID_PARAMETER
;
175 RegInitialize(); /* HACK until delay-loading is implemented */
176 RtlEnterCriticalSection (&HandleTableCS
);
178 if (Key
== HKEY_CURRENT_USER
)
179 DefDisabled
= DefaultHandleHKUDisabled
;
181 DefDisabled
= DefaultHandlesDisabled
;
185 Handle
= &DefaultHandleTable
[Index
];
186 DoOpen
= (*Handle
== NULL
);
196 /* create/open the default handle */
197 Status
= OpenPredefinedKey(Index
,
201 if (NT_SUCCESS(Status
))
206 *(PULONG_PTR
)Handle
|= 0x1;
209 RtlLeaveCriticalSection (&HandleTableCS
);
216 CloseDefaultKeys(VOID
)
219 RegInitialize(); /* HACK until delay-loading is implemented */
220 RtlEnterCriticalSection(&HandleTableCS
);
222 for (i
= 0; i
< MAX_DEFAULT_HANDLES
; i
++)
224 if (DefaultHandleTable
[i
] != NULL
)
226 NtClose(DefaultHandleTable
[i
]);
227 DefaultHandleTable
[i
] = NULL
;
231 RtlLeaveCriticalSection(&HandleTableCS
);
236 OpenClassesRootKey(_Out_ PHANDLE KeyHandle
)
238 OBJECT_ATTRIBUTES Attributes
;
239 UNICODE_STRING KeyName
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\Software\\CLASSES");
242 TRACE("OpenClassesRootKey()\n");
244 InitializeObjectAttributes(&Attributes
,
246 OBJ_CASE_INSENSITIVE
,
249 Status
= NtOpenKey(KeyHandle
,
253 if (!NT_SUCCESS(Status
))
256 /* Mark it as HKCR */
257 MakeHKCRKey((HKEY
*)KeyHandle
);
264 OpenLocalMachineKey(PHANDLE KeyHandle
)
266 OBJECT_ATTRIBUTES Attributes
;
267 UNICODE_STRING KeyName
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine");
270 TRACE("OpenLocalMachineKey()\n");
272 InitializeObjectAttributes(&Attributes
,
274 OBJ_CASE_INSENSITIVE
,
277 Status
= NtOpenKey(KeyHandle
,
281 TRACE("NtOpenKey(%wZ) => %08x\n", &KeyName
, Status
);
288 OpenUsersKey(PHANDLE KeyHandle
)
290 OBJECT_ATTRIBUTES Attributes
;
291 UNICODE_STRING KeyName
= RTL_CONSTANT_STRING(L
"\\Registry\\User");
293 TRACE("OpenUsersKey()\n");
295 InitializeObjectAttributes(&Attributes
,
297 OBJ_CASE_INSENSITIVE
,
300 return NtOpenKey(KeyHandle
,
307 OpenCurrentConfigKey (PHANDLE KeyHandle
)
309 OBJECT_ATTRIBUTES Attributes
;
310 UNICODE_STRING KeyName
=
311 RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\System\\CurrentControlSet\\Hardware Profiles\\Current");
313 TRACE("OpenCurrentConfigKey()\n");
315 InitializeObjectAttributes(&Attributes
,
317 OBJ_CASE_INSENSITIVE
,
320 return NtOpenKey(KeyHandle
,
326 /************************************************************************
327 * RegDisablePredefinedCache
332 RegDisablePredefinedCache(VOID
)
334 RegInitialize(); /* HACK until delay-loading is implemented */
335 RtlEnterCriticalSection(&HandleTableCS
);
336 DefaultHandleHKUDisabled
= TRUE
;
337 RtlLeaveCriticalSection(&HandleTableCS
);
338 return ERROR_SUCCESS
;
342 /************************************************************************
343 * RegDisablePredefinedCacheEx
348 RegDisablePredefinedCacheEx(VOID
)
350 RegInitialize(); /* HACK until delay-loading is implemented */
351 RtlEnterCriticalSection(&HandleTableCS
);
352 DefaultHandlesDisabled
= TRUE
;
353 DefaultHandleHKUDisabled
= TRUE
;
354 RtlLeaveCriticalSection(&HandleTableCS
);
355 return ERROR_SUCCESS
;
359 /************************************************************************
360 * RegOverridePredefKey
365 RegOverridePredefKey(IN HKEY hKey
,
366 IN HKEY hNewHKey OPTIONAL
)
368 LONG ErrorCode
= ERROR_SUCCESS
;
370 if ((hKey
== HKEY_CLASSES_ROOT
||
371 hKey
== HKEY_CURRENT_CONFIG
||
372 hKey
== HKEY_CURRENT_USER
||
373 hKey
== HKEY_LOCAL_MACHINE
||
374 hKey
== HKEY_PERFORMANCE_DATA
||
375 hKey
== HKEY_USERS
) &&
376 !IsPredefKey(hNewHKey
))
381 Index
= GetPredefKeyIndex(hKey
);
382 Handle
= &DefaultHandleTable
[Index
];
384 if (hNewHKey
== NULL
)
386 /* restore the default mapping */
387 NTSTATUS Status
= OpenPredefinedKey(Index
,
389 if (!NT_SUCCESS(Status
))
391 return RtlNtStatusToDosError(Status
);
394 ASSERT(hNewHKey
!= NULL
);
396 RegInitialize(); /* HACK until delay-loading is implemented */
397 RtlEnterCriticalSection(&HandleTableCS
);
399 /* close the currently mapped handle if existing */
405 /* update the mapping */
408 RtlLeaveCriticalSection(&HandleTableCS
);
411 ErrorCode
= ERROR_INVALID_HANDLE
;
417 /************************************************************************
423 RegCloseKey(HKEY hKey
)
427 /* don't close null handle or a pseudo handle */
428 if ((!hKey
) || (((ULONG_PTR
)hKey
& 0xF0000000) == 0x80000000))
430 return ERROR_INVALID_HANDLE
;
433 Status
= NtClose(hKey
);
434 if (!NT_SUCCESS(Status
))
436 return RtlNtStatusToDosError(Status
);
439 return ERROR_SUCCESS
;
444 RegpCopyTree(IN HKEY hKeySrc
,
449 LIST_ENTRY ListEntry
;
452 } REGP_COPY_KEYS
, *PREGP_COPY_KEYS
;
454 LIST_ENTRY copyQueueHead
;
455 PREGP_COPY_KEYS copyKeys
, newCopyKeys
;
458 KEY_VALUE_FULL_INFORMATION
*KeyValue
;
459 KEY_NODE_INFORMATION
*KeyNode
;
462 ULONG Index
, BufferSizeRequired
, BufferSize
= 0x200;
463 NTSTATUS Status
= STATUS_SUCCESS
;
464 NTSTATUS Status2
= STATUS_SUCCESS
;
466 InitializeListHead(©QueueHead
);
468 Info
.Buffer
= RtlAllocateHeap(ProcessHeap
,
471 if (Info
.Buffer
== NULL
)
473 return STATUS_INSUFFICIENT_RESOURCES
;
476 copyKeys
= RtlAllocateHeap(ProcessHeap
,
478 sizeof(REGP_COPY_KEYS
));
479 if (copyKeys
!= NULL
)
481 copyKeys
->hKeySrc
= hKeySrc
;
482 copyKeys
->hKeyDest
= hKeyDest
;
483 InsertHeadList(©QueueHead
,
484 ©Keys
->ListEntry
);
486 /* FIXME - copy security from hKeySrc to hKeyDest or just for the subkeys? */
490 copyKeys
= CONTAINING_RECORD(copyQueueHead
.Flink
,
494 /* enumerate all values and copy them */
498 Status2
= NtEnumerateValueKey(copyKeys
->hKeySrc
,
500 KeyValueFullInformation
,
503 &BufferSizeRequired
);
504 if (NT_SUCCESS(Status2
))
506 UNICODE_STRING ValueName
;
509 /* don't use RtlInitUnicodeString as the string is not NULL-terminated! */
510 ValueName
.Length
= Info
.KeyValue
->NameLength
;
511 ValueName
.MaximumLength
= ValueName
.Length
;
512 ValueName
.Buffer
= Info
.KeyValue
->Name
;
514 Data
= (PVOID
)((ULONG_PTR
)Info
.KeyValue
+ Info
.KeyValue
->DataOffset
);
516 Status2
= NtSetValueKey(copyKeys
->hKeyDest
,
518 Info
.KeyValue
->TitleIndex
,
521 Info
.KeyValue
->DataLength
);
523 /* don't break, let's try to copy as many values as possible */
524 if (!NT_SUCCESS(Status2
) && NT_SUCCESS(Status
))
531 else if (Status2
== STATUS_BUFFER_OVERFLOW
)
535 ASSERT(BufferSize
< BufferSizeRequired
);
537 Buffer
= RtlReAllocateHeap(ProcessHeap
,
543 Info
.Buffer
= Buffer
;
548 /* don't break, let's try to copy as many values as possible */
549 Status2
= STATUS_INSUFFICIENT_RESOURCES
;
552 if (NT_SUCCESS(Status
))
560 /* break to avoid an infinite loop in case of denied access or
562 if (Status2
!= STATUS_NO_MORE_ENTRIES
&& NT_SUCCESS(Status
))
571 /* enumerate all subkeys and open and enqueue them */
575 Status2
= NtEnumerateKey(copyKeys
->hKeySrc
,
580 &BufferSizeRequired
);
581 if (NT_SUCCESS(Status2
))
583 HANDLE KeyHandle
, NewKeyHandle
;
584 OBJECT_ATTRIBUTES ObjectAttributes
;
585 UNICODE_STRING SubKeyName
, ClassName
;
587 /* don't use RtlInitUnicodeString as the string is not NULL-terminated! */
588 SubKeyName
.Length
= Info
.KeyNode
->NameLength
;
589 SubKeyName
.MaximumLength
= SubKeyName
.Length
;
590 SubKeyName
.Buffer
= Info
.KeyNode
->Name
;
591 ClassName
.Length
= Info
.KeyNode
->ClassLength
;
592 ClassName
.MaximumLength
= ClassName
.Length
;
593 ClassName
.Buffer
= (PWSTR
)((ULONG_PTR
)Info
.KeyNode
+ Info
.KeyNode
->ClassOffset
);
595 /* open the subkey with sufficient rights */
597 InitializeObjectAttributes(&ObjectAttributes
,
599 OBJ_CASE_INSENSITIVE
,
603 Status2
= NtOpenKey(&KeyHandle
,
604 KEY_ENUMERATE_SUB_KEYS
| KEY_QUERY_VALUE
,
606 if (NT_SUCCESS(Status2
))
608 /* FIXME - attempt to query the security information */
610 InitializeObjectAttributes(&ObjectAttributes
,
612 OBJ_CASE_INSENSITIVE
,
616 Status2
= NtCreateKey(&NewKeyHandle
,
619 Info
.KeyNode
->TitleIndex
,
623 if (NT_SUCCESS(Status2
))
625 newCopyKeys
= RtlAllocateHeap(ProcessHeap
,
627 sizeof(REGP_COPY_KEYS
));
628 if (newCopyKeys
!= NULL
)
630 /* save the handles and enqueue the subkey */
631 newCopyKeys
->hKeySrc
= KeyHandle
;
632 newCopyKeys
->hKeyDest
= NewKeyHandle
;
633 InsertTailList(©QueueHead
,
634 &newCopyKeys
->ListEntry
);
639 NtClose(NewKeyHandle
);
641 Status2
= STATUS_INSUFFICIENT_RESOURCES
;
650 if (!NT_SUCCESS(Status2
) && NT_SUCCESS(Status
))
657 else if (Status2
== STATUS_BUFFER_OVERFLOW
)
661 ASSERT(BufferSize
< BufferSizeRequired
);
663 Buffer
= RtlReAllocateHeap(ProcessHeap
,
669 Info
.Buffer
= Buffer
;
674 /* don't break, let's try to copy as many keys as possible */
675 Status2
= STATUS_INSUFFICIENT_RESOURCES
;
678 if (NT_SUCCESS(Status
))
686 /* break to avoid an infinite loop in case of denied access or
688 if (Status2
!= STATUS_NO_MORE_ENTRIES
&& NT_SUCCESS(Status
))
697 /* close the handles and remove the entry from the list */
698 if (copyKeys
->hKeySrc
!= hKeySrc
)
700 NtClose(copyKeys
->hKeySrc
);
702 if (copyKeys
->hKeyDest
!= hKeyDest
)
704 NtClose(copyKeys
->hKeyDest
);
707 RemoveEntryList(©Keys
->ListEntry
);
709 RtlFreeHeap(ProcessHeap
,
712 } while (!IsListEmpty(©QueueHead
));
715 Status
= STATUS_INSUFFICIENT_RESOURCES
;
717 RtlFreeHeap(ProcessHeap
,
725 /************************************************************************
731 RegCopyTreeW(IN HKEY hKeySrc
,
732 IN LPCWSTR lpSubKey OPTIONAL
,
735 HANDLE DestKeyHandle
, KeyHandle
, CurKey
, SubKeyHandle
= NULL
;
738 Status
= MapDefaultKey(&KeyHandle
,
740 if (!NT_SUCCESS(Status
))
742 return RtlNtStatusToDosError(Status
);
745 Status
= MapDefaultKey(&DestKeyHandle
,
747 if (!NT_SUCCESS(Status
))
752 if (lpSubKey
!= NULL
)
754 OBJECT_ATTRIBUTES ObjectAttributes
;
755 UNICODE_STRING SubKeyName
;
757 RtlInitUnicodeString(&SubKeyName
,
760 InitializeObjectAttributes(&ObjectAttributes
,
762 OBJ_CASE_INSENSITIVE
,
766 Status
= NtOpenKey(&SubKeyHandle
,
767 KEY_ENUMERATE_SUB_KEYS
| KEY_QUERY_VALUE
,
769 if (!NT_SUCCESS(Status
))
774 CurKey
= SubKeyHandle
;
779 Status
= RegpCopyTree(CurKey
,
782 if (SubKeyHandle
!= NULL
)
784 NtClose(SubKeyHandle
);
788 ClosePredefKey(DestKeyHandle
);
790 ClosePredefKey(KeyHandle
);
792 if (!NT_SUCCESS(Status
))
794 return RtlNtStatusToDosError(Status
);
797 return ERROR_SUCCESS
;
801 /************************************************************************
807 RegCopyTreeA(IN HKEY hKeySrc
,
808 IN LPCSTR lpSubKey OPTIONAL
,
811 UNICODE_STRING SubKeyName
= { 0, 0, NULL
};
814 if (lpSubKey
!= NULL
&&
815 !RtlCreateUnicodeStringFromAsciiz(&SubKeyName
,
818 return ERROR_NOT_ENOUGH_MEMORY
;
821 Ret
= RegCopyTreeW(hKeySrc
,
825 RtlFreeUnicodeString(&SubKeyName
);
831 /************************************************************************
832 * RegConnectRegistryA
837 RegConnectRegistryA(IN LPCSTR lpMachineName
,
841 UNICODE_STRING MachineName
= { 0, 0, NULL
};
844 if (lpMachineName
!= NULL
&&
845 !RtlCreateUnicodeStringFromAsciiz(&MachineName
,
846 (LPSTR
)lpMachineName
))
848 return ERROR_NOT_ENOUGH_MEMORY
;
851 Ret
= RegConnectRegistryW(MachineName
.Buffer
,
855 RtlFreeUnicodeString(&MachineName
);
861 /************************************************************************
862 * RegConnectRegistryW
867 RegConnectRegistryW(LPCWSTR lpMachineName
,
873 TRACE("(%s,%p,%p): stub\n",debugstr_w(lpMachineName
),hKey
,phkResult
);
875 if (!lpMachineName
|| !*lpMachineName
)
877 /* Use the local machine name */
878 ret
= RegOpenKeyW( hKey
, NULL
, phkResult
);
882 WCHAR compName
[MAX_COMPUTERNAME_LENGTH
+ 1];
883 DWORD len
= sizeof(compName
) / sizeof(WCHAR
);
885 /* MSDN says lpMachineName must start with \\ : not so */
886 if( lpMachineName
[0] == '\\' && lpMachineName
[1] == '\\')
889 if (GetComputerNameW(compName
, &len
))
891 if (!_wcsicmp(lpMachineName
, compName
))
892 ret
= RegOpenKeyW(hKey
, NULL
, phkResult
);
895 FIXME("Connect to %s is not supported.\n",debugstr_w(lpMachineName
));
896 ret
= ERROR_BAD_NETPATH
;
900 ret
= GetLastError();
907 /************************************************************************
910 * Create key and all necessary intermediate keys
913 CreateNestedKey(PHKEY KeyHandle
,
914 POBJECT_ATTRIBUTES ObjectAttributes
,
915 PUNICODE_STRING ClassString
,
918 DWORD
*lpdwDisposition
)
920 OBJECT_ATTRIBUTES LocalObjectAttributes
;
921 UNICODE_STRING LocalKeyName
;
924 ULONG FullNameLength
;
927 HANDLE LocalKeyHandle
;
929 Status
= NtCreateKey((PHANDLE
) KeyHandle
,
935 (PULONG
)lpdwDisposition
);
936 TRACE("NtCreateKey(%wZ) called (Status %lx)\n", ObjectAttributes
->ObjectName
, Status
);
937 if (Status
!= STATUS_OBJECT_NAME_NOT_FOUND
)
940 /* Copy object attributes */
941 RtlCopyMemory(&LocalObjectAttributes
,
943 sizeof(OBJECT_ATTRIBUTES
));
944 RtlCreateUnicodeString(&LocalKeyName
,
945 ObjectAttributes
->ObjectName
->Buffer
);
946 LocalObjectAttributes
.ObjectName
= &LocalKeyName
;
947 FullNameLength
= LocalKeyName
.Length
/ sizeof(WCHAR
);
949 LocalKeyHandle
= NULL
;
951 /* Remove the last part of the key name and try to create the key again. */
952 while (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
954 Ptr
= wcsrchr(LocalKeyName
.Buffer
, '\\');
955 if (Ptr
== NULL
|| Ptr
== LocalKeyName
.Buffer
)
957 Status
= STATUS_UNSUCCESSFUL
;
962 LocalKeyName
.Length
= wcslen(LocalKeyName
.Buffer
) * sizeof(WCHAR
);
964 Status
= NtCreateKey(&LocalKeyHandle
,
966 &LocalObjectAttributes
,
971 TRACE("NtCreateKey(%wZ) called (Status %lx)\n", &LocalKeyName
, Status
);
974 if (!NT_SUCCESS(Status
))
976 RtlFreeUnicodeString(&LocalKeyName
);
980 /* Add removed parts of the key name and create them too. */
981 Length
= wcslen(LocalKeyName
.Buffer
);
985 NtClose (LocalKeyHandle
);
987 LocalKeyName
.Buffer
[Length
] = L
'\\';
988 Length
= wcslen (LocalKeyName
.Buffer
);
989 LocalKeyName
.Length
= Length
* sizeof(WCHAR
);
991 if (Length
== FullNameLength
)
993 Status
= NtCreateKey((PHANDLE
) KeyHandle
,
999 (PULONG
)lpdwDisposition
);
1003 Status
= NtCreateKey(&LocalKeyHandle
,
1005 &LocalObjectAttributes
,
1010 TRACE("NtCreateKey(%wZ) called (Status %lx)\n", &LocalKeyName
, Status
);
1011 if (!NT_SUCCESS(Status
))
1015 RtlFreeUnicodeString(&LocalKeyName
);
1021 /************************************************************************
1029 _In_ LPCSTR lpSubKey
,
1030 _In_ DWORD Reserved
,
1032 _In_ DWORD dwOptions
,
1033 _In_ REGSAM samDesired
,
1034 _In_ LPSECURITY_ATTRIBUTES lpSecurityAttributes
,
1035 _Out_ PHKEY phkResult
,
1036 _Out_ LPDWORD lpdwDisposition
)
1038 UNICODE_STRING SubKeyString
;
1039 UNICODE_STRING ClassString
;
1042 RtlCreateUnicodeStringFromAsciiz(&ClassString
, lpClass
);
1043 RtlCreateUnicodeStringFromAsciiz(&SubKeyString
, (LPSTR
)lpSubKey
);
1045 ErrorCode
= RegCreateKeyExW(
1047 SubKeyString
.Buffer
,
1052 lpSecurityAttributes
,
1056 RtlFreeUnicodeString(&SubKeyString
);
1057 RtlFreeUnicodeString(&ClassString
);
1063 /************************************************************************
1069 RegCreateKeyExW(HKEY hKey
,
1075 LPSECURITY_ATTRIBUTES lpSecurityAttributes
,
1077 LPDWORD lpdwDisposition
)
1079 UNICODE_STRING SubKeyString
;
1080 UNICODE_STRING ClassString
;
1081 OBJECT_ATTRIBUTES ObjectAttributes
;
1083 ULONG Attributes
= OBJ_CASE_INSENSITIVE
;
1086 TRACE("RegCreateKeyExW() called\n");
1088 if (lpSecurityAttributes
&& lpSecurityAttributes
->nLength
!= sizeof(SECURITY_ATTRIBUTES
))
1089 return ERROR_INVALID_USER_BUFFER
;
1091 /* get the real parent key */
1092 Status
= MapDefaultKey(&ParentKey
,
1094 if (!NT_SUCCESS(Status
))
1096 return RtlNtStatusToDosError(Status
);
1099 TRACE("ParentKey %p\n", ParentKey
);
1101 if (dwOptions
& REG_OPTION_OPEN_LINK
)
1102 Attributes
|= OBJ_OPENLINK
;
1104 RtlInitUnicodeString(&ClassString
,
1106 RtlInitUnicodeString(&SubKeyString
,
1108 InitializeObjectAttributes(&ObjectAttributes
,
1112 lpSecurityAttributes
? (PSECURITY_DESCRIPTOR
)lpSecurityAttributes
->lpSecurityDescriptor
: NULL
);
1113 Status
= CreateNestedKey(phkResult
,
1115 (lpClass
== NULL
)? NULL
: &ClassString
,
1120 ClosePredefKey(ParentKey
);
1122 TRACE("Status %x\n", Status
);
1123 if (!NT_SUCCESS(Status
))
1125 return RtlNtStatusToDosError(Status
);
1128 if (IsHKCRKey(ParentKey
))
1129 MakeHKCRKey(phkResult
);
1131 return ERROR_SUCCESS
;
1135 /************************************************************************
1141 RegCreateKeyA(HKEY hKey
,
1145 return RegCreateKeyExA(hKey
,
1157 /************************************************************************
1163 RegCreateKeyW(HKEY hKey
,
1167 return RegCreateKeyExW(hKey
,
1179 /************************************************************************
1188 _In_ LPCSTR lpSubKey
)
1190 return RegDeleteKeyExA(hKey
, lpSubKey
, 0, 0);
1194 /************************************************************************
1203 _In_ LPCWSTR lpSubKey
)
1205 return RegDeleteKeyExW(hKey
, lpSubKey
, 0, 0);
1209 /************************************************************************
1218 _In_ LPCSTR lpSubKey
,
1219 _In_ REGSAM samDesired
,
1220 _In_ DWORD Reserved
)
1223 UNICODE_STRING SubKeyName
;
1225 RtlCreateUnicodeStringFromAsciiz(&SubKeyName
, (LPSTR
)lpSubKey
);
1227 ErrorCode
= RegDeleteKeyExW(hKey
, SubKeyName
.Buffer
, samDesired
, Reserved
);
1229 RtlFreeUnicodeString(&SubKeyName
);
1235 /************************************************************************
1244 _In_ LPCWSTR lpSubKey
,
1245 _In_ REGSAM samDesired
,
1246 _In_ DWORD Reserved
)
1248 OBJECT_ATTRIBUTES ObjectAttributes
;
1249 UNICODE_STRING SubKeyName
;
1254 /* Make sure we got a subkey */
1258 return ERROR_INVALID_PARAMETER
;
1261 Status
= MapDefaultKey(&ParentKey
,
1263 if (!NT_SUCCESS(Status
))
1265 return RtlNtStatusToDosError(Status
);
1268 if (IsHKCRKey(ParentKey
))
1269 return DeleteHKCRKey(ParentKey
, lpSubKey
, samDesired
, Reserved
);
1271 if (samDesired
& KEY_WOW64_32KEY
)
1272 ERR("Wow64 not yet supported!\n");
1274 if (samDesired
& KEY_WOW64_64KEY
)
1275 ERR("Wow64 not yet supported!\n");
1278 RtlInitUnicodeString(&SubKeyName
,
1280 InitializeObjectAttributes(&ObjectAttributes
,
1282 OBJ_CASE_INSENSITIVE
,
1285 Status
= NtOpenKey(&TargetKey
,
1288 if (!NT_SUCCESS(Status
))
1293 Status
= NtDeleteKey(TargetKey
);
1297 ClosePredefKey(ParentKey
);
1299 if (!NT_SUCCESS(Status
))
1301 return RtlNtStatusToDosError(Status
);
1304 return ERROR_SUCCESS
;
1308 /************************************************************************
1309 * RegDeleteKeyValueW
1314 RegDeleteKeyValueW(IN HKEY hKey
,
1315 IN LPCWSTR lpSubKey OPTIONAL
,
1316 IN LPCWSTR lpValueName OPTIONAL
)
1318 UNICODE_STRING ValueName
;
1319 HANDLE KeyHandle
, CurKey
, SubKeyHandle
= NULL
;
1322 Status
= MapDefaultKey(&KeyHandle
,
1324 if (!NT_SUCCESS(Status
))
1326 return RtlNtStatusToDosError(Status
);
1329 if (lpSubKey
!= NULL
)
1331 OBJECT_ATTRIBUTES ObjectAttributes
;
1332 UNICODE_STRING SubKeyName
;
1334 RtlInitUnicodeString(&SubKeyName
,
1337 InitializeObjectAttributes(&ObjectAttributes
,
1339 OBJ_CASE_INSENSITIVE
,
1343 Status
= NtOpenKey(&SubKeyHandle
,
1346 if (!NT_SUCCESS(Status
))
1351 CurKey
= SubKeyHandle
;
1356 RtlInitUnicodeString(&ValueName
,
1357 (LPWSTR
)lpValueName
);
1359 Status
= NtDeleteValueKey(CurKey
,
1362 if (SubKeyHandle
!= NULL
)
1364 NtClose(SubKeyHandle
);
1368 ClosePredefKey(KeyHandle
);
1370 if (!NT_SUCCESS(Status
))
1372 return RtlNtStatusToDosError(Status
);
1375 return ERROR_SUCCESS
;
1379 /************************************************************************
1380 * RegDeleteKeyValueA
1385 RegDeleteKeyValueA(IN HKEY hKey
,
1386 IN LPCSTR lpSubKey OPTIONAL
,
1387 IN LPCSTR lpValueName OPTIONAL
)
1389 UNICODE_STRING SubKey
= { 0, 0, NULL
}, ValueName
= { 0, 0, NULL
};
1392 if (lpSubKey
!= NULL
&&
1393 !RtlCreateUnicodeStringFromAsciiz(&SubKey
,
1396 return ERROR_NOT_ENOUGH_MEMORY
;
1399 if (lpValueName
!= NULL
&&
1400 !RtlCreateUnicodeStringFromAsciiz(&ValueName
,
1401 (LPSTR
)lpValueName
))
1403 RtlFreeUnicodeString(&SubKey
);
1404 return ERROR_NOT_ENOUGH_MEMORY
;
1407 Ret
= RegDeleteKeyValueW(hKey
,
1411 RtlFreeUnicodeString(&SubKey
);
1412 RtlFreeUnicodeString(&ValueName
);
1418 // Non-recursive RegDeleteTreeW implementation by Thomas, however it needs bugfixing
1420 RegpDeleteTree(IN HKEY hKey
)
1424 LIST_ENTRY ListEntry
;
1426 } REGP_DEL_KEYS
, *PREG_DEL_KEYS
;
1428 LIST_ENTRY delQueueHead
;
1429 PREG_DEL_KEYS delKeys
, newDelKeys
;
1432 PKEY_BASIC_INFORMATION BasicInfo
;
1433 PREG_DEL_KEYS KeyDelRoot
;
1434 NTSTATUS Status
= STATUS_SUCCESS
;
1435 NTSTATUS Status2
= STATUS_SUCCESS
;
1437 InitializeListHead(&delQueueHead
);
1439 ProcessHeap
= RtlGetProcessHeap();
1441 /* NOTE: no need to allocate enough memory for an additional KEY_BASIC_INFORMATION
1442 structure for the root key, we only do that for subkeys as we need to
1443 allocate REGP_DEL_KEYS structures anyway! */
1444 KeyDelRoot
= RtlAllocateHeap(ProcessHeap
,
1446 sizeof(REGP_DEL_KEYS
));
1447 if (KeyDelRoot
!= NULL
)
1449 KeyDelRoot
->KeyHandle
= hKey
;
1450 InsertTailList(&delQueueHead
,
1451 &KeyDelRoot
->ListEntry
);
1455 delKeys
= CONTAINING_RECORD(delQueueHead
.Flink
,
1464 /* check if this key contains subkeys and delete them first by queuing
1465 them at the head of the list */
1466 Status2
= NtEnumerateKey(delKeys
->KeyHandle
,
1468 KeyBasicInformation
,
1473 if (NT_SUCCESS(Status2
))
1475 OBJECT_ATTRIBUTES ObjectAttributes
;
1476 UNICODE_STRING SubKeyName
;
1478 ASSERT(newDelKeys
!= NULL
);
1479 ASSERT(BasicInfo
!= NULL
);
1481 /* don't use RtlInitUnicodeString as the string is not NULL-terminated! */
1482 SubKeyName
.Length
= BasicInfo
->NameLength
;
1483 SubKeyName
.MaximumLength
= BasicInfo
->NameLength
;
1484 SubKeyName
.Buffer
= BasicInfo
->Name
;
1486 InitializeObjectAttributes(&ObjectAttributes
,
1488 OBJ_CASE_INSENSITIVE
,
1492 /* open the subkey */
1493 Status2
= NtOpenKey(&newDelKeys
->KeyHandle
,
1494 DELETE
| KEY_ENUMERATE_SUB_KEYS
,
1496 if (!NT_SUCCESS(Status2
))
1501 /* enqueue this key to the head of the deletion queue */
1502 InsertHeadList(&delQueueHead
,
1503 &newDelKeys
->ListEntry
);
1505 /* try again from the head of the list */
1510 if (Status2
== STATUS_BUFFER_TOO_SMALL
)
1512 newDelKeys
= RtlAllocateHeap(ProcessHeap
,
1514 BufferSize
+ sizeof(REGP_DEL_KEYS
));
1515 if (newDelKeys
!= NULL
)
1517 BasicInfo
= (PKEY_BASIC_INFORMATION
)(newDelKeys
+ 1);
1520 goto ReadFirstSubKey
;
1524 /* don't break, let's try to delete as many keys as possible */
1525 Status2
= STATUS_INSUFFICIENT_RESOURCES
;
1526 goto SubKeyFailureNoFree
;
1529 else if (Status2
== STATUS_BUFFER_OVERFLOW
)
1531 PREG_DEL_KEYS newDelKeys2
;
1533 ASSERT(newDelKeys
!= NULL
);
1535 /* we need more memory to query the key name */
1536 newDelKeys2
= RtlReAllocateHeap(ProcessHeap
,
1539 BufferSize
+ sizeof(REGP_DEL_KEYS
));
1540 if (newDelKeys2
!= NULL
)
1542 newDelKeys
= newDelKeys2
;
1543 BasicInfo
= (PKEY_BASIC_INFORMATION
)(newDelKeys
+ 1);
1546 goto ReadFirstSubKey
;
1550 /* don't break, let's try to delete as many keys as possible */
1551 Status2
= STATUS_INSUFFICIENT_RESOURCES
;
1554 else if (Status2
== STATUS_NO_MORE_ENTRIES
)
1556 /* in some race conditions where another thread would delete
1557 the same tree at the same time, newDelKeys could actually
1559 if (newDelKeys
!= NULL
)
1561 RtlFreeHeap(ProcessHeap
,
1569 /* newDelKeys can be NULL here when NtEnumerateKey returned an
1570 error other than STATUS_BUFFER_TOO_SMALL or STATUS_BUFFER_OVERFLOW! */
1571 if (newDelKeys
!= NULL
)
1573 RtlFreeHeap(ProcessHeap
,
1578 SubKeyFailureNoFree
:
1579 /* don't break, let's try to delete as many keys as possible */
1580 if (NT_SUCCESS(Status
))
1586 Status2
= NtDeleteKey(delKeys
->KeyHandle
);
1588 /* NOTE: do NOT close the handle anymore, it's invalid already! */
1590 if (!NT_SUCCESS(Status2
))
1592 /* close the key handle so we don't leak handles for keys we were
1593 unable to delete. But only do this for handles not supplied
1596 if (delKeys
->KeyHandle
!= hKey
)
1598 NtClose(delKeys
->KeyHandle
);
1601 if (NT_SUCCESS(Status
))
1603 /* don't break, let's try to delete as many keys as possible */
1608 /* remove the entry from the list */
1609 RemoveEntryList(&delKeys
->ListEntry
);
1611 RtlFreeHeap(ProcessHeap
,
1614 } while (!IsListEmpty(&delQueueHead
));
1617 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1623 /************************************************************************
1629 RegDeleteTreeW(IN HKEY hKey
,
1630 IN LPCWSTR lpSubKey OPTIONAL
)
1632 HANDLE KeyHandle
, CurKey
, SubKeyHandle
= NULL
;
1635 Status
= MapDefaultKey(&KeyHandle
,
1637 if (!NT_SUCCESS(Status
))
1639 return RtlNtStatusToDosError(Status
);
1642 if (lpSubKey
!= NULL
)
1644 OBJECT_ATTRIBUTES ObjectAttributes
;
1645 UNICODE_STRING SubKeyName
;
1647 RtlInitUnicodeString(&SubKeyName
,
1650 InitializeObjectAttributes(&ObjectAttributes
,
1652 OBJ_CASE_INSENSITIVE
,
1656 Status
= NtOpenKey(&SubKeyHandle
,
1657 DELETE
| KEY_ENUMERATE_SUB_KEYS
,
1659 if (!NT_SUCCESS(Status
))
1664 CurKey
= SubKeyHandle
;
1669 Status
= RegpDeleteTree(CurKey
);
1671 if (NT_SUCCESS(Status
))
1673 /* make sure we only close hKey (KeyHandle) when the caller specified a
1674 subkey, because the handle would be invalid already! */
1675 if (CurKey
!= KeyHandle
)
1677 ClosePredefKey(KeyHandle
);
1680 return ERROR_SUCCESS
;
1684 /* make sure we close all handles we created! */
1685 if (SubKeyHandle
!= NULL
)
1687 NtClose(SubKeyHandle
);
1691 ClosePredefKey(KeyHandle
);
1693 return RtlNtStatusToDosError(Status
);
1699 /************************************************************************
1706 RegDeleteTreeW(HKEY hKey
,
1710 DWORD dwMaxSubkeyLen
, dwMaxValueLen
;
1711 DWORD dwMaxLen
, dwSize
;
1715 WCHAR szNameBuf
[MAX_PATH
], *lpszName
= szNameBuf
;
1717 TRACE("(hkey=%p,%p %s)\n", hKey
, lpszSubKey
, debugstr_w(lpszSubKey
));
1719 Status
= MapDefaultKey(&KeyHandle
,
1721 if (!NT_SUCCESS(Status
))
1723 return RtlNtStatusToDosError(Status
);
1726 hSubKey
= KeyHandle
;
1730 ret
= RegOpenKeyExW(KeyHandle
, lpszSubKey
, 0, KEY_READ
, &hSubKey
);
1733 ClosePredefKey(KeyHandle
);
1738 /* Get highest length for keys, values */
1739 ret
= RegQueryInfoKeyW(hSubKey
, NULL
, NULL
, NULL
, NULL
,
1740 &dwMaxSubkeyLen
, NULL
, NULL
, &dwMaxValueLen
, NULL
, NULL
, NULL
);
1741 if (ret
) goto cleanup
;
1745 dwMaxLen
= max(dwMaxSubkeyLen
, dwMaxValueLen
);
1746 if (dwMaxLen
> sizeof(szNameBuf
)/sizeof(WCHAR
))
1748 /* Name too big: alloc a buffer for it */
1749 if (!(lpszName
= RtlAllocateHeap( RtlGetProcessHeap(), 0, dwMaxLen
*sizeof(WCHAR
))))
1751 ret
= ERROR_NOT_ENOUGH_MEMORY
;
1757 /* Recursively delete all the subkeys */
1761 if (RegEnumKeyExW(hSubKey
, 0, lpszName
, &dwSize
, NULL
,
1762 NULL
, NULL
, NULL
)) break;
1764 ret
= RegDeleteTreeW(hSubKey
, lpszName
);
1765 if (ret
) goto cleanup
;
1769 ret
= RegDeleteKeyW(KeyHandle
, lpszSubKey
);
1774 if (RegEnumValueW(KeyHandle
, 0, lpszName
, &dwSize
,
1775 NULL
, NULL
, NULL
, NULL
)) break;
1777 ret
= RegDeleteValueW(KeyHandle
, lpszName
);
1778 if (ret
) goto cleanup
;
1782 /* Free buffer if allocated */
1783 if (lpszName
!= szNameBuf
)
1784 RtlFreeHeap( RtlGetProcessHeap(), 0, lpszName
);
1786 RegCloseKey(hSubKey
);
1788 ClosePredefKey(KeyHandle
);
1794 /************************************************************************
1800 RegDeleteTreeA(IN HKEY hKey
,
1801 IN LPCSTR lpSubKey OPTIONAL
)
1803 UNICODE_STRING SubKeyName
= { 0, 0, NULL
};
1806 if (lpSubKey
!= NULL
&&
1807 !RtlCreateUnicodeStringFromAsciiz(&SubKeyName
,
1810 return ERROR_NOT_ENOUGH_MEMORY
;
1813 Ret
= RegDeleteTreeW(hKey
,
1816 RtlFreeUnicodeString(&SubKeyName
);
1822 /************************************************************************
1823 * RegDisableReflectionKey
1828 RegDisableReflectionKey(IN HKEY hBase
)
1830 FIXME("RegDisableReflectionKey(0x%p) UNIMPLEMENTED!\n", hBase
);
1831 return ERROR_CALL_NOT_IMPLEMENTED
;
1835 /************************************************************************
1836 * RegEnableReflectionKey
1841 RegEnableReflectionKey(IN HKEY hBase
)
1843 FIXME("RegEnableReflectionKey(0x%p) UNIMPLEMENTED!\n", hBase
);
1844 return ERROR_CALL_NOT_IMPLEMENTED
;
1848 /******************************************************************************
1849 * RegpApplyRestrictions [internal]
1851 * Helper function for RegGetValueA/W.
1854 RegpApplyRestrictions(DWORD dwFlags
,
1859 /* Check if the type is restricted by the passed flags */
1860 if (*ret
== ERROR_SUCCESS
|| *ret
== ERROR_MORE_DATA
)
1866 case REG_NONE
: dwMask
= RRF_RT_REG_NONE
; break;
1867 case REG_SZ
: dwMask
= RRF_RT_REG_SZ
; break;
1868 case REG_EXPAND_SZ
: dwMask
= RRF_RT_REG_EXPAND_SZ
; break;
1869 case REG_MULTI_SZ
: dwMask
= RRF_RT_REG_MULTI_SZ
; break;
1870 case REG_BINARY
: dwMask
= RRF_RT_REG_BINARY
; break;
1871 case REG_DWORD
: dwMask
= RRF_RT_REG_DWORD
; break;
1872 case REG_QWORD
: dwMask
= RRF_RT_REG_QWORD
; break;
1875 if (dwFlags
& dwMask
)
1877 /* Type is not restricted, check for size mismatch */
1878 if (dwType
== REG_BINARY
)
1882 if ((dwFlags
& RRF_RT_ANY
) == RRF_RT_DWORD
)
1884 else if ((dwFlags
& RRF_RT_ANY
) == RRF_RT_QWORD
)
1887 if (cbExpect
&& cbData
!= cbExpect
)
1888 *ret
= ERROR_DATATYPE_MISMATCH
;
1891 else *ret
= ERROR_UNSUPPORTED_TYPE
;
1896 /******************************************************************************
1897 * RegGetValueW [ADVAPI32.@]
1899 * Retrieves the type and data for a value name associated with a key,
1900 * optionally expanding its content and restricting its type.
1903 * hKey [I] Handle to an open key.
1904 * pszSubKey [I] Name of the subkey of hKey.
1905 * pszValue [I] Name of value under hKey/szSubKey to query.
1906 * dwFlags [I] Flags restricting the value type to retrieve.
1907 * pdwType [O] Destination for the values type, may be NULL.
1908 * pvData [O] Destination for the values content, may be NULL.
1909 * pcbData [I/O] Size of pvData, updated with the size in bytes required to
1910 * retrieve the whole content, including the trailing '\0'
1914 * Success: ERROR_SUCCESS
1915 * Failure: nonzero error code from Winerror.h
1918 * - Unless RRF_NOEXPAND is specified, REG_EXPAND_SZ values are automatically
1919 * expanded and pdwType is set to REG_SZ instead.
1920 * - Restrictions are applied after expanding, using RRF_RT_REG_EXPAND_SZ
1921 * without RRF_NOEXPAND is thus not allowed.
1922 * An exception is the case where RRF_RT_ANY is specified, because then
1923 * RRF_NOEXPAND is allowed.
1926 RegGetValueW(HKEY hKey
,
1934 DWORD dwType
, cbData
= pcbData
? *pcbData
: 0;
1938 TRACE("(%p,%s,%s,%ld,%p,%p,%p=%ld)\n",
1939 hKey
, debugstr_w(pszSubKey
), debugstr_w(pszValue
), dwFlags
, pdwType
,
1940 pvData
, pcbData
, cbData
);
1942 if (pvData
&& !pcbData
)
1943 return ERROR_INVALID_PARAMETER
;
1944 if ((dwFlags
& RRF_RT_REG_EXPAND_SZ
) && !(dwFlags
& RRF_NOEXPAND
) &&
1945 ((dwFlags
& RRF_RT_ANY
) != RRF_RT_ANY
))
1946 return ERROR_INVALID_PARAMETER
;
1948 if (pszSubKey
&& pszSubKey
[0])
1950 ret
= RegOpenKeyExW(hKey
, pszSubKey
, 0, KEY_QUERY_VALUE
, &hKey
);
1951 if (ret
!= ERROR_SUCCESS
) return ret
;
1954 ret
= RegQueryValueExW(hKey
, pszValue
, NULL
, &dwType
, pvData
, &cbData
);
1956 /* If we are going to expand we need to read in the whole the value even
1957 * if the passed buffer was too small as the expanded string might be
1958 * smaller than the unexpanded one and could fit into cbData bytes. */
1959 if ((ret
== ERROR_SUCCESS
|| ret
== ERROR_MORE_DATA
) &&
1960 dwType
== REG_EXPAND_SZ
&& !(dwFlags
& RRF_NOEXPAND
))
1964 HeapFree(GetProcessHeap(), 0, pvBuf
);
1966 pvBuf
= HeapAlloc(GetProcessHeap(), 0, cbData
);
1969 ret
= ERROR_NOT_ENOUGH_MEMORY
;
1973 if (ret
== ERROR_MORE_DATA
|| !pvData
)
1974 ret
= RegQueryValueExW(hKey
, pszValue
, NULL
,
1975 &dwType
, pvBuf
, &cbData
);
1978 /* Even if cbData was large enough we have to copy the
1979 * string since ExpandEnvironmentStrings can't handle
1980 * overlapping buffers. */
1981 CopyMemory(pvBuf
, pvData
, cbData
);
1984 /* Both the type or the value itself could have been modified in
1985 * between so we have to keep retrying until the buffer is large
1986 * enough or we no longer have to expand the value. */
1988 while (dwType
== REG_EXPAND_SZ
&& ret
== ERROR_MORE_DATA
);
1990 if (ret
== ERROR_SUCCESS
)
1992 /* Recheck dwType in case it changed since the first call */
1993 if (dwType
== REG_EXPAND_SZ
)
1995 cbData
= ExpandEnvironmentStringsW(pvBuf
, pvData
,
1996 pcbData
? *pcbData
: 0) * sizeof(WCHAR
);
1998 if (pvData
&& pcbData
&& cbData
> *pcbData
)
1999 ret
= ERROR_MORE_DATA
;
2002 CopyMemory(pvData
, pvBuf
, *pcbData
);
2005 HeapFree(GetProcessHeap(), 0, pvBuf
);
2008 if (pszSubKey
&& pszSubKey
[0])
2011 RegpApplyRestrictions(dwFlags
, dwType
, cbData
, &ret
);
2013 if (pvData
&& ret
!= ERROR_SUCCESS
&& (dwFlags
& RRF_ZEROONFAILURE
))
2014 ZeroMemory(pvData
, *pcbData
);
2026 /******************************************************************************
2027 * RegGetValueA [ADVAPI32.@]
2032 RegGetValueA(HKEY hKey
,
2040 DWORD dwType
, cbData
= pcbData
? *pcbData
: 0;
2044 TRACE("(%p,%s,%s,%ld,%p,%p,%p=%ld)\n",
2045 hKey
, pszSubKey
, pszValue
, dwFlags
, pdwType
, pvData
, pcbData
,
2048 if (pvData
&& !pcbData
)
2049 return ERROR_INVALID_PARAMETER
;
2050 if ((dwFlags
& RRF_RT_REG_EXPAND_SZ
) && !(dwFlags
& RRF_NOEXPAND
) &&
2051 ((dwFlags
& RRF_RT_ANY
) != RRF_RT_ANY
))
2052 return ERROR_INVALID_PARAMETER
;
2054 if (pszSubKey
&& pszSubKey
[0])
2056 ret
= RegOpenKeyExA(hKey
, pszSubKey
, 0, KEY_QUERY_VALUE
, &hKey
);
2057 if (ret
!= ERROR_SUCCESS
) return ret
;
2060 ret
= RegQueryValueExA(hKey
, pszValue
, NULL
, &dwType
, pvData
, &cbData
);
2062 /* If we are going to expand we need to read in the whole the value even
2063 * if the passed buffer was too small as the expanded string might be
2064 * smaller than the unexpanded one and could fit into cbData bytes. */
2065 if ((ret
== ERROR_SUCCESS
|| ret
== ERROR_MORE_DATA
) &&
2066 (dwType
== REG_EXPAND_SZ
&& !(dwFlags
& RRF_NOEXPAND
)))
2069 HeapFree(GetProcessHeap(), 0, pvBuf
);
2071 pvBuf
= HeapAlloc(GetProcessHeap(), 0, cbData
);
2074 ret
= ERROR_NOT_ENOUGH_MEMORY
;
2078 if (ret
== ERROR_MORE_DATA
|| !pvData
)
2079 ret
= RegQueryValueExA(hKey
, pszValue
, NULL
,
2080 &dwType
, pvBuf
, &cbData
);
2083 /* Even if cbData was large enough we have to copy the
2084 * string since ExpandEnvironmentStrings can't handle
2085 * overlapping buffers. */
2086 CopyMemory(pvBuf
, pvData
, cbData
);
2089 /* Both the type or the value itself could have been modified in
2090 * between so we have to keep retrying until the buffer is large
2091 * enough or we no longer have to expand the value. */
2092 } while (dwType
== REG_EXPAND_SZ
&& ret
== ERROR_MORE_DATA
);
2094 if (ret
== ERROR_SUCCESS
)
2096 /* Recheck dwType in case it changed since the first call */
2097 if (dwType
== REG_EXPAND_SZ
)
2099 cbData
= ExpandEnvironmentStringsA(pvBuf
, pvData
,
2100 pcbData
? *pcbData
: 0);
2102 if(pvData
&& pcbData
&& cbData
> *pcbData
)
2103 ret
= ERROR_MORE_DATA
;
2106 CopyMemory(pvData
, pvBuf
, *pcbData
);
2109 HeapFree(GetProcessHeap(), 0, pvBuf
);
2112 if (pszSubKey
&& pszSubKey
[0])
2115 RegpApplyRestrictions(dwFlags
, dwType
, cbData
, &ret
);
2117 if (pvData
&& ret
!= ERROR_SUCCESS
&& (dwFlags
& RRF_ZEROONFAILURE
))
2118 ZeroMemory(pvData
, *pcbData
);
2120 if (pdwType
) *pdwType
= dwType
;
2121 if (pcbData
) *pcbData
= cbData
;
2127 /************************************************************************
2133 RegSetKeyValueW(IN HKEY hKey
,
2134 IN LPCWSTR lpSubKey OPTIONAL
,
2135 IN LPCWSTR lpValueName OPTIONAL
,
2137 IN LPCVOID lpData OPTIONAL
,
2140 HANDLE KeyHandle
, CurKey
, SubKeyHandle
= NULL
;
2144 Status
= MapDefaultKey(&KeyHandle
,
2146 if (!NT_SUCCESS(Status
))
2148 return RtlNtStatusToDosError(Status
);
2151 if (lpSubKey
!= NULL
)
2153 OBJECT_ATTRIBUTES ObjectAttributes
;
2154 UNICODE_STRING SubKeyName
;
2156 RtlInitUnicodeString(&SubKeyName
,
2159 InitializeObjectAttributes(&ObjectAttributes
,
2161 OBJ_CASE_INSENSITIVE
,
2165 Status
= NtOpenKey(&SubKeyHandle
,
2168 if (!NT_SUCCESS(Status
))
2170 Ret
= RtlNtStatusToDosError(Status
);
2174 CurKey
= SubKeyHandle
;
2179 Ret
= RegSetValueExW(CurKey
,
2186 if (SubKeyHandle
!= NULL
)
2188 NtClose(SubKeyHandle
);
2192 ClosePredefKey(KeyHandle
);
2198 /************************************************************************
2204 RegSetKeyValueA(IN HKEY hKey
,
2205 IN LPCSTR lpSubKey OPTIONAL
,
2206 IN LPCSTR lpValueName OPTIONAL
,
2208 IN LPCVOID lpData OPTIONAL
,
2211 HANDLE KeyHandle
, CurKey
, SubKeyHandle
= NULL
;
2215 Status
= MapDefaultKey(&KeyHandle
,
2217 if (!NT_SUCCESS(Status
))
2219 return RtlNtStatusToDosError(Status
);
2222 if (lpSubKey
!= NULL
)
2224 OBJECT_ATTRIBUTES ObjectAttributes
;
2225 UNICODE_STRING SubKeyName
;
2227 if (!RtlCreateUnicodeStringFromAsciiz(&SubKeyName
,
2230 Ret
= ERROR_NOT_ENOUGH_MEMORY
;
2234 InitializeObjectAttributes(&ObjectAttributes
,
2236 OBJ_CASE_INSENSITIVE
,
2240 Status
= NtOpenKey(&SubKeyHandle
,
2244 RtlFreeUnicodeString(&SubKeyName
);
2246 if (!NT_SUCCESS(Status
))
2248 Ret
= RtlNtStatusToDosError(Status
);
2252 CurKey
= SubKeyHandle
;
2257 Ret
= RegSetValueExA(CurKey
,
2264 if (SubKeyHandle
!= NULL
)
2266 NtClose(SubKeyHandle
);
2270 ClosePredefKey(KeyHandle
);
2276 /************************************************************************
2282 RegDeleteValueA(HKEY hKey
,
2285 UNICODE_STRING ValueName
;
2289 Status
= MapDefaultKey(&KeyHandle
,
2291 if (!NT_SUCCESS(Status
))
2293 return RtlNtStatusToDosError(Status
);
2296 RtlCreateUnicodeStringFromAsciiz(&ValueName
,
2297 (LPSTR
)lpValueName
);
2298 Status
= NtDeleteValueKey(KeyHandle
,
2300 RtlFreeUnicodeString (&ValueName
);
2302 ClosePredefKey(KeyHandle
);
2304 if (!NT_SUCCESS(Status
))
2306 return RtlNtStatusToDosError(Status
);
2309 return ERROR_SUCCESS
;
2313 /************************************************************************
2319 RegDeleteValueW(HKEY hKey
,
2320 LPCWSTR lpValueName
)
2322 UNICODE_STRING ValueName
;
2326 Status
= MapDefaultKey(&KeyHandle
,
2328 if (!NT_SUCCESS(Status
))
2330 return RtlNtStatusToDosError(Status
);
2333 RtlInitUnicodeString(&ValueName
,
2334 (LPWSTR
)lpValueName
);
2336 Status
= NtDeleteValueKey(KeyHandle
,
2339 ClosePredefKey(KeyHandle
);
2341 if (!NT_SUCCESS(Status
))
2343 return RtlNtStatusToDosError(Status
);
2346 return ERROR_SUCCESS
;
2350 /************************************************************************
2356 RegEnumKeyA(HKEY hKey
,
2364 return RegEnumKeyExA(hKey
,
2375 /************************************************************************
2381 RegEnumKeyW(HKEY hKey
,
2389 return RegEnumKeyExW(hKey
,
2400 /************************************************************************
2406 RegEnumKeyExA(HKEY hKey
,
2413 PFILETIME lpftLastWriteTime
)
2417 KEY_NODE_INFORMATION Node
;
2418 KEY_BASIC_INFORMATION Basic
;
2421 UNICODE_STRING StringU
;
2422 ANSI_STRING StringA
;
2423 LONG ErrorCode
= ERROR_SUCCESS
;
2425 DWORD ClassLength
= 0;
2431 TRACE("RegEnumKeyExA(hKey 0x%x, dwIndex %d, lpName 0x%x, *lpcbName %d, lpClass 0x%x, lpcbClass %d)\n",
2432 hKey
, dwIndex
, lpName
, *lpcbName
, lpClass
, lpcbClass
? *lpcbClass
: 0);
2434 if ((lpClass
) && (!lpcbClass
))
2436 return ERROR_INVALID_PARAMETER
;
2439 Status
= MapDefaultKey(&KeyHandle
, hKey
);
2440 if (!NT_SUCCESS(Status
))
2442 return RtlNtStatusToDosError(Status
);
2447 NameLength
= min (*lpcbName
- 1 , REG_MAX_NAME_SIZE
) * sizeof (WCHAR
);
2458 ClassLength
= min (*lpcbClass
-1, REG_MAX_NAME_SIZE
) * sizeof(WCHAR
);
2465 /* The class name should start at a dword boundary */
2466 BufferSize
= ((sizeof(KEY_NODE_INFORMATION
) + NameLength
+ 3) & ~3) + ClassLength
;
2470 BufferSize
= sizeof(KEY_BASIC_INFORMATION
) + NameLength
;
2473 KeyInfo
= RtlAllocateHeap (ProcessHeap
, 0, BufferSize
);
2474 if (KeyInfo
== NULL
)
2476 ErrorCode
= ERROR_OUTOFMEMORY
;
2480 Status
= NtEnumerateKey(KeyHandle
,
2482 lpClass
== NULL
? KeyBasicInformation
: KeyNodeInformation
,
2486 TRACE("NtEnumerateKey() returned status 0x%X\n", Status
);
2487 if (!NT_SUCCESS(Status
))
2489 ErrorCode
= RtlNtStatusToDosError (Status
);
2493 if (lpClass
== NULL
)
2495 if (KeyInfo
->Basic
.NameLength
> NameLength
)
2497 ErrorCode
= ERROR_BUFFER_OVERFLOW
;
2501 StringU
.Buffer
= KeyInfo
->Basic
.Name
;
2502 StringU
.Length
= KeyInfo
->Basic
.NameLength
;
2503 StringU
.MaximumLength
= KeyInfo
->Basic
.NameLength
;
2508 if (KeyInfo
->Node
.NameLength
> NameLength
||
2509 KeyInfo
->Node
.ClassLength
> ClassLength
)
2511 ErrorCode
= ERROR_BUFFER_OVERFLOW
;
2515 StringA
.Buffer
= lpClass
;
2517 StringA
.MaximumLength
= *lpcbClass
;
2518 StringU
.Buffer
= (PWCHAR
)((ULONG_PTR
)KeyInfo
->Node
.Name
+ KeyInfo
->Node
.ClassOffset
);
2519 StringU
.Length
= KeyInfo
->Node
.ClassLength
;
2520 StringU
.MaximumLength
= KeyInfo
->Node
.ClassLength
;
2521 RtlUnicodeStringToAnsiString (&StringA
, &StringU
, FALSE
);
2522 lpClass
[StringA
.Length
] = 0;
2523 *lpcbClass
= StringA
.Length
;
2524 StringU
.Buffer
= KeyInfo
->Node
.Name
;
2525 StringU
.Length
= KeyInfo
->Node
.NameLength
;
2526 StringU
.MaximumLength
= KeyInfo
->Node
.NameLength
;
2530 if (ErrorCode
== ERROR_SUCCESS
)
2532 StringA
.Buffer
= lpName
;
2534 StringA
.MaximumLength
= *lpcbName
;
2535 RtlUnicodeStringToAnsiString (&StringA
, &StringU
, FALSE
);
2536 lpName
[StringA
.Length
] = 0;
2537 *lpcbName
= StringA
.Length
;
2538 if (lpftLastWriteTime
!= NULL
)
2540 if (lpClass
== NULL
)
2542 lpftLastWriteTime
->dwLowDateTime
= KeyInfo
->Basic
.LastWriteTime
.u
.LowPart
;
2543 lpftLastWriteTime
->dwHighDateTime
= KeyInfo
->Basic
.LastWriteTime
.u
.HighPart
;
2547 lpftLastWriteTime
->dwLowDateTime
= KeyInfo
->Node
.LastWriteTime
.u
.LowPart
;
2548 lpftLastWriteTime
->dwHighDateTime
= KeyInfo
->Node
.LastWriteTime
.u
.HighPart
;
2554 /*TRACE("Key Namea0 Length %d\n", StringU.Length);*/ /* BUGBUG could be uninitialized */
2555 TRACE("Key Name1 Length %d\n", NameLength
);
2556 TRACE("Key Name Length %d\n", *lpcbName
);
2557 TRACE("Key Name %s\n", lpName
);
2559 RtlFreeHeap(ProcessHeap
,
2564 ClosePredefKey(KeyHandle
);
2570 /************************************************************************
2576 RegEnumKeyExW(HKEY hKey
,
2583 PFILETIME lpftLastWriteTime
)
2587 KEY_NODE_INFORMATION Node
;
2588 KEY_BASIC_INFORMATION Basic
;
2594 ULONG ClassLength
= 0;
2596 LONG ErrorCode
= ERROR_SUCCESS
;
2599 Status
= MapDefaultKey(&KeyHandle
,
2601 if (!NT_SUCCESS(Status
))
2603 return RtlNtStatusToDosError(Status
);
2608 NameLength
= min (*lpcbName
- 1, REG_MAX_NAME_SIZE
) * sizeof (WCHAR
);
2619 ClassLength
= min (*lpcbClass
- 1, REG_MAX_NAME_SIZE
) * sizeof(WCHAR
);
2626 BufferSize
= ((sizeof(KEY_NODE_INFORMATION
) + NameLength
+ 3) & ~3) + ClassLength
;
2630 BufferSize
= sizeof(KEY_BASIC_INFORMATION
) + NameLength
;
2633 KeyInfo
= RtlAllocateHeap(ProcessHeap
,
2636 if (KeyInfo
== NULL
)
2638 ErrorCode
= ERROR_OUTOFMEMORY
;
2642 Status
= NtEnumerateKey(KeyHandle
,
2644 lpClass
? KeyNodeInformation
: KeyBasicInformation
,
2648 TRACE("NtEnumerateKey() returned status 0x%X\n", Status
);
2649 if (!NT_SUCCESS(Status
))
2651 ErrorCode
= RtlNtStatusToDosError (Status
);
2655 if (lpClass
== NULL
)
2657 if (KeyInfo
->Basic
.NameLength
> NameLength
)
2659 ErrorCode
= ERROR_BUFFER_OVERFLOW
;
2663 RtlCopyMemory(lpName
,
2664 KeyInfo
->Basic
.Name
,
2665 KeyInfo
->Basic
.NameLength
);
2666 *lpcbName
= (DWORD
)(KeyInfo
->Basic
.NameLength
/ sizeof(WCHAR
));
2667 lpName
[*lpcbName
] = 0;
2672 if (KeyInfo
->Node
.NameLength
> NameLength
||
2673 KeyInfo
->Node
.ClassLength
> ClassLength
)
2675 ErrorCode
= ERROR_BUFFER_OVERFLOW
;
2679 RtlCopyMemory(lpName
,
2681 KeyInfo
->Node
.NameLength
);
2682 *lpcbName
= KeyInfo
->Node
.NameLength
/ sizeof(WCHAR
);
2683 lpName
[*lpcbName
] = 0;
2684 RtlCopyMemory(lpClass
,
2685 (PVOID
)((ULONG_PTR
)KeyInfo
->Node
.Name
+ KeyInfo
->Node
.ClassOffset
),
2686 KeyInfo
->Node
.ClassLength
);
2687 *lpcbClass
= (DWORD
)(KeyInfo
->Node
.ClassLength
/ sizeof(WCHAR
));
2688 lpClass
[*lpcbClass
] = 0;
2692 if (ErrorCode
== ERROR_SUCCESS
&& lpftLastWriteTime
!= NULL
)
2694 if (lpClass
== NULL
)
2696 lpftLastWriteTime
->dwLowDateTime
= KeyInfo
->Basic
.LastWriteTime
.u
.LowPart
;
2697 lpftLastWriteTime
->dwHighDateTime
= KeyInfo
->Basic
.LastWriteTime
.u
.HighPart
;
2701 lpftLastWriteTime
->dwLowDateTime
= KeyInfo
->Node
.LastWriteTime
.u
.LowPart
;
2702 lpftLastWriteTime
->dwHighDateTime
= KeyInfo
->Node
.LastWriteTime
.u
.HighPart
;
2707 RtlFreeHeap(ProcessHeap
,
2712 ClosePredefKey(KeyHandle
);
2718 /************************************************************************
2724 RegEnumValueA(HKEY hKey
,
2736 char buffer
[256], *buf_ptr
= buffer
;
2737 KEY_VALUE_FULL_INFORMATION
*info
= (KEY_VALUE_FULL_INFORMATION
*)buffer
;
2738 static const int info_size
= FIELD_OFFSET( KEY_VALUE_FULL_INFORMATION
, Name
);
2740 //TRACE("(%p,%ld,%p,%p,%p,%p,%p,%p)\n",
2741 // hkey, index, value, val_count, reserved, type, data, count );
2743 /* NT only checks count, not val_count */
2744 if ((data
&& !count
) || reserved
)
2745 return ERROR_INVALID_PARAMETER
;
2747 status
= MapDefaultKey(&KeyHandle
, hKey
);
2748 if (!NT_SUCCESS(status
))
2750 return RtlNtStatusToDosError(status
);
2753 total_size
= info_size
+ (MAX_PATH
+ 1) * sizeof(WCHAR
);
2754 if (data
) total_size
+= *count
;
2755 total_size
= min( sizeof(buffer
), total_size
);
2757 status
= NtEnumerateValueKey( KeyHandle
, index
, KeyValueFullInformation
,
2758 buffer
, total_size
, &total_size
);
2759 if (status
&& (status
!= STATUS_BUFFER_OVERFLOW
) && (status
!= STATUS_BUFFER_TOO_SMALL
)) goto done
;
2761 /* we need to fetch the contents for a string type even if not requested,
2762 * because we need to compute the length of the ASCII string. */
2763 if (value
|| data
|| is_string(info
->Type
))
2765 /* retry with a dynamically allocated buffer */
2766 while ((status
== STATUS_BUFFER_OVERFLOW
) || (status
== STATUS_BUFFER_TOO_SMALL
))
2768 if (buf_ptr
!= buffer
) HeapFree( GetProcessHeap(), 0, buf_ptr
);
2769 if (!(buf_ptr
= HeapAlloc( GetProcessHeap(), 0, total_size
)))
2771 status
= STATUS_INSUFFICIENT_RESOURCES
;
2774 info
= (KEY_VALUE_FULL_INFORMATION
*)buf_ptr
;
2775 status
= NtEnumerateValueKey( KeyHandle
, index
, KeyValueFullInformation
,
2776 buf_ptr
, total_size
, &total_size
);
2779 if (status
) goto done
;
2781 if (is_string(info
->Type
))
2784 RtlUnicodeToMultiByteSize( &len
, (WCHAR
*)(buf_ptr
+ info
->DataOffset
),
2788 if (len
> *count
) status
= STATUS_BUFFER_OVERFLOW
;
2791 RtlUnicodeToMultiByteN( (PCHAR
)data
, len
, NULL
, (WCHAR
*)(buf_ptr
+ info
->DataOffset
),
2793 /* if the type is REG_SZ and data is not 0-terminated
2794 * and there is enough space in the buffer NT appends a \0 */
2795 if (len
< *count
&& data
[len
-1]) data
[len
] = 0;
2798 info
->DataLength
= len
;
2802 if (info
->DataLength
> *count
) status
= STATUS_BUFFER_OVERFLOW
;
2803 else memcpy( data
, buf_ptr
+ info
->DataOffset
, info
->DataLength
);
2806 if (value
&& !status
)
2810 RtlUnicodeToMultiByteSize( &len
, info
->Name
, info
->NameLength
);
2811 if (len
>= *val_count
)
2813 status
= STATUS_BUFFER_OVERFLOW
;
2816 len
= *val_count
- 1;
2817 RtlUnicodeToMultiByteN( value
, len
, NULL
, info
->Name
, info
->NameLength
);
2823 RtlUnicodeToMultiByteN( value
, len
, NULL
, info
->Name
, info
->NameLength
);
2829 else status
= STATUS_SUCCESS
;
2831 if (type
) *type
= info
->Type
;
2832 if (count
) *count
= info
->DataLength
;
2835 if (buf_ptr
!= buffer
) HeapFree( GetProcessHeap(), 0, buf_ptr
);
2836 ClosePredefKey(KeyHandle
);
2837 return RtlNtStatusToDosError(status
);
2841 /******************************************************************************
2842 * RegEnumValueW [ADVAPI32.@]
2846 * hkey [I] Handle to key to query
2847 * index [I] Index of value to query
2848 * value [O] Value string
2849 * val_count [I/O] Size of value buffer (in wchars)
2850 * reserved [I] Reserved
2851 * type [O] Type code
2852 * data [O] Value data
2853 * count [I/O] Size of data buffer (in bytes)
2856 * Success: ERROR_SUCCESS
2857 * Failure: nonzero error code from Winerror.h
2860 RegEnumValueW(HKEY hKey
,
2872 char buffer
[256], *buf_ptr
= buffer
;
2873 KEY_VALUE_FULL_INFORMATION
*info
= (KEY_VALUE_FULL_INFORMATION
*)buffer
;
2874 static const int info_size
= FIELD_OFFSET( KEY_VALUE_FULL_INFORMATION
, Name
);
2876 //TRACE("(%p,%ld,%p,%p,%p,%p,%p,%p)\n",
2877 // hkey, index, value, val_count, reserved, type, data, count );
2879 /* NT only checks count, not val_count */
2880 if ((data
&& !count
) || reserved
) return ERROR_INVALID_PARAMETER
;
2882 status
= MapDefaultKey(&KeyHandle
, hKey
);
2883 if (!NT_SUCCESS(status
))
2885 return RtlNtStatusToDosError(status
);
2888 total_size
= info_size
+ (MAX_PATH
+ 1) * sizeof(WCHAR
);
2889 if (data
) total_size
+= *count
;
2890 total_size
= min( sizeof(buffer
), total_size
);
2892 status
= NtEnumerateValueKey( KeyHandle
, index
, KeyValueFullInformation
,
2893 buffer
, total_size
, &total_size
);
2894 if (status
&& (status
!= STATUS_BUFFER_OVERFLOW
) && (status
!= STATUS_BUFFER_TOO_SMALL
)) goto done
;
2898 /* retry with a dynamically allocated buffer */
2899 while ((status
== STATUS_BUFFER_OVERFLOW
) || (status
== STATUS_BUFFER_TOO_SMALL
))
2901 if (buf_ptr
!= buffer
) HeapFree( GetProcessHeap(), 0, buf_ptr
);
2902 if (!(buf_ptr
= HeapAlloc( GetProcessHeap(), 0, total_size
)))
2904 status
= ERROR_NOT_ENOUGH_MEMORY
;
2907 info
= (KEY_VALUE_FULL_INFORMATION
*)buf_ptr
;
2908 status
= NtEnumerateValueKey( KeyHandle
, index
, KeyValueFullInformation
,
2909 buf_ptr
, total_size
, &total_size
);
2912 if (status
) goto done
;
2916 if (info
->NameLength
/sizeof(WCHAR
) >= *val_count
)
2918 status
= STATUS_BUFFER_OVERFLOW
;
2921 memcpy( value
, info
->Name
, info
->NameLength
);
2922 *val_count
= info
->NameLength
/ sizeof(WCHAR
);
2923 value
[*val_count
] = 0;
2928 if (info
->DataLength
> *count
)
2930 status
= STATUS_BUFFER_OVERFLOW
;
2933 memcpy( data
, buf_ptr
+ info
->DataOffset
, info
->DataLength
);
2934 if (is_string(info
->Type
) && info
->DataLength
<= *count
- sizeof(WCHAR
))
2936 /* if the type is REG_SZ and data is not 0-terminated
2937 * and there is enough space in the buffer NT appends a \0 */
2938 WCHAR
*ptr
= (WCHAR
*)(data
+ info
->DataLength
);
2939 if (ptr
> (WCHAR
*)data
&& ptr
[-1]) *ptr
= 0;
2943 else status
= STATUS_SUCCESS
;
2946 if (type
) *type
= info
->Type
;
2947 if (count
) *count
= info
->DataLength
;
2950 if (buf_ptr
!= buffer
) HeapFree( GetProcessHeap(), 0, buf_ptr
);
2951 ClosePredefKey(KeyHandle
);
2952 return RtlNtStatusToDosError(status
);
2956 /************************************************************************
2962 RegFlushKey(HKEY hKey
)
2967 if (hKey
== HKEY_PERFORMANCE_DATA
)
2969 return ERROR_SUCCESS
;
2972 Status
= MapDefaultKey(&KeyHandle
,
2974 if (!NT_SUCCESS(Status
))
2976 return RtlNtStatusToDosError(Status
);
2979 Status
= NtFlushKey(KeyHandle
);
2981 ClosePredefKey(KeyHandle
);
2983 if (!NT_SUCCESS(Status
))
2985 return RtlNtStatusToDosError(Status
);
2988 return ERROR_SUCCESS
;
2992 /************************************************************************
2998 RegGetKeySecurity(HKEY hKey
,
2999 SECURITY_INFORMATION SecurityInformation
,
3000 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
3001 LPDWORD lpcbSecurityDescriptor
)
3006 if (hKey
== HKEY_PERFORMANCE_DATA
)
3008 return ERROR_INVALID_HANDLE
;
3011 Status
= MapDefaultKey(&KeyHandle
,
3013 if (!NT_SUCCESS(Status
))
3015 TRACE("MapDefaultKey() failed (Status %lx)\n", Status
);
3016 return RtlNtStatusToDosError(Status
);
3019 Status
= NtQuerySecurityObject(KeyHandle
,
3020 SecurityInformation
,
3021 pSecurityDescriptor
,
3022 *lpcbSecurityDescriptor
,
3023 lpcbSecurityDescriptor
);
3025 ClosePredefKey(KeyHandle
);
3027 if (!NT_SUCCESS(Status
))
3029 WARN("NtQuerySecurityObject() failed (Status %lx)\n", Status
);
3030 return RtlNtStatusToDosError(Status
);
3033 return ERROR_SUCCESS
;
3037 /************************************************************************
3043 RegLoadKeyA(HKEY hKey
,
3047 UNICODE_STRING FileName
;
3048 UNICODE_STRING KeyName
;
3051 RtlCreateUnicodeStringFromAsciiz(&KeyName
,
3053 RtlCreateUnicodeStringFromAsciiz(&FileName
,
3056 ErrorCode
= RegLoadKeyW(hKey
,
3060 RtlFreeUnicodeString(&FileName
);
3061 RtlFreeUnicodeString(&KeyName
);
3067 /************************************************************************
3073 RegLoadKeyW(HKEY hKey
,
3077 OBJECT_ATTRIBUTES FileObjectAttributes
;
3078 OBJECT_ATTRIBUTES KeyObjectAttributes
;
3079 UNICODE_STRING FileName
;
3080 UNICODE_STRING KeyName
;
3083 LONG ErrorCode
= ERROR_SUCCESS
;
3085 if (hKey
== HKEY_PERFORMANCE_DATA
)
3087 return ERROR_INVALID_HANDLE
;
3090 Status
= MapDefaultKey(&KeyHandle
,
3092 if (!NT_SUCCESS(Status
))
3094 return RtlNtStatusToDosError(Status
);
3097 if (!RtlDosPathNameToNtPathName_U(lpFile
,
3102 ErrorCode
= ERROR_BAD_PATHNAME
;
3106 InitializeObjectAttributes(&FileObjectAttributes
,
3108 OBJ_CASE_INSENSITIVE
,
3112 RtlInitUnicodeString(&KeyName
,
3115 InitializeObjectAttributes(&KeyObjectAttributes
,
3117 OBJ_CASE_INSENSITIVE
,
3121 Status
= NtLoadKey(&KeyObjectAttributes
,
3122 &FileObjectAttributes
);
3124 RtlFreeHeap(RtlGetProcessHeap(),
3128 if (!NT_SUCCESS(Status
))
3130 ErrorCode
= RtlNtStatusToDosError(Status
);
3135 ClosePredefKey(KeyHandle
);
3141 /************************************************************************
3142 * RegNotifyChangeKeyValue
3147 RegNotifyChangeKeyValue(HKEY hKey
,
3149 DWORD dwNotifyFilter
,
3153 IO_STATUS_BLOCK IoStatusBlock
;
3156 LONG ErrorCode
= ERROR_SUCCESS
;
3158 if (hKey
== HKEY_PERFORMANCE_DATA
)
3160 return ERROR_INVALID_HANDLE
;
3163 if (fAsynchronous
== TRUE
&& hEvent
== NULL
)
3165 return ERROR_INVALID_PARAMETER
;
3168 Status
= MapDefaultKey(&KeyHandle
,
3170 if (!NT_SUCCESS(Status
))
3172 return RtlNtStatusToDosError(Status
);
3175 /* FIXME: Remote key handles must fail */
3177 Status
= NtNotifyChangeKey(KeyHandle
,
3187 if (!NT_SUCCESS(Status
) && Status
!= STATUS_TIMEOUT
)
3189 ErrorCode
= RtlNtStatusToDosError(Status
);
3192 ClosePredefKey(KeyHandle
);
3198 /************************************************************************
3199 * RegOpenCurrentUser
3204 RegOpenCurrentUser(IN REGSAM samDesired
,
3205 OUT PHKEY phkResult
)
3209 Status
= RtlOpenCurrentUser((ACCESS_MASK
)samDesired
,
3210 (PHANDLE
)phkResult
);
3211 if (!NT_SUCCESS(Status
))
3213 /* NOTE - don't set the last error code! just return the error! */
3214 return RtlNtStatusToDosError(Status
);
3217 return ERROR_SUCCESS
;
3221 /************************************************************************
3224 * 20050503 Fireball - imported from WINE
3229 RegOpenKeyA(HKEY hKey
,
3233 TRACE("RegOpenKeyA hKey 0x%x lpSubKey %s phkResult %p\n",
3234 hKey
, lpSubKey
, phkResult
);
3237 return ERROR_INVALID_PARAMETER
;
3239 if (!hKey
&& lpSubKey
&& phkResult
)
3241 return ERROR_INVALID_HANDLE
;
3244 if (!lpSubKey
|| !*lpSubKey
)
3247 return ERROR_SUCCESS
;
3250 return RegOpenKeyExA(hKey
,
3258 /************************************************************************
3263 * 20050503 Fireball - imported from WINE
3268 RegOpenKeyW(HKEY hKey
,
3272 TRACE("RegOpenKeyW hKey 0x%x lpSubKey %S phkResult %p\n",
3273 hKey
, lpSubKey
, phkResult
);
3276 return ERROR_INVALID_PARAMETER
;
3278 if (!hKey
&& lpSubKey
&& phkResult
)
3280 return ERROR_INVALID_HANDLE
;
3283 if (!lpSubKey
|| !*lpSubKey
)
3286 return ERROR_SUCCESS
;
3289 return RegOpenKeyExW(hKey
,
3297 /************************************************************************
3305 _In_ LPCSTR lpSubKey
,
3306 _In_ DWORD ulOptions
,
3307 _In_ REGSAM samDesired
,
3308 _Out_ PHKEY phkResult
)
3310 UNICODE_STRING SubKeyString
;
3313 TRACE("RegOpenKeyExA hKey 0x%x lpSubKey %s ulOptions 0x%x samDesired 0x%x phkResult %p\n",
3314 hKey
, lpSubKey
, ulOptions
, samDesired
, phkResult
);
3316 RtlCreateUnicodeStringFromAsciiz(&SubKeyString
,
3319 ErrorCode
= RegOpenKeyExW(hKey
, SubKeyString
.Buffer
, ulOptions
, samDesired
, phkResult
);
3321 RtlFreeUnicodeString(&SubKeyString
);
3327 /************************************************************************
3333 RegOpenKeyExW(HKEY hKey
,
3339 OBJECT_ATTRIBUTES ObjectAttributes
;
3340 UNICODE_STRING SubKeyString
;
3343 ULONG Attributes
= OBJ_CASE_INSENSITIVE
;
3344 LONG ErrorCode
= ERROR_SUCCESS
;
3346 TRACE("RegOpenKeyExW hKey 0x%x lpSubKey %S ulOptions 0x%x samDesired 0x%x phkResult %p\n",
3347 hKey
, lpSubKey
, ulOptions
, samDesired
, phkResult
);
3350 return ERROR_INVALID_PARAMETER
;
3353 Status
= MapDefaultKey(&KeyHandle
, hKey
);
3354 if (!NT_SUCCESS(Status
))
3356 return RtlNtStatusToDosError(Status
);
3359 if (IsHKCRKey(KeyHandle
))
3360 return OpenHKCRKey(KeyHandle
, lpSubKey
, ulOptions
, samDesired
, phkResult
);
3362 if (ulOptions
& REG_OPTION_OPEN_LINK
)
3363 Attributes
|= OBJ_OPENLINK
;
3365 if (lpSubKey
!= NULL
)
3366 RtlInitUnicodeString(&SubKeyString
, (LPWSTR
)lpSubKey
);
3368 RtlInitUnicodeString(&SubKeyString
, (LPWSTR
)L
"");
3370 InitializeObjectAttributes(&ObjectAttributes
,
3376 Status
= NtOpenKey((PHANDLE
)phkResult
,
3380 if (!NT_SUCCESS(Status
))
3382 ErrorCode
= RtlNtStatusToDosError(Status
);
3386 ClosePredefKey(KeyHandle
);
3392 /************************************************************************
3393 * RegOpenUserClassesRoot
3398 RegOpenUserClassesRoot(IN HANDLE hToken
,
3400 IN REGSAM samDesired
,
3401 OUT PHKEY phkResult
)
3403 const WCHAR UserClassesKeyPrefix
[] = L
"\\Registry\\User\\";
3404 const WCHAR UserClassesKeySuffix
[] = L
"_Classes";
3405 PTOKEN_USER TokenUserData
;
3406 ULONG RequiredLength
;
3407 UNICODE_STRING UserSidString
, UserClassesKeyRoot
;
3408 OBJECT_ATTRIBUTES ObjectAttributes
;
3411 /* check parameters */
3412 if (hToken
== NULL
|| dwOptions
!= 0 || phkResult
== NULL
)
3414 return ERROR_INVALID_PARAMETER
;
3418 * Get the user sid from the token
3422 /* determine how much memory we need */
3423 Status
= NtQueryInformationToken(hToken
,
3428 if (!NT_SUCCESS(Status
) && (Status
!= STATUS_BUFFER_TOO_SMALL
))
3430 /* NOTE - as opposed to all other registry functions windows does indeed
3431 change the last error code in case the caller supplied a invalid
3432 handle for example! */
3433 return RtlNtStatusToDosError(Status
);
3435 RegInitialize(); /* HACK until delay-loading is implemented */
3436 TokenUserData
= RtlAllocateHeap(ProcessHeap
,
3439 if (TokenUserData
== NULL
)
3441 return ERROR_NOT_ENOUGH_MEMORY
;
3444 /* attempt to read the information */
3445 Status
= NtQueryInformationToken(hToken
,
3450 if (!NT_SUCCESS(Status
))
3452 RtlFreeHeap(ProcessHeap
,
3455 if (Status
== STATUS_BUFFER_TOO_SMALL
)
3457 /* the information appears to have changed?! try again */
3461 /* NOTE - as opposed to all other registry functions windows does indeed
3462 change the last error code in case the caller supplied a invalid
3463 handle for example! */
3464 return RtlNtStatusToDosError(Status
);
3468 * Build the absolute path for the user's registry in the form
3469 * "\Registry\User\<SID>_Classes"
3471 Status
= RtlConvertSidToUnicodeString(&UserSidString
,
3472 TokenUserData
->User
.Sid
,
3475 /* we don't need the user data anymore, free it */
3476 RtlFreeHeap(ProcessHeap
,
3480 if (!NT_SUCCESS(Status
))
3482 return RtlNtStatusToDosError(Status
);
3485 /* allocate enough memory for the entire key string */
3486 UserClassesKeyRoot
.Length
= 0;
3487 UserClassesKeyRoot
.MaximumLength
= UserSidString
.Length
+
3488 sizeof(UserClassesKeyPrefix
) +
3489 sizeof(UserClassesKeySuffix
);
3490 UserClassesKeyRoot
.Buffer
= RtlAllocateHeap(ProcessHeap
,
3492 UserClassesKeyRoot
.MaximumLength
);
3493 if (UserClassesKeyRoot
.Buffer
== NULL
)
3495 RtlFreeUnicodeString(&UserSidString
);
3496 return RtlNtStatusToDosError(Status
);
3499 /* build the string */
3500 RtlAppendUnicodeToString(&UserClassesKeyRoot
,
3501 UserClassesKeyPrefix
);
3502 RtlAppendUnicodeStringToString(&UserClassesKeyRoot
,
3504 RtlAppendUnicodeToString(&UserClassesKeyRoot
,
3505 UserClassesKeySuffix
);
3507 TRACE("RegOpenUserClassesRoot: Absolute path: %wZ\n", &UserClassesKeyRoot
);
3512 InitializeObjectAttributes(&ObjectAttributes
,
3513 &UserClassesKeyRoot
,
3514 OBJ_CASE_INSENSITIVE
,
3518 Status
= NtOpenKey((PHANDLE
)phkResult
,
3522 RtlFreeUnicodeString(&UserSidString
);
3523 RtlFreeUnicodeString(&UserClassesKeyRoot
);
3525 if (!NT_SUCCESS(Status
))
3527 return RtlNtStatusToDosError(Status
);
3530 return ERROR_SUCCESS
;
3534 /************************************************************************
3540 RegQueryInfoKeyA(HKEY hKey
,
3545 LPDWORD lpcbMaxSubKeyLen
,
3546 LPDWORD lpcbMaxClassLen
,
3548 LPDWORD lpcbMaxValueNameLen
,
3549 LPDWORD lpcbMaxValueLen
,
3550 LPDWORD lpcbSecurityDescriptor
,
3551 PFILETIME lpftLastWriteTime
)
3553 WCHAR ClassName
[MAX_PATH
];
3554 UNICODE_STRING UnicodeString
;
3555 ANSI_STRING AnsiString
;
3558 RtlInitUnicodeString(&UnicodeString
,
3560 if (lpClass
!= NULL
)
3562 UnicodeString
.Buffer
= &ClassName
[0];
3563 UnicodeString
.MaximumLength
= sizeof(ClassName
);
3564 AnsiString
.MaximumLength
= *lpcbClass
;
3567 ErrorCode
= RegQueryInfoKeyW(hKey
,
3568 UnicodeString
.Buffer
,
3575 lpcbMaxValueNameLen
,
3577 lpcbSecurityDescriptor
,
3579 if ((ErrorCode
== ERROR_SUCCESS
) && (lpClass
!= NULL
))
3581 AnsiString
.Buffer
= lpClass
;
3582 AnsiString
.Length
= 0;
3583 UnicodeString
.Length
= *lpcbClass
* sizeof(WCHAR
);
3584 RtlUnicodeStringToAnsiString(&AnsiString
,
3587 *lpcbClass
= AnsiString
.Length
;
3588 lpClass
[AnsiString
.Length
] = 0;
3595 /************************************************************************
3601 RegQueryInfoKeyW(HKEY hKey
,
3606 LPDWORD lpcbMaxSubKeyLen
,
3607 LPDWORD lpcbMaxClassLen
,
3609 LPDWORD lpcbMaxValueNameLen
,
3610 LPDWORD lpcbMaxValueLen
,
3611 LPDWORD lpcbSecurityDescriptor
,
3612 PFILETIME lpftLastWriteTime
)
3614 KEY_FULL_INFORMATION FullInfoBuffer
;
3615 PKEY_FULL_INFORMATION FullInfo
;
3617 ULONG ClassLength
= 0;
3621 LONG ErrorCode
= ERROR_SUCCESS
;
3623 if ((lpClass
) && (!lpcbClass
))
3625 return ERROR_INVALID_PARAMETER
;
3628 Status
= MapDefaultKey(&KeyHandle
,
3630 if (!NT_SUCCESS(Status
))
3632 return RtlNtStatusToDosError(Status
);
3635 if (lpClass
!= NULL
)
3639 ClassLength
= min(*lpcbClass
- 1, REG_MAX_NAME_SIZE
) * sizeof(WCHAR
);
3646 FullInfoSize
= sizeof(KEY_FULL_INFORMATION
) + ((ClassLength
+ 3) & ~3);
3647 FullInfo
= RtlAllocateHeap(ProcessHeap
,
3650 if (FullInfo
== NULL
)
3652 ErrorCode
= ERROR_OUTOFMEMORY
;
3656 FullInfo
->ClassLength
= ClassLength
;
3660 FullInfoSize
= sizeof(KEY_FULL_INFORMATION
);
3661 FullInfo
= &FullInfoBuffer
;
3662 FullInfo
->ClassLength
= 0;
3664 FullInfo
->ClassOffset
= FIELD_OFFSET(KEY_FULL_INFORMATION
, Class
);
3666 Status
= NtQueryKey(KeyHandle
,
3671 TRACE("NtQueryKey() returned status 0x%X\n", Status
);
3672 if (!NT_SUCCESS(Status
))
3674 if (lpClass
!= NULL
)
3676 RtlFreeHeap(ProcessHeap
,
3681 ErrorCode
= RtlNtStatusToDosError(Status
);
3685 TRACE("SubKeys %d\n", FullInfo
->SubKeys
);
3686 if (lpcSubKeys
!= NULL
)
3688 *lpcSubKeys
= FullInfo
->SubKeys
;
3691 TRACE("MaxNameLen %lu\n", FullInfo
->MaxNameLen
);
3692 if (lpcbMaxSubKeyLen
!= NULL
)
3694 *lpcbMaxSubKeyLen
= FullInfo
->MaxNameLen
/ sizeof(WCHAR
) + 1;
3697 TRACE("MaxClassLen %lu\n", FullInfo
->MaxClassLen
);
3698 if (lpcbMaxClassLen
!= NULL
)
3700 *lpcbMaxClassLen
= FullInfo
->MaxClassLen
/ sizeof(WCHAR
) + 1;
3703 TRACE("Values %lu\n", FullInfo
->Values
);
3704 if (lpcValues
!= NULL
)
3706 *lpcValues
= FullInfo
->Values
;
3709 TRACE("MaxValueNameLen %lu\n", FullInfo
->MaxValueNameLen
);
3710 if (lpcbMaxValueNameLen
!= NULL
)
3712 *lpcbMaxValueNameLen
= FullInfo
->MaxValueNameLen
/ sizeof(WCHAR
) + 1;
3715 TRACE("MaxValueDataLen %lu\n", FullInfo
->MaxValueDataLen
);
3716 if (lpcbMaxValueLen
!= NULL
)
3718 *lpcbMaxValueLen
= FullInfo
->MaxValueDataLen
;
3721 if (lpcbSecurityDescriptor
!= NULL
)
3723 Status
= NtQuerySecurityObject(KeyHandle
,
3724 OWNER_SECURITY_INFORMATION
|
3725 GROUP_SECURITY_INFORMATION
|
3726 DACL_SECURITY_INFORMATION
,
3729 lpcbSecurityDescriptor
);
3730 if (!NT_SUCCESS(Status
) && Status
!= STATUS_BUFFER_TOO_SMALL
)
3732 if (lpClass
!= NULL
)
3734 RtlFreeHeap(ProcessHeap
,
3739 ErrorCode
= RtlNtStatusToDosError(Status
);
3744 if (lpftLastWriteTime
!= NULL
)
3746 lpftLastWriteTime
->dwLowDateTime
= FullInfo
->LastWriteTime
.u
.LowPart
;
3747 lpftLastWriteTime
->dwHighDateTime
= FullInfo
->LastWriteTime
.u
.HighPart
;
3750 if (lpClass
!= NULL
)
3752 if (FullInfo
->ClassLength
> ClassLength
)
3754 ErrorCode
= ERROR_BUFFER_OVERFLOW
;
3758 RtlCopyMemory(lpClass
,
3760 FullInfo
->ClassLength
);
3761 *lpcbClass
= FullInfo
->ClassLength
/ sizeof(WCHAR
);
3762 lpClass
[*lpcbClass
] = 0;
3765 RtlFreeHeap(ProcessHeap
,
3771 ClosePredefKey(KeyHandle
);
3777 /************************************************************************
3778 * RegQueryMultipleValuesA
3783 RegQueryMultipleValuesA(HKEY hKey
,
3790 DWORD maxBytes
= *ldwTotsize
;
3791 LPSTR bufptr
= (LPSTR
)lpValueBuf
;
3794 if (maxBytes
>= (1024*1024))
3795 return ERROR_TRANSFER_TOO_LONG
;
3799 TRACE("RegQueryMultipleValuesA(%p,%p,%ld,%p,%p=%ld)\n",
3800 hKey
, val_list
, num_vals
, lpValueBuf
, ldwTotsize
, *ldwTotsize
);
3802 for (i
= 0; i
< num_vals
; i
++)
3804 val_list
[i
].ve_valuelen
= 0;
3805 ErrorCode
= RegQueryValueExA(hKey
,
3806 val_list
[i
].ve_valuename
,
3810 &val_list
[i
].ve_valuelen
);
3811 if (ErrorCode
!= ERROR_SUCCESS
)
3816 if (lpValueBuf
!= NULL
&& *ldwTotsize
+ val_list
[i
].ve_valuelen
<= maxBytes
)
3818 ErrorCode
= RegQueryValueExA(hKey
,
3819 val_list
[i
].ve_valuename
,
3821 &val_list
[i
].ve_type
,
3823 &val_list
[i
].ve_valuelen
);
3824 if (ErrorCode
!= ERROR_SUCCESS
)
3829 val_list
[i
].ve_valueptr
= (DWORD_PTR
)bufptr
;
3831 bufptr
+= val_list
[i
].ve_valuelen
;
3834 *ldwTotsize
+= val_list
[i
].ve_valuelen
;
3837 return (lpValueBuf
!= NULL
&& *ldwTotsize
<= maxBytes
) ? ERROR_SUCCESS
: ERROR_MORE_DATA
;
3841 /************************************************************************
3842 * RegQueryMultipleValuesW
3847 RegQueryMultipleValuesW(HKEY hKey
,
3854 DWORD maxBytes
= *ldwTotsize
;
3855 LPSTR bufptr
= (LPSTR
)lpValueBuf
;
3858 if (maxBytes
>= (1024*1024))
3859 return ERROR_TRANSFER_TOO_LONG
;
3863 TRACE("RegQueryMultipleValuesW(%p,%p,%ld,%p,%p=%ld)\n",
3864 hKey
, val_list
, num_vals
, lpValueBuf
, ldwTotsize
, *ldwTotsize
);
3866 for (i
= 0; i
< num_vals
; i
++)
3868 val_list
[i
].ve_valuelen
= 0;
3869 ErrorCode
= RegQueryValueExW(hKey
,
3870 val_list
[i
].ve_valuename
,
3874 &val_list
[i
].ve_valuelen
);
3875 if (ErrorCode
!= ERROR_SUCCESS
)
3880 if (lpValueBuf
!= NULL
&& *ldwTotsize
+ val_list
[i
].ve_valuelen
<= maxBytes
)
3882 ErrorCode
= RegQueryValueExW(hKey
,
3883 val_list
[i
].ve_valuename
,
3885 &val_list
[i
].ve_type
,
3887 &val_list
[i
].ve_valuelen
);
3888 if (ErrorCode
!= ERROR_SUCCESS
)
3893 val_list
[i
].ve_valueptr
= (DWORD_PTR
)bufptr
;
3895 bufptr
+= val_list
[i
].ve_valuelen
;
3898 *ldwTotsize
+= val_list
[i
].ve_valuelen
;
3901 return (lpValueBuf
!= NULL
&& *ldwTotsize
<= maxBytes
) ? ERROR_SUCCESS
: ERROR_MORE_DATA
;
3905 /************************************************************************
3906 * RegQueryReflectionKey
3911 RegQueryReflectionKey(IN HKEY hBase
,
3912 OUT BOOL
* bIsReflectionDisabled
)
3914 FIXME("RegQueryReflectionKey(0x%p, 0x%p) UNIMPLEMENTED!\n",
3915 hBase
, bIsReflectionDisabled
);
3916 return ERROR_CALL_NOT_IMPLEMENTED
;
3920 /******************************************************************************
3921 * RegQueryValueExA [ADVAPI32.@]
3923 * Get the type and contents of a specified value under with a key.
3926 * hkey [I] Handle of the key to query
3927 * name [I] Name of value under hkey to query
3928 * reserved [I] Reserved - must be NULL
3929 * type [O] Destination for the value type, or NULL if not required
3930 * data [O] Destination for the values contents, or NULL if not required
3931 * count [I/O] Size of data, updated with the number of bytes returned
3934 * Success: ERROR_SUCCESS. *count is updated with the number of bytes copied to data.
3935 * Failure: ERROR_INVALID_HANDLE, if hkey is invalid.
3936 * ERROR_INVALID_PARAMETER, if any other parameter is invalid.
3937 * ERROR_MORE_DATA, if on input *count is too small to hold the contents.
3940 * MSDN states that if data is too small it is partially filled. In reality
3941 * it remains untouched.
3945 RegQueryValueExA(HKEY hkeyorg
,
3955 UNICODE_STRING nameW
;
3956 DWORD total_size
, datalen
= 0;
3957 char buffer
[256], *buf_ptr
= buffer
;
3958 KEY_VALUE_PARTIAL_INFORMATION
*info
= (KEY_VALUE_PARTIAL_INFORMATION
*)buffer
;
3959 static const int info_size
= offsetof( KEY_VALUE_PARTIAL_INFORMATION
, Data
);
3961 TRACE("(%p,%s,%p,%p,%p,%p=%d)\n",
3962 hkeyorg
, debugstr_a(name
), reserved
, type
, data
, count
, count
? *count
: 0 );
3964 if ((data
&& !count
) || reserved
) return ERROR_INVALID_PARAMETER
;
3965 status
= MapDefaultKey(&hkey
, hkeyorg
);
3966 if (!NT_SUCCESS(status
))
3968 return RtlNtStatusToDosError(status
);
3971 if (count
) datalen
= *count
;
3972 if (!data
&& count
) *count
= 0;
3974 RtlInitAnsiString( &nameA
, name
);
3975 if ((status
= RtlAnsiStringToUnicodeString( &nameW
, &nameA
, TRUE
)))
3977 ClosePredefKey(hkey
);
3978 return RtlNtStatusToDosError(status
);
3981 status
= NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
,
3982 buffer
, sizeof(buffer
), &total_size
);
3983 if (status
&& status
!= STATUS_BUFFER_OVERFLOW
) goto done
;
3985 /* we need to fetch the contents for a string type even if not requested,
3986 * because we need to compute the length of the ASCII string. */
3987 if (data
|| is_string(info
->Type
))
3989 /* retry with a dynamically allocated buffer */
3990 while (status
== STATUS_BUFFER_OVERFLOW
)
3992 if (buf_ptr
!= buffer
) HeapFree( GetProcessHeap(), 0, buf_ptr
);
3993 if (!(buf_ptr
= HeapAlloc( GetProcessHeap(), 0, total_size
)))
3995 status
= STATUS_NO_MEMORY
;
3998 info
= (KEY_VALUE_PARTIAL_INFORMATION
*)buf_ptr
;
3999 status
= NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
,
4000 buf_ptr
, total_size
, &total_size
);
4003 if (status
) goto done
;
4005 if (is_string(info
->Type
))
4009 RtlUnicodeToMultiByteSize( &len
, (WCHAR
*)(buf_ptr
+ info_size
),
4010 total_size
- info_size
);
4013 if (len
> datalen
) status
= STATUS_BUFFER_OVERFLOW
;
4016 RtlUnicodeToMultiByteN( (char*)data
, len
, NULL
, (WCHAR
*)(buf_ptr
+ info_size
),
4017 total_size
- info_size
);
4018 /* if the type is REG_SZ and data is not 0-terminated
4019 * and there is enough space in the buffer NT appends a \0 */
4020 if (len
< datalen
&& data
[len
-1]) data
[len
] = 0;
4023 total_size
= len
+ info_size
;
4027 if (total_size
- info_size
> datalen
) status
= STATUS_BUFFER_OVERFLOW
;
4028 else memcpy( data
, buf_ptr
+ info_size
, total_size
- info_size
);
4031 else status
= STATUS_SUCCESS
;
4033 if (type
) *type
= info
->Type
;
4034 if (count
) *count
= total_size
- info_size
;
4037 if (buf_ptr
!= buffer
) HeapFree( GetProcessHeap(), 0, buf_ptr
);
4038 RtlFreeUnicodeString( &nameW
);
4039 ClosePredefKey(hkey
);
4040 return RtlNtStatusToDosError(status
);
4044 /************************************************************************
4051 RegQueryValueExW(HKEY hkeyorg
,
4060 UNICODE_STRING name_str
;
4062 char buffer
[256], *buf_ptr
= buffer
;
4063 KEY_VALUE_PARTIAL_INFORMATION
*info
= (KEY_VALUE_PARTIAL_INFORMATION
*)buffer
;
4064 static const int info_size
= offsetof( KEY_VALUE_PARTIAL_INFORMATION
, Data
);
4066 TRACE("(%p,%s,%p,%p,%p,%p=%d)\n",
4067 hkeyorg
, debugstr_w(name
), reserved
, type
, data
, count
,
4068 (count
&& data
) ? *count
: 0 );
4070 if ((data
&& !count
) || reserved
) return ERROR_INVALID_PARAMETER
;
4072 status
= MapDefaultKey(&hkey
, hkeyorg
);
4073 if (!NT_SUCCESS(status
))
4075 return RtlNtStatusToDosError(status
);
4078 RtlInitUnicodeString( &name_str
, name
);
4080 if (data
) total_size
= min( sizeof(buffer
), *count
+ info_size
);
4083 total_size
= info_size
;
4084 if (count
) *count
= 0;
4087 /* this matches Win9x behaviour - NT sets *type to a random value */
4088 if (type
) *type
= REG_NONE
;
4090 status
= NtQueryValueKey( hkey
, &name_str
, KeyValuePartialInformation
,
4091 buffer
, total_size
, &total_size
);
4092 if (!NT_SUCCESS(status
) && status
!= STATUS_BUFFER_OVERFLOW
) goto done
;
4096 /* retry with a dynamically allocated buffer */
4097 while (status
== STATUS_BUFFER_OVERFLOW
&& total_size
- info_size
<= *count
)
4099 if (buf_ptr
!= buffer
) HeapFree( GetProcessHeap(), 0, buf_ptr
);
4100 if (!(buf_ptr
= HeapAlloc( GetProcessHeap(), 0, total_size
)))
4102 ClosePredefKey(hkey
);
4103 return ERROR_NOT_ENOUGH_MEMORY
;
4105 info
= (KEY_VALUE_PARTIAL_INFORMATION
*)buf_ptr
;
4106 status
= NtQueryValueKey( hkey
, &name_str
, KeyValuePartialInformation
,
4107 buf_ptr
, total_size
, &total_size
);
4110 if (NT_SUCCESS(status
))
4112 memcpy( data
, buf_ptr
+ info_size
, total_size
- info_size
);
4113 /* if the type is REG_SZ and data is not 0-terminated
4114 * and there is enough space in the buffer NT appends a \0 */
4115 if (is_string(info
->Type
) && total_size
- info_size
<= *count
-sizeof(WCHAR
))
4117 WCHAR
*ptr
= (WCHAR
*)(data
+ total_size
- info_size
);
4118 if (ptr
> (WCHAR
*)data
&& ptr
[-1]) *ptr
= 0;
4121 else if (status
!= STATUS_BUFFER_OVERFLOW
) goto done
;
4123 else status
= STATUS_SUCCESS
;
4125 if (type
) *type
= info
->Type
;
4126 if (count
) *count
= total_size
- info_size
;
4129 if (buf_ptr
!= buffer
) HeapFree( GetProcessHeap(), 0, buf_ptr
);
4130 ClosePredefKey(hkey
);
4131 return RtlNtStatusToDosError(status
);
4135 /************************************************************************
4140 LSTATUS WINAPI
RegQueryValueA( HKEY hkey
, LPCSTR name
, LPSTR data
, LPLONG count
)
4145 TRACE("(%p,%s,%p,%d)\n", hkey
, debugstr_a(name
), data
, count
? *count
: 0 );
4147 if (name
&& name
[0])
4149 if ((ret
= RegOpenKeyA( hkey
, name
, &subkey
)) != ERROR_SUCCESS
) return ret
;
4151 ret
= RegQueryValueExA( subkey
, NULL
, NULL
, NULL
, (LPBYTE
)data
, (LPDWORD
)count
);
4152 if (subkey
!= hkey
) RegCloseKey( subkey
);
4153 if (ret
== ERROR_FILE_NOT_FOUND
)
4155 /* return empty string if default value not found */
4156 if (data
) *data
= 0;
4157 if (count
) *count
= 1;
4158 ret
= ERROR_SUCCESS
;
4164 /************************************************************************
4169 LSTATUS WINAPI
RegQueryValueW( HKEY hkey
, LPCWSTR name
, LPWSTR data
, LPLONG count
)
4174 TRACE("(%p,%s,%p,%d)\n", hkey
, debugstr_w(name
), data
, count
? *count
: 0 );
4177 return ERROR_INVALID_HANDLE
;
4179 if (name
&& name
[0])
4181 ret
= RegOpenKeyW( hkey
, name
, &subkey
);
4182 if (ret
!= ERROR_SUCCESS
)
4188 ret
= RegQueryValueExW( subkey
, NULL
, NULL
, NULL
, (LPBYTE
)data
, (LPDWORD
)count
);
4192 RegCloseKey( subkey
);
4195 if (ret
== ERROR_FILE_NOT_FOUND
)
4197 /* return empty string if default value not found */
4201 *count
= sizeof(WCHAR
);
4202 ret
= ERROR_SUCCESS
;
4208 /************************************************************************
4214 RegReplaceKeyA(HKEY hKey
,
4219 UNICODE_STRING SubKey
;
4220 UNICODE_STRING NewFile
;
4221 UNICODE_STRING OldFile
;
4224 RtlCreateUnicodeStringFromAsciiz(&SubKey
,
4226 RtlCreateUnicodeStringFromAsciiz(&OldFile
,
4228 RtlCreateUnicodeStringFromAsciiz(&NewFile
,
4231 ErrorCode
= RegReplaceKeyW(hKey
,
4236 RtlFreeUnicodeString(&OldFile
);
4237 RtlFreeUnicodeString(&NewFile
);
4238 RtlFreeUnicodeString(&SubKey
);
4244 /************************************************************************
4250 RegReplaceKeyW(HKEY hKey
,
4255 OBJECT_ATTRIBUTES KeyObjectAttributes
;
4256 OBJECT_ATTRIBUTES NewObjectAttributes
;
4257 OBJECT_ATTRIBUTES OldObjectAttributes
;
4258 UNICODE_STRING SubKeyName
;
4259 UNICODE_STRING NewFileName
;
4260 UNICODE_STRING OldFileName
;
4261 BOOLEAN CloseRealKey
;
4262 HANDLE RealKeyHandle
;
4265 LONG ErrorCode
= ERROR_SUCCESS
;
4267 if (hKey
== HKEY_PERFORMANCE_DATA
)
4269 return ERROR_INVALID_HANDLE
;
4272 Status
= MapDefaultKey(&KeyHandle
,
4274 if (!NT_SUCCESS(Status
))
4276 return RtlNtStatusToDosError(Status
);
4279 /* Open the real key */
4280 if (lpSubKey
!= NULL
&& *lpSubKey
!= (WCHAR
)0)
4282 RtlInitUnicodeString(&SubKeyName
,
4284 InitializeObjectAttributes(&KeyObjectAttributes
,
4286 OBJ_CASE_INSENSITIVE
,
4289 Status
= NtOpenKey(&RealKeyHandle
,
4291 &KeyObjectAttributes
);
4292 if (!NT_SUCCESS(Status
))
4294 ErrorCode
= RtlNtStatusToDosError(Status
);
4298 CloseRealKey
= TRUE
;
4302 RealKeyHandle
= KeyHandle
;
4303 CloseRealKey
= FALSE
;
4306 /* Convert new file name */
4307 if (!RtlDosPathNameToNtPathName_U(lpNewFile
,
4314 NtClose(RealKeyHandle
);
4317 ErrorCode
= ERROR_INVALID_PARAMETER
;
4321 InitializeObjectAttributes(&NewObjectAttributes
,
4323 OBJ_CASE_INSENSITIVE
,
4327 /* Convert old file name */
4328 if (!RtlDosPathNameToNtPathName_U(lpOldFile
,
4333 RtlFreeHeap(RtlGetProcessHeap (),
4335 NewFileName
.Buffer
);
4338 NtClose(RealKeyHandle
);
4341 ErrorCode
= ERROR_INVALID_PARAMETER
;
4345 InitializeObjectAttributes(&OldObjectAttributes
,
4347 OBJ_CASE_INSENSITIVE
,
4351 Status
= NtReplaceKey(&NewObjectAttributes
,
4353 &OldObjectAttributes
);
4355 RtlFreeHeap(RtlGetProcessHeap(),
4357 OldFileName
.Buffer
);
4358 RtlFreeHeap(RtlGetProcessHeap(),
4360 NewFileName
.Buffer
);
4364 NtClose(RealKeyHandle
);
4367 if (!NT_SUCCESS(Status
))
4369 return RtlNtStatusToDosError(Status
);
4373 ClosePredefKey(KeyHandle
);
4379 /************************************************************************
4385 RegRestoreKeyA(HKEY hKey
,
4389 UNICODE_STRING FileName
;
4392 RtlCreateUnicodeStringFromAsciiz(&FileName
,
4395 ErrorCode
= RegRestoreKeyW(hKey
,
4399 RtlFreeUnicodeString(&FileName
);
4405 /************************************************************************
4411 RegRestoreKeyW(HKEY hKey
,
4415 OBJECT_ATTRIBUTES ObjectAttributes
;
4416 IO_STATUS_BLOCK IoStatusBlock
;
4417 UNICODE_STRING FileName
;
4422 if (hKey
== HKEY_PERFORMANCE_DATA
)
4424 return ERROR_INVALID_HANDLE
;
4427 Status
= MapDefaultKey(&KeyHandle
,
4429 if (!NT_SUCCESS(Status
))
4431 return RtlNtStatusToDosError(Status
);
4434 if (!RtlDosPathNameToNtPathName_U(lpFile
,
4439 Status
= STATUS_INVALID_PARAMETER
;
4443 InitializeObjectAttributes(&ObjectAttributes
,
4445 OBJ_CASE_INSENSITIVE
,
4449 Status
= NtOpenFile(&FileHandle
,
4454 FILE_SYNCHRONOUS_IO_NONALERT
);
4455 RtlFreeHeap(RtlGetProcessHeap(),
4458 if (!NT_SUCCESS(Status
))
4463 Status
= NtRestoreKey(KeyHandle
,
4466 NtClose (FileHandle
);
4469 ClosePredefKey(KeyHandle
);
4471 if (!NT_SUCCESS(Status
))
4473 return RtlNtStatusToDosError(Status
);
4476 return ERROR_SUCCESS
;
4480 /************************************************************************
4486 RegSaveKeyA(HKEY hKey
,
4488 LPSECURITY_ATTRIBUTES lpSecurityAttributes
)
4490 UNICODE_STRING FileName
;
4493 RtlCreateUnicodeStringFromAsciiz(&FileName
,
4495 ErrorCode
= RegSaveKeyW(hKey
,
4497 lpSecurityAttributes
);
4498 RtlFreeUnicodeString(&FileName
);
4504 /************************************************************************
4510 RegSaveKeyW(HKEY hKey
,
4512 LPSECURITY_ATTRIBUTES lpSecurityAttributes
)
4514 PSECURITY_DESCRIPTOR SecurityDescriptor
= NULL
;
4515 OBJECT_ATTRIBUTES ObjectAttributes
;
4516 UNICODE_STRING FileName
;
4517 IO_STATUS_BLOCK IoStatusBlock
;
4522 Status
= MapDefaultKey(&KeyHandle
,
4524 if (!NT_SUCCESS(Status
))
4526 return RtlNtStatusToDosError(Status
);
4529 if (!RtlDosPathNameToNtPathName_U(lpFile
,
4534 Status
= STATUS_INVALID_PARAMETER
;
4538 if (lpSecurityAttributes
!= NULL
)
4540 SecurityDescriptor
= lpSecurityAttributes
->lpSecurityDescriptor
;
4543 InitializeObjectAttributes(&ObjectAttributes
,
4545 OBJ_CASE_INSENSITIVE
,
4547 SecurityDescriptor
);
4548 Status
= NtCreateFile(&FileHandle
,
4549 GENERIC_WRITE
| SYNCHRONIZE
,
4553 FILE_ATTRIBUTE_NORMAL
,
4556 FILE_OPEN_FOR_BACKUP_INTENT
| FILE_SYNCHRONOUS_IO_NONALERT
,
4559 RtlFreeHeap(RtlGetProcessHeap(),
4562 if (!NT_SUCCESS(Status
))
4567 Status
= NtSaveKey(KeyHandle
,
4569 NtClose (FileHandle
);
4572 ClosePredefKey(KeyHandle
);
4574 if (!NT_SUCCESS(Status
))
4576 return RtlNtStatusToDosError(Status
);
4579 return ERROR_SUCCESS
;
4583 /************************************************************************
4590 RegSaveKeyExA(HKEY hKey
,
4592 LPSECURITY_ATTRIBUTES lpSecurityAttributes
,
4595 UNICODE_STRING FileName
;
4598 RtlCreateUnicodeStringFromAsciiz(&FileName
,
4600 ErrorCode
= RegSaveKeyExW(hKey
,
4602 lpSecurityAttributes
,
4604 RtlFreeUnicodeString(&FileName
);
4610 /************************************************************************
4617 RegSaveKeyExW(HKEY hKey
,
4619 LPSECURITY_ATTRIBUTES lpSecurityAttributes
,
4624 case REG_STANDARD_FORMAT
:
4625 case REG_LATEST_FORMAT
:
4626 case REG_NO_COMPRESSION
:
4629 return ERROR_INVALID_PARAMETER
;
4632 FIXME("RegSaveKeyExW(): Flags ignored!\n");
4634 return RegSaveKeyW(hKey
,
4636 lpSecurityAttributes
);
4640 /************************************************************************
4646 RegSetKeySecurity(HKEY hKey
,
4647 SECURITY_INFORMATION SecurityInformation
,
4648 PSECURITY_DESCRIPTOR pSecurityDescriptor
)
4653 if (hKey
== HKEY_PERFORMANCE_DATA
)
4655 return ERROR_INVALID_HANDLE
;
4658 Status
= MapDefaultKey(&KeyHandle
,
4660 if (!NT_SUCCESS(Status
))
4662 return RtlNtStatusToDosError(Status
);
4665 Status
= NtSetSecurityObject(KeyHandle
,
4666 SecurityInformation
,
4667 pSecurityDescriptor
);
4669 ClosePredefKey(KeyHandle
);
4671 if (!NT_SUCCESS(Status
))
4673 return RtlNtStatusToDosError(Status
);
4676 return ERROR_SUCCESS
;
4680 /************************************************************************
4686 RegSetValueExA(HKEY hKey
,
4693 UNICODE_STRING ValueName
;
4695 ANSI_STRING AnsiString
;
4696 UNICODE_STRING Data
;
4702 /* Convert SubKey name to Unicode */
4703 if (lpValueName
!= NULL
&& lpValueName
[0] != '\0')
4706 bConverted
= RtlCreateUnicodeStringFromAsciiz(&ValueName
,
4709 return ERROR_NOT_ENOUGH_MEMORY
;
4713 ValueName
.Buffer
= NULL
;
4716 pValueName
= (LPWSTR
)ValueName
.Buffer
;
4719 if (is_string(dwType
) && (cbData
!= 0))
4721 /* Convert ANSI string Data to Unicode */
4722 /* If last character NOT zero then increment length */
4723 LONG bNoNulledStr
= ((lpData
[cbData
-1] != '\0') ? 1 : 0);
4724 AnsiString
.Buffer
= (PSTR
)lpData
;
4725 AnsiString
.Length
= cbData
+ bNoNulledStr
;
4726 AnsiString
.MaximumLength
= cbData
+ bNoNulledStr
;
4727 Status
= RtlAnsiStringToUnicodeString(&Data
,
4731 if (!NT_SUCCESS(Status
))
4733 if (pValueName
!= NULL
)
4734 RtlFreeUnicodeString(&ValueName
);
4736 return RtlNtStatusToDosError(Status
);
4738 pData
= (LPBYTE
)Data
.Buffer
;
4739 DataSize
= cbData
* sizeof(WCHAR
);
4744 pData
= (LPBYTE
)lpData
;
4748 ErrorCode
= RegSetValueExW(hKey
,
4755 if (pValueName
!= NULL
)
4756 RtlFreeUnicodeString(&ValueName
);
4758 if (Data
.Buffer
!= NULL
)
4759 RtlFreeUnicodeString(&Data
);
4765 /************************************************************************
4771 RegSetValueExW(HKEY hKey
,
4772 LPCWSTR lpValueName
,
4778 UNICODE_STRING ValueName
;
4782 if (is_string(dwType
) && (cbData
!= 0))
4784 PWSTR pwsData
= (PWSTR
)lpData
;
4788 if((pwsData
[cbData
/ sizeof(WCHAR
) - 1] != L
'\0') &&
4789 (pwsData
[cbData
/ sizeof(WCHAR
)] == L
'\0'))
4791 /* Increment length if last character is not zero and next is zero */
4792 cbData
+= sizeof(WCHAR
);
4795 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
4797 _SEH2_YIELD(return ERROR_NOACCESS
);
4802 Status
= MapDefaultKey(&KeyHandle
,
4804 if (!NT_SUCCESS(Status
))
4806 return RtlNtStatusToDosError(Status
);
4809 RtlInitUnicodeString(&ValueName
, lpValueName
);
4811 Status
= NtSetValueKey(KeyHandle
,
4818 ClosePredefKey(KeyHandle
);
4820 if (!NT_SUCCESS(Status
))
4822 return RtlNtStatusToDosError(Status
);
4825 return ERROR_SUCCESS
;
4829 /************************************************************************
4835 RegSetValueA(HKEY hKeyOriginal
,
4846 TRACE("(%p,%s,%d,%s,%d)\n", hKeyOriginal
, debugstr_a(lpSubKey
), dwType
, debugstr_a(lpData
), cbData
);
4848 if (dwType
!= REG_SZ
|| !lpData
) return ERROR_INVALID_PARAMETER
;
4850 Status
= MapDefaultKey(&hKey
, hKeyOriginal
);
4851 if (!NT_SUCCESS(Status
))
4853 return RtlNtStatusToDosError (Status
);
4857 if (lpSubKey
&& lpSubKey
[0]) /* need to create the subkey */
4859 ret
= RegCreateKeyA(hKey
, lpSubKey
, &subkey
);
4860 if (ret
!= ERROR_SUCCESS
)
4864 ret
= RegSetValueExA( subkey
, NULL
, 0, REG_SZ
, (const BYTE
*)lpData
, strlen(lpData
)+1 );
4866 RegCloseKey(subkey
);
4869 ClosePredefKey(hKey
);
4875 /************************************************************************
4881 RegSetValueW(HKEY hKeyOriginal
,
4892 TRACE("(%p,%s,%d,%s,%d)\n", hKeyOriginal
, debugstr_w(lpSubKey
), dwType
, debugstr_w(lpData
), cbData
);
4894 if (dwType
!= REG_SZ
|| !lpData
)
4895 return ERROR_INVALID_PARAMETER
;
4897 Status
= MapDefaultKey(&hKey
,
4899 if (!NT_SUCCESS(Status
))
4901 return RtlNtStatusToDosError(Status
);
4905 if (lpSubKey
&& lpSubKey
[0]) /* need to create the subkey */
4907 ret
= RegCreateKeyW(hKey
, lpSubKey
, &subkey
);
4908 if (ret
!= ERROR_SUCCESS
)
4912 ret
= RegSetValueExW( subkey
, NULL
, 0, REG_SZ
, (const BYTE
*)lpData
,
4913 (wcslen( lpData
) + 1) * sizeof(WCHAR
) );
4915 RegCloseKey(subkey
);
4918 ClosePredefKey(hKey
);
4924 /************************************************************************
4930 RegUnLoadKeyA(HKEY hKey
,
4933 UNICODE_STRING KeyName
;
4936 RtlCreateUnicodeStringFromAsciiz(&KeyName
,
4939 ErrorCode
= RegUnLoadKeyW(hKey
,
4942 RtlFreeUnicodeString (&KeyName
);
4948 /************************************************************************
4954 RegUnLoadKeyW(HKEY hKey
,
4957 OBJECT_ATTRIBUTES ObjectAttributes
;
4958 UNICODE_STRING KeyName
;
4962 if (hKey
== HKEY_PERFORMANCE_DATA
)
4964 return ERROR_INVALID_HANDLE
;
4967 Status
= MapDefaultKey(&KeyHandle
, hKey
);
4968 if (!NT_SUCCESS(Status
))
4970 return RtlNtStatusToDosError(Status
);
4973 RtlInitUnicodeString(&KeyName
,
4976 InitializeObjectAttributes(&ObjectAttributes
,
4978 OBJ_CASE_INSENSITIVE
,
4982 Status
= NtUnloadKey(&ObjectAttributes
);
4984 ClosePredefKey(KeyHandle
);
4986 if (!NT_SUCCESS(Status
))
4988 return RtlNtStatusToDosError(Status
);
4991 return ERROR_SUCCESS
;
4995 /******************************************************************************
4996 * load_string [Internal]
4998 * This is basically a copy of user32/resource.c's LoadStringW. Necessary to
4999 * avoid importing user32, which is higher level than advapi32. Helper for
5002 static int load_string(HINSTANCE hModule
, UINT resId
, LPWSTR pwszBuffer
, INT cMaxChars
)
5009 /* Negative values have to be inverted. */
5010 if (HIWORD(resId
) == 0xffff)
5011 resId
= (UINT
)(-((INT
)resId
));
5013 /* Load the resource into memory and get a pointer to it. */
5014 hResource
= FindResourceW(hModule
, MAKEINTRESOURCEW(LOWORD(resId
>> 4) + 1), (LPWSTR
)RT_STRING
);
5015 if (!hResource
) return 0;
5016 hMemory
= LoadResource(hModule
, hResource
);
5017 if (!hMemory
) return 0;
5018 pString
= LockResource(hMemory
);
5020 /* Strings are length-prefixed. Lowest nibble of resId is an index. */
5021 idxString
= resId
& 0xf;
5022 while (idxString
--) pString
+= *pString
+ 1;
5024 /* If no buffer is given, return length of the string. */
5025 if (!pwszBuffer
) return *pString
;
5027 /* Else copy over the string, respecting the buffer size. */
5028 cMaxChars
= (*pString
< cMaxChars
) ? *pString
: (cMaxChars
- 1);
5031 memcpy(pwszBuffer
, pString
+1, cMaxChars
* sizeof(WCHAR
));
5032 pwszBuffer
[cMaxChars
] = L
'\0';
5039 /************************************************************************
5045 RegLoadMUIStringW(IN HKEY hKey
,
5046 IN LPCWSTR pszValue OPTIONAL
,
5047 OUT LPWSTR pszOutBuf
,
5049 OUT LPDWORD pcbData OPTIONAL
,
5051 IN LPCWSTR pszDirectory OPTIONAL
)
5053 DWORD dwValueType
, cbData
;
5054 LPWSTR pwszTempBuffer
= NULL
, pwszExpandedBuffer
= NULL
;
5057 /* Parameter sanity checks. */
5058 if (!hKey
|| !pszOutBuf
)
5059 return ERROR_INVALID_PARAMETER
;
5061 if (pszDirectory
&& *pszDirectory
)
5063 FIXME("BaseDir parameter not yet supported!\n");
5064 return ERROR_INVALID_PARAMETER
;
5067 /* Check for value existence and correctness of it's type, allocate a buffer and load it. */
5068 result
= RegQueryValueExW(hKey
, pszValue
, NULL
, &dwValueType
, NULL
, &cbData
);
5069 if (result
!= ERROR_SUCCESS
) goto cleanup
;
5070 if (!(dwValueType
== REG_SZ
|| dwValueType
== REG_EXPAND_SZ
) || !cbData
)
5072 result
= ERROR_FILE_NOT_FOUND
;
5075 pwszTempBuffer
= HeapAlloc(GetProcessHeap(), 0, cbData
);
5076 if (!pwszTempBuffer
)
5078 result
= ERROR_NOT_ENOUGH_MEMORY
;
5081 result
= RegQueryValueExW(hKey
, pszValue
, NULL
, &dwValueType
, (LPBYTE
)pwszTempBuffer
, &cbData
);
5082 if (result
!= ERROR_SUCCESS
) goto cleanup
;
5084 /* Expand environment variables, if appropriate, or copy the original string over. */
5085 if (dwValueType
== REG_EXPAND_SZ
)
5087 cbData
= ExpandEnvironmentStringsW(pwszTempBuffer
, NULL
, 0) * sizeof(WCHAR
);
5088 if (!cbData
) goto cleanup
;
5089 pwszExpandedBuffer
= HeapAlloc(GetProcessHeap(), 0, cbData
);
5090 if (!pwszExpandedBuffer
)
5092 result
= ERROR_NOT_ENOUGH_MEMORY
;
5095 ExpandEnvironmentStringsW(pwszTempBuffer
, pwszExpandedBuffer
, cbData
);
5099 pwszExpandedBuffer
= HeapAlloc(GetProcessHeap(), 0, cbData
);
5100 memcpy(pwszExpandedBuffer
, pwszTempBuffer
, cbData
);
5103 /* If the value references a resource based string, parse the value and load the string.
5104 * Else just copy over the original value. */
5105 result
= ERROR_SUCCESS
;
5106 if (*pwszExpandedBuffer
!= L
'@') /* '@' is the prefix for resource based string entries. */
5108 lstrcpynW(pszOutBuf
, pwszExpandedBuffer
, cbOutBuf
/ sizeof(WCHAR
));
5112 WCHAR
*pComma
= wcsrchr(pwszExpandedBuffer
, L
',');
5116 /* Format of the expanded value is 'path_to_dll,-resId' */
5117 if (!pComma
|| pComma
[1] != L
'-')
5119 result
= ERROR_BADKEY
;
5123 uiStringId
= _wtoi(pComma
+2);
5126 hModule
= LoadLibraryExW(pwszExpandedBuffer
+ 1, NULL
, LOAD_LIBRARY_AS_DATAFILE
);
5127 if (!hModule
|| !load_string(hModule
, uiStringId
, pszOutBuf
, cbOutBuf
/ sizeof(WCHAR
)))
5128 result
= ERROR_BADKEY
;
5129 FreeLibrary(hModule
);
5133 HeapFree(GetProcessHeap(), 0, pwszTempBuffer
);
5134 HeapFree(GetProcessHeap(), 0, pwszExpandedBuffer
);
5139 /************************************************************************
5145 RegLoadMUIStringA(IN HKEY hKey
,
5146 IN LPCSTR pszValue OPTIONAL
,
5147 OUT LPSTR pszOutBuf
,
5149 OUT LPDWORD pcbData OPTIONAL
,
5151 IN LPCSTR pszDirectory OPTIONAL
)
5153 UNICODE_STRING valueW
, baseDirW
;
5155 DWORD cbData
= cbOutBuf
* sizeof(WCHAR
);
5158 valueW
.Buffer
= baseDirW
.Buffer
= pwszBuffer
= NULL
;
5159 if (!RtlCreateUnicodeStringFromAsciiz(&valueW
, pszValue
) ||
5160 !RtlCreateUnicodeStringFromAsciiz(&baseDirW
, pszDirectory
) ||
5161 !(pwszBuffer
= HeapAlloc(GetProcessHeap(), 0, cbData
)))
5163 result
= ERROR_NOT_ENOUGH_MEMORY
;
5167 result
= RegLoadMUIStringW(hKey
, valueW
.Buffer
, pwszBuffer
, cbData
, NULL
, Flags
,
5170 if (result
== ERROR_SUCCESS
)
5172 cbData
= WideCharToMultiByte(CP_ACP
, 0, pwszBuffer
, -1, pszOutBuf
, cbOutBuf
, NULL
, NULL
);
5178 HeapFree(GetProcessHeap(), 0, pwszBuffer
);
5179 RtlFreeUnicodeString(&baseDirW
);
5180 RtlFreeUnicodeString(&valueW
);