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>
21 WINE_DEFAULT_DEBUG_CHANNEL(reg
);
23 /* DEFINES ******************************************************************/
25 #define MAX_DEFAULT_HANDLES 6
26 #define REG_MAX_NAME_SIZE 256
27 #define REG_MAX_DATA_SIZE 2048
29 /* GLOBALS ******************************************************************/
31 static RTL_CRITICAL_SECTION HandleTableCS
;
32 static HANDLE DefaultHandleTable
[MAX_DEFAULT_HANDLES
];
33 static HANDLE ProcessHeap
;
34 static BOOLEAN DefaultHandlesDisabled
= FALSE
;
35 static BOOLEAN DefaultHandleHKUDisabled
= FALSE
;
36 static BOOLEAN DllInitialized
= FALSE
; /* HACK */
38 /* PROTOTYPES ***************************************************************/
40 static NTSTATUS
MapDefaultKey (PHANDLE ParentKey
, HKEY Key
);
41 static VOID
CloseDefaultKeys(VOID
);
42 #define ClosePredefKey(Handle) \
43 if ((ULONG_PTR)Handle & 0x1) { \
46 #define IsPredefKey(HKey) \
47 (((ULONG_PTR)(HKey) & 0xF0000000) == 0x80000000)
48 #define GetPredefKeyIndex(HKey) \
49 ((ULONG_PTR)(HKey) & 0x0FFFFFFF)
51 static NTSTATUS
OpenClassesRootKey(PHANDLE KeyHandle
);
52 static NTSTATUS
OpenLocalMachineKey (PHANDLE KeyHandle
);
53 static NTSTATUS
OpenUsersKey (PHANDLE KeyHandle
);
54 static NTSTATUS
OpenCurrentConfigKey(PHANDLE KeyHandle
);
57 /* FUNCTIONS ****************************************************************/
58 /* check if value type needs string conversion (Ansi<->Unicode) */
59 __inline
static int is_string( DWORD type
)
61 return (type
== REG_SZ
) || (type
== REG_EXPAND_SZ
) || (type
== REG_MULTI_SZ
);
64 /************************************************************************
65 * RegInitDefaultHandles
70 TRACE("RegInitialize()\n");
75 ProcessHeap
= RtlGetProcessHeap();
76 RtlZeroMemory(DefaultHandleTable
,
77 MAX_DEFAULT_HANDLES
* sizeof(HANDLE
));
78 RtlInitializeCriticalSection(&HandleTableCS
);
80 DllInitialized
= TRUE
;
87 /************************************************************************
93 TRACE("RegCleanup()\n");
96 RtlDeleteCriticalSection(&HandleTableCS
);
103 OpenPredefinedKey(IN ULONG Index
,
110 case 0: /* HKEY_CLASSES_ROOT */
111 Status
= OpenClassesRootKey (Handle
);
114 case 1: /* HKEY_CURRENT_USER */
115 Status
= RtlOpenCurrentUser (MAXIMUM_ALLOWED
,
119 case 2: /* HKEY_LOCAL_MACHINE */
120 Status
= OpenLocalMachineKey (Handle
);
123 case 3: /* HKEY_USERS */
124 Status
= OpenUsersKey (Handle
);
127 case 4: /* HKEY_PERFORMANCE_DATA */
128 Status
= OpenPerformanceDataKey (Handle
);
132 case 5: /* HKEY_CURRENT_CONFIG */
133 Status
= OpenCurrentConfigKey (Handle
);
136 case 6: /* HKEY_DYN_DATA */
137 Status
= STATUS_NOT_IMPLEMENTED
;
141 WARN("MapDefaultHandle() no handle creator\n");
142 Status
= STATUS_INVALID_PARAMETER
;
151 MapDefaultKey(OUT PHANDLE RealKey
,
156 BOOLEAN DoOpen
, DefDisabled
;
157 NTSTATUS Status
= STATUS_SUCCESS
;
159 TRACE("MapDefaultKey (Key %x)\n", Key
);
161 if (!IsPredefKey(Key
))
163 *RealKey
= (HANDLE
)((ULONG_PTR
)Key
& ~0x1);
164 return STATUS_SUCCESS
;
167 /* Handle special cases here */
168 Index
= GetPredefKeyIndex(Key
);
169 if (Index
>= MAX_DEFAULT_HANDLES
)
171 return STATUS_INVALID_PARAMETER
;
173 RegInitialize(); /* HACK until delay-loading is implemented */
174 RtlEnterCriticalSection (&HandleTableCS
);
176 if (Key
== HKEY_CURRENT_USER
)
177 DefDisabled
= DefaultHandleHKUDisabled
;
179 DefDisabled
= DefaultHandlesDisabled
;
183 Handle
= &DefaultHandleTable
[Index
];
184 DoOpen
= (*Handle
== NULL
);
194 /* create/open the default handle */
195 Status
= OpenPredefinedKey(Index
,
199 if (NT_SUCCESS(Status
))
204 *(PULONG_PTR
)Handle
|= 0x1;
207 RtlLeaveCriticalSection (&HandleTableCS
);
214 CloseDefaultKeys(VOID
)
217 RegInitialize(); /* HACK until delay-loading is implemented */
218 RtlEnterCriticalSection(&HandleTableCS
);
220 for (i
= 0; i
< MAX_DEFAULT_HANDLES
; i
++)
222 if (DefaultHandleTable
[i
] != NULL
)
224 NtClose(DefaultHandleTable
[i
]);
225 DefaultHandleTable
[i
] = NULL
;
229 RtlLeaveCriticalSection(&HandleTableCS
);
234 OpenClassesRootKey(PHANDLE KeyHandle
)
236 OBJECT_ATTRIBUTES Attributes
;
237 UNICODE_STRING KeyName
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\Software\\CLASSES");
239 TRACE("OpenClassesRootKey()\n");
241 InitializeObjectAttributes(&Attributes
,
243 OBJ_CASE_INSENSITIVE
,
246 return NtOpenKey(KeyHandle
,
253 OpenLocalMachineKey(PHANDLE KeyHandle
)
255 OBJECT_ATTRIBUTES Attributes
;
256 UNICODE_STRING KeyName
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine");
259 TRACE("OpenLocalMachineKey()\n");
261 InitializeObjectAttributes(&Attributes
,
263 OBJ_CASE_INSENSITIVE
,
266 Status
= NtOpenKey(KeyHandle
,
270 TRACE("NtOpenKey(%wZ) => %08x\n", &KeyName
, Status
);
277 OpenUsersKey(PHANDLE KeyHandle
)
279 OBJECT_ATTRIBUTES Attributes
;
280 UNICODE_STRING KeyName
= RTL_CONSTANT_STRING(L
"\\Registry\\User");
282 TRACE("OpenUsersKey()\n");
284 InitializeObjectAttributes(&Attributes
,
286 OBJ_CASE_INSENSITIVE
,
289 return NtOpenKey(KeyHandle
,
296 OpenCurrentConfigKey (PHANDLE KeyHandle
)
298 OBJECT_ATTRIBUTES Attributes
;
299 UNICODE_STRING KeyName
=
300 RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\System\\CurrentControlSet\\Hardware Profiles\\Current");
302 TRACE("OpenCurrentConfigKey()\n");
304 InitializeObjectAttributes(&Attributes
,
306 OBJ_CASE_INSENSITIVE
,
309 return NtOpenKey(KeyHandle
,
315 /************************************************************************
316 * RegDisablePredefinedCache
321 RegDisablePredefinedCache(VOID
)
323 RegInitialize(); /* HACK until delay-loading is implemented */
324 RtlEnterCriticalSection(&HandleTableCS
);
325 DefaultHandleHKUDisabled
= TRUE
;
326 RtlLeaveCriticalSection(&HandleTableCS
);
327 return ERROR_SUCCESS
;
331 /************************************************************************
332 * RegDisablePredefinedCacheEx
337 RegDisablePredefinedCacheEx(VOID
)
339 RegInitialize(); /* HACK until delay-loading is implemented */
340 RtlEnterCriticalSection(&HandleTableCS
);
341 DefaultHandlesDisabled
= TRUE
;
342 DefaultHandleHKUDisabled
= TRUE
;
343 RtlLeaveCriticalSection(&HandleTableCS
);
344 return ERROR_SUCCESS
;
348 /************************************************************************
349 * RegOverridePredefKey
354 RegOverridePredefKey(IN HKEY hKey
,
355 IN HKEY hNewHKey OPTIONAL
)
357 LONG ErrorCode
= ERROR_SUCCESS
;
359 if ((hKey
== HKEY_CLASSES_ROOT
||
360 hKey
== HKEY_CURRENT_CONFIG
||
361 hKey
== HKEY_CURRENT_USER
||
362 hKey
== HKEY_LOCAL_MACHINE
||
363 hKey
== HKEY_PERFORMANCE_DATA
||
364 hKey
== HKEY_USERS
) &&
365 !IsPredefKey(hNewHKey
))
370 Index
= GetPredefKeyIndex(hKey
);
371 Handle
= &DefaultHandleTable
[Index
];
373 if (hNewHKey
== NULL
)
375 /* restore the default mapping */
376 NTSTATUS Status
= OpenPredefinedKey(Index
,
378 if (!NT_SUCCESS(Status
))
380 return RtlNtStatusToDosError(Status
);
383 ASSERT(hNewHKey
!= NULL
);
385 RegInitialize(); /* HACK until delay-loading is implemented */
386 RtlEnterCriticalSection(&HandleTableCS
);
388 /* close the currently mapped handle if existing */
394 /* update the mapping */
397 RtlLeaveCriticalSection(&HandleTableCS
);
400 ErrorCode
= ERROR_INVALID_HANDLE
;
406 /************************************************************************
412 RegCloseKey(HKEY hKey
)
416 /* don't close null handle or a pseudo handle */
417 if ((!hKey
) || (((ULONG_PTR
)hKey
& 0xF0000000) == 0x80000000))
419 return ERROR_INVALID_HANDLE
;
422 Status
= NtClose(hKey
);
423 if (!NT_SUCCESS(Status
))
425 return RtlNtStatusToDosError(Status
);
428 return ERROR_SUCCESS
;
433 RegpCopyTree(IN HKEY hKeySrc
,
438 LIST_ENTRY ListEntry
;
441 } REGP_COPY_KEYS
, *PREGP_COPY_KEYS
;
443 LIST_ENTRY copyQueueHead
;
444 PREGP_COPY_KEYS copyKeys
, newCopyKeys
;
447 KEY_VALUE_FULL_INFORMATION
*KeyValue
;
448 KEY_NODE_INFORMATION
*KeyNode
;
451 ULONG Index
, BufferSizeRequired
, BufferSize
= 0x200;
452 NTSTATUS Status
= STATUS_SUCCESS
;
453 NTSTATUS Status2
= STATUS_SUCCESS
;
455 InitializeListHead(©QueueHead
);
457 Info
.Buffer
= RtlAllocateHeap(ProcessHeap
,
460 if (Info
.Buffer
== NULL
)
462 return STATUS_INSUFFICIENT_RESOURCES
;
465 copyKeys
= RtlAllocateHeap(ProcessHeap
,
467 sizeof(REGP_COPY_KEYS
));
468 if (copyKeys
!= NULL
)
470 copyKeys
->hKeySrc
= hKeySrc
;
471 copyKeys
->hKeyDest
= hKeyDest
;
472 InsertHeadList(©QueueHead
,
473 ©Keys
->ListEntry
);
475 /* FIXME - copy security from hKeySrc to hKeyDest or just for the subkeys? */
479 copyKeys
= CONTAINING_RECORD(copyQueueHead
.Flink
,
483 /* enumerate all values and copy them */
487 Status2
= NtEnumerateValueKey(copyKeys
->hKeySrc
,
489 KeyValueFullInformation
,
492 &BufferSizeRequired
);
493 if (NT_SUCCESS(Status2
))
495 UNICODE_STRING ValueName
;
498 /* don't use RtlInitUnicodeString as the string is not NULL-terminated! */
499 ValueName
.Length
= Info
.KeyValue
->NameLength
;
500 ValueName
.MaximumLength
= ValueName
.Length
;
501 ValueName
.Buffer
= Info
.KeyValue
->Name
;
503 Data
= (PVOID
)((ULONG_PTR
)Info
.KeyValue
+ Info
.KeyValue
->DataOffset
);
505 Status2
= NtSetValueKey(copyKeys
->hKeyDest
,
507 Info
.KeyValue
->TitleIndex
,
510 Info
.KeyValue
->DataLength
);
512 /* don't break, let's try to copy as many values as possible */
513 if (!NT_SUCCESS(Status2
) && NT_SUCCESS(Status
))
520 else if (Status2
== STATUS_BUFFER_OVERFLOW
)
524 ASSERT(BufferSize
< BufferSizeRequired
);
526 Buffer
= RtlReAllocateHeap(ProcessHeap
,
532 Info
.Buffer
= Buffer
;
537 /* don't break, let's try to copy as many values as possible */
538 Status2
= STATUS_INSUFFICIENT_RESOURCES
;
541 if (NT_SUCCESS(Status
))
549 /* break to avoid an infinite loop in case of denied access or
551 if (Status2
!= STATUS_NO_MORE_ENTRIES
&& NT_SUCCESS(Status
))
560 /* enumerate all subkeys and open and enqueue them */
564 Status2
= NtEnumerateKey(copyKeys
->hKeySrc
,
569 &BufferSizeRequired
);
570 if (NT_SUCCESS(Status2
))
572 HANDLE KeyHandle
, NewKeyHandle
;
573 OBJECT_ATTRIBUTES ObjectAttributes
;
574 UNICODE_STRING SubKeyName
, ClassName
;
576 /* don't use RtlInitUnicodeString as the string is not NULL-terminated! */
577 SubKeyName
.Length
= Info
.KeyNode
->NameLength
;
578 SubKeyName
.MaximumLength
= SubKeyName
.Length
;
579 SubKeyName
.Buffer
= Info
.KeyNode
->Name
;
580 ClassName
.Length
= Info
.KeyNode
->ClassLength
;
581 ClassName
.MaximumLength
= ClassName
.Length
;
582 ClassName
.Buffer
= (PWSTR
)((ULONG_PTR
)Info
.KeyNode
+ Info
.KeyNode
->ClassOffset
);
584 /* open the subkey with sufficient rights */
586 InitializeObjectAttributes(&ObjectAttributes
,
588 OBJ_CASE_INSENSITIVE
,
592 Status2
= NtOpenKey(&KeyHandle
,
593 KEY_ENUMERATE_SUB_KEYS
| KEY_QUERY_VALUE
,
595 if (NT_SUCCESS(Status2
))
597 /* FIXME - attempt to query the security information */
599 InitializeObjectAttributes(&ObjectAttributes
,
601 OBJ_CASE_INSENSITIVE
,
605 Status2
= NtCreateKey(&NewKeyHandle
,
608 Info
.KeyNode
->TitleIndex
,
612 if (NT_SUCCESS(Status2
))
614 newCopyKeys
= RtlAllocateHeap(ProcessHeap
,
616 sizeof(REGP_COPY_KEYS
));
617 if (newCopyKeys
!= NULL
)
619 /* save the handles and enqueue the subkey */
620 newCopyKeys
->hKeySrc
= KeyHandle
;
621 newCopyKeys
->hKeyDest
= NewKeyHandle
;
622 InsertTailList(©QueueHead
,
623 &newCopyKeys
->ListEntry
);
628 NtClose(NewKeyHandle
);
630 Status2
= STATUS_INSUFFICIENT_RESOURCES
;
639 if (!NT_SUCCESS(Status2
) && NT_SUCCESS(Status
))
646 else if (Status2
== STATUS_BUFFER_OVERFLOW
)
650 ASSERT(BufferSize
< BufferSizeRequired
);
652 Buffer
= RtlReAllocateHeap(ProcessHeap
,
658 Info
.Buffer
= Buffer
;
663 /* don't break, let's try to copy as many keys as possible */
664 Status2
= STATUS_INSUFFICIENT_RESOURCES
;
667 if (NT_SUCCESS(Status
))
675 /* break to avoid an infinite loop in case of denied access or
677 if (Status2
!= STATUS_NO_MORE_ENTRIES
&& NT_SUCCESS(Status
))
686 /* close the handles and remove the entry from the list */
687 if (copyKeys
->hKeySrc
!= hKeySrc
)
689 NtClose(copyKeys
->hKeySrc
);
691 if (copyKeys
->hKeyDest
!= hKeyDest
)
693 NtClose(copyKeys
->hKeyDest
);
696 RemoveEntryList(©Keys
->ListEntry
);
698 RtlFreeHeap(ProcessHeap
,
701 } while (!IsListEmpty(©QueueHead
));
704 Status
= STATUS_INSUFFICIENT_RESOURCES
;
706 RtlFreeHeap(ProcessHeap
,
714 /************************************************************************
720 RegCopyTreeW(IN HKEY hKeySrc
,
721 IN LPCWSTR lpSubKey OPTIONAL
,
724 HANDLE DestKeyHandle
, KeyHandle
, CurKey
, SubKeyHandle
= NULL
;
727 Status
= MapDefaultKey(&KeyHandle
,
729 if (!NT_SUCCESS(Status
))
731 return RtlNtStatusToDosError(Status
);
734 Status
= MapDefaultKey(&DestKeyHandle
,
736 if (!NT_SUCCESS(Status
))
741 if (lpSubKey
!= NULL
)
743 OBJECT_ATTRIBUTES ObjectAttributes
;
744 UNICODE_STRING SubKeyName
;
746 RtlInitUnicodeString(&SubKeyName
,
749 InitializeObjectAttributes(&ObjectAttributes
,
751 OBJ_CASE_INSENSITIVE
,
755 Status
= NtOpenKey(&SubKeyHandle
,
756 KEY_ENUMERATE_SUB_KEYS
| KEY_QUERY_VALUE
,
758 if (!NT_SUCCESS(Status
))
763 CurKey
= SubKeyHandle
;
768 Status
= RegpCopyTree(CurKey
,
771 if (SubKeyHandle
!= NULL
)
773 NtClose(SubKeyHandle
);
777 ClosePredefKey(DestKeyHandle
);
779 ClosePredefKey(KeyHandle
);
781 if (!NT_SUCCESS(Status
))
783 return RtlNtStatusToDosError(Status
);
786 return ERROR_SUCCESS
;
790 /************************************************************************
796 RegCopyTreeA(IN HKEY hKeySrc
,
797 IN LPCSTR lpSubKey OPTIONAL
,
800 UNICODE_STRING SubKeyName
= { 0, 0, NULL
};
803 if (lpSubKey
!= NULL
&&
804 !RtlCreateUnicodeStringFromAsciiz(&SubKeyName
,
807 return ERROR_NOT_ENOUGH_MEMORY
;
810 Ret
= RegCopyTreeW(hKeySrc
,
814 RtlFreeUnicodeString(&SubKeyName
);
820 /************************************************************************
821 * RegConnectRegistryA
826 RegConnectRegistryA(IN LPCSTR lpMachineName
,
830 UNICODE_STRING MachineName
= { 0, 0, NULL
};
833 if (lpMachineName
!= NULL
&&
834 !RtlCreateUnicodeStringFromAsciiz(&MachineName
,
835 (LPSTR
)lpMachineName
))
837 return ERROR_NOT_ENOUGH_MEMORY
;
840 Ret
= RegConnectRegistryW(MachineName
.Buffer
,
844 RtlFreeUnicodeString(&MachineName
);
850 /************************************************************************
851 * RegConnectRegistryW
856 RegConnectRegistryW(LPCWSTR lpMachineName
,
862 TRACE("(%s,%p,%p): stub\n",debugstr_w(lpMachineName
),hKey
,phkResult
);
864 if (!lpMachineName
|| !*lpMachineName
)
866 /* Use the local machine name */
867 ret
= RegOpenKeyW( hKey
, NULL
, phkResult
);
871 WCHAR compName
[MAX_COMPUTERNAME_LENGTH
+ 1];
872 DWORD len
= sizeof(compName
) / sizeof(WCHAR
);
874 /* MSDN says lpMachineName must start with \\ : not so */
875 if( lpMachineName
[0] == '\\' && lpMachineName
[1] == '\\')
878 if (GetComputerNameW(compName
, &len
))
880 if (!_wcsicmp(lpMachineName
, compName
))
881 ret
= RegOpenKeyW(hKey
, NULL
, phkResult
);
884 FIXME("Connect to %s is not supported.\n",debugstr_w(lpMachineName
));
885 ret
= ERROR_BAD_NETPATH
;
889 ret
= GetLastError();
896 /************************************************************************
899 * Create key and all necessary intermediate keys
902 CreateNestedKey(PHKEY KeyHandle
,
903 POBJECT_ATTRIBUTES ObjectAttributes
,
904 PUNICODE_STRING ClassString
,
907 DWORD
*lpdwDisposition
)
909 OBJECT_ATTRIBUTES LocalObjectAttributes
;
910 UNICODE_STRING LocalKeyName
;
913 ULONG FullNameLength
;
916 HANDLE LocalKeyHandle
;
918 Status
= NtCreateKey((PHANDLE
) KeyHandle
,
924 (PULONG
)lpdwDisposition
);
925 TRACE("NtCreateKey(%wZ) called (Status %lx)\n", ObjectAttributes
->ObjectName
, Status
);
926 if (Status
!= STATUS_OBJECT_NAME_NOT_FOUND
)
929 /* Copy object attributes */
930 RtlCopyMemory(&LocalObjectAttributes
,
932 sizeof(OBJECT_ATTRIBUTES
));
933 RtlCreateUnicodeString(&LocalKeyName
,
934 ObjectAttributes
->ObjectName
->Buffer
);
935 LocalObjectAttributes
.ObjectName
= &LocalKeyName
;
936 FullNameLength
= LocalKeyName
.Length
/ sizeof(WCHAR
);
938 LocalKeyHandle
= NULL
;
940 /* Remove the last part of the key name and try to create the key again. */
941 while (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
943 Ptr
= wcsrchr(LocalKeyName
.Buffer
, '\\');
944 if (Ptr
== NULL
|| Ptr
== LocalKeyName
.Buffer
)
946 Status
= STATUS_UNSUCCESSFUL
;
951 LocalKeyName
.Length
= wcslen(LocalKeyName
.Buffer
) * sizeof(WCHAR
);
953 Status
= NtCreateKey(&LocalKeyHandle
,
955 &LocalObjectAttributes
,
960 TRACE("NtCreateKey(%wZ) called (Status %lx)\n", &LocalKeyName
, Status
);
963 if (!NT_SUCCESS(Status
))
965 RtlFreeUnicodeString(&LocalKeyName
);
969 /* Add removed parts of the key name and create them too. */
970 Length
= wcslen(LocalKeyName
.Buffer
);
974 NtClose (LocalKeyHandle
);
976 LocalKeyName
.Buffer
[Length
] = L
'\\';
977 Length
= wcslen (LocalKeyName
.Buffer
);
978 LocalKeyName
.Length
= Length
* sizeof(WCHAR
);
980 if (Length
== FullNameLength
)
982 Status
= NtCreateKey((PHANDLE
) KeyHandle
,
988 (PULONG
)lpdwDisposition
);
992 Status
= NtCreateKey(&LocalKeyHandle
,
994 &LocalObjectAttributes
,
999 TRACE("NtCreateKey(%wZ) called (Status %lx)\n", &LocalKeyName
, Status
);
1000 if (!NT_SUCCESS(Status
))
1004 RtlFreeUnicodeString(&LocalKeyName
);
1010 /************************************************************************
1016 RegCreateKeyExA(HKEY hKey
,
1022 LPSECURITY_ATTRIBUTES lpSecurityAttributes
,
1024 LPDWORD lpdwDisposition
)
1026 UNICODE_STRING SubKeyString
;
1027 UNICODE_STRING ClassString
;
1028 OBJECT_ATTRIBUTES ObjectAttributes
;
1030 ULONG Attributes
= OBJ_CASE_INSENSITIVE
;
1033 TRACE("RegCreateKeyExA() called\n");
1035 if (lpSecurityAttributes
&& lpSecurityAttributes
->nLength
!= sizeof(SECURITY_ATTRIBUTES
))
1036 return ERROR_INVALID_USER_BUFFER
;
1038 /* get the real parent key */
1039 Status
= MapDefaultKey(&ParentKey
,
1041 if (!NT_SUCCESS(Status
))
1043 return RtlNtStatusToDosError(Status
);
1046 TRACE("ParentKey %p\n", ParentKey
);
1048 if (lpClass
!= NULL
)
1050 RtlCreateUnicodeStringFromAsciiz(&ClassString
,
1054 if (dwOptions
& REG_OPTION_OPEN_LINK
)
1055 Attributes
|= OBJ_OPENLINK
;
1057 RtlCreateUnicodeStringFromAsciiz(&SubKeyString
,
1059 InitializeObjectAttributes(&ObjectAttributes
,
1063 lpSecurityAttributes
? (PSECURITY_DESCRIPTOR
)lpSecurityAttributes
->lpSecurityDescriptor
: NULL
);
1064 Status
= CreateNestedKey(phkResult
,
1066 (lpClass
== NULL
)? NULL
: &ClassString
,
1070 RtlFreeUnicodeString(&SubKeyString
);
1071 if (lpClass
!= NULL
)
1073 RtlFreeUnicodeString(&ClassString
);
1076 ClosePredefKey(ParentKey
);
1078 TRACE("Status %x\n", Status
);
1079 if (!NT_SUCCESS(Status
))
1081 return RtlNtStatusToDosError(Status
);
1084 return ERROR_SUCCESS
;
1088 /************************************************************************
1094 RegCreateKeyExW(HKEY hKey
,
1100 LPSECURITY_ATTRIBUTES lpSecurityAttributes
,
1102 LPDWORD lpdwDisposition
)
1104 UNICODE_STRING SubKeyString
;
1105 UNICODE_STRING ClassString
;
1106 OBJECT_ATTRIBUTES ObjectAttributes
;
1108 ULONG Attributes
= OBJ_CASE_INSENSITIVE
;
1111 TRACE("RegCreateKeyExW() called\n");
1113 if (lpSecurityAttributes
&& lpSecurityAttributes
->nLength
!= sizeof(SECURITY_ATTRIBUTES
))
1114 return ERROR_INVALID_USER_BUFFER
;
1116 /* get the real parent key */
1117 Status
= MapDefaultKey(&ParentKey
,
1119 if (!NT_SUCCESS(Status
))
1121 return RtlNtStatusToDosError(Status
);
1124 TRACE("ParentKey %p\n", ParentKey
);
1126 if (dwOptions
& REG_OPTION_OPEN_LINK
)
1127 Attributes
|= OBJ_OPENLINK
;
1129 RtlInitUnicodeString(&ClassString
,
1131 RtlInitUnicodeString(&SubKeyString
,
1133 InitializeObjectAttributes(&ObjectAttributes
,
1137 lpSecurityAttributes
? (PSECURITY_DESCRIPTOR
)lpSecurityAttributes
->lpSecurityDescriptor
: NULL
);
1138 Status
= CreateNestedKey(phkResult
,
1140 (lpClass
== NULL
)? NULL
: &ClassString
,
1145 ClosePredefKey(ParentKey
);
1147 TRACE("Status %x\n", Status
);
1148 if (!NT_SUCCESS(Status
))
1150 return RtlNtStatusToDosError(Status
);
1153 return ERROR_SUCCESS
;
1157 /************************************************************************
1163 RegCreateKeyA(HKEY hKey
,
1167 return RegCreateKeyExA(hKey
,
1179 /************************************************************************
1185 RegCreateKeyW(HKEY hKey
,
1189 return RegCreateKeyExW(hKey
,
1201 /************************************************************************
1207 RegDeleteKeyA(HKEY hKey
,
1210 OBJECT_ATTRIBUTES ObjectAttributes
;
1211 UNICODE_STRING SubKeyName
;
1216 /* Make sure we got a subkey */
1220 return ERROR_INVALID_PARAMETER
;
1223 Status
= MapDefaultKey(&ParentKey
,
1225 if (!NT_SUCCESS(Status
))
1227 return RtlNtStatusToDosError(Status
);
1230 RtlCreateUnicodeStringFromAsciiz(&SubKeyName
,
1232 InitializeObjectAttributes(&ObjectAttributes
,
1234 OBJ_CASE_INSENSITIVE
,
1238 Status
= NtOpenKey(&TargetKey
,
1241 RtlFreeUnicodeString(&SubKeyName
);
1242 if (!NT_SUCCESS(Status
))
1247 Status
= NtDeleteKey(TargetKey
);
1248 NtClose (TargetKey
);
1251 ClosePredefKey(ParentKey
);
1253 if (!NT_SUCCESS(Status
))
1255 return RtlNtStatusToDosError(Status
);
1258 return ERROR_SUCCESS
;
1262 /************************************************************************
1268 RegDeleteKeyW(HKEY hKey
,
1271 OBJECT_ATTRIBUTES ObjectAttributes
;
1272 UNICODE_STRING SubKeyName
;
1277 /* Make sure we got a subkey */
1281 return ERROR_INVALID_PARAMETER
;
1284 Status
= MapDefaultKey(&ParentKey
,
1286 if (!NT_SUCCESS(Status
))
1288 return RtlNtStatusToDosError(Status
);
1291 RtlInitUnicodeString(&SubKeyName
,
1293 InitializeObjectAttributes(&ObjectAttributes
,
1295 OBJ_CASE_INSENSITIVE
,
1298 Status
= NtOpenKey(&TargetKey
,
1301 if (!NT_SUCCESS(Status
))
1306 Status
= NtDeleteKey(TargetKey
);
1310 ClosePredefKey(ParentKey
);
1312 if (!NT_SUCCESS(Status
))
1314 return RtlNtStatusToDosError(Status
);
1317 return ERROR_SUCCESS
;
1321 /************************************************************************
1328 RegDeleteKeyExA(HKEY hKey
,
1333 OBJECT_ATTRIBUTES ObjectAttributes
;
1334 UNICODE_STRING SubKeyName
;
1339 /* Make sure we got a subkey */
1343 return ERROR_INVALID_PARAMETER
;
1346 Status
= MapDefaultKey(&ParentKey
,
1348 if (!NT_SUCCESS(Status
))
1350 return RtlNtStatusToDosError(Status
);
1353 if (samDesired
& KEY_WOW64_32KEY
)
1354 ERR("Wow64 not yet supported!\n");
1356 if (samDesired
& KEY_WOW64_64KEY
)
1357 ERR("Wow64 not yet supported!\n");
1359 RtlCreateUnicodeStringFromAsciiz(&SubKeyName
,
1361 InitializeObjectAttributes(&ObjectAttributes
,
1363 OBJ_CASE_INSENSITIVE
,
1367 Status
= NtOpenKey(&TargetKey
,
1370 RtlFreeUnicodeString(&SubKeyName
);
1371 if (!NT_SUCCESS(Status
))
1376 Status
= NtDeleteKey(TargetKey
);
1377 NtClose (TargetKey
);
1380 ClosePredefKey(ParentKey
);
1382 if (!NT_SUCCESS(Status
))
1384 return RtlNtStatusToDosError(Status
);
1387 return ERROR_SUCCESS
;
1391 /************************************************************************
1398 RegDeleteKeyExW(HKEY hKey
,
1403 OBJECT_ATTRIBUTES ObjectAttributes
;
1404 UNICODE_STRING SubKeyName
;
1409 /* Make sure we got a subkey */
1413 return ERROR_INVALID_PARAMETER
;
1416 Status
= MapDefaultKey(&ParentKey
,
1418 if (!NT_SUCCESS(Status
))
1420 return RtlNtStatusToDosError(Status
);
1423 if (samDesired
& KEY_WOW64_32KEY
)
1424 ERR("Wow64 not yet supported!\n");
1426 if (samDesired
& KEY_WOW64_64KEY
)
1427 ERR("Wow64 not yet supported!\n");
1430 RtlInitUnicodeString(&SubKeyName
,
1432 InitializeObjectAttributes(&ObjectAttributes
,
1434 OBJ_CASE_INSENSITIVE
,
1437 Status
= NtOpenKey(&TargetKey
,
1440 if (!NT_SUCCESS(Status
))
1445 Status
= NtDeleteKey(TargetKey
);
1449 ClosePredefKey(ParentKey
);
1451 if (!NT_SUCCESS(Status
))
1453 return RtlNtStatusToDosError(Status
);
1456 return ERROR_SUCCESS
;
1460 /************************************************************************
1461 * RegDeleteKeyValueW
1466 RegDeleteKeyValueW(IN HKEY hKey
,
1467 IN LPCWSTR lpSubKey OPTIONAL
,
1468 IN LPCWSTR lpValueName OPTIONAL
)
1470 UNICODE_STRING ValueName
;
1471 HANDLE KeyHandle
, CurKey
, SubKeyHandle
= NULL
;
1474 Status
= MapDefaultKey(&KeyHandle
,
1476 if (!NT_SUCCESS(Status
))
1478 return RtlNtStatusToDosError(Status
);
1481 if (lpSubKey
!= NULL
)
1483 OBJECT_ATTRIBUTES ObjectAttributes
;
1484 UNICODE_STRING SubKeyName
;
1486 RtlInitUnicodeString(&SubKeyName
,
1489 InitializeObjectAttributes(&ObjectAttributes
,
1491 OBJ_CASE_INSENSITIVE
,
1495 Status
= NtOpenKey(&SubKeyHandle
,
1498 if (!NT_SUCCESS(Status
))
1503 CurKey
= SubKeyHandle
;
1508 RtlInitUnicodeString(&ValueName
,
1509 (LPWSTR
)lpValueName
);
1511 Status
= NtDeleteValueKey(CurKey
,
1514 if (SubKeyHandle
!= NULL
)
1516 NtClose(SubKeyHandle
);
1520 ClosePredefKey(KeyHandle
);
1522 if (!NT_SUCCESS(Status
))
1524 return RtlNtStatusToDosError(Status
);
1527 return ERROR_SUCCESS
;
1531 /************************************************************************
1532 * RegDeleteKeyValueA
1537 RegDeleteKeyValueA(IN HKEY hKey
,
1538 IN LPCSTR lpSubKey OPTIONAL
,
1539 IN LPCSTR lpValueName OPTIONAL
)
1541 UNICODE_STRING SubKey
= { 0, 0, NULL
}, ValueName
= { 0, 0, NULL
};
1544 if (lpSubKey
!= NULL
&&
1545 !RtlCreateUnicodeStringFromAsciiz(&SubKey
,
1548 return ERROR_NOT_ENOUGH_MEMORY
;
1551 if (lpValueName
!= NULL
&&
1552 !RtlCreateUnicodeStringFromAsciiz(&ValueName
,
1553 (LPSTR
)lpValueName
))
1555 RtlFreeUnicodeString(&SubKey
);
1556 return ERROR_NOT_ENOUGH_MEMORY
;
1559 Ret
= RegDeleteKeyValueW(hKey
,
1563 RtlFreeUnicodeString(&SubKey
);
1564 RtlFreeUnicodeString(&ValueName
);
1570 // Non-recursive RegDeleteTreeW implementation by Thomas, however it needs bugfixing
1572 RegpDeleteTree(IN HKEY hKey
)
1576 LIST_ENTRY ListEntry
;
1578 } REGP_DEL_KEYS
, *PREG_DEL_KEYS
;
1580 LIST_ENTRY delQueueHead
;
1581 PREG_DEL_KEYS delKeys
, newDelKeys
;
1584 PKEY_BASIC_INFORMATION BasicInfo
;
1585 PREG_DEL_KEYS KeyDelRoot
;
1586 NTSTATUS Status
= STATUS_SUCCESS
;
1587 NTSTATUS Status2
= STATUS_SUCCESS
;
1589 InitializeListHead(&delQueueHead
);
1591 ProcessHeap
= RtlGetProcessHeap();
1593 /* NOTE: no need to allocate enough memory for an additional KEY_BASIC_INFORMATION
1594 structure for the root key, we only do that for subkeys as we need to
1595 allocate REGP_DEL_KEYS structures anyway! */
1596 KeyDelRoot
= RtlAllocateHeap(ProcessHeap
,
1598 sizeof(REGP_DEL_KEYS
));
1599 if (KeyDelRoot
!= NULL
)
1601 KeyDelRoot
->KeyHandle
= hKey
;
1602 InsertTailList(&delQueueHead
,
1603 &KeyDelRoot
->ListEntry
);
1607 delKeys
= CONTAINING_RECORD(delQueueHead
.Flink
,
1616 /* check if this key contains subkeys and delete them first by queuing
1617 them at the head of the list */
1618 Status2
= NtEnumerateKey(delKeys
->KeyHandle
,
1620 KeyBasicInformation
,
1625 if (NT_SUCCESS(Status2
))
1627 OBJECT_ATTRIBUTES ObjectAttributes
;
1628 UNICODE_STRING SubKeyName
;
1630 ASSERT(newDelKeys
!= NULL
);
1631 ASSERT(BasicInfo
!= NULL
);
1633 /* don't use RtlInitUnicodeString as the string is not NULL-terminated! */
1634 SubKeyName
.Length
= BasicInfo
->NameLength
;
1635 SubKeyName
.MaximumLength
= BasicInfo
->NameLength
;
1636 SubKeyName
.Buffer
= BasicInfo
->Name
;
1638 InitializeObjectAttributes(&ObjectAttributes
,
1640 OBJ_CASE_INSENSITIVE
,
1644 /* open the subkey */
1645 Status2
= NtOpenKey(&newDelKeys
->KeyHandle
,
1646 DELETE
| KEY_ENUMERATE_SUB_KEYS
,
1648 if (!NT_SUCCESS(Status2
))
1653 /* enqueue this key to the head of the deletion queue */
1654 InsertHeadList(&delQueueHead
,
1655 &newDelKeys
->ListEntry
);
1657 /* try again from the head of the list */
1662 if (Status2
== STATUS_BUFFER_TOO_SMALL
)
1664 newDelKeys
= RtlAllocateHeap(ProcessHeap
,
1666 BufferSize
+ sizeof(REGP_DEL_KEYS
));
1667 if (newDelKeys
!= NULL
)
1669 BasicInfo
= (PKEY_BASIC_INFORMATION
)(newDelKeys
+ 1);
1672 goto ReadFirstSubKey
;
1676 /* don't break, let's try to delete as many keys as possible */
1677 Status2
= STATUS_INSUFFICIENT_RESOURCES
;
1678 goto SubKeyFailureNoFree
;
1681 else if (Status2
== STATUS_BUFFER_OVERFLOW
)
1683 PREG_DEL_KEYS newDelKeys2
;
1685 ASSERT(newDelKeys
!= NULL
);
1687 /* we need more memory to query the key name */
1688 newDelKeys2
= RtlReAllocateHeap(ProcessHeap
,
1691 BufferSize
+ sizeof(REGP_DEL_KEYS
));
1692 if (newDelKeys2
!= NULL
)
1694 newDelKeys
= newDelKeys2
;
1695 BasicInfo
= (PKEY_BASIC_INFORMATION
)(newDelKeys
+ 1);
1698 goto ReadFirstSubKey
;
1702 /* don't break, let's try to delete as many keys as possible */
1703 Status2
= STATUS_INSUFFICIENT_RESOURCES
;
1706 else if (Status2
== STATUS_NO_MORE_ENTRIES
)
1708 /* in some race conditions where another thread would delete
1709 the same tree at the same time, newDelKeys could actually
1711 if (newDelKeys
!= NULL
)
1713 RtlFreeHeap(ProcessHeap
,
1721 /* newDelKeys can be NULL here when NtEnumerateKey returned an
1722 error other than STATUS_BUFFER_TOO_SMALL or STATUS_BUFFER_OVERFLOW! */
1723 if (newDelKeys
!= NULL
)
1725 RtlFreeHeap(ProcessHeap
,
1730 SubKeyFailureNoFree
:
1731 /* don't break, let's try to delete as many keys as possible */
1732 if (NT_SUCCESS(Status
))
1738 Status2
= NtDeleteKey(delKeys
->KeyHandle
);
1740 /* NOTE: do NOT close the handle anymore, it's invalid already! */
1742 if (!NT_SUCCESS(Status2
))
1744 /* close the key handle so we don't leak handles for keys we were
1745 unable to delete. But only do this for handles not supplied
1748 if (delKeys
->KeyHandle
!= hKey
)
1750 NtClose(delKeys
->KeyHandle
);
1753 if (NT_SUCCESS(Status
))
1755 /* don't break, let's try to delete as many keys as possible */
1760 /* remove the entry from the list */
1761 RemoveEntryList(&delKeys
->ListEntry
);
1763 RtlFreeHeap(ProcessHeap
,
1766 } while (!IsListEmpty(&delQueueHead
));
1769 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1775 /************************************************************************
1781 RegDeleteTreeW(IN HKEY hKey
,
1782 IN LPCWSTR lpSubKey OPTIONAL
)
1784 HANDLE KeyHandle
, CurKey
, SubKeyHandle
= NULL
;
1787 Status
= MapDefaultKey(&KeyHandle
,
1789 if (!NT_SUCCESS(Status
))
1791 return RtlNtStatusToDosError(Status
);
1794 if (lpSubKey
!= NULL
)
1796 OBJECT_ATTRIBUTES ObjectAttributes
;
1797 UNICODE_STRING SubKeyName
;
1799 RtlInitUnicodeString(&SubKeyName
,
1802 InitializeObjectAttributes(&ObjectAttributes
,
1804 OBJ_CASE_INSENSITIVE
,
1808 Status
= NtOpenKey(&SubKeyHandle
,
1809 DELETE
| KEY_ENUMERATE_SUB_KEYS
,
1811 if (!NT_SUCCESS(Status
))
1816 CurKey
= SubKeyHandle
;
1821 Status
= RegpDeleteTree(CurKey
);
1823 if (NT_SUCCESS(Status
))
1825 /* make sure we only close hKey (KeyHandle) when the caller specified a
1826 subkey, because the handle would be invalid already! */
1827 if (CurKey
!= KeyHandle
)
1829 ClosePredefKey(KeyHandle
);
1832 return ERROR_SUCCESS
;
1836 /* make sure we close all handles we created! */
1837 if (SubKeyHandle
!= NULL
)
1839 NtClose(SubKeyHandle
);
1843 ClosePredefKey(KeyHandle
);
1845 return RtlNtStatusToDosError(Status
);
1851 /************************************************************************
1858 RegDeleteTreeW(HKEY hKey
,
1862 DWORD dwMaxSubkeyLen
, dwMaxValueLen
;
1863 DWORD dwMaxLen
, dwSize
;
1867 WCHAR szNameBuf
[MAX_PATH
], *lpszName
= szNameBuf
;
1869 TRACE("(hkey=%p,%p %s)\n", hKey
, lpszSubKey
, debugstr_w(lpszSubKey
));
1871 Status
= MapDefaultKey(&KeyHandle
,
1873 if (!NT_SUCCESS(Status
))
1875 return RtlNtStatusToDosError(Status
);
1878 hSubKey
= KeyHandle
;
1882 ret
= RegOpenKeyExW(KeyHandle
, lpszSubKey
, 0, KEY_READ
, &hSubKey
);
1885 ClosePredefKey(KeyHandle
);
1890 /* Get highest length for keys, values */
1891 ret
= RegQueryInfoKeyW(hSubKey
, NULL
, NULL
, NULL
, NULL
,
1892 &dwMaxSubkeyLen
, NULL
, NULL
, &dwMaxValueLen
, NULL
, NULL
, NULL
);
1893 if (ret
) goto cleanup
;
1897 dwMaxLen
= max(dwMaxSubkeyLen
, dwMaxValueLen
);
1898 if (dwMaxLen
> sizeof(szNameBuf
)/sizeof(WCHAR
))
1900 /* Name too big: alloc a buffer for it */
1901 if (!(lpszName
= RtlAllocateHeap( RtlGetProcessHeap(), 0, dwMaxLen
*sizeof(WCHAR
))))
1903 ret
= ERROR_NOT_ENOUGH_MEMORY
;
1909 /* Recursively delete all the subkeys */
1913 if (RegEnumKeyExW(hSubKey
, 0, lpszName
, &dwSize
, NULL
,
1914 NULL
, NULL
, NULL
)) break;
1916 ret
= RegDeleteTreeW(hSubKey
, lpszName
);
1917 if (ret
) goto cleanup
;
1921 ret
= RegDeleteKeyW(KeyHandle
, lpszSubKey
);
1926 if (RegEnumValueW(KeyHandle
, 0, lpszName
, &dwSize
,
1927 NULL
, NULL
, NULL
, NULL
)) break;
1929 ret
= RegDeleteValueW(KeyHandle
, lpszName
);
1930 if (ret
) goto cleanup
;
1934 /* Free buffer if allocated */
1935 if (lpszName
!= szNameBuf
)
1936 RtlFreeHeap( RtlGetProcessHeap(), 0, lpszName
);
1938 RegCloseKey(hSubKey
);
1940 ClosePredefKey(KeyHandle
);
1946 /************************************************************************
1952 RegDeleteTreeA(IN HKEY hKey
,
1953 IN LPCSTR lpSubKey OPTIONAL
)
1955 UNICODE_STRING SubKeyName
= { 0, 0, NULL
};
1958 if (lpSubKey
!= NULL
&&
1959 !RtlCreateUnicodeStringFromAsciiz(&SubKeyName
,
1962 return ERROR_NOT_ENOUGH_MEMORY
;
1965 Ret
= RegDeleteTreeW(hKey
,
1968 RtlFreeUnicodeString(&SubKeyName
);
1974 /************************************************************************
1975 * RegDisableReflectionKey
1980 RegDisableReflectionKey(IN HKEY hBase
)
1982 FIXME("RegDisableReflectionKey(0x%p) UNIMPLEMENTED!\n", hBase
);
1983 return ERROR_CALL_NOT_IMPLEMENTED
;
1987 /************************************************************************
1988 * RegEnableReflectionKey
1993 RegEnableReflectionKey(IN HKEY hBase
)
1995 FIXME("RegEnableReflectionKey(0x%p) UNIMPLEMENTED!\n", hBase
);
1996 return ERROR_CALL_NOT_IMPLEMENTED
;
2000 /******************************************************************************
2001 * RegpApplyRestrictions [internal]
2003 * Helper function for RegGetValueA/W.
2006 RegpApplyRestrictions(DWORD dwFlags
,
2011 /* Check if the type is restricted by the passed flags */
2012 if (*ret
== ERROR_SUCCESS
|| *ret
== ERROR_MORE_DATA
)
2018 case REG_NONE
: dwMask
= RRF_RT_REG_NONE
; break;
2019 case REG_SZ
: dwMask
= RRF_RT_REG_SZ
; break;
2020 case REG_EXPAND_SZ
: dwMask
= RRF_RT_REG_EXPAND_SZ
; break;
2021 case REG_MULTI_SZ
: dwMask
= RRF_RT_REG_MULTI_SZ
; break;
2022 case REG_BINARY
: dwMask
= RRF_RT_REG_BINARY
; break;
2023 case REG_DWORD
: dwMask
= RRF_RT_REG_DWORD
; break;
2024 case REG_QWORD
: dwMask
= RRF_RT_REG_QWORD
; break;
2027 if (dwFlags
& dwMask
)
2029 /* Type is not restricted, check for size mismatch */
2030 if (dwType
== REG_BINARY
)
2034 if ((dwFlags
& RRF_RT_ANY
) == RRF_RT_DWORD
)
2036 else if ((dwFlags
& RRF_RT_ANY
) == RRF_RT_QWORD
)
2039 if (cbExpect
&& cbData
!= cbExpect
)
2040 *ret
= ERROR_DATATYPE_MISMATCH
;
2043 else *ret
= ERROR_UNSUPPORTED_TYPE
;
2048 /******************************************************************************
2049 * RegGetValueW [ADVAPI32.@]
2051 * Retrieves the type and data for a value name associated with a key,
2052 * optionally expanding its content and restricting its type.
2055 * hKey [I] Handle to an open key.
2056 * pszSubKey [I] Name of the subkey of hKey.
2057 * pszValue [I] Name of value under hKey/szSubKey to query.
2058 * dwFlags [I] Flags restricting the value type to retrieve.
2059 * pdwType [O] Destination for the values type, may be NULL.
2060 * pvData [O] Destination for the values content, may be NULL.
2061 * pcbData [I/O] Size of pvData, updated with the size in bytes required to
2062 * retrieve the whole content, including the trailing '\0'
2066 * Success: ERROR_SUCCESS
2067 * Failure: nonzero error code from Winerror.h
2070 * - Unless RRF_NOEXPAND is specified, REG_EXPAND_SZ values are automatically
2071 * expanded and pdwType is set to REG_SZ instead.
2072 * - Restrictions are applied after expanding, using RRF_RT_REG_EXPAND_SZ
2073 * without RRF_NOEXPAND is thus not allowed.
2074 * An exception is the case where RRF_RT_ANY is specified, because then
2075 * RRF_NOEXPAND is allowed.
2078 RegGetValueW(HKEY hKey
,
2086 DWORD dwType
, cbData
= pcbData
? *pcbData
: 0;
2090 TRACE("(%p,%s,%s,%ld,%p,%p,%p=%ld)\n",
2091 hKey
, debugstr_w(pszSubKey
), debugstr_w(pszValue
), dwFlags
, pdwType
,
2092 pvData
, pcbData
, cbData
);
2094 if (pvData
&& !pcbData
)
2095 return ERROR_INVALID_PARAMETER
;
2096 if ((dwFlags
& RRF_RT_REG_EXPAND_SZ
) && !(dwFlags
& RRF_NOEXPAND
) &&
2097 ((dwFlags
& RRF_RT_ANY
) != RRF_RT_ANY
))
2098 return ERROR_INVALID_PARAMETER
;
2100 if (pszSubKey
&& pszSubKey
[0])
2102 ret
= RegOpenKeyExW(hKey
, pszSubKey
, 0, KEY_QUERY_VALUE
, &hKey
);
2103 if (ret
!= ERROR_SUCCESS
) return ret
;
2106 ret
= RegQueryValueExW(hKey
, pszValue
, NULL
, &dwType
, pvData
, &cbData
);
2108 /* If we are going to expand we need to read in the whole the value even
2109 * if the passed buffer was too small as the expanded string might be
2110 * smaller than the unexpanded one and could fit into cbData bytes. */
2111 if ((ret
== ERROR_SUCCESS
|| ret
== ERROR_MORE_DATA
) &&
2112 dwType
== REG_EXPAND_SZ
&& !(dwFlags
& RRF_NOEXPAND
))
2116 HeapFree(GetProcessHeap(), 0, pvBuf
);
2118 pvBuf
= HeapAlloc(GetProcessHeap(), 0, cbData
);
2121 ret
= ERROR_NOT_ENOUGH_MEMORY
;
2125 if (ret
== ERROR_MORE_DATA
|| !pvData
)
2126 ret
= RegQueryValueExW(hKey
, pszValue
, NULL
,
2127 &dwType
, pvBuf
, &cbData
);
2130 /* Even if cbData was large enough we have to copy the
2131 * string since ExpandEnvironmentStrings can't handle
2132 * overlapping buffers. */
2133 CopyMemory(pvBuf
, pvData
, cbData
);
2136 /* Both the type or the value itself could have been modified in
2137 * between so we have to keep retrying until the buffer is large
2138 * enough or we no longer have to expand the value. */
2140 while (dwType
== REG_EXPAND_SZ
&& ret
== ERROR_MORE_DATA
);
2142 if (ret
== ERROR_SUCCESS
)
2144 /* Recheck dwType in case it changed since the first call */
2145 if (dwType
== REG_EXPAND_SZ
)
2147 cbData
= ExpandEnvironmentStringsW(pvBuf
, pvData
,
2148 pcbData
? *pcbData
: 0) * sizeof(WCHAR
);
2150 if (pvData
&& pcbData
&& cbData
> *pcbData
)
2151 ret
= ERROR_MORE_DATA
;
2154 CopyMemory(pvData
, pvBuf
, *pcbData
);
2157 HeapFree(GetProcessHeap(), 0, pvBuf
);
2160 if (pszSubKey
&& pszSubKey
[0])
2163 RegpApplyRestrictions(dwFlags
, dwType
, cbData
, &ret
);
2165 if (pvData
&& ret
!= ERROR_SUCCESS
&& (dwFlags
& RRF_ZEROONFAILURE
))
2166 ZeroMemory(pvData
, *pcbData
);
2178 /******************************************************************************
2179 * RegGetValueA [ADVAPI32.@]
2184 RegGetValueA(HKEY hKey
,
2192 DWORD dwType
, cbData
= pcbData
? *pcbData
: 0;
2196 TRACE("(%p,%s,%s,%ld,%p,%p,%p=%ld)\n",
2197 hKey
, pszSubKey
, pszValue
, dwFlags
, pdwType
, pvData
, pcbData
,
2200 if (pvData
&& !pcbData
)
2201 return ERROR_INVALID_PARAMETER
;
2202 if ((dwFlags
& RRF_RT_REG_EXPAND_SZ
) && !(dwFlags
& RRF_NOEXPAND
) &&
2203 ((dwFlags
& RRF_RT_ANY
) != RRF_RT_ANY
))
2204 return ERROR_INVALID_PARAMETER
;
2206 if (pszSubKey
&& pszSubKey
[0])
2208 ret
= RegOpenKeyExA(hKey
, pszSubKey
, 0, KEY_QUERY_VALUE
, &hKey
);
2209 if (ret
!= ERROR_SUCCESS
) return ret
;
2212 ret
= RegQueryValueExA(hKey
, pszValue
, NULL
, &dwType
, pvData
, &cbData
);
2214 /* If we are going to expand we need to read in the whole the value even
2215 * if the passed buffer was too small as the expanded string might be
2216 * smaller than the unexpanded one and could fit into cbData bytes. */
2217 if ((ret
== ERROR_SUCCESS
|| ret
== ERROR_MORE_DATA
) &&
2218 (dwType
== REG_EXPAND_SZ
&& !(dwFlags
& RRF_NOEXPAND
)))
2221 HeapFree(GetProcessHeap(), 0, pvBuf
);
2223 pvBuf
= HeapAlloc(GetProcessHeap(), 0, cbData
);
2226 ret
= ERROR_NOT_ENOUGH_MEMORY
;
2230 if (ret
== ERROR_MORE_DATA
|| !pvData
)
2231 ret
= RegQueryValueExA(hKey
, pszValue
, NULL
,
2232 &dwType
, pvBuf
, &cbData
);
2235 /* Even if cbData was large enough we have to copy the
2236 * string since ExpandEnvironmentStrings can't handle
2237 * overlapping buffers. */
2238 CopyMemory(pvBuf
, pvData
, cbData
);
2241 /* Both the type or the value itself could have been modified in
2242 * between so we have to keep retrying until the buffer is large
2243 * enough or we no longer have to expand the value. */
2244 } while (dwType
== REG_EXPAND_SZ
&& ret
== ERROR_MORE_DATA
);
2246 if (ret
== ERROR_SUCCESS
)
2248 /* Recheck dwType in case it changed since the first call */
2249 if (dwType
== REG_EXPAND_SZ
)
2251 cbData
= ExpandEnvironmentStringsA(pvBuf
, pvData
,
2252 pcbData
? *pcbData
: 0);
2254 if(pvData
&& pcbData
&& cbData
> *pcbData
)
2255 ret
= ERROR_MORE_DATA
;
2258 CopyMemory(pvData
, pvBuf
, *pcbData
);
2261 HeapFree(GetProcessHeap(), 0, pvBuf
);
2264 if (pszSubKey
&& pszSubKey
[0])
2267 RegpApplyRestrictions(dwFlags
, dwType
, cbData
, &ret
);
2269 if (pvData
&& ret
!= ERROR_SUCCESS
&& (dwFlags
& RRF_ZEROONFAILURE
))
2270 ZeroMemory(pvData
, *pcbData
);
2272 if (pdwType
) *pdwType
= dwType
;
2273 if (pcbData
) *pcbData
= cbData
;
2279 /************************************************************************
2285 RegSetKeyValueW(IN HKEY hKey
,
2286 IN LPCWSTR lpSubKey OPTIONAL
,
2287 IN LPCWSTR lpValueName OPTIONAL
,
2289 IN LPCVOID lpData OPTIONAL
,
2292 HANDLE KeyHandle
, CurKey
, SubKeyHandle
= NULL
;
2296 Status
= MapDefaultKey(&KeyHandle
,
2298 if (!NT_SUCCESS(Status
))
2300 return RtlNtStatusToDosError(Status
);
2303 if (lpSubKey
!= NULL
)
2305 OBJECT_ATTRIBUTES ObjectAttributes
;
2306 UNICODE_STRING SubKeyName
;
2308 RtlInitUnicodeString(&SubKeyName
,
2311 InitializeObjectAttributes(&ObjectAttributes
,
2313 OBJ_CASE_INSENSITIVE
,
2317 Status
= NtOpenKey(&SubKeyHandle
,
2320 if (!NT_SUCCESS(Status
))
2322 Ret
= RtlNtStatusToDosError(Status
);
2326 CurKey
= SubKeyHandle
;
2331 Ret
= RegSetValueExW(CurKey
,
2338 if (SubKeyHandle
!= NULL
)
2340 NtClose(SubKeyHandle
);
2344 ClosePredefKey(KeyHandle
);
2350 /************************************************************************
2356 RegSetKeyValueA(IN HKEY hKey
,
2357 IN LPCSTR lpSubKey OPTIONAL
,
2358 IN LPCSTR lpValueName OPTIONAL
,
2360 IN LPCVOID lpData OPTIONAL
,
2363 HANDLE KeyHandle
, CurKey
, SubKeyHandle
= NULL
;
2367 Status
= MapDefaultKey(&KeyHandle
,
2369 if (!NT_SUCCESS(Status
))
2371 return RtlNtStatusToDosError(Status
);
2374 if (lpSubKey
!= NULL
)
2376 OBJECT_ATTRIBUTES ObjectAttributes
;
2377 UNICODE_STRING SubKeyName
;
2379 if (!RtlCreateUnicodeStringFromAsciiz(&SubKeyName
,
2382 Ret
= ERROR_NOT_ENOUGH_MEMORY
;
2386 InitializeObjectAttributes(&ObjectAttributes
,
2388 OBJ_CASE_INSENSITIVE
,
2392 Status
= NtOpenKey(&SubKeyHandle
,
2396 RtlFreeUnicodeString(&SubKeyName
);
2398 if (!NT_SUCCESS(Status
))
2400 Ret
= RtlNtStatusToDosError(Status
);
2404 CurKey
= SubKeyHandle
;
2409 Ret
= RegSetValueExA(CurKey
,
2416 if (SubKeyHandle
!= NULL
)
2418 NtClose(SubKeyHandle
);
2422 ClosePredefKey(KeyHandle
);
2428 /************************************************************************
2434 RegDeleteValueA(HKEY hKey
,
2437 UNICODE_STRING ValueName
;
2441 Status
= MapDefaultKey(&KeyHandle
,
2443 if (!NT_SUCCESS(Status
))
2445 return RtlNtStatusToDosError(Status
);
2448 RtlCreateUnicodeStringFromAsciiz(&ValueName
,
2449 (LPSTR
)lpValueName
);
2450 Status
= NtDeleteValueKey(KeyHandle
,
2452 RtlFreeUnicodeString (&ValueName
);
2454 ClosePredefKey(KeyHandle
);
2456 if (!NT_SUCCESS(Status
))
2458 return RtlNtStatusToDosError(Status
);
2461 return ERROR_SUCCESS
;
2465 /************************************************************************
2471 RegDeleteValueW(HKEY hKey
,
2472 LPCWSTR lpValueName
)
2474 UNICODE_STRING ValueName
;
2478 Status
= MapDefaultKey(&KeyHandle
,
2480 if (!NT_SUCCESS(Status
))
2482 return RtlNtStatusToDosError(Status
);
2485 RtlInitUnicodeString(&ValueName
,
2486 (LPWSTR
)lpValueName
);
2488 Status
= NtDeleteValueKey(KeyHandle
,
2491 ClosePredefKey(KeyHandle
);
2493 if (!NT_SUCCESS(Status
))
2495 return RtlNtStatusToDosError(Status
);
2498 return ERROR_SUCCESS
;
2502 /************************************************************************
2508 RegEnumKeyA(HKEY hKey
,
2516 return RegEnumKeyExA(hKey
,
2527 /************************************************************************
2533 RegEnumKeyW(HKEY hKey
,
2541 return RegEnumKeyExW(hKey
,
2552 /************************************************************************
2558 RegEnumKeyExA(HKEY hKey
,
2565 PFILETIME lpftLastWriteTime
)
2569 KEY_NODE_INFORMATION Node
;
2570 KEY_BASIC_INFORMATION Basic
;
2573 UNICODE_STRING StringU
;
2574 ANSI_STRING StringA
;
2575 LONG ErrorCode
= ERROR_SUCCESS
;
2577 DWORD ClassLength
= 0;
2583 TRACE("RegEnumKeyExA(hKey 0x%x, dwIndex %d, lpName 0x%x, *lpcbName %d, lpClass 0x%x, lpcbClass %d)\n",
2584 hKey
, dwIndex
, lpName
, *lpcbName
, lpClass
, lpcbClass
? *lpcbClass
: 0);
2586 if ((lpClass
) && (!lpcbClass
))
2588 return ERROR_INVALID_PARAMETER
;
2591 Status
= MapDefaultKey(&KeyHandle
, hKey
);
2592 if (!NT_SUCCESS(Status
))
2594 return RtlNtStatusToDosError(Status
);
2599 NameLength
= min (*lpcbName
- 1 , REG_MAX_NAME_SIZE
) * sizeof (WCHAR
);
2610 ClassLength
= min (*lpcbClass
-1, REG_MAX_NAME_SIZE
) * sizeof(WCHAR
);
2617 /* The class name should start at a dword boundary */
2618 BufferSize
= ((sizeof(KEY_NODE_INFORMATION
) + NameLength
+ 3) & ~3) + ClassLength
;
2622 BufferSize
= sizeof(KEY_BASIC_INFORMATION
) + NameLength
;
2625 KeyInfo
= RtlAllocateHeap (ProcessHeap
, 0, BufferSize
);
2626 if (KeyInfo
== NULL
)
2628 ErrorCode
= ERROR_OUTOFMEMORY
;
2632 Status
= NtEnumerateKey(KeyHandle
,
2634 lpClass
== NULL
? KeyBasicInformation
: KeyNodeInformation
,
2638 TRACE("NtEnumerateKey() returned status 0x%X\n", Status
);
2639 if (!NT_SUCCESS(Status
))
2641 ErrorCode
= RtlNtStatusToDosError (Status
);
2645 if (lpClass
== NULL
)
2647 if (KeyInfo
->Basic
.NameLength
> NameLength
)
2649 ErrorCode
= ERROR_BUFFER_OVERFLOW
;
2653 StringU
.Buffer
= KeyInfo
->Basic
.Name
;
2654 StringU
.Length
= KeyInfo
->Basic
.NameLength
;
2655 StringU
.MaximumLength
= KeyInfo
->Basic
.NameLength
;
2660 if (KeyInfo
->Node
.NameLength
> NameLength
||
2661 KeyInfo
->Node
.ClassLength
> ClassLength
)
2663 ErrorCode
= ERROR_BUFFER_OVERFLOW
;
2667 StringA
.Buffer
= lpClass
;
2669 StringA
.MaximumLength
= *lpcbClass
;
2670 StringU
.Buffer
= (PWCHAR
)((ULONG_PTR
)KeyInfo
->Node
.Name
+ KeyInfo
->Node
.ClassOffset
);
2671 StringU
.Length
= KeyInfo
->Node
.ClassLength
;
2672 StringU
.MaximumLength
= KeyInfo
->Node
.ClassLength
;
2673 RtlUnicodeStringToAnsiString (&StringA
, &StringU
, FALSE
);
2674 lpClass
[StringA
.Length
] = 0;
2675 *lpcbClass
= StringA
.Length
;
2676 StringU
.Buffer
= KeyInfo
->Node
.Name
;
2677 StringU
.Length
= KeyInfo
->Node
.NameLength
;
2678 StringU
.MaximumLength
= KeyInfo
->Node
.NameLength
;
2682 if (ErrorCode
== ERROR_SUCCESS
)
2684 StringA
.Buffer
= lpName
;
2686 StringA
.MaximumLength
= *lpcbName
;
2687 RtlUnicodeStringToAnsiString (&StringA
, &StringU
, FALSE
);
2688 lpName
[StringA
.Length
] = 0;
2689 *lpcbName
= StringA
.Length
;
2690 if (lpftLastWriteTime
!= NULL
)
2692 if (lpClass
== NULL
)
2694 lpftLastWriteTime
->dwLowDateTime
= KeyInfo
->Basic
.LastWriteTime
.u
.LowPart
;
2695 lpftLastWriteTime
->dwHighDateTime
= KeyInfo
->Basic
.LastWriteTime
.u
.HighPart
;
2699 lpftLastWriteTime
->dwLowDateTime
= KeyInfo
->Node
.LastWriteTime
.u
.LowPart
;
2700 lpftLastWriteTime
->dwHighDateTime
= KeyInfo
->Node
.LastWriteTime
.u
.HighPart
;
2706 /*TRACE("Key Namea0 Length %d\n", StringU.Length);*/ /* BUGBUG could be uninitialized */
2707 TRACE("Key Name1 Length %d\n", NameLength
);
2708 TRACE("Key Name Length %d\n", *lpcbName
);
2709 TRACE("Key Name %s\n", lpName
);
2711 RtlFreeHeap(ProcessHeap
,
2716 ClosePredefKey(KeyHandle
);
2722 /************************************************************************
2728 RegEnumKeyExW(HKEY hKey
,
2735 PFILETIME lpftLastWriteTime
)
2739 KEY_NODE_INFORMATION Node
;
2740 KEY_BASIC_INFORMATION Basic
;
2746 ULONG ClassLength
= 0;
2748 LONG ErrorCode
= ERROR_SUCCESS
;
2751 Status
= MapDefaultKey(&KeyHandle
,
2753 if (!NT_SUCCESS(Status
))
2755 return RtlNtStatusToDosError(Status
);
2760 NameLength
= min (*lpcbName
- 1, REG_MAX_NAME_SIZE
) * sizeof (WCHAR
);
2771 ClassLength
= min (*lpcbClass
- 1, REG_MAX_NAME_SIZE
) * sizeof(WCHAR
);
2778 BufferSize
= ((sizeof(KEY_NODE_INFORMATION
) + NameLength
+ 3) & ~3) + ClassLength
;
2782 BufferSize
= sizeof(KEY_BASIC_INFORMATION
) + NameLength
;
2785 KeyInfo
= RtlAllocateHeap(ProcessHeap
,
2788 if (KeyInfo
== NULL
)
2790 ErrorCode
= ERROR_OUTOFMEMORY
;
2794 Status
= NtEnumerateKey(KeyHandle
,
2796 lpClass
? KeyNodeInformation
: KeyBasicInformation
,
2800 TRACE("NtEnumerateKey() returned status 0x%X\n", Status
);
2801 if (!NT_SUCCESS(Status
))
2803 ErrorCode
= RtlNtStatusToDosError (Status
);
2807 if (lpClass
== NULL
)
2809 if (KeyInfo
->Basic
.NameLength
> NameLength
)
2811 ErrorCode
= ERROR_BUFFER_OVERFLOW
;
2815 RtlCopyMemory(lpName
,
2816 KeyInfo
->Basic
.Name
,
2817 KeyInfo
->Basic
.NameLength
);
2818 *lpcbName
= (DWORD
)(KeyInfo
->Basic
.NameLength
/ sizeof(WCHAR
));
2819 lpName
[*lpcbName
] = 0;
2824 if (KeyInfo
->Node
.NameLength
> NameLength
||
2825 KeyInfo
->Node
.ClassLength
> ClassLength
)
2827 ErrorCode
= ERROR_BUFFER_OVERFLOW
;
2831 RtlCopyMemory(lpName
,
2833 KeyInfo
->Node
.NameLength
);
2834 *lpcbName
= KeyInfo
->Node
.NameLength
/ sizeof(WCHAR
);
2835 lpName
[*lpcbName
] = 0;
2836 RtlCopyMemory(lpClass
,
2837 (PVOID
)((ULONG_PTR
)KeyInfo
->Node
.Name
+ KeyInfo
->Node
.ClassOffset
),
2838 KeyInfo
->Node
.ClassLength
);
2839 *lpcbClass
= (DWORD
)(KeyInfo
->Node
.ClassLength
/ sizeof(WCHAR
));
2840 lpClass
[*lpcbClass
] = 0;
2844 if (ErrorCode
== ERROR_SUCCESS
&& lpftLastWriteTime
!= NULL
)
2846 if (lpClass
== NULL
)
2848 lpftLastWriteTime
->dwLowDateTime
= KeyInfo
->Basic
.LastWriteTime
.u
.LowPart
;
2849 lpftLastWriteTime
->dwHighDateTime
= KeyInfo
->Basic
.LastWriteTime
.u
.HighPart
;
2853 lpftLastWriteTime
->dwLowDateTime
= KeyInfo
->Node
.LastWriteTime
.u
.LowPart
;
2854 lpftLastWriteTime
->dwHighDateTime
= KeyInfo
->Node
.LastWriteTime
.u
.HighPart
;
2859 RtlFreeHeap(ProcessHeap
,
2864 ClosePredefKey(KeyHandle
);
2870 /************************************************************************
2876 RegEnumValueA(HKEY hKey
,
2888 char buffer
[256], *buf_ptr
= buffer
;
2889 KEY_VALUE_FULL_INFORMATION
*info
= (KEY_VALUE_FULL_INFORMATION
*)buffer
;
2890 static const int info_size
= FIELD_OFFSET( KEY_VALUE_FULL_INFORMATION
, Name
);
2892 //TRACE("(%p,%ld,%p,%p,%p,%p,%p,%p)\n",
2893 // hkey, index, value, val_count, reserved, type, data, count );
2895 /* NT only checks count, not val_count */
2896 if ((data
&& !count
) || reserved
)
2897 return ERROR_INVALID_PARAMETER
;
2899 status
= MapDefaultKey(&KeyHandle
, hKey
);
2900 if (!NT_SUCCESS(status
))
2902 return RtlNtStatusToDosError(status
);
2905 total_size
= info_size
+ (MAX_PATH
+ 1) * sizeof(WCHAR
);
2906 if (data
) total_size
+= *count
;
2907 total_size
= min( sizeof(buffer
), total_size
);
2909 status
= NtEnumerateValueKey( KeyHandle
, index
, KeyValueFullInformation
,
2910 buffer
, total_size
, &total_size
);
2911 if (status
&& (status
!= STATUS_BUFFER_OVERFLOW
) && (status
!= STATUS_BUFFER_TOO_SMALL
)) goto done
;
2913 /* we need to fetch the contents for a string type even if not requested,
2914 * because we need to compute the length of the ASCII string. */
2915 if (value
|| data
|| is_string(info
->Type
))
2917 /* retry with a dynamically allocated buffer */
2918 while ((status
== STATUS_BUFFER_OVERFLOW
) || (status
== STATUS_BUFFER_TOO_SMALL
))
2920 if (buf_ptr
!= buffer
) HeapFree( GetProcessHeap(), 0, buf_ptr
);
2921 if (!(buf_ptr
= HeapAlloc( GetProcessHeap(), 0, total_size
)))
2923 status
= STATUS_INSUFFICIENT_RESOURCES
;
2926 info
= (KEY_VALUE_FULL_INFORMATION
*)buf_ptr
;
2927 status
= NtEnumerateValueKey( KeyHandle
, index
, KeyValueFullInformation
,
2928 buf_ptr
, total_size
, &total_size
);
2931 if (status
) goto done
;
2933 if (is_string(info
->Type
))
2936 RtlUnicodeToMultiByteSize( &len
, (WCHAR
*)(buf_ptr
+ info
->DataOffset
),
2940 if (len
> *count
) status
= STATUS_BUFFER_OVERFLOW
;
2943 RtlUnicodeToMultiByteN( (PCHAR
)data
, len
, NULL
, (WCHAR
*)(buf_ptr
+ info
->DataOffset
),
2945 /* if the type is REG_SZ and data is not 0-terminated
2946 * and there is enough space in the buffer NT appends a \0 */
2947 if (len
< *count
&& data
[len
-1]) data
[len
] = 0;
2950 info
->DataLength
= len
;
2954 if (info
->DataLength
> *count
) status
= STATUS_BUFFER_OVERFLOW
;
2955 else memcpy( data
, buf_ptr
+ info
->DataOffset
, info
->DataLength
);
2958 if (value
&& !status
)
2962 RtlUnicodeToMultiByteSize( &len
, info
->Name
, info
->NameLength
);
2963 if (len
>= *val_count
)
2965 status
= STATUS_BUFFER_OVERFLOW
;
2968 len
= *val_count
- 1;
2969 RtlUnicodeToMultiByteN( value
, len
, NULL
, info
->Name
, info
->NameLength
);
2975 RtlUnicodeToMultiByteN( value
, len
, NULL
, info
->Name
, info
->NameLength
);
2981 else status
= STATUS_SUCCESS
;
2983 if (type
) *type
= info
->Type
;
2984 if (count
) *count
= info
->DataLength
;
2987 if (buf_ptr
!= buffer
) HeapFree( GetProcessHeap(), 0, buf_ptr
);
2988 ClosePredefKey(KeyHandle
);
2989 return RtlNtStatusToDosError(status
);
2993 /******************************************************************************
2994 * RegEnumValueW [ADVAPI32.@]
2998 * hkey [I] Handle to key to query
2999 * index [I] Index of value to query
3000 * value [O] Value string
3001 * val_count [I/O] Size of value buffer (in wchars)
3002 * reserved [I] Reserved
3003 * type [O] Type code
3004 * data [O] Value data
3005 * count [I/O] Size of data buffer (in bytes)
3008 * Success: ERROR_SUCCESS
3009 * Failure: nonzero error code from Winerror.h
3012 RegEnumValueW(HKEY hKey
,
3024 char buffer
[256], *buf_ptr
= buffer
;
3025 KEY_VALUE_FULL_INFORMATION
*info
= (KEY_VALUE_FULL_INFORMATION
*)buffer
;
3026 static const int info_size
= FIELD_OFFSET( KEY_VALUE_FULL_INFORMATION
, Name
);
3028 //TRACE("(%p,%ld,%p,%p,%p,%p,%p,%p)\n",
3029 // hkey, index, value, val_count, reserved, type, data, count );
3031 /* NT only checks count, not val_count */
3032 if ((data
&& !count
) || reserved
) return ERROR_INVALID_PARAMETER
;
3034 status
= MapDefaultKey(&KeyHandle
, hKey
);
3035 if (!NT_SUCCESS(status
))
3037 return RtlNtStatusToDosError(status
);
3040 total_size
= info_size
+ (MAX_PATH
+ 1) * sizeof(WCHAR
);
3041 if (data
) total_size
+= *count
;
3042 total_size
= min( sizeof(buffer
), total_size
);
3044 status
= NtEnumerateValueKey( KeyHandle
, index
, KeyValueFullInformation
,
3045 buffer
, total_size
, &total_size
);
3046 if (status
&& (status
!= STATUS_BUFFER_OVERFLOW
) && (status
!= STATUS_BUFFER_TOO_SMALL
)) goto done
;
3050 /* retry with a dynamically allocated buffer */
3051 while ((status
== STATUS_BUFFER_OVERFLOW
) || (status
== STATUS_BUFFER_TOO_SMALL
))
3053 if (buf_ptr
!= buffer
) HeapFree( GetProcessHeap(), 0, buf_ptr
);
3054 if (!(buf_ptr
= HeapAlloc( GetProcessHeap(), 0, total_size
)))
3056 status
= ERROR_NOT_ENOUGH_MEMORY
;
3059 info
= (KEY_VALUE_FULL_INFORMATION
*)buf_ptr
;
3060 status
= NtEnumerateValueKey( KeyHandle
, index
, KeyValueFullInformation
,
3061 buf_ptr
, total_size
, &total_size
);
3064 if (status
) goto done
;
3068 if (info
->NameLength
/sizeof(WCHAR
) >= *val_count
)
3070 status
= STATUS_BUFFER_OVERFLOW
;
3073 memcpy( value
, info
->Name
, info
->NameLength
);
3074 *val_count
= info
->NameLength
/ sizeof(WCHAR
);
3075 value
[*val_count
] = 0;
3080 if (info
->DataLength
> *count
)
3082 status
= STATUS_BUFFER_OVERFLOW
;
3085 memcpy( data
, buf_ptr
+ info
->DataOffset
, info
->DataLength
);
3086 if (is_string(info
->Type
) && info
->DataLength
<= *count
- sizeof(WCHAR
))
3088 /* if the type is REG_SZ and data is not 0-terminated
3089 * and there is enough space in the buffer NT appends a \0 */
3090 WCHAR
*ptr
= (WCHAR
*)(data
+ info
->DataLength
);
3091 if (ptr
> (WCHAR
*)data
&& ptr
[-1]) *ptr
= 0;
3095 else status
= STATUS_SUCCESS
;
3098 if (type
) *type
= info
->Type
;
3099 if (count
) *count
= info
->DataLength
;
3102 if (buf_ptr
!= buffer
) HeapFree( GetProcessHeap(), 0, buf_ptr
);
3103 ClosePredefKey(KeyHandle
);
3104 return RtlNtStatusToDosError(status
);
3108 /************************************************************************
3114 RegFlushKey(HKEY hKey
)
3119 if (hKey
== HKEY_PERFORMANCE_DATA
)
3121 return ERROR_SUCCESS
;
3124 Status
= MapDefaultKey(&KeyHandle
,
3126 if (!NT_SUCCESS(Status
))
3128 return RtlNtStatusToDosError(Status
);
3131 Status
= NtFlushKey(KeyHandle
);
3133 ClosePredefKey(KeyHandle
);
3135 if (!NT_SUCCESS(Status
))
3137 return RtlNtStatusToDosError(Status
);
3140 return ERROR_SUCCESS
;
3144 /************************************************************************
3150 RegGetKeySecurity(HKEY hKey
,
3151 SECURITY_INFORMATION SecurityInformation
,
3152 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
3153 LPDWORD lpcbSecurityDescriptor
)
3158 if (hKey
== HKEY_PERFORMANCE_DATA
)
3160 return ERROR_INVALID_HANDLE
;
3163 Status
= MapDefaultKey(&KeyHandle
,
3165 if (!NT_SUCCESS(Status
))
3167 TRACE("MapDefaultKey() failed (Status %lx)\n", Status
);
3168 return RtlNtStatusToDosError(Status
);
3171 Status
= NtQuerySecurityObject(KeyHandle
,
3172 SecurityInformation
,
3173 pSecurityDescriptor
,
3174 *lpcbSecurityDescriptor
,
3175 lpcbSecurityDescriptor
);
3177 ClosePredefKey(KeyHandle
);
3179 if (!NT_SUCCESS(Status
))
3181 WARN("NtQuerySecurityObject() failed (Status %lx)\n", Status
);
3182 return RtlNtStatusToDosError(Status
);
3185 return ERROR_SUCCESS
;
3189 /************************************************************************
3195 RegLoadKeyA(HKEY hKey
,
3199 UNICODE_STRING FileName
;
3200 UNICODE_STRING KeyName
;
3203 RtlCreateUnicodeStringFromAsciiz(&KeyName
,
3205 RtlCreateUnicodeStringFromAsciiz(&FileName
,
3208 ErrorCode
= RegLoadKeyW(hKey
,
3212 RtlFreeUnicodeString(&FileName
);
3213 RtlFreeUnicodeString(&KeyName
);
3219 /************************************************************************
3225 RegLoadKeyW(HKEY hKey
,
3229 OBJECT_ATTRIBUTES FileObjectAttributes
;
3230 OBJECT_ATTRIBUTES KeyObjectAttributes
;
3231 UNICODE_STRING FileName
;
3232 UNICODE_STRING KeyName
;
3235 LONG ErrorCode
= ERROR_SUCCESS
;
3237 if (hKey
== HKEY_PERFORMANCE_DATA
)
3239 return ERROR_INVALID_HANDLE
;
3242 Status
= MapDefaultKey(&KeyHandle
,
3244 if (!NT_SUCCESS(Status
))
3246 return RtlNtStatusToDosError(Status
);
3249 if (!RtlDosPathNameToNtPathName_U(lpFile
,
3254 ErrorCode
= ERROR_BAD_PATHNAME
;
3258 InitializeObjectAttributes(&FileObjectAttributes
,
3260 OBJ_CASE_INSENSITIVE
,
3264 RtlInitUnicodeString(&KeyName
,
3267 InitializeObjectAttributes(&KeyObjectAttributes
,
3269 OBJ_CASE_INSENSITIVE
,
3273 Status
= NtLoadKey(&KeyObjectAttributes
,
3274 &FileObjectAttributes
);
3276 RtlFreeHeap(RtlGetProcessHeap(),
3280 if (!NT_SUCCESS(Status
))
3282 ErrorCode
= RtlNtStatusToDosError(Status
);
3287 ClosePredefKey(KeyHandle
);
3293 /************************************************************************
3294 * RegNotifyChangeKeyValue
3299 RegNotifyChangeKeyValue(HKEY hKey
,
3301 DWORD dwNotifyFilter
,
3305 IO_STATUS_BLOCK IoStatusBlock
;
3308 LONG ErrorCode
= ERROR_SUCCESS
;
3310 if (hKey
== HKEY_PERFORMANCE_DATA
)
3312 return ERROR_INVALID_HANDLE
;
3315 if (fAsynchronous
== TRUE
&& hEvent
== NULL
)
3317 return ERROR_INVALID_PARAMETER
;
3320 Status
= MapDefaultKey(&KeyHandle
,
3322 if (!NT_SUCCESS(Status
))
3324 return RtlNtStatusToDosError(Status
);
3327 /* FIXME: Remote key handles must fail */
3329 Status
= NtNotifyChangeKey(KeyHandle
,
3339 if (!NT_SUCCESS(Status
) && Status
!= STATUS_TIMEOUT
)
3341 ErrorCode
= RtlNtStatusToDosError(Status
);
3344 ClosePredefKey(KeyHandle
);
3350 /************************************************************************
3351 * RegOpenCurrentUser
3356 RegOpenCurrentUser(IN REGSAM samDesired
,
3357 OUT PHKEY phkResult
)
3361 Status
= RtlOpenCurrentUser((ACCESS_MASK
)samDesired
,
3362 (PHANDLE
)phkResult
);
3363 if (!NT_SUCCESS(Status
))
3365 /* NOTE - don't set the last error code! just return the error! */
3366 return RtlNtStatusToDosError(Status
);
3369 return ERROR_SUCCESS
;
3373 /************************************************************************
3376 * 20050503 Fireball - imported from WINE
3381 RegOpenKeyA(HKEY hKey
,
3385 TRACE("RegOpenKeyA hKey 0x%x lpSubKey %s phkResult %p\n",
3386 hKey
, lpSubKey
, phkResult
);
3389 return ERROR_INVALID_PARAMETER
;
3391 if (!hKey
&& lpSubKey
&& phkResult
)
3393 return ERROR_INVALID_HANDLE
;
3396 if (!lpSubKey
|| !*lpSubKey
)
3399 return ERROR_SUCCESS
;
3402 return RegOpenKeyExA(hKey
,
3410 /************************************************************************
3415 * 20050503 Fireball - imported from WINE
3420 RegOpenKeyW(HKEY hKey
,
3424 TRACE("RegOpenKeyW hKey 0x%x lpSubKey %S phkResult %p\n",
3425 hKey
, lpSubKey
, phkResult
);
3428 return ERROR_INVALID_PARAMETER
;
3430 if (!hKey
&& lpSubKey
&& phkResult
)
3432 return ERROR_INVALID_HANDLE
;
3435 if (!lpSubKey
|| !*lpSubKey
)
3438 return ERROR_SUCCESS
;
3441 return RegOpenKeyExW(hKey
,
3449 /************************************************************************
3455 RegOpenKeyExA(HKEY hKey
,
3461 OBJECT_ATTRIBUTES ObjectAttributes
;
3462 UNICODE_STRING SubKeyString
;
3465 ULONG Attributes
= OBJ_CASE_INSENSITIVE
;
3466 LONG ErrorCode
= ERROR_SUCCESS
;
3468 TRACE("RegOpenKeyExA hKey 0x%x lpSubKey %s ulOptions 0x%x samDesired 0x%x phkResult %p\n",
3469 hKey
, lpSubKey
, ulOptions
, samDesired
, phkResult
);
3472 return ERROR_INVALID_PARAMETER
;
3475 Status
= MapDefaultKey(&KeyHandle
,
3477 if (!NT_SUCCESS(Status
))
3479 return RtlNtStatusToDosError(Status
);
3482 if (ulOptions
& REG_OPTION_OPEN_LINK
)
3483 Attributes
|= OBJ_OPENLINK
;
3485 RtlCreateUnicodeStringFromAsciiz(&SubKeyString
,
3487 InitializeObjectAttributes(&ObjectAttributes
,
3493 Status
= NtOpenKey((PHANDLE
)phkResult
,
3496 RtlFreeUnicodeString(&SubKeyString
);
3497 if (!NT_SUCCESS(Status
))
3499 ErrorCode
= RtlNtStatusToDosError(Status
);
3502 ClosePredefKey(KeyHandle
);
3508 /************************************************************************
3514 RegOpenKeyExW(HKEY hKey
,
3520 OBJECT_ATTRIBUTES ObjectAttributes
;
3521 UNICODE_STRING SubKeyString
;
3524 ULONG Attributes
= OBJ_CASE_INSENSITIVE
;
3525 LONG ErrorCode
= ERROR_SUCCESS
;
3527 TRACE("RegOpenKeyExW hKey 0x%x lpSubKey %S ulOptions 0x%x samDesired 0x%x phkResult %p\n",
3528 hKey
, lpSubKey
, ulOptions
, samDesired
, phkResult
);
3531 return ERROR_INVALID_PARAMETER
;
3534 Status
= MapDefaultKey(&KeyHandle
, hKey
);
3535 if (!NT_SUCCESS(Status
))
3537 return RtlNtStatusToDosError(Status
);
3540 if (ulOptions
& REG_OPTION_OPEN_LINK
)
3541 Attributes
|= OBJ_OPENLINK
;
3543 if (lpSubKey
!= NULL
)
3544 RtlInitUnicodeString(&SubKeyString
, (LPWSTR
)lpSubKey
);
3546 RtlInitUnicodeString(&SubKeyString
, (LPWSTR
)L
"");
3548 InitializeObjectAttributes(&ObjectAttributes
,
3554 Status
= NtOpenKey((PHANDLE
)phkResult
,
3557 if (!NT_SUCCESS(Status
))
3559 ErrorCode
= RtlNtStatusToDosError(Status
);
3562 ClosePredefKey(KeyHandle
);
3568 /************************************************************************
3569 * RegOpenUserClassesRoot
3574 RegOpenUserClassesRoot(IN HANDLE hToken
,
3576 IN REGSAM samDesired
,
3577 OUT PHKEY phkResult
)
3579 const WCHAR UserClassesKeyPrefix
[] = L
"\\Registry\\User\\";
3580 const WCHAR UserClassesKeySuffix
[] = L
"_Classes";
3581 PTOKEN_USER TokenUserData
;
3582 ULONG RequiredLength
;
3583 UNICODE_STRING UserSidString
, UserClassesKeyRoot
;
3584 OBJECT_ATTRIBUTES ObjectAttributes
;
3587 /* check parameters */
3588 if (hToken
== NULL
|| dwOptions
!= 0 || phkResult
== NULL
)
3590 return ERROR_INVALID_PARAMETER
;
3594 * Get the user sid from the token
3598 /* determine how much memory we need */
3599 Status
= NtQueryInformationToken(hToken
,
3604 if (!NT_SUCCESS(Status
) && (Status
!= STATUS_BUFFER_TOO_SMALL
))
3606 /* NOTE - as opposed to all other registry functions windows does indeed
3607 change the last error code in case the caller supplied a invalid
3608 handle for example! */
3609 return RtlNtStatusToDosError(Status
);
3611 RegInitialize(); /* HACK until delay-loading is implemented */
3612 TokenUserData
= RtlAllocateHeap(ProcessHeap
,
3615 if (TokenUserData
== NULL
)
3617 return ERROR_NOT_ENOUGH_MEMORY
;
3620 /* attempt to read the information */
3621 Status
= NtQueryInformationToken(hToken
,
3626 if (!NT_SUCCESS(Status
))
3628 RtlFreeHeap(ProcessHeap
,
3631 if (Status
== STATUS_BUFFER_TOO_SMALL
)
3633 /* the information appears to have changed?! try again */
3637 /* NOTE - as opposed to all other registry functions windows does indeed
3638 change the last error code in case the caller supplied a invalid
3639 handle for example! */
3640 return RtlNtStatusToDosError(Status
);
3644 * Build the absolute path for the user's registry in the form
3645 * "\Registry\User\<SID>_Classes"
3647 Status
= RtlConvertSidToUnicodeString(&UserSidString
,
3648 TokenUserData
->User
.Sid
,
3651 /* we don't need the user data anymore, free it */
3652 RtlFreeHeap(ProcessHeap
,
3656 if (!NT_SUCCESS(Status
))
3658 return RtlNtStatusToDosError(Status
);
3661 /* allocate enough memory for the entire key string */
3662 UserClassesKeyRoot
.Length
= 0;
3663 UserClassesKeyRoot
.MaximumLength
= UserSidString
.Length
+
3664 sizeof(UserClassesKeyPrefix
) +
3665 sizeof(UserClassesKeySuffix
);
3666 UserClassesKeyRoot
.Buffer
= RtlAllocateHeap(ProcessHeap
,
3668 UserClassesKeyRoot
.MaximumLength
);
3669 if (UserClassesKeyRoot
.Buffer
== NULL
)
3671 RtlFreeUnicodeString(&UserSidString
);
3672 return RtlNtStatusToDosError(Status
);
3675 /* build the string */
3676 RtlAppendUnicodeToString(&UserClassesKeyRoot
,
3677 UserClassesKeyPrefix
);
3678 RtlAppendUnicodeStringToString(&UserClassesKeyRoot
,
3680 RtlAppendUnicodeToString(&UserClassesKeyRoot
,
3681 UserClassesKeySuffix
);
3683 TRACE("RegOpenUserClassesRoot: Absolute path: %wZ\n", &UserClassesKeyRoot
);
3688 InitializeObjectAttributes(&ObjectAttributes
,
3689 &UserClassesKeyRoot
,
3690 OBJ_CASE_INSENSITIVE
,
3694 Status
= NtOpenKey((PHANDLE
)phkResult
,
3698 RtlFreeUnicodeString(&UserSidString
);
3699 RtlFreeUnicodeString(&UserClassesKeyRoot
);
3701 if (!NT_SUCCESS(Status
))
3703 return RtlNtStatusToDosError(Status
);
3706 return ERROR_SUCCESS
;
3710 /************************************************************************
3716 RegQueryInfoKeyA(HKEY hKey
,
3721 LPDWORD lpcbMaxSubKeyLen
,
3722 LPDWORD lpcbMaxClassLen
,
3724 LPDWORD lpcbMaxValueNameLen
,
3725 LPDWORD lpcbMaxValueLen
,
3726 LPDWORD lpcbSecurityDescriptor
,
3727 PFILETIME lpftLastWriteTime
)
3729 WCHAR ClassName
[MAX_PATH
];
3730 UNICODE_STRING UnicodeString
;
3731 ANSI_STRING AnsiString
;
3734 RtlInitUnicodeString(&UnicodeString
,
3736 if (lpClass
!= NULL
)
3738 UnicodeString
.Buffer
= &ClassName
[0];
3739 UnicodeString
.MaximumLength
= sizeof(ClassName
);
3740 AnsiString
.MaximumLength
= *lpcbClass
;
3743 ErrorCode
= RegQueryInfoKeyW(hKey
,
3744 UnicodeString
.Buffer
,
3751 lpcbMaxValueNameLen
,
3753 lpcbSecurityDescriptor
,
3755 if ((ErrorCode
== ERROR_SUCCESS
) && (lpClass
!= NULL
))
3757 AnsiString
.Buffer
= lpClass
;
3758 AnsiString
.Length
= 0;
3759 UnicodeString
.Length
= *lpcbClass
* sizeof(WCHAR
);
3760 RtlUnicodeStringToAnsiString(&AnsiString
,
3763 *lpcbClass
= AnsiString
.Length
;
3764 lpClass
[AnsiString
.Length
] = 0;
3771 /************************************************************************
3777 RegQueryInfoKeyW(HKEY hKey
,
3782 LPDWORD lpcbMaxSubKeyLen
,
3783 LPDWORD lpcbMaxClassLen
,
3785 LPDWORD lpcbMaxValueNameLen
,
3786 LPDWORD lpcbMaxValueLen
,
3787 LPDWORD lpcbSecurityDescriptor
,
3788 PFILETIME lpftLastWriteTime
)
3790 KEY_FULL_INFORMATION FullInfoBuffer
;
3791 PKEY_FULL_INFORMATION FullInfo
;
3793 ULONG ClassLength
= 0;
3797 LONG ErrorCode
= ERROR_SUCCESS
;
3799 if ((lpClass
) && (!lpcbClass
))
3801 return ERROR_INVALID_PARAMETER
;
3804 Status
= MapDefaultKey(&KeyHandle
,
3806 if (!NT_SUCCESS(Status
))
3808 return RtlNtStatusToDosError(Status
);
3811 if (lpClass
!= NULL
)
3815 ClassLength
= min(*lpcbClass
- 1, REG_MAX_NAME_SIZE
) * sizeof(WCHAR
);
3822 FullInfoSize
= sizeof(KEY_FULL_INFORMATION
) + ((ClassLength
+ 3) & ~3);
3823 FullInfo
= RtlAllocateHeap(ProcessHeap
,
3826 if (FullInfo
== NULL
)
3828 ErrorCode
= ERROR_OUTOFMEMORY
;
3832 FullInfo
->ClassLength
= ClassLength
;
3836 FullInfoSize
= sizeof(KEY_FULL_INFORMATION
);
3837 FullInfo
= &FullInfoBuffer
;
3838 FullInfo
->ClassLength
= 0;
3840 FullInfo
->ClassOffset
= FIELD_OFFSET(KEY_FULL_INFORMATION
, Class
);
3842 Status
= NtQueryKey(KeyHandle
,
3847 TRACE("NtQueryKey() returned status 0x%X\n", Status
);
3848 if (!NT_SUCCESS(Status
))
3850 if (lpClass
!= NULL
)
3852 RtlFreeHeap(ProcessHeap
,
3857 ErrorCode
= RtlNtStatusToDosError(Status
);
3861 TRACE("SubKeys %d\n", FullInfo
->SubKeys
);
3862 if (lpcSubKeys
!= NULL
)
3864 *lpcSubKeys
= FullInfo
->SubKeys
;
3867 TRACE("MaxNameLen %lu\n", FullInfo
->MaxNameLen
);
3868 if (lpcbMaxSubKeyLen
!= NULL
)
3870 *lpcbMaxSubKeyLen
= FullInfo
->MaxNameLen
/ sizeof(WCHAR
) + 1;
3873 TRACE("MaxClassLen %lu\n", FullInfo
->MaxClassLen
);
3874 if (lpcbMaxClassLen
!= NULL
)
3876 *lpcbMaxClassLen
= FullInfo
->MaxClassLen
/ sizeof(WCHAR
) + 1;
3879 TRACE("Values %lu\n", FullInfo
->Values
);
3880 if (lpcValues
!= NULL
)
3882 *lpcValues
= FullInfo
->Values
;
3885 TRACE("MaxValueNameLen %lu\n", FullInfo
->MaxValueNameLen
);
3886 if (lpcbMaxValueNameLen
!= NULL
)
3888 *lpcbMaxValueNameLen
= FullInfo
->MaxValueNameLen
/ sizeof(WCHAR
) + 1;
3891 TRACE("MaxValueDataLen %lu\n", FullInfo
->MaxValueDataLen
);
3892 if (lpcbMaxValueLen
!= NULL
)
3894 *lpcbMaxValueLen
= FullInfo
->MaxValueDataLen
;
3897 if (lpcbSecurityDescriptor
!= NULL
)
3899 Status
= NtQuerySecurityObject(KeyHandle
,
3900 OWNER_SECURITY_INFORMATION
|
3901 GROUP_SECURITY_INFORMATION
|
3902 DACL_SECURITY_INFORMATION
,
3905 lpcbSecurityDescriptor
);
3906 if (!NT_SUCCESS(Status
) && Status
!= STATUS_BUFFER_TOO_SMALL
)
3908 if (lpClass
!= NULL
)
3910 RtlFreeHeap(ProcessHeap
,
3915 ErrorCode
= RtlNtStatusToDosError(Status
);
3920 if (lpftLastWriteTime
!= NULL
)
3922 lpftLastWriteTime
->dwLowDateTime
= FullInfo
->LastWriteTime
.u
.LowPart
;
3923 lpftLastWriteTime
->dwHighDateTime
= FullInfo
->LastWriteTime
.u
.HighPart
;
3926 if (lpClass
!= NULL
)
3928 if (FullInfo
->ClassLength
> ClassLength
)
3930 ErrorCode
= ERROR_BUFFER_OVERFLOW
;
3934 RtlCopyMemory(lpClass
,
3936 FullInfo
->ClassLength
);
3937 *lpcbClass
= FullInfo
->ClassLength
/ sizeof(WCHAR
);
3938 lpClass
[*lpcbClass
] = 0;
3941 RtlFreeHeap(ProcessHeap
,
3947 ClosePredefKey(KeyHandle
);
3953 /************************************************************************
3954 * RegQueryMultipleValuesA
3959 RegQueryMultipleValuesA(HKEY hKey
,
3966 DWORD maxBytes
= *ldwTotsize
;
3967 LPSTR bufptr
= (LPSTR
)lpValueBuf
;
3970 if (maxBytes
>= (1024*1024))
3971 return ERROR_TRANSFER_TOO_LONG
;
3975 TRACE("RegQueryMultipleValuesA(%p,%p,%ld,%p,%p=%ld)\n",
3976 hKey
, val_list
, num_vals
, lpValueBuf
, ldwTotsize
, *ldwTotsize
);
3978 for (i
= 0; i
< num_vals
; i
++)
3980 val_list
[i
].ve_valuelen
= 0;
3981 ErrorCode
= RegQueryValueExA(hKey
,
3982 val_list
[i
].ve_valuename
,
3986 &val_list
[i
].ve_valuelen
);
3987 if (ErrorCode
!= ERROR_SUCCESS
)
3992 if (lpValueBuf
!= NULL
&& *ldwTotsize
+ val_list
[i
].ve_valuelen
<= maxBytes
)
3994 ErrorCode
= RegQueryValueExA(hKey
,
3995 val_list
[i
].ve_valuename
,
3997 &val_list
[i
].ve_type
,
3999 &val_list
[i
].ve_valuelen
);
4000 if (ErrorCode
!= ERROR_SUCCESS
)
4005 val_list
[i
].ve_valueptr
= (DWORD_PTR
)bufptr
;
4007 bufptr
+= val_list
[i
].ve_valuelen
;
4010 *ldwTotsize
+= val_list
[i
].ve_valuelen
;
4013 return (lpValueBuf
!= NULL
&& *ldwTotsize
<= maxBytes
) ? ERROR_SUCCESS
: ERROR_MORE_DATA
;
4017 /************************************************************************
4018 * RegQueryMultipleValuesW
4023 RegQueryMultipleValuesW(HKEY hKey
,
4030 DWORD maxBytes
= *ldwTotsize
;
4031 LPSTR bufptr
= (LPSTR
)lpValueBuf
;
4034 if (maxBytes
>= (1024*1024))
4035 return ERROR_TRANSFER_TOO_LONG
;
4039 TRACE("RegQueryMultipleValuesW(%p,%p,%ld,%p,%p=%ld)\n",
4040 hKey
, val_list
, num_vals
, lpValueBuf
, ldwTotsize
, *ldwTotsize
);
4042 for (i
= 0; i
< num_vals
; i
++)
4044 val_list
[i
].ve_valuelen
= 0;
4045 ErrorCode
= RegQueryValueExW(hKey
,
4046 val_list
[i
].ve_valuename
,
4050 &val_list
[i
].ve_valuelen
);
4051 if (ErrorCode
!= ERROR_SUCCESS
)
4056 if (lpValueBuf
!= NULL
&& *ldwTotsize
+ val_list
[i
].ve_valuelen
<= maxBytes
)
4058 ErrorCode
= RegQueryValueExW(hKey
,
4059 val_list
[i
].ve_valuename
,
4061 &val_list
[i
].ve_type
,
4063 &val_list
[i
].ve_valuelen
);
4064 if (ErrorCode
!= ERROR_SUCCESS
)
4069 val_list
[i
].ve_valueptr
= (DWORD_PTR
)bufptr
;
4071 bufptr
+= val_list
[i
].ve_valuelen
;
4074 *ldwTotsize
+= val_list
[i
].ve_valuelen
;
4077 return (lpValueBuf
!= NULL
&& *ldwTotsize
<= maxBytes
) ? ERROR_SUCCESS
: ERROR_MORE_DATA
;
4081 /************************************************************************
4082 * RegQueryReflectionKey
4087 RegQueryReflectionKey(IN HKEY hBase
,
4088 OUT BOOL
* bIsReflectionDisabled
)
4090 FIXME("RegQueryReflectionKey(0x%p, 0x%p) UNIMPLEMENTED!\n",
4091 hBase
, bIsReflectionDisabled
);
4092 return ERROR_CALL_NOT_IMPLEMENTED
;
4096 /******************************************************************************
4097 * RegQueryValueExA [ADVAPI32.@]
4099 * Get the type and contents of a specified value under with a key.
4102 * hkey [I] Handle of the key to query
4103 * name [I] Name of value under hkey to query
4104 * reserved [I] Reserved - must be NULL
4105 * type [O] Destination for the value type, or NULL if not required
4106 * data [O] Destination for the values contents, or NULL if not required
4107 * count [I/O] Size of data, updated with the number of bytes returned
4110 * Success: ERROR_SUCCESS. *count is updated with the number of bytes copied to data.
4111 * Failure: ERROR_INVALID_HANDLE, if hkey is invalid.
4112 * ERROR_INVALID_PARAMETER, if any other parameter is invalid.
4113 * ERROR_MORE_DATA, if on input *count is too small to hold the contents.
4116 * MSDN states that if data is too small it is partially filled. In reality
4117 * it remains untouched.
4121 RegQueryValueExA(HKEY hkeyorg
,
4131 UNICODE_STRING nameW
;
4132 DWORD total_size
, datalen
= 0;
4133 char buffer
[256], *buf_ptr
= buffer
;
4134 KEY_VALUE_PARTIAL_INFORMATION
*info
= (KEY_VALUE_PARTIAL_INFORMATION
*)buffer
;
4135 static const int info_size
= offsetof( KEY_VALUE_PARTIAL_INFORMATION
, Data
);
4137 TRACE("(%p,%s,%p,%p,%p,%p=%d)\n",
4138 hkeyorg
, debugstr_a(name
), reserved
, type
, data
, count
, count
? *count
: 0 );
4140 if ((data
&& !count
) || reserved
) return ERROR_INVALID_PARAMETER
;
4141 status
= MapDefaultKey(&hkey
, hkeyorg
);
4142 if (!NT_SUCCESS(status
))
4144 return RtlNtStatusToDosError(status
);
4147 if (count
) datalen
= *count
;
4148 if (!data
&& count
) *count
= 0;
4150 RtlInitAnsiString( &nameA
, name
);
4151 if ((status
= RtlAnsiStringToUnicodeString( &nameW
, &nameA
, TRUE
)))
4153 ClosePredefKey(hkey
);
4154 return RtlNtStatusToDosError(status
);
4157 status
= NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
,
4158 buffer
, sizeof(buffer
), &total_size
);
4159 if (status
&& status
!= STATUS_BUFFER_OVERFLOW
) goto done
;
4161 /* we need to fetch the contents for a string type even if not requested,
4162 * because we need to compute the length of the ASCII string. */
4163 if (data
|| is_string(info
->Type
))
4165 /* retry with a dynamically allocated buffer */
4166 while (status
== STATUS_BUFFER_OVERFLOW
)
4168 if (buf_ptr
!= buffer
) HeapFree( GetProcessHeap(), 0, buf_ptr
);
4169 if (!(buf_ptr
= HeapAlloc( GetProcessHeap(), 0, total_size
)))
4171 status
= STATUS_NO_MEMORY
;
4174 info
= (KEY_VALUE_PARTIAL_INFORMATION
*)buf_ptr
;
4175 status
= NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
,
4176 buf_ptr
, total_size
, &total_size
);
4179 if (status
) goto done
;
4181 if (is_string(info
->Type
))
4185 RtlUnicodeToMultiByteSize( &len
, (WCHAR
*)(buf_ptr
+ info_size
),
4186 total_size
- info_size
);
4189 if (len
> datalen
) status
= STATUS_BUFFER_OVERFLOW
;
4192 RtlUnicodeToMultiByteN( (char*)data
, len
, NULL
, (WCHAR
*)(buf_ptr
+ info_size
),
4193 total_size
- info_size
);
4194 /* if the type is REG_SZ and data is not 0-terminated
4195 * and there is enough space in the buffer NT appends a \0 */
4196 if (len
< datalen
&& data
[len
-1]) data
[len
] = 0;
4199 total_size
= len
+ info_size
;
4203 if (total_size
- info_size
> datalen
) status
= STATUS_BUFFER_OVERFLOW
;
4204 else memcpy( data
, buf_ptr
+ info_size
, total_size
- info_size
);
4207 else status
= STATUS_SUCCESS
;
4209 if (type
) *type
= info
->Type
;
4210 if (count
) *count
= total_size
- info_size
;
4213 if (buf_ptr
!= buffer
) HeapFree( GetProcessHeap(), 0, buf_ptr
);
4214 RtlFreeUnicodeString( &nameW
);
4215 ClosePredefKey(hkey
);
4216 return RtlNtStatusToDosError(status
);
4220 /************************************************************************
4227 RegQueryValueExW(HKEY hkeyorg
,
4236 UNICODE_STRING name_str
;
4238 char buffer
[256], *buf_ptr
= buffer
;
4239 KEY_VALUE_PARTIAL_INFORMATION
*info
= (KEY_VALUE_PARTIAL_INFORMATION
*)buffer
;
4240 static const int info_size
= offsetof( KEY_VALUE_PARTIAL_INFORMATION
, Data
);
4242 TRACE("(%p,%s,%p,%p,%p,%p=%d)\n",
4243 hkeyorg
, debugstr_w(name
), reserved
, type
, data
, count
,
4244 (count
&& data
) ? *count
: 0 );
4246 if ((data
&& !count
) || reserved
) return ERROR_INVALID_PARAMETER
;
4248 status
= MapDefaultKey(&hkey
, hkeyorg
);
4249 if (!NT_SUCCESS(status
))
4251 return RtlNtStatusToDosError(status
);
4254 RtlInitUnicodeString( &name_str
, name
);
4256 if (data
) total_size
= min( sizeof(buffer
), *count
+ info_size
);
4259 total_size
= info_size
;
4260 if (count
) *count
= 0;
4263 /* this matches Win9x behaviour - NT sets *type to a random value */
4264 if (type
) *type
= REG_NONE
;
4266 status
= NtQueryValueKey( hkey
, &name_str
, KeyValuePartialInformation
,
4267 buffer
, total_size
, &total_size
);
4268 if (!NT_SUCCESS(status
) && status
!= STATUS_BUFFER_OVERFLOW
) goto done
;
4272 /* retry with a dynamically allocated buffer */
4273 while (status
== STATUS_BUFFER_OVERFLOW
&& total_size
- info_size
<= *count
)
4275 if (buf_ptr
!= buffer
) HeapFree( GetProcessHeap(), 0, buf_ptr
);
4276 if (!(buf_ptr
= HeapAlloc( GetProcessHeap(), 0, total_size
)))
4278 ClosePredefKey(hkey
);
4279 return ERROR_NOT_ENOUGH_MEMORY
;
4281 info
= (KEY_VALUE_PARTIAL_INFORMATION
*)buf_ptr
;
4282 status
= NtQueryValueKey( hkey
, &name_str
, KeyValuePartialInformation
,
4283 buf_ptr
, total_size
, &total_size
);
4286 if (NT_SUCCESS(status
))
4288 memcpy( data
, buf_ptr
+ info_size
, total_size
- info_size
);
4289 /* if the type is REG_SZ and data is not 0-terminated
4290 * and there is enough space in the buffer NT appends a \0 */
4291 if (is_string(info
->Type
) && total_size
- info_size
<= *count
-sizeof(WCHAR
))
4293 WCHAR
*ptr
= (WCHAR
*)(data
+ total_size
- info_size
);
4294 if (ptr
> (WCHAR
*)data
&& ptr
[-1]) *ptr
= 0;
4297 else if (status
!= STATUS_BUFFER_OVERFLOW
) goto done
;
4299 else status
= STATUS_SUCCESS
;
4301 if (type
) *type
= info
->Type
;
4302 if (count
) *count
= total_size
- info_size
;
4305 if (buf_ptr
!= buffer
) HeapFree( GetProcessHeap(), 0, buf_ptr
);
4306 ClosePredefKey(hkey
);
4307 return RtlNtStatusToDosError(status
);
4311 /************************************************************************
4316 LSTATUS WINAPI
RegQueryValueA( HKEY hkey
, LPCSTR name
, LPSTR data
, LPLONG count
)
4321 TRACE("(%p,%s,%p,%d)\n", hkey
, debugstr_a(name
), data
, count
? *count
: 0 );
4323 if (name
&& name
[0])
4325 if ((ret
= RegOpenKeyA( hkey
, name
, &subkey
)) != ERROR_SUCCESS
) return ret
;
4327 ret
= RegQueryValueExA( subkey
, NULL
, NULL
, NULL
, (LPBYTE
)data
, (LPDWORD
)count
);
4328 if (subkey
!= hkey
) RegCloseKey( subkey
);
4329 if (ret
== ERROR_FILE_NOT_FOUND
)
4331 /* return empty string if default value not found */
4332 if (data
) *data
= 0;
4333 if (count
) *count
= 1;
4334 ret
= ERROR_SUCCESS
;
4340 /************************************************************************
4345 LSTATUS WINAPI
RegQueryValueW( HKEY hkey
, LPCWSTR name
, LPWSTR data
, LPLONG count
)
4350 TRACE("(%p,%s,%p,%d)\n", hkey
, debugstr_w(name
), data
, count
? *count
: 0 );
4353 return ERROR_INVALID_HANDLE
;
4355 if (name
&& name
[0])
4357 ret
= RegOpenKeyW( hkey
, name
, &subkey
);
4358 if (ret
!= ERROR_SUCCESS
)
4364 ret
= RegQueryValueExW( subkey
, NULL
, NULL
, NULL
, (LPBYTE
)data
, (LPDWORD
)count
);
4368 RegCloseKey( subkey
);
4371 if (ret
== ERROR_FILE_NOT_FOUND
)
4373 /* return empty string if default value not found */
4377 *count
= sizeof(WCHAR
);
4378 ret
= ERROR_SUCCESS
;
4384 /************************************************************************
4390 RegReplaceKeyA(HKEY hKey
,
4395 UNICODE_STRING SubKey
;
4396 UNICODE_STRING NewFile
;
4397 UNICODE_STRING OldFile
;
4400 RtlCreateUnicodeStringFromAsciiz(&SubKey
,
4402 RtlCreateUnicodeStringFromAsciiz(&OldFile
,
4404 RtlCreateUnicodeStringFromAsciiz(&NewFile
,
4407 ErrorCode
= RegReplaceKeyW(hKey
,
4412 RtlFreeUnicodeString(&OldFile
);
4413 RtlFreeUnicodeString(&NewFile
);
4414 RtlFreeUnicodeString(&SubKey
);
4420 /************************************************************************
4426 RegReplaceKeyW(HKEY hKey
,
4431 OBJECT_ATTRIBUTES KeyObjectAttributes
;
4432 OBJECT_ATTRIBUTES NewObjectAttributes
;
4433 OBJECT_ATTRIBUTES OldObjectAttributes
;
4434 UNICODE_STRING SubKeyName
;
4435 UNICODE_STRING NewFileName
;
4436 UNICODE_STRING OldFileName
;
4437 BOOLEAN CloseRealKey
;
4438 HANDLE RealKeyHandle
;
4441 LONG ErrorCode
= ERROR_SUCCESS
;
4443 if (hKey
== HKEY_PERFORMANCE_DATA
)
4445 return ERROR_INVALID_HANDLE
;
4448 Status
= MapDefaultKey(&KeyHandle
,
4450 if (!NT_SUCCESS(Status
))
4452 return RtlNtStatusToDosError(Status
);
4455 /* Open the real key */
4456 if (lpSubKey
!= NULL
&& *lpSubKey
!= (WCHAR
)0)
4458 RtlInitUnicodeString(&SubKeyName
,
4460 InitializeObjectAttributes(&KeyObjectAttributes
,
4462 OBJ_CASE_INSENSITIVE
,
4465 Status
= NtOpenKey(&RealKeyHandle
,
4467 &KeyObjectAttributes
);
4468 if (!NT_SUCCESS(Status
))
4470 ErrorCode
= RtlNtStatusToDosError(Status
);
4474 CloseRealKey
= TRUE
;
4478 RealKeyHandle
= KeyHandle
;
4479 CloseRealKey
= FALSE
;
4482 /* Convert new file name */
4483 if (!RtlDosPathNameToNtPathName_U(lpNewFile
,
4490 NtClose(RealKeyHandle
);
4493 ErrorCode
= ERROR_INVALID_PARAMETER
;
4497 InitializeObjectAttributes(&NewObjectAttributes
,
4499 OBJ_CASE_INSENSITIVE
,
4503 /* Convert old file name */
4504 if (!RtlDosPathNameToNtPathName_U(lpOldFile
,
4509 RtlFreeHeap(RtlGetProcessHeap (),
4511 NewFileName
.Buffer
);
4514 NtClose(RealKeyHandle
);
4517 ErrorCode
= ERROR_INVALID_PARAMETER
;
4521 InitializeObjectAttributes(&OldObjectAttributes
,
4523 OBJ_CASE_INSENSITIVE
,
4527 Status
= NtReplaceKey(&NewObjectAttributes
,
4529 &OldObjectAttributes
);
4531 RtlFreeHeap(RtlGetProcessHeap(),
4533 OldFileName
.Buffer
);
4534 RtlFreeHeap(RtlGetProcessHeap(),
4536 NewFileName
.Buffer
);
4540 NtClose(RealKeyHandle
);
4543 if (!NT_SUCCESS(Status
))
4545 return RtlNtStatusToDosError(Status
);
4549 ClosePredefKey(KeyHandle
);
4555 /************************************************************************
4561 RegRestoreKeyA(HKEY hKey
,
4565 UNICODE_STRING FileName
;
4568 RtlCreateUnicodeStringFromAsciiz(&FileName
,
4571 ErrorCode
= RegRestoreKeyW(hKey
,
4575 RtlFreeUnicodeString(&FileName
);
4581 /************************************************************************
4587 RegRestoreKeyW(HKEY hKey
,
4591 OBJECT_ATTRIBUTES ObjectAttributes
;
4592 IO_STATUS_BLOCK IoStatusBlock
;
4593 UNICODE_STRING FileName
;
4598 if (hKey
== HKEY_PERFORMANCE_DATA
)
4600 return ERROR_INVALID_HANDLE
;
4603 Status
= MapDefaultKey(&KeyHandle
,
4605 if (!NT_SUCCESS(Status
))
4607 return RtlNtStatusToDosError(Status
);
4610 if (!RtlDosPathNameToNtPathName_U(lpFile
,
4615 Status
= STATUS_INVALID_PARAMETER
;
4619 InitializeObjectAttributes(&ObjectAttributes
,
4621 OBJ_CASE_INSENSITIVE
,
4625 Status
= NtOpenFile(&FileHandle
,
4630 FILE_SYNCHRONOUS_IO_NONALERT
);
4631 RtlFreeHeap(RtlGetProcessHeap(),
4634 if (!NT_SUCCESS(Status
))
4639 Status
= NtRestoreKey(KeyHandle
,
4642 NtClose (FileHandle
);
4645 ClosePredefKey(KeyHandle
);
4647 if (!NT_SUCCESS(Status
))
4649 return RtlNtStatusToDosError(Status
);
4652 return ERROR_SUCCESS
;
4656 /************************************************************************
4662 RegSaveKeyA(HKEY hKey
,
4664 LPSECURITY_ATTRIBUTES lpSecurityAttributes
)
4666 UNICODE_STRING FileName
;
4669 RtlCreateUnicodeStringFromAsciiz(&FileName
,
4671 ErrorCode
= RegSaveKeyW(hKey
,
4673 lpSecurityAttributes
);
4674 RtlFreeUnicodeString(&FileName
);
4680 /************************************************************************
4686 RegSaveKeyW(HKEY hKey
,
4688 LPSECURITY_ATTRIBUTES lpSecurityAttributes
)
4690 PSECURITY_DESCRIPTOR SecurityDescriptor
= NULL
;
4691 OBJECT_ATTRIBUTES ObjectAttributes
;
4692 UNICODE_STRING FileName
;
4693 IO_STATUS_BLOCK IoStatusBlock
;
4698 Status
= MapDefaultKey(&KeyHandle
,
4700 if (!NT_SUCCESS(Status
))
4702 return RtlNtStatusToDosError(Status
);
4705 if (!RtlDosPathNameToNtPathName_U(lpFile
,
4710 Status
= STATUS_INVALID_PARAMETER
;
4714 if (lpSecurityAttributes
!= NULL
)
4716 SecurityDescriptor
= lpSecurityAttributes
->lpSecurityDescriptor
;
4719 InitializeObjectAttributes(&ObjectAttributes
,
4721 OBJ_CASE_INSENSITIVE
,
4723 SecurityDescriptor
);
4724 Status
= NtCreateFile(&FileHandle
,
4725 GENERIC_WRITE
| SYNCHRONIZE
,
4729 FILE_ATTRIBUTE_NORMAL
,
4732 FILE_OPEN_FOR_BACKUP_INTENT
| FILE_SYNCHRONOUS_IO_NONALERT
,
4735 RtlFreeHeap(RtlGetProcessHeap(),
4738 if (!NT_SUCCESS(Status
))
4743 Status
= NtSaveKey(KeyHandle
,
4745 NtClose (FileHandle
);
4748 ClosePredefKey(KeyHandle
);
4750 if (!NT_SUCCESS(Status
))
4752 return RtlNtStatusToDosError(Status
);
4755 return ERROR_SUCCESS
;
4759 /************************************************************************
4766 RegSaveKeyExA(HKEY hKey
,
4768 LPSECURITY_ATTRIBUTES lpSecurityAttributes
,
4771 UNICODE_STRING FileName
;
4774 RtlCreateUnicodeStringFromAsciiz(&FileName
,
4776 ErrorCode
= RegSaveKeyExW(hKey
,
4778 lpSecurityAttributes
,
4780 RtlFreeUnicodeString(&FileName
);
4786 /************************************************************************
4793 RegSaveKeyExW(HKEY hKey
,
4795 LPSECURITY_ATTRIBUTES lpSecurityAttributes
,
4800 case REG_STANDARD_FORMAT
:
4801 case REG_LATEST_FORMAT
:
4802 case REG_NO_COMPRESSION
:
4805 return ERROR_INVALID_PARAMETER
;
4808 FIXME("RegSaveKeyExW(): Flags ignored!\n");
4810 return RegSaveKeyW(hKey
,
4812 lpSecurityAttributes
);
4816 /************************************************************************
4822 RegSetKeySecurity(HKEY hKey
,
4823 SECURITY_INFORMATION SecurityInformation
,
4824 PSECURITY_DESCRIPTOR pSecurityDescriptor
)
4829 if (hKey
== HKEY_PERFORMANCE_DATA
)
4831 return ERROR_INVALID_HANDLE
;
4834 Status
= MapDefaultKey(&KeyHandle
,
4836 if (!NT_SUCCESS(Status
))
4838 return RtlNtStatusToDosError(Status
);
4841 Status
= NtSetSecurityObject(KeyHandle
,
4842 SecurityInformation
,
4843 pSecurityDescriptor
);
4845 ClosePredefKey(KeyHandle
);
4847 if (!NT_SUCCESS(Status
))
4849 return RtlNtStatusToDosError(Status
);
4852 return ERROR_SUCCESS
;
4856 /************************************************************************
4862 RegSetValueExA(HKEY hKey
,
4869 UNICODE_STRING ValueName
;
4871 ANSI_STRING AnsiString
;
4872 UNICODE_STRING Data
;
4878 /* Convert SubKey name to Unicode */
4879 if (lpValueName
!= NULL
&& lpValueName
[0] != '\0')
4882 bConverted
= RtlCreateUnicodeStringFromAsciiz(&ValueName
,
4885 return ERROR_NOT_ENOUGH_MEMORY
;
4889 ValueName
.Buffer
= NULL
;
4892 pValueName
= (LPWSTR
)ValueName
.Buffer
;
4895 if (is_string(dwType
) && (cbData
!= 0))
4897 /* Convert ANSI string Data to Unicode */
4898 /* If last character NOT zero then increment length */
4899 LONG bNoNulledStr
= ((lpData
[cbData
-1] != '\0') ? 1 : 0);
4900 AnsiString
.Buffer
= (PSTR
)lpData
;
4901 AnsiString
.Length
= cbData
+ bNoNulledStr
;
4902 AnsiString
.MaximumLength
= cbData
+ bNoNulledStr
;
4903 Status
= RtlAnsiStringToUnicodeString(&Data
,
4907 if (!NT_SUCCESS(Status
))
4909 if (pValueName
!= NULL
)
4910 RtlFreeUnicodeString(&ValueName
);
4912 return RtlNtStatusToDosError(Status
);
4914 pData
= (LPBYTE
)Data
.Buffer
;
4915 DataSize
= cbData
* sizeof(WCHAR
);
4920 pData
= (LPBYTE
)lpData
;
4924 ErrorCode
= RegSetValueExW(hKey
,
4931 if (pValueName
!= NULL
)
4932 RtlFreeUnicodeString(&ValueName
);
4934 if (Data
.Buffer
!= NULL
)
4935 RtlFreeUnicodeString(&Data
);
4941 /************************************************************************
4947 RegSetValueExW(HKEY hKey
,
4948 LPCWSTR lpValueName
,
4954 UNICODE_STRING ValueName
;
4958 if (is_string(dwType
) && (cbData
!= 0))
4960 PWSTR pwsData
= (PWSTR
)lpData
;
4964 if((pwsData
[cbData
/ sizeof(WCHAR
) - 1] != L
'\0') &&
4965 (pwsData
[cbData
/ sizeof(WCHAR
)] == L
'\0'))
4967 /* Increment length if last character is not zero and next is zero */
4968 cbData
+= sizeof(WCHAR
);
4971 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
4973 _SEH2_YIELD(return ERROR_NOACCESS
);
4978 Status
= MapDefaultKey(&KeyHandle
,
4980 if (!NT_SUCCESS(Status
))
4982 return RtlNtStatusToDosError(Status
);
4985 RtlInitUnicodeString(&ValueName
, lpValueName
);
4987 Status
= NtSetValueKey(KeyHandle
,
4994 ClosePredefKey(KeyHandle
);
4996 if (!NT_SUCCESS(Status
))
4998 return RtlNtStatusToDosError(Status
);
5001 return ERROR_SUCCESS
;
5005 /************************************************************************
5011 RegSetValueA(HKEY hKeyOriginal
,
5022 TRACE("(%p,%s,%d,%s,%d)\n", hKeyOriginal
, debugstr_a(lpSubKey
), dwType
, debugstr_a(lpData
), cbData
);
5024 if (dwType
!= REG_SZ
|| !lpData
) return ERROR_INVALID_PARAMETER
;
5026 Status
= MapDefaultKey(&hKey
, hKeyOriginal
);
5027 if (!NT_SUCCESS(Status
))
5029 return RtlNtStatusToDosError (Status
);
5033 if (lpSubKey
&& lpSubKey
[0]) /* need to create the subkey */
5035 ret
= RegCreateKeyA(hKey
, lpSubKey
, &subkey
);
5036 if (ret
!= ERROR_SUCCESS
)
5040 ret
= RegSetValueExA( subkey
, NULL
, 0, REG_SZ
, (const BYTE
*)lpData
, strlen(lpData
)+1 );
5042 RegCloseKey(subkey
);
5045 ClosePredefKey(hKey
);
5051 /************************************************************************
5057 RegSetValueW(HKEY hKeyOriginal
,
5068 TRACE("(%p,%s,%d,%s,%d)\n", hKeyOriginal
, debugstr_w(lpSubKey
), dwType
, debugstr_w(lpData
), cbData
);
5070 if (dwType
!= REG_SZ
|| !lpData
)
5071 return ERROR_INVALID_PARAMETER
;
5073 Status
= MapDefaultKey(&hKey
,
5075 if (!NT_SUCCESS(Status
))
5077 return RtlNtStatusToDosError(Status
);
5081 if (lpSubKey
&& lpSubKey
[0]) /* need to create the subkey */
5083 ret
= RegCreateKeyW(hKey
, lpSubKey
, &subkey
);
5084 if (ret
!= ERROR_SUCCESS
)
5088 ret
= RegSetValueExW( subkey
, NULL
, 0, REG_SZ
, (const BYTE
*)lpData
,
5089 (wcslen( lpData
) + 1) * sizeof(WCHAR
) );
5091 RegCloseKey(subkey
);
5094 ClosePredefKey(hKey
);
5100 /************************************************************************
5106 RegUnLoadKeyA(HKEY hKey
,
5109 UNICODE_STRING KeyName
;
5112 RtlCreateUnicodeStringFromAsciiz(&KeyName
,
5115 ErrorCode
= RegUnLoadKeyW(hKey
,
5118 RtlFreeUnicodeString (&KeyName
);
5124 /************************************************************************
5130 RegUnLoadKeyW(HKEY hKey
,
5133 OBJECT_ATTRIBUTES ObjectAttributes
;
5134 UNICODE_STRING KeyName
;
5138 if (hKey
== HKEY_PERFORMANCE_DATA
)
5140 return ERROR_INVALID_HANDLE
;
5143 Status
= MapDefaultKey(&KeyHandle
, hKey
);
5144 if (!NT_SUCCESS(Status
))
5146 return RtlNtStatusToDosError(Status
);
5149 RtlInitUnicodeString(&KeyName
,
5152 InitializeObjectAttributes(&ObjectAttributes
,
5154 OBJ_CASE_INSENSITIVE
,
5158 Status
= NtUnloadKey(&ObjectAttributes
);
5160 ClosePredefKey(KeyHandle
);
5162 if (!NT_SUCCESS(Status
))
5164 return RtlNtStatusToDosError(Status
);
5167 return ERROR_SUCCESS
;
5171 /******************************************************************************
5172 * load_string [Internal]
5174 * This is basically a copy of user32/resource.c's LoadStringW. Necessary to
5175 * avoid importing user32, which is higher level than advapi32. Helper for
5178 static int load_string(HINSTANCE hModule
, UINT resId
, LPWSTR pwszBuffer
, INT cMaxChars
)
5185 /* Negative values have to be inverted. */
5186 if (HIWORD(resId
) == 0xffff)
5187 resId
= (UINT
)(-((INT
)resId
));
5189 /* Load the resource into memory and get a pointer to it. */
5190 hResource
= FindResourceW(hModule
, MAKEINTRESOURCEW(LOWORD(resId
>> 4) + 1), (LPWSTR
)RT_STRING
);
5191 if (!hResource
) return 0;
5192 hMemory
= LoadResource(hModule
, hResource
);
5193 if (!hMemory
) return 0;
5194 pString
= LockResource(hMemory
);
5196 /* Strings are length-prefixed. Lowest nibble of resId is an index. */
5197 idxString
= resId
& 0xf;
5198 while (idxString
--) pString
+= *pString
+ 1;
5200 /* If no buffer is given, return length of the string. */
5201 if (!pwszBuffer
) return *pString
;
5203 /* Else copy over the string, respecting the buffer size. */
5204 cMaxChars
= (*pString
< cMaxChars
) ? *pString
: (cMaxChars
- 1);
5207 memcpy(pwszBuffer
, pString
+1, cMaxChars
* sizeof(WCHAR
));
5208 pwszBuffer
[cMaxChars
] = L
'\0';
5215 /************************************************************************
5221 RegLoadMUIStringW(IN HKEY hKey
,
5222 IN LPCWSTR pszValue OPTIONAL
,
5223 OUT LPWSTR pszOutBuf
,
5225 OUT LPDWORD pcbData OPTIONAL
,
5227 IN LPCWSTR pszDirectory OPTIONAL
)
5229 DWORD dwValueType
, cbData
;
5230 LPWSTR pwszTempBuffer
= NULL
, pwszExpandedBuffer
= NULL
;
5233 /* Parameter sanity checks. */
5234 if (!hKey
|| !pszOutBuf
)
5235 return ERROR_INVALID_PARAMETER
;
5237 if (pszDirectory
&& *pszDirectory
)
5239 FIXME("BaseDir parameter not yet supported!\n");
5240 return ERROR_INVALID_PARAMETER
;
5243 /* Check for value existence and correctness of it's type, allocate a buffer and load it. */
5244 result
= RegQueryValueExW(hKey
, pszValue
, NULL
, &dwValueType
, NULL
, &cbData
);
5245 if (result
!= ERROR_SUCCESS
) goto cleanup
;
5246 if (!(dwValueType
== REG_SZ
|| dwValueType
== REG_EXPAND_SZ
) || !cbData
)
5248 result
= ERROR_FILE_NOT_FOUND
;
5251 pwszTempBuffer
= HeapAlloc(GetProcessHeap(), 0, cbData
);
5252 if (!pwszTempBuffer
)
5254 result
= ERROR_NOT_ENOUGH_MEMORY
;
5257 result
= RegQueryValueExW(hKey
, pszValue
, NULL
, &dwValueType
, (LPBYTE
)pwszTempBuffer
, &cbData
);
5258 if (result
!= ERROR_SUCCESS
) goto cleanup
;
5260 /* Expand environment variables, if appropriate, or copy the original string over. */
5261 if (dwValueType
== REG_EXPAND_SZ
)
5263 cbData
= ExpandEnvironmentStringsW(pwszTempBuffer
, NULL
, 0) * sizeof(WCHAR
);
5264 if (!cbData
) goto cleanup
;
5265 pwszExpandedBuffer
= HeapAlloc(GetProcessHeap(), 0, cbData
);
5266 if (!pwszExpandedBuffer
)
5268 result
= ERROR_NOT_ENOUGH_MEMORY
;
5271 ExpandEnvironmentStringsW(pwszTempBuffer
, pwszExpandedBuffer
, cbData
);
5275 pwszExpandedBuffer
= HeapAlloc(GetProcessHeap(), 0, cbData
);
5276 memcpy(pwszExpandedBuffer
, pwszTempBuffer
, cbData
);
5279 /* If the value references a resource based string, parse the value and load the string.
5280 * Else just copy over the original value. */
5281 result
= ERROR_SUCCESS
;
5282 if (*pwszExpandedBuffer
!= L
'@') /* '@' is the prefix for resource based string entries. */
5284 lstrcpynW(pszOutBuf
, pwszExpandedBuffer
, cbOutBuf
/ sizeof(WCHAR
));
5288 WCHAR
*pComma
= wcsrchr(pwszExpandedBuffer
, L
',');
5292 /* Format of the expanded value is 'path_to_dll,-resId' */
5293 if (!pComma
|| pComma
[1] != L
'-')
5295 result
= ERROR_BADKEY
;
5299 uiStringId
= _wtoi(pComma
+2);
5302 hModule
= LoadLibraryExW(pwszExpandedBuffer
+ 1, NULL
, LOAD_LIBRARY_AS_DATAFILE
);
5303 if (!hModule
|| !load_string(hModule
, uiStringId
, pszOutBuf
, cbOutBuf
/ sizeof(WCHAR
)))
5304 result
= ERROR_BADKEY
;
5305 FreeLibrary(hModule
);
5309 HeapFree(GetProcessHeap(), 0, pwszTempBuffer
);
5310 HeapFree(GetProcessHeap(), 0, pwszExpandedBuffer
);
5315 /************************************************************************
5321 RegLoadMUIStringA(IN HKEY hKey
,
5322 IN LPCSTR pszValue OPTIONAL
,
5323 OUT LPSTR pszOutBuf
,
5325 OUT LPDWORD pcbData OPTIONAL
,
5327 IN LPCSTR pszDirectory OPTIONAL
)
5329 UNICODE_STRING valueW
, baseDirW
;
5331 DWORD cbData
= cbOutBuf
* sizeof(WCHAR
);
5334 valueW
.Buffer
= baseDirW
.Buffer
= pwszBuffer
= NULL
;
5335 if (!RtlCreateUnicodeStringFromAsciiz(&valueW
, pszValue
) ||
5336 !RtlCreateUnicodeStringFromAsciiz(&baseDirW
, pszDirectory
) ||
5337 !(pwszBuffer
= HeapAlloc(GetProcessHeap(), 0, cbData
)))
5339 result
= ERROR_NOT_ENOUGH_MEMORY
;
5343 result
= RegLoadMUIStringW(hKey
, valueW
.Buffer
, pwszBuffer
, cbData
, NULL
, Flags
,
5346 if (result
== ERROR_SUCCESS
)
5348 cbData
= WideCharToMultiByte(CP_ACP
, 0, pwszBuffer
, -1, pszOutBuf
, cbOutBuf
, NULL
, NULL
);
5354 HeapFree(GetProcessHeap(), 0, pwszBuffer
);
5355 RtlFreeUnicodeString(&baseDirW
);
5356 RtlFreeUnicodeString(&valueW
);