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 *****************************************************************/
17 #include <wine/debug.h>
19 WINE_DEFAULT_DEBUG_CHANNEL(reg
);
21 /* DEFINES ******************************************************************/
23 #define MAX_DEFAULT_HANDLES 6
24 #define REG_MAX_NAME_SIZE 256
25 #define REG_MAX_DATA_SIZE 2048
27 /* GLOBALS ******************************************************************/
29 static RTL_CRITICAL_SECTION HandleTableCS
;
30 static HANDLE DefaultHandleTable
[MAX_DEFAULT_HANDLES
];
31 static HANDLE ProcessHeap
;
32 static BOOLEAN DefaultHandlesDisabled
= FALSE
;
33 static BOOLEAN DefaultHandleHKUDisabled
= FALSE
;
35 /* PROTOTYPES ***************************************************************/
37 static NTSTATUS
MapDefaultKey (PHANDLE ParentKey
, HKEY Key
);
38 static VOID
CloseDefaultKeys(VOID
);
39 #define ClosePredefKey(Handle) \
40 if ((ULONG_PTR)Handle & 0x1) { \
43 #define IsPredefKey(HKey) \
44 (((ULONG)(HKey) & 0xF0000000) == 0x80000000)
45 #define GetPredefKeyIndex(HKey) \
46 ((ULONG)(HKey) & 0x0FFFFFFF)
48 static NTSTATUS
OpenClassesRootKey(PHANDLE KeyHandle
);
49 static NTSTATUS
OpenLocalMachineKey (PHANDLE KeyHandle
);
50 static NTSTATUS
OpenUsersKey (PHANDLE KeyHandle
);
51 static NTSTATUS
OpenCurrentConfigKey(PHANDLE KeyHandle
);
54 /* FUNCTIONS ****************************************************************/
55 /* check if value type needs string conversion (Ansi<->Unicode) */
56 __inline
static int is_string( DWORD type
)
58 return (type
== REG_SZ
) || (type
== REG_EXPAND_SZ
) || (type
== REG_MULTI_SZ
);
61 /************************************************************************
62 * RegInitDefaultHandles
67 TRACE("RegInitialize()\n");
69 ProcessHeap
= RtlGetProcessHeap();
70 RtlZeroMemory(DefaultHandleTable
,
71 MAX_DEFAULT_HANDLES
* sizeof(HANDLE
));
72 RtlInitializeCriticalSection(&HandleTableCS
);
78 /************************************************************************
84 TRACE("RegCleanup()\n");
87 RtlDeleteCriticalSection(&HandleTableCS
);
94 OpenPredefinedKey(IN ULONG Index
,
101 case 0: /* HKEY_CLASSES_ROOT */
102 Status
= OpenClassesRootKey (Handle
);
105 case 1: /* HKEY_CURRENT_USER */
106 Status
= RtlOpenCurrentUser (MAXIMUM_ALLOWED
,
110 case 2: /* HKEY_LOCAL_MACHINE */
111 Status
= OpenLocalMachineKey (Handle
);
114 case 3: /* HKEY_USERS */
115 Status
= OpenUsersKey (Handle
);
118 case 4: /* HKEY_PERFORMANCE_DATA */
119 Status
= OpenPerformanceDataKey (Handle
);
123 case 5: /* HKEY_CURRENT_CONFIG */
124 Status
= OpenCurrentConfigKey (Handle
);
127 case 6: /* HKEY_DYN_DATA */
128 Status
= STATUS_NOT_IMPLEMENTED
;
132 WARN("MapDefaultHandle() no handle creator\n");
133 Status
= STATUS_INVALID_PARAMETER
;
142 MapDefaultKey(OUT PHANDLE RealKey
,
147 BOOLEAN DoOpen
, DefDisabled
;
148 NTSTATUS Status
= STATUS_SUCCESS
;
150 TRACE("MapDefaultKey (Key %x)\n", Key
);
152 if (!IsPredefKey(Key
))
154 *RealKey
= (HANDLE
)((ULONG_PTR
)Key
& ~0x1);
155 return STATUS_SUCCESS
;
158 /* Handle special cases here */
159 Index
= GetPredefKeyIndex(Key
);
160 if (Index
>= MAX_DEFAULT_HANDLES
)
162 return STATUS_INVALID_PARAMETER
;
165 RtlEnterCriticalSection (&HandleTableCS
);
167 if (Key
== HKEY_CURRENT_USER
)
168 DefDisabled
= DefaultHandleHKUDisabled
;
170 DefDisabled
= DefaultHandlesDisabled
;
174 Handle
= &DefaultHandleTable
[Index
];
175 DoOpen
= (*Handle
== NULL
);
185 /* create/open the default handle */
186 Status
= OpenPredefinedKey(Index
,
190 if (NT_SUCCESS(Status
))
195 *(PULONG_PTR
)Handle
|= 0x1;
198 RtlLeaveCriticalSection (&HandleTableCS
);
205 CloseDefaultKeys(VOID
)
209 RtlEnterCriticalSection(&HandleTableCS
);
211 for (i
= 0; i
< MAX_DEFAULT_HANDLES
; i
++)
213 if (DefaultHandleTable
[i
] != NULL
)
215 NtClose(DefaultHandleTable
[i
]);
216 DefaultHandleTable
[i
] = NULL
;
220 RtlLeaveCriticalSection(&HandleTableCS
);
225 OpenClassesRootKey(PHANDLE KeyHandle
)
227 OBJECT_ATTRIBUTES Attributes
;
228 UNICODE_STRING KeyName
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\Software\\CLASSES");
230 TRACE("OpenClassesRootKey()\n");
232 InitializeObjectAttributes(&Attributes
,
234 OBJ_CASE_INSENSITIVE
,
237 return NtOpenKey(KeyHandle
,
244 OpenLocalMachineKey(PHANDLE KeyHandle
)
246 OBJECT_ATTRIBUTES Attributes
;
247 UNICODE_STRING KeyName
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine");
250 TRACE("OpenLocalMachineKey()\n");
252 InitializeObjectAttributes(&Attributes
,
254 OBJ_CASE_INSENSITIVE
,
257 Status
= NtOpenKey(KeyHandle
,
261 TRACE("NtOpenKey(%wZ) => %08x\n", &KeyName
, Status
);
268 OpenUsersKey(PHANDLE KeyHandle
)
270 OBJECT_ATTRIBUTES Attributes
;
271 UNICODE_STRING KeyName
= RTL_CONSTANT_STRING(L
"\\Registry\\User");
273 TRACE("OpenUsersKey()\n");
275 InitializeObjectAttributes(&Attributes
,
277 OBJ_CASE_INSENSITIVE
,
280 return NtOpenKey(KeyHandle
,
287 OpenCurrentConfigKey (PHANDLE KeyHandle
)
289 OBJECT_ATTRIBUTES Attributes
;
290 UNICODE_STRING KeyName
=
291 RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\System\\CurrentControlSet\\Hardware Profiles\\Current");
293 TRACE("OpenCurrentConfigKey()\n");
295 InitializeObjectAttributes(&Attributes
,
297 OBJ_CASE_INSENSITIVE
,
300 return NtOpenKey(KeyHandle
,
306 /************************************************************************
307 * RegDisablePredefinedCache
312 RegDisablePredefinedCache(VOID
)
314 RtlEnterCriticalSection(&HandleTableCS
);
315 DefaultHandleHKUDisabled
= TRUE
;
316 RtlLeaveCriticalSection(&HandleTableCS
);
317 return ERROR_SUCCESS
;
321 /************************************************************************
322 * RegDisablePredefinedCacheEx
327 RegDisablePredefinedCacheEx(VOID
)
329 RtlEnterCriticalSection(&HandleTableCS
);
330 DefaultHandlesDisabled
= TRUE
;
331 DefaultHandleHKUDisabled
= TRUE
;
332 RtlLeaveCriticalSection(&HandleTableCS
);
333 return ERROR_SUCCESS
;
337 /************************************************************************
338 * RegOverridePredefKey
343 RegOverridePredefKey(IN HKEY hKey
,
344 IN HKEY hNewHKey OPTIONAL
)
346 LONG ErrorCode
= ERROR_SUCCESS
;
348 if ((hKey
== HKEY_CLASSES_ROOT
||
349 hKey
== HKEY_CURRENT_CONFIG
||
350 hKey
== HKEY_CURRENT_USER
||
351 hKey
== HKEY_LOCAL_MACHINE
||
352 hKey
== HKEY_PERFORMANCE_DATA
||
353 hKey
== HKEY_USERS
) &&
354 !IsPredefKey(hNewHKey
))
359 Index
= GetPredefKeyIndex(hKey
);
360 Handle
= &DefaultHandleTable
[Index
];
362 if (hNewHKey
== NULL
)
364 /* restore the default mapping */
365 NTSTATUS Status
= OpenPredefinedKey(Index
,
367 if (!NT_SUCCESS(Status
))
369 return RtlNtStatusToDosError(Status
);
372 ASSERT(hNewHKey
!= NULL
);
375 RtlEnterCriticalSection(&HandleTableCS
);
377 /* close the currently mapped handle if existing */
383 /* update the mapping */
386 RtlLeaveCriticalSection(&HandleTableCS
);
389 ErrorCode
= ERROR_INVALID_HANDLE
;
395 /************************************************************************
401 RegCloseKey(HKEY hKey
)
405 /* don't close null handle or a pseudo handle */
406 if ((!hKey
) || (((ULONG_PTR
)hKey
& 0xF0000000) == 0x80000000))
408 return ERROR_INVALID_HANDLE
;
411 Status
= NtClose(hKey
);
412 if (!NT_SUCCESS(Status
))
414 return RtlNtStatusToDosError(Status
);
417 return ERROR_SUCCESS
;
422 RegpCopyTree(IN HKEY hKeySrc
,
427 LIST_ENTRY ListEntry
;
430 } REGP_COPY_KEYS
, *PREGP_COPY_KEYS
;
432 LIST_ENTRY copyQueueHead
;
433 PREGP_COPY_KEYS copyKeys
, newCopyKeys
;
436 KEY_VALUE_FULL_INFORMATION
*KeyValue
;
437 KEY_NODE_INFORMATION
*KeyNode
;
440 ULONG Index
, BufferSizeRequired
, BufferSize
= 0x200;
441 NTSTATUS Status
= STATUS_SUCCESS
;
442 NTSTATUS Status2
= STATUS_SUCCESS
;
444 InitializeListHead(©QueueHead
);
446 Info
.Buffer
= RtlAllocateHeap(ProcessHeap
,
449 if (Info
.Buffer
== NULL
)
451 return STATUS_INSUFFICIENT_RESOURCES
;
454 copyKeys
= RtlAllocateHeap(ProcessHeap
,
456 sizeof(REGP_COPY_KEYS
));
457 if (copyKeys
!= NULL
)
459 copyKeys
->hKeySrc
= hKeySrc
;
460 copyKeys
->hKeyDest
= hKeyDest
;
461 InsertHeadList(©QueueHead
,
462 ©Keys
->ListEntry
);
464 /* FIXME - copy security from hKeySrc to hKeyDest or just for the subkeys? */
468 copyKeys
= CONTAINING_RECORD(copyQueueHead
.Flink
,
472 /* enumerate all values and copy them */
476 Status2
= NtEnumerateValueKey(copyKeys
->hKeySrc
,
478 KeyValueFullInformation
,
481 &BufferSizeRequired
);
482 if (NT_SUCCESS(Status2
))
484 UNICODE_STRING ValueName
;
487 /* don't use RtlInitUnicodeString as the string is not NULL-terminated! */
488 ValueName
.Length
= Info
.KeyValue
->NameLength
;
489 ValueName
.MaximumLength
= ValueName
.Length
;
490 ValueName
.Buffer
= Info
.KeyValue
->Name
;
492 Data
= (PVOID
)((ULONG_PTR
)Info
.KeyValue
+ Info
.KeyValue
->DataOffset
);
494 Status2
= NtSetValueKey(copyKeys
->hKeyDest
,
496 Info
.KeyValue
->TitleIndex
,
499 Info
.KeyValue
->DataLength
);
501 /* don't break, let's try to copy as many values as possible */
502 if (!NT_SUCCESS(Status2
) && NT_SUCCESS(Status
))
509 else if (Status2
== STATUS_BUFFER_OVERFLOW
)
513 ASSERT(BufferSize
< BufferSizeRequired
);
515 Buffer
= RtlReAllocateHeap(ProcessHeap
,
521 Info
.Buffer
= Buffer
;
526 /* don't break, let's try to copy as many values as possible */
527 Status2
= STATUS_INSUFFICIENT_RESOURCES
;
530 if (NT_SUCCESS(Status
))
538 /* break to avoid an infinite loop in case of denied access or
540 if (Status2
!= STATUS_NO_MORE_ENTRIES
&& NT_SUCCESS(Status
))
549 /* enumerate all subkeys and open and enqueue them */
553 Status2
= NtEnumerateKey(copyKeys
->hKeySrc
,
558 &BufferSizeRequired
);
559 if (NT_SUCCESS(Status2
))
561 HANDLE KeyHandle
, NewKeyHandle
;
562 OBJECT_ATTRIBUTES ObjectAttributes
;
563 UNICODE_STRING SubKeyName
, ClassName
;
565 /* don't use RtlInitUnicodeString as the string is not NULL-terminated! */
566 SubKeyName
.Length
= Info
.KeyNode
->NameLength
;
567 SubKeyName
.MaximumLength
= SubKeyName
.Length
;
568 SubKeyName
.Buffer
= Info
.KeyNode
->Name
;
569 ClassName
.Length
= Info
.KeyNode
->ClassLength
;
570 ClassName
.MaximumLength
= ClassName
.Length
;
571 ClassName
.Buffer
= (PWSTR
)((ULONG_PTR
)Info
.KeyNode
+ Info
.KeyNode
->ClassOffset
);
573 /* open the subkey with sufficient rights */
575 InitializeObjectAttributes(&ObjectAttributes
,
577 OBJ_CASE_INSENSITIVE
,
581 Status2
= NtOpenKey(&KeyHandle
,
582 KEY_ENUMERATE_SUB_KEYS
| KEY_QUERY_VALUE
,
584 if (NT_SUCCESS(Status2
))
586 /* FIXME - attempt to query the security information */
588 InitializeObjectAttributes(&ObjectAttributes
,
590 OBJ_CASE_INSENSITIVE
,
594 Status2
= NtCreateKey(&NewKeyHandle
,
597 Info
.KeyNode
->TitleIndex
,
601 if (NT_SUCCESS(Status2
))
603 newCopyKeys
= RtlAllocateHeap(ProcessHeap
,
605 sizeof(REGP_COPY_KEYS
));
606 if (newCopyKeys
!= NULL
)
608 /* save the handles and enqueue the subkey */
609 newCopyKeys
->hKeySrc
= KeyHandle
;
610 newCopyKeys
->hKeyDest
= NewKeyHandle
;
611 InsertTailList(©QueueHead
,
612 &newCopyKeys
->ListEntry
);
617 NtClose(NewKeyHandle
);
619 Status2
= STATUS_INSUFFICIENT_RESOURCES
;
628 if (!NT_SUCCESS(Status2
) && NT_SUCCESS(Status
))
635 else if (Status2
== STATUS_BUFFER_OVERFLOW
)
639 ASSERT(BufferSize
< BufferSizeRequired
);
641 Buffer
= RtlReAllocateHeap(ProcessHeap
,
647 Info
.Buffer
= Buffer
;
652 /* don't break, let's try to copy as many keys as possible */
653 Status2
= STATUS_INSUFFICIENT_RESOURCES
;
656 if (NT_SUCCESS(Status
))
664 /* break to avoid an infinite loop in case of denied access or
666 if (Status2
!= STATUS_NO_MORE_ENTRIES
&& NT_SUCCESS(Status
))
675 /* close the handles and remove the entry from the list */
676 if (copyKeys
->hKeySrc
!= hKeySrc
)
678 NtClose(copyKeys
->hKeySrc
);
680 if (copyKeys
->hKeyDest
!= hKeyDest
)
682 NtClose(copyKeys
->hKeyDest
);
685 RemoveEntryList(©Keys
->ListEntry
);
687 RtlFreeHeap(ProcessHeap
,
690 } while (!IsListEmpty(©QueueHead
));
693 Status
= STATUS_INSUFFICIENT_RESOURCES
;
695 RtlFreeHeap(ProcessHeap
,
703 /************************************************************************
709 RegCopyTreeW(IN HKEY hKeySrc
,
710 IN LPCWSTR lpSubKey OPTIONAL
,
713 HANDLE DestKeyHandle
, KeyHandle
, CurKey
, SubKeyHandle
= NULL
;
716 Status
= MapDefaultKey(&KeyHandle
,
718 if (!NT_SUCCESS(Status
))
720 return RtlNtStatusToDosError(Status
);
723 Status
= MapDefaultKey(&DestKeyHandle
,
725 if (!NT_SUCCESS(Status
))
730 if (lpSubKey
!= NULL
)
732 OBJECT_ATTRIBUTES ObjectAttributes
;
733 UNICODE_STRING SubKeyName
;
735 RtlInitUnicodeString(&SubKeyName
,
738 InitializeObjectAttributes(&ObjectAttributes
,
740 OBJ_CASE_INSENSITIVE
,
744 Status
= NtOpenKey(&SubKeyHandle
,
745 KEY_ENUMERATE_SUB_KEYS
| KEY_QUERY_VALUE
,
747 if (!NT_SUCCESS(Status
))
752 CurKey
= SubKeyHandle
;
757 Status
= RegpCopyTree(CurKey
,
760 if (SubKeyHandle
!= NULL
)
762 NtClose(SubKeyHandle
);
766 ClosePredefKey(DestKeyHandle
);
768 ClosePredefKey(KeyHandle
);
770 if (!NT_SUCCESS(Status
))
772 return RtlNtStatusToDosError(Status
);
775 return ERROR_SUCCESS
;
779 /************************************************************************
785 RegCopyTreeA(IN HKEY hKeySrc
,
786 IN LPCSTR lpSubKey OPTIONAL
,
789 UNICODE_STRING SubKeyName
= { 0, 0, NULL
};
792 if (lpSubKey
!= NULL
&&
793 !RtlCreateUnicodeStringFromAsciiz(&SubKeyName
,
796 return ERROR_NOT_ENOUGH_MEMORY
;
799 Ret
= RegCopyTreeW(hKeySrc
,
803 RtlFreeUnicodeString(&SubKeyName
);
809 /************************************************************************
810 * RegConnectRegistryA
815 RegConnectRegistryA(IN LPCSTR lpMachineName
,
819 UNICODE_STRING MachineName
= { 0, 0, NULL
};
822 if (lpMachineName
!= NULL
&&
823 !RtlCreateUnicodeStringFromAsciiz(&MachineName
,
824 (LPSTR
)lpMachineName
))
826 return ERROR_NOT_ENOUGH_MEMORY
;
829 Ret
= RegConnectRegistryW(MachineName
.Buffer
,
833 RtlFreeUnicodeString(&MachineName
);
839 /************************************************************************
840 * RegConnectRegistryW
845 RegConnectRegistryW(LPCWSTR lpMachineName
,
851 TRACE("(%s,%p,%p): stub\n",debugstr_w(lpMachineName
),hKey
,phkResult
);
853 if (!lpMachineName
|| !*lpMachineName
)
855 /* Use the local machine name */
856 ret
= RegOpenKeyW( hKey
, NULL
, phkResult
);
860 WCHAR compName
[MAX_COMPUTERNAME_LENGTH
+ 1];
861 DWORD len
= sizeof(compName
) / sizeof(WCHAR
);
863 /* MSDN says lpMachineName must start with \\ : not so */
864 if( lpMachineName
[0] == '\\' && lpMachineName
[1] == '\\')
867 if (GetComputerNameW(compName
, &len
))
869 if (!_wcsicmp(lpMachineName
, compName
))
870 ret
= RegOpenKeyW(hKey
, NULL
, phkResult
);
873 FIXME("Connect to %s is not supported.\n",debugstr_w(lpMachineName
));
874 ret
= ERROR_BAD_NETPATH
;
878 ret
= GetLastError();
885 /************************************************************************
888 * Create key and all necessary intermediate keys
891 CreateNestedKey(PHKEY KeyHandle
,
892 POBJECT_ATTRIBUTES ObjectAttributes
,
893 PUNICODE_STRING ClassString
,
896 DWORD
*lpdwDisposition
)
898 OBJECT_ATTRIBUTES LocalObjectAttributes
;
899 UNICODE_STRING LocalKeyName
;
902 ULONG FullNameLength
;
905 HANDLE LocalKeyHandle
;
907 Status
= NtCreateKey((PHANDLE
) KeyHandle
,
913 (PULONG
)lpdwDisposition
);
914 TRACE("NtCreateKey(%wZ) called (Status %lx)\n", ObjectAttributes
->ObjectName
, Status
);
915 if (Status
!= STATUS_OBJECT_NAME_NOT_FOUND
)
918 /* Copy object attributes */
919 RtlCopyMemory(&LocalObjectAttributes
,
921 sizeof(OBJECT_ATTRIBUTES
));
922 RtlCreateUnicodeString(&LocalKeyName
,
923 ObjectAttributes
->ObjectName
->Buffer
);
924 LocalObjectAttributes
.ObjectName
= &LocalKeyName
;
925 FullNameLength
= LocalKeyName
.Length
/ sizeof(WCHAR
);
927 LocalKeyHandle
= NULL
;
929 /* Remove the last part of the key name and try to create the key again. */
930 while (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
932 Ptr
= wcsrchr(LocalKeyName
.Buffer
, '\\');
933 if (Ptr
== NULL
|| Ptr
== LocalKeyName
.Buffer
)
935 Status
= STATUS_UNSUCCESSFUL
;
940 LocalKeyName
.Length
= wcslen(LocalKeyName
.Buffer
) * sizeof(WCHAR
);
942 Status
= NtCreateKey(&LocalKeyHandle
,
944 &LocalObjectAttributes
,
949 TRACE("NtCreateKey(%wZ) called (Status %lx)\n", &LocalKeyName
, Status
);
952 if (!NT_SUCCESS(Status
))
954 RtlFreeUnicodeString(&LocalKeyName
);
958 /* Add removed parts of the key name and create them too. */
959 Length
= wcslen(LocalKeyName
.Buffer
);
963 NtClose (LocalKeyHandle
);
965 LocalKeyName
.Buffer
[Length
] = L
'\\';
966 Length
= wcslen (LocalKeyName
.Buffer
);
967 LocalKeyName
.Length
= Length
* sizeof(WCHAR
);
969 if (Length
== FullNameLength
)
971 Status
= NtCreateKey((PHANDLE
) KeyHandle
,
977 (PULONG
)lpdwDisposition
);
981 Status
= NtCreateKey(&LocalKeyHandle
,
983 &LocalObjectAttributes
,
988 TRACE("NtCreateKey(%wZ) called (Status %lx)\n", &LocalKeyName
, Status
);
989 if (!NT_SUCCESS(Status
))
993 RtlFreeUnicodeString(&LocalKeyName
);
999 /************************************************************************
1005 RegCreateKeyExA(HKEY hKey
,
1011 LPSECURITY_ATTRIBUTES lpSecurityAttributes
,
1013 LPDWORD lpdwDisposition
)
1015 UNICODE_STRING SubKeyString
;
1016 UNICODE_STRING ClassString
;
1017 OBJECT_ATTRIBUTES Attributes
;
1021 TRACE("RegCreateKeyExA() called\n");
1023 if (lpSecurityAttributes
&& lpSecurityAttributes
->nLength
!= sizeof(SECURITY_ATTRIBUTES
))
1024 return ERROR_INVALID_USER_BUFFER
;
1026 /* get the real parent key */
1027 Status
= MapDefaultKey(&ParentKey
,
1029 if (!NT_SUCCESS(Status
))
1031 return RtlNtStatusToDosError(Status
);
1034 TRACE("ParentKey %p\n", ParentKey
);
1036 if (lpClass
!= NULL
)
1038 RtlCreateUnicodeStringFromAsciiz(&ClassString
,
1042 RtlCreateUnicodeStringFromAsciiz(&SubKeyString
,
1044 InitializeObjectAttributes(&Attributes
,
1046 OBJ_CASE_INSENSITIVE
,
1048 lpSecurityAttributes
? (PSECURITY_DESCRIPTOR
)lpSecurityAttributes
->lpSecurityDescriptor
: NULL
);
1049 Status
= CreateNestedKey(phkResult
,
1051 (lpClass
== NULL
)? NULL
: &ClassString
,
1055 RtlFreeUnicodeString(&SubKeyString
);
1056 if (lpClass
!= NULL
)
1058 RtlFreeUnicodeString(&ClassString
);
1061 ClosePredefKey(ParentKey
);
1063 TRACE("Status %x\n", Status
);
1064 if (!NT_SUCCESS(Status
))
1066 return RtlNtStatusToDosError(Status
);
1069 return ERROR_SUCCESS
;
1073 /************************************************************************
1079 RegCreateKeyExW(HKEY hKey
,
1085 LPSECURITY_ATTRIBUTES lpSecurityAttributes
,
1087 LPDWORD lpdwDisposition
)
1089 UNICODE_STRING SubKeyString
;
1090 UNICODE_STRING ClassString
;
1091 OBJECT_ATTRIBUTES Attributes
;
1095 TRACE("RegCreateKeyExW() called\n");
1097 if (lpSecurityAttributes
&& lpSecurityAttributes
->nLength
!= sizeof(SECURITY_ATTRIBUTES
))
1098 return ERROR_INVALID_USER_BUFFER
;
1100 /* get the real parent key */
1101 Status
= MapDefaultKey(&ParentKey
,
1103 if (!NT_SUCCESS(Status
))
1105 return RtlNtStatusToDosError(Status
);
1108 TRACE("ParentKey %p\n", ParentKey
);
1110 RtlInitUnicodeString(&ClassString
,
1112 RtlInitUnicodeString(&SubKeyString
,
1114 InitializeObjectAttributes(&Attributes
,
1116 OBJ_CASE_INSENSITIVE
,
1118 lpSecurityAttributes
? (PSECURITY_DESCRIPTOR
)lpSecurityAttributes
->lpSecurityDescriptor
: NULL
);
1119 Status
= CreateNestedKey(phkResult
,
1121 (lpClass
== NULL
)? NULL
: &ClassString
,
1126 ClosePredefKey(ParentKey
);
1128 TRACE("Status %x\n", Status
);
1129 if (!NT_SUCCESS(Status
))
1131 return RtlNtStatusToDosError(Status
);
1134 return ERROR_SUCCESS
;
1138 /************************************************************************
1144 RegCreateKeyA(HKEY hKey
,
1148 return RegCreateKeyExA(hKey
,
1160 /************************************************************************
1166 RegCreateKeyW(HKEY hKey
,
1170 return RegCreateKeyExW(hKey
,
1182 /************************************************************************
1188 RegDeleteKeyA(HKEY hKey
,
1191 OBJECT_ATTRIBUTES ObjectAttributes
;
1192 UNICODE_STRING SubKeyName
;
1197 /* Make sure we got a subkey */
1201 return ERROR_INVALID_PARAMETER
;
1204 Status
= MapDefaultKey(&ParentKey
,
1206 if (!NT_SUCCESS(Status
))
1208 return RtlNtStatusToDosError(Status
);
1211 RtlCreateUnicodeStringFromAsciiz(&SubKeyName
,
1213 InitializeObjectAttributes(&ObjectAttributes
,
1215 OBJ_CASE_INSENSITIVE
,
1219 Status
= NtOpenKey(&TargetKey
,
1222 RtlFreeUnicodeString(&SubKeyName
);
1223 if (!NT_SUCCESS(Status
))
1228 Status
= NtDeleteKey(TargetKey
);
1229 NtClose (TargetKey
);
1232 ClosePredefKey(ParentKey
);
1234 if (!NT_SUCCESS(Status
))
1236 return RtlNtStatusToDosError(Status
);
1239 return ERROR_SUCCESS
;
1243 /************************************************************************
1249 RegDeleteKeyW(HKEY hKey
,
1252 OBJECT_ATTRIBUTES ObjectAttributes
;
1253 UNICODE_STRING SubKeyName
;
1258 /* Make sure we got a subkey */
1262 return ERROR_INVALID_PARAMETER
;
1265 Status
= MapDefaultKey(&ParentKey
,
1267 if (!NT_SUCCESS(Status
))
1269 return RtlNtStatusToDosError(Status
);
1272 RtlInitUnicodeString(&SubKeyName
,
1274 InitializeObjectAttributes(&ObjectAttributes
,
1276 OBJ_CASE_INSENSITIVE
,
1279 Status
= NtOpenKey(&TargetKey
,
1282 if (!NT_SUCCESS(Status
))
1287 Status
= NtDeleteKey(TargetKey
);
1291 ClosePredefKey(ParentKey
);
1293 if (!NT_SUCCESS(Status
))
1295 return RtlNtStatusToDosError(Status
);
1298 return ERROR_SUCCESS
;
1302 /************************************************************************
1309 RegDeleteKeyExA(HKEY hKey
,
1314 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1315 return ERROR_CALL_NOT_IMPLEMENTED
;
1319 /************************************************************************
1326 RegDeleteKeyExW(HKEY hKey
,
1331 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1332 return ERROR_CALL_NOT_IMPLEMENTED
;
1336 /************************************************************************
1337 * RegDeleteKeyValueW
1342 RegDeleteKeyValueW(IN HKEY hKey
,
1343 IN LPCWSTR lpSubKey OPTIONAL
,
1344 IN LPCWSTR lpValueName OPTIONAL
)
1346 UNICODE_STRING ValueName
;
1347 HANDLE KeyHandle
, CurKey
, SubKeyHandle
= NULL
;
1350 Status
= MapDefaultKey(&KeyHandle
,
1352 if (!NT_SUCCESS(Status
))
1354 return RtlNtStatusToDosError(Status
);
1357 if (lpSubKey
!= NULL
)
1359 OBJECT_ATTRIBUTES ObjectAttributes
;
1360 UNICODE_STRING SubKeyName
;
1362 RtlInitUnicodeString(&SubKeyName
,
1365 InitializeObjectAttributes(&ObjectAttributes
,
1367 OBJ_CASE_INSENSITIVE
,
1371 Status
= NtOpenKey(&SubKeyHandle
,
1374 if (!NT_SUCCESS(Status
))
1379 CurKey
= SubKeyHandle
;
1384 RtlInitUnicodeString(&ValueName
,
1385 (LPWSTR
)lpValueName
);
1387 Status
= NtDeleteValueKey(CurKey
,
1390 if (SubKeyHandle
!= NULL
)
1392 NtClose(SubKeyHandle
);
1396 ClosePredefKey(KeyHandle
);
1398 if (!NT_SUCCESS(Status
))
1400 return RtlNtStatusToDosError(Status
);
1403 return ERROR_SUCCESS
;
1407 /************************************************************************
1408 * RegDeleteKeyValueA
1413 RegDeleteKeyValueA(IN HKEY hKey
,
1414 IN LPCSTR lpSubKey OPTIONAL
,
1415 IN LPCSTR lpValueName OPTIONAL
)
1417 UNICODE_STRING SubKey
= { 0, 0, NULL
}, ValueName
= { 0, 0, NULL
};
1420 if (lpSubKey
!= NULL
&&
1421 !RtlCreateUnicodeStringFromAsciiz(&SubKey
,
1424 return ERROR_NOT_ENOUGH_MEMORY
;
1427 if (lpValueName
!= NULL
&&
1428 !RtlCreateUnicodeStringFromAsciiz(&ValueName
,
1429 (LPSTR
)lpValueName
))
1431 RtlFreeUnicodeString(&SubKey
);
1432 return ERROR_NOT_ENOUGH_MEMORY
;
1435 Ret
= RegDeleteKeyValueW(hKey
,
1439 RtlFreeUnicodeString(&SubKey
);
1440 RtlFreeUnicodeString(&ValueName
);
1446 // Non-recursive RegDeleteTreeW implementation by Thomas, however it needs bugfixing
1448 RegpDeleteTree(IN HKEY hKey
)
1452 LIST_ENTRY ListEntry
;
1454 } REGP_DEL_KEYS
, *PREG_DEL_KEYS
;
1456 LIST_ENTRY delQueueHead
;
1457 PREG_DEL_KEYS delKeys
, newDelKeys
;
1460 PKEY_BASIC_INFORMATION BasicInfo
;
1461 PREG_DEL_KEYS KeyDelRoot
;
1462 NTSTATUS Status
= STATUS_SUCCESS
;
1463 NTSTATUS Status2
= STATUS_SUCCESS
;
1465 InitializeListHead(&delQueueHead
);
1467 ProcessHeap
= RtlGetProcessHeap();
1469 /* NOTE: no need to allocate enough memory for an additional KEY_BASIC_INFORMATION
1470 structure for the root key, we only do that for subkeys as we need to
1471 allocate REGP_DEL_KEYS structures anyway! */
1472 KeyDelRoot
= RtlAllocateHeap(ProcessHeap
,
1474 sizeof(REGP_DEL_KEYS
));
1475 if (KeyDelRoot
!= NULL
)
1477 KeyDelRoot
->KeyHandle
= hKey
;
1478 InsertTailList(&delQueueHead
,
1479 &KeyDelRoot
->ListEntry
);
1483 delKeys
= CONTAINING_RECORD(delQueueHead
.Flink
,
1492 /* check if this key contains subkeys and delete them first by queuing
1493 them at the head of the list */
1494 Status2
= NtEnumerateKey(delKeys
->KeyHandle
,
1496 KeyBasicInformation
,
1501 if (NT_SUCCESS(Status2
))
1503 OBJECT_ATTRIBUTES ObjectAttributes
;
1504 UNICODE_STRING SubKeyName
;
1506 ASSERT(newDelKeys
!= NULL
);
1507 ASSERT(BasicInfo
!= NULL
);
1509 /* don't use RtlInitUnicodeString as the string is not NULL-terminated! */
1510 SubKeyName
.Length
= BasicInfo
->NameLength
;
1511 SubKeyName
.MaximumLength
= BasicInfo
->NameLength
;
1512 SubKeyName
.Buffer
= BasicInfo
->Name
;
1514 InitializeObjectAttributes(&ObjectAttributes
,
1516 OBJ_CASE_INSENSITIVE
,
1520 /* open the subkey */
1521 Status2
= NtOpenKey(&newDelKeys
->KeyHandle
,
1522 DELETE
| KEY_ENUMERATE_SUB_KEYS
,
1524 if (!NT_SUCCESS(Status2
))
1529 /* enqueue this key to the head of the deletion queue */
1530 InsertHeadList(&delQueueHead
,
1531 &newDelKeys
->ListEntry
);
1533 /* try again from the head of the list */
1538 if (Status2
== STATUS_BUFFER_TOO_SMALL
)
1540 newDelKeys
= RtlAllocateHeap(ProcessHeap
,
1542 BufferSize
+ sizeof(REGP_DEL_KEYS
));
1543 if (newDelKeys
!= NULL
)
1545 BasicInfo
= (PKEY_BASIC_INFORMATION
)(newDelKeys
+ 1);
1548 goto ReadFirstSubKey
;
1552 /* don't break, let's try to delete as many keys as possible */
1553 Status2
= STATUS_INSUFFICIENT_RESOURCES
;
1554 goto SubKeyFailureNoFree
;
1557 else if (Status2
== STATUS_BUFFER_OVERFLOW
)
1559 PREG_DEL_KEYS newDelKeys2
;
1561 ASSERT(newDelKeys
!= NULL
);
1563 /* we need more memory to query the key name */
1564 newDelKeys2
= RtlReAllocateHeap(ProcessHeap
,
1567 BufferSize
+ sizeof(REGP_DEL_KEYS
));
1568 if (newDelKeys2
!= NULL
)
1570 newDelKeys
= newDelKeys2
;
1571 BasicInfo
= (PKEY_BASIC_INFORMATION
)(newDelKeys
+ 1);
1574 goto ReadFirstSubKey
;
1578 /* don't break, let's try to delete as many keys as possible */
1579 Status2
= STATUS_INSUFFICIENT_RESOURCES
;
1582 else if (Status2
== STATUS_NO_MORE_ENTRIES
)
1584 /* in some race conditions where another thread would delete
1585 the same tree at the same time, newDelKeys could actually
1587 if (newDelKeys
!= NULL
)
1589 RtlFreeHeap(ProcessHeap
,
1597 /* newDelKeys can be NULL here when NtEnumerateKey returned an
1598 error other than STATUS_BUFFER_TOO_SMALL or STATUS_BUFFER_OVERFLOW! */
1599 if (newDelKeys
!= NULL
)
1601 RtlFreeHeap(ProcessHeap
,
1606 SubKeyFailureNoFree
:
1607 /* don't break, let's try to delete as many keys as possible */
1608 if (NT_SUCCESS(Status
))
1614 Status2
= NtDeleteKey(delKeys
->KeyHandle
);
1616 /* NOTE: do NOT close the handle anymore, it's invalid already! */
1618 if (!NT_SUCCESS(Status2
))
1620 /* close the key handle so we don't leak handles for keys we were
1621 unable to delete. But only do this for handles not supplied
1624 if (delKeys
->KeyHandle
!= hKey
)
1626 NtClose(delKeys
->KeyHandle
);
1629 if (NT_SUCCESS(Status
))
1631 /* don't break, let's try to delete as many keys as possible */
1636 /* remove the entry from the list */
1637 RemoveEntryList(&delKeys
->ListEntry
);
1639 RtlFreeHeap(ProcessHeap
,
1642 } while (!IsListEmpty(&delQueueHead
));
1645 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1651 /************************************************************************
1657 RegDeleteTreeW(IN HKEY hKey
,
1658 IN LPCWSTR lpSubKey OPTIONAL
)
1660 HANDLE KeyHandle
, CurKey
, SubKeyHandle
= NULL
;
1663 Status
= MapDefaultKey(&KeyHandle
,
1665 if (!NT_SUCCESS(Status
))
1667 return RtlNtStatusToDosError(Status
);
1670 if (lpSubKey
!= NULL
)
1672 OBJECT_ATTRIBUTES ObjectAttributes
;
1673 UNICODE_STRING SubKeyName
;
1675 RtlInitUnicodeString(&SubKeyName
,
1678 InitializeObjectAttributes(&ObjectAttributes
,
1680 OBJ_CASE_INSENSITIVE
,
1684 Status
= NtOpenKey(&SubKeyHandle
,
1685 DELETE
| KEY_ENUMERATE_SUB_KEYS
,
1687 if (!NT_SUCCESS(Status
))
1692 CurKey
= SubKeyHandle
;
1697 Status
= RegpDeleteTree(CurKey
);
1699 if (NT_SUCCESS(Status
))
1701 /* make sure we only close hKey (KeyHandle) when the caller specified a
1702 subkey, because the handle would be invalid already! */
1703 if (CurKey
!= KeyHandle
)
1705 ClosePredefKey(KeyHandle
);
1708 return ERROR_SUCCESS
;
1712 /* make sure we close all handles we created! */
1713 if (SubKeyHandle
!= NULL
)
1715 NtClose(SubKeyHandle
);
1719 ClosePredefKey(KeyHandle
);
1721 return RtlNtStatusToDosError(Status
);
1727 /************************************************************************
1734 RegDeleteTreeW(HKEY hKey
,
1738 DWORD dwMaxSubkeyLen
, dwMaxValueLen
;
1739 DWORD dwMaxLen
, dwSize
;
1743 WCHAR szNameBuf
[MAX_PATH
], *lpszName
= szNameBuf
;
1745 TRACE("(hkey=%p,%p %s)\n", hKey
, lpszSubKey
, debugstr_w(lpszSubKey
));
1747 Status
= MapDefaultKey(&KeyHandle
,
1749 if (!NT_SUCCESS(Status
))
1751 return RtlNtStatusToDosError(Status
);
1754 hSubKey
= KeyHandle
;
1758 ret
= RegOpenKeyExW(KeyHandle
, lpszSubKey
, 0, KEY_READ
, &hSubKey
);
1761 ClosePredefKey(KeyHandle
);
1766 /* Get highest length for keys, values */
1767 ret
= RegQueryInfoKeyW(hSubKey
, NULL
, NULL
, NULL
, NULL
,
1768 &dwMaxSubkeyLen
, NULL
, NULL
, &dwMaxValueLen
, NULL
, NULL
, NULL
);
1769 if (ret
) goto cleanup
;
1773 dwMaxLen
= max(dwMaxSubkeyLen
, dwMaxValueLen
);
1774 if (dwMaxLen
> sizeof(szNameBuf
)/sizeof(WCHAR
))
1776 /* Name too big: alloc a buffer for it */
1777 if (!(lpszName
= RtlAllocateHeap( RtlGetProcessHeap(), 0, dwMaxLen
*sizeof(WCHAR
))))
1779 ret
= ERROR_NOT_ENOUGH_MEMORY
;
1785 /* Recursively delete all the subkeys */
1789 if (RegEnumKeyExW(hSubKey
, 0, lpszName
, &dwSize
, NULL
,
1790 NULL
, NULL
, NULL
)) break;
1792 ret
= RegDeleteTreeW(hSubKey
, lpszName
);
1793 if (ret
) goto cleanup
;
1797 ret
= RegDeleteKeyW(KeyHandle
, lpszSubKey
);
1802 if (RegEnumValueW(KeyHandle
, 0, lpszName
, &dwSize
,
1803 NULL
, NULL
, NULL
, NULL
)) break;
1805 ret
= RegDeleteValueW(KeyHandle
, lpszName
);
1806 if (ret
) goto cleanup
;
1810 /* Free buffer if allocated */
1811 if (lpszName
!= szNameBuf
)
1812 RtlFreeHeap( RtlGetProcessHeap(), 0, lpszName
);
1814 RegCloseKey(hSubKey
);
1816 ClosePredefKey(KeyHandle
);
1822 /************************************************************************
1828 RegDeleteTreeA(IN HKEY hKey
,
1829 IN LPCSTR lpSubKey OPTIONAL
)
1831 UNICODE_STRING SubKeyName
= { 0, 0, NULL
};
1834 if (lpSubKey
!= NULL
&&
1835 !RtlCreateUnicodeStringFromAsciiz(&SubKeyName
,
1838 return ERROR_NOT_ENOUGH_MEMORY
;
1841 Ret
= RegDeleteTreeW(hKey
,
1844 RtlFreeUnicodeString(&SubKeyName
);
1850 /************************************************************************
1851 * RegDisableReflectionKey
1856 RegDisableReflectionKey(IN HKEY hBase
)
1858 FIXME("RegDisableReflectionKey(0x%p) UNIMPLEMENTED!\n", hBase
);
1859 return ERROR_CALL_NOT_IMPLEMENTED
;
1863 /************************************************************************
1864 * RegEnableReflectionKey
1869 RegEnableReflectionKey(IN HKEY hBase
)
1871 FIXME("RegEnableReflectionKey(0x%p) UNIMPLEMENTED!\n", hBase
);
1872 return ERROR_CALL_NOT_IMPLEMENTED
;
1876 /******************************************************************************
1877 * RegpApplyRestrictions [internal]
1879 * Helper function for RegGetValueA/W.
1882 RegpApplyRestrictions(DWORD dwFlags
,
1887 /* Check if the type is restricted by the passed flags */
1888 if (*ret
== ERROR_SUCCESS
|| *ret
== ERROR_MORE_DATA
)
1894 case REG_NONE
: dwMask
= RRF_RT_REG_NONE
; break;
1895 case REG_SZ
: dwMask
= RRF_RT_REG_SZ
; break;
1896 case REG_EXPAND_SZ
: dwMask
= RRF_RT_REG_EXPAND_SZ
; break;
1897 case REG_MULTI_SZ
: dwMask
= RRF_RT_REG_MULTI_SZ
; break;
1898 case REG_BINARY
: dwMask
= RRF_RT_REG_BINARY
; break;
1899 case REG_DWORD
: dwMask
= RRF_RT_REG_DWORD
; break;
1900 case REG_QWORD
: dwMask
= RRF_RT_REG_QWORD
; break;
1903 if (dwFlags
& dwMask
)
1905 /* Type is not restricted, check for size mismatch */
1906 if (dwType
== REG_BINARY
)
1910 if ((dwFlags
& RRF_RT_DWORD
) == RRF_RT_DWORD
)
1912 else if ((dwFlags
& RRF_RT_QWORD
) == RRF_RT_QWORD
)
1915 if (cbExpect
&& cbData
!= cbExpect
)
1916 *ret
= ERROR_DATATYPE_MISMATCH
;
1919 else *ret
= ERROR_UNSUPPORTED_TYPE
;
1924 /******************************************************************************
1925 * RegGetValueW [ADVAPI32.@]
1927 * Retrieves the type and data for a value name associated with a key,
1928 * optionally expanding its content and restricting its type.
1931 * hKey [I] Handle to an open key.
1932 * pszSubKey [I] Name of the subkey of hKey.
1933 * pszValue [I] Name of value under hKey/szSubKey to query.
1934 * dwFlags [I] Flags restricting the value type to retrieve.
1935 * pdwType [O] Destination for the values type, may be NULL.
1936 * pvData [O] Destination for the values content, may be NULL.
1937 * pcbData [I/O] Size of pvData, updated with the size in bytes required to
1938 * retrieve the whole content, including the trailing '\0'
1942 * Success: ERROR_SUCCESS
1943 * Failure: nonzero error code from Winerror.h
1946 * - Unless RRF_NOEXPAND is specified, REG_EXPAND_SZ values are automatically
1947 * expanded and pdwType is set to REG_SZ instead.
1948 * - Restrictions are applied after expanding, using RRF_RT_REG_EXPAND_SZ
1949 * without RRF_NOEXPAND is thus not allowed.
1950 * An exception is the case where RRF_RT_ANY is specified, because then
1951 * RRF_NOEXPAND is allowed.
1954 RegGetValueW(HKEY hKey
,
1962 DWORD dwType
, cbData
= pcbData
? *pcbData
: 0;
1966 TRACE("(%p,%s,%s,%ld,%p,%p,%p=%ld)\n",
1967 hKey
, debugstr_w(pszSubKey
), debugstr_w(pszValue
), dwFlags
, pdwType
,
1968 pvData
, pcbData
, cbData
);
1970 if (pvData
&& !pcbData
)
1971 return ERROR_INVALID_PARAMETER
;
1972 if ((dwFlags
& RRF_RT_REG_EXPAND_SZ
) && !(dwFlags
& RRF_NOEXPAND
) &&
1973 ((dwFlags
& RRF_RT_ANY
) != RRF_RT_ANY
))
1974 return ERROR_INVALID_PARAMETER
;
1976 if (pszSubKey
&& pszSubKey
[0])
1978 ret
= RegOpenKeyExW(hKey
, pszSubKey
, 0, KEY_QUERY_VALUE
, &hKey
);
1979 if (ret
!= ERROR_SUCCESS
) return ret
;
1982 ret
= RegQueryValueExW(hKey
, pszValue
, NULL
, &dwType
, pvData
, &cbData
);
1984 /* If we are going to expand we need to read in the whole the value even
1985 * if the passed buffer was too small as the expanded string might be
1986 * smaller than the unexpanded one and could fit into cbData bytes. */
1987 if ((ret
== ERROR_SUCCESS
|| ret
== ERROR_MORE_DATA
) &&
1988 dwType
== REG_EXPAND_SZ
&& !(dwFlags
& RRF_NOEXPAND
))
1992 HeapFree(GetProcessHeap(), 0, pvBuf
);
1994 pvBuf
= HeapAlloc(GetProcessHeap(), 0, cbData
);
1997 ret
= ERROR_NOT_ENOUGH_MEMORY
;
2001 if (ret
== ERROR_MORE_DATA
|| !pvData
)
2002 ret
= RegQueryValueExW(hKey
, pszValue
, NULL
,
2003 &dwType
, pvBuf
, &cbData
);
2006 /* Even if cbData was large enough we have to copy the
2007 * string since ExpandEnvironmentStrings can't handle
2008 * overlapping buffers. */
2009 CopyMemory(pvBuf
, pvData
, cbData
);
2012 /* Both the type or the value itself could have been modified in
2013 * between so we have to keep retrying until the buffer is large
2014 * enough or we no longer have to expand the value. */
2016 while (dwType
== REG_EXPAND_SZ
&& ret
== ERROR_MORE_DATA
);
2018 if (ret
== ERROR_SUCCESS
)
2020 /* Recheck dwType in case it changed since the first call */
2021 if (dwType
== REG_EXPAND_SZ
)
2023 cbData
= ExpandEnvironmentStringsW(pvBuf
, pvData
,
2024 pcbData
? *pcbData
: 0) * sizeof(WCHAR
);
2026 if (pvData
&& pcbData
&& cbData
> *pcbData
)
2027 ret
= ERROR_MORE_DATA
;
2030 CopyMemory(pvData
, pvBuf
, *pcbData
);
2033 HeapFree(GetProcessHeap(), 0, pvBuf
);
2036 if (pszSubKey
&& pszSubKey
[0])
2039 RegpApplyRestrictions(dwFlags
, dwType
, cbData
, &ret
);
2041 if (pvData
&& ret
!= ERROR_SUCCESS
&& (dwFlags
& RRF_ZEROONFAILURE
))
2042 ZeroMemory(pvData
, *pcbData
);
2054 /******************************************************************************
2055 * RegGetValueA [ADVAPI32.@]
2060 RegGetValueA(HKEY hKey
,
2068 DWORD dwType
, cbData
= pcbData
? *pcbData
: 0;
2072 TRACE("(%p,%s,%s,%ld,%p,%p,%p=%ld)\n",
2073 hKey
, pszSubKey
, pszValue
, dwFlags
, pdwType
, pvData
, pcbData
,
2076 if (pvData
&& !pcbData
)
2077 return ERROR_INVALID_PARAMETER
;
2078 if ((dwFlags
& RRF_RT_REG_EXPAND_SZ
) && !(dwFlags
& RRF_NOEXPAND
) &&
2079 ((dwFlags
& RRF_RT_ANY
) != RRF_RT_ANY
))
2080 return ERROR_INVALID_PARAMETER
;
2082 if (pszSubKey
&& pszSubKey
[0])
2084 ret
= RegOpenKeyExA(hKey
, pszSubKey
, 0, KEY_QUERY_VALUE
, &hKey
);
2085 if (ret
!= ERROR_SUCCESS
) return ret
;
2088 ret
= RegQueryValueExA(hKey
, pszValue
, NULL
, &dwType
, pvData
, &cbData
);
2090 /* If we are going to expand we need to read in the whole the value even
2091 * if the passed buffer was too small as the expanded string might be
2092 * smaller than the unexpanded one and could fit into cbData bytes. */
2093 if ((ret
== ERROR_SUCCESS
|| ret
== ERROR_MORE_DATA
) &&
2094 (dwType
== REG_EXPAND_SZ
&& !(dwFlags
& RRF_NOEXPAND
)))
2097 HeapFree(GetProcessHeap(), 0, pvBuf
);
2099 pvBuf
= HeapAlloc(GetProcessHeap(), 0, cbData
);
2102 ret
= ERROR_NOT_ENOUGH_MEMORY
;
2106 if (ret
== ERROR_MORE_DATA
|| !pvData
)
2107 ret
= RegQueryValueExA(hKey
, pszValue
, NULL
,
2108 &dwType
, pvBuf
, &cbData
);
2111 /* Even if cbData was large enough we have to copy the
2112 * string since ExpandEnvironmentStrings can't handle
2113 * overlapping buffers. */
2114 CopyMemory(pvBuf
, pvData
, cbData
);
2117 /* Both the type or the value itself could have been modified in
2118 * between so we have to keep retrying until the buffer is large
2119 * enough or we no longer have to expand the value. */
2120 } while (dwType
== REG_EXPAND_SZ
&& ret
== ERROR_MORE_DATA
);
2122 if (ret
== ERROR_SUCCESS
)
2124 /* Recheck dwType in case it changed since the first call */
2125 if (dwType
== REG_EXPAND_SZ
)
2127 cbData
= ExpandEnvironmentStringsA(pvBuf
, pvData
,
2128 pcbData
? *pcbData
: 0);
2130 if(pvData
&& pcbData
&& cbData
> *pcbData
)
2131 ret
= ERROR_MORE_DATA
;
2134 CopyMemory(pvData
, pvBuf
, *pcbData
);
2137 HeapFree(GetProcessHeap(), 0, pvBuf
);
2140 if (pszSubKey
&& pszSubKey
[0])
2143 RegpApplyRestrictions(dwFlags
, dwType
, cbData
, &ret
);
2145 if (pvData
&& ret
!= ERROR_SUCCESS
&& (dwFlags
& RRF_ZEROONFAILURE
))
2146 ZeroMemory(pvData
, *pcbData
);
2148 if (pdwType
) *pdwType
= dwType
;
2149 if (pcbData
) *pcbData
= cbData
;
2155 /************************************************************************
2161 RegSetKeyValueW(IN HKEY hKey
,
2162 IN LPCWSTR lpSubKey OPTIONAL
,
2163 IN LPCWSTR lpValueName OPTIONAL
,
2165 IN LPCVOID lpData OPTIONAL
,
2168 HANDLE KeyHandle
, CurKey
, SubKeyHandle
= NULL
;
2172 Status
= MapDefaultKey(&KeyHandle
,
2174 if (!NT_SUCCESS(Status
))
2176 return RtlNtStatusToDosError(Status
);
2179 if (lpSubKey
!= NULL
)
2181 OBJECT_ATTRIBUTES ObjectAttributes
;
2182 UNICODE_STRING SubKeyName
;
2184 RtlInitUnicodeString(&SubKeyName
,
2187 InitializeObjectAttributes(&ObjectAttributes
,
2189 OBJ_CASE_INSENSITIVE
,
2193 Status
= NtOpenKey(&SubKeyHandle
,
2196 if (!NT_SUCCESS(Status
))
2198 Ret
= RtlNtStatusToDosError(Status
);
2202 CurKey
= SubKeyHandle
;
2207 Ret
= RegSetValueExW(CurKey
,
2214 if (SubKeyHandle
!= NULL
)
2216 NtClose(SubKeyHandle
);
2220 ClosePredefKey(KeyHandle
);
2226 /************************************************************************
2232 RegSetKeyValueA(IN HKEY hKey
,
2233 IN LPCSTR lpSubKey OPTIONAL
,
2234 IN LPCSTR lpValueName OPTIONAL
,
2236 IN LPCVOID lpData OPTIONAL
,
2239 HANDLE KeyHandle
, CurKey
, SubKeyHandle
= NULL
;
2243 Status
= MapDefaultKey(&KeyHandle
,
2245 if (!NT_SUCCESS(Status
))
2247 return RtlNtStatusToDosError(Status
);
2250 if (lpSubKey
!= NULL
)
2252 OBJECT_ATTRIBUTES ObjectAttributes
;
2253 UNICODE_STRING SubKeyName
;
2255 if (!RtlCreateUnicodeStringFromAsciiz(&SubKeyName
,
2258 Ret
= ERROR_NOT_ENOUGH_MEMORY
;
2262 InitializeObjectAttributes(&ObjectAttributes
,
2264 OBJ_CASE_INSENSITIVE
,
2268 Status
= NtOpenKey(&SubKeyHandle
,
2272 RtlFreeUnicodeString(&SubKeyName
);
2274 if (!NT_SUCCESS(Status
))
2276 Ret
= RtlNtStatusToDosError(Status
);
2280 CurKey
= SubKeyHandle
;
2285 Ret
= RegSetValueExA(CurKey
,
2292 if (SubKeyHandle
!= NULL
)
2294 NtClose(SubKeyHandle
);
2298 ClosePredefKey(KeyHandle
);
2304 /************************************************************************
2310 RegDeleteValueA(HKEY hKey
,
2313 UNICODE_STRING ValueName
;
2317 Status
= MapDefaultKey(&KeyHandle
,
2319 if (!NT_SUCCESS(Status
))
2321 return RtlNtStatusToDosError(Status
);
2324 RtlCreateUnicodeStringFromAsciiz(&ValueName
,
2325 (LPSTR
)lpValueName
);
2326 Status
= NtDeleteValueKey(KeyHandle
,
2328 RtlFreeUnicodeString (&ValueName
);
2330 ClosePredefKey(KeyHandle
);
2332 if (!NT_SUCCESS(Status
))
2334 return RtlNtStatusToDosError(Status
);
2337 return ERROR_SUCCESS
;
2341 /************************************************************************
2347 RegDeleteValueW(HKEY hKey
,
2348 LPCWSTR lpValueName
)
2350 UNICODE_STRING ValueName
;
2354 Status
= MapDefaultKey(&KeyHandle
,
2356 if (!NT_SUCCESS(Status
))
2358 return RtlNtStatusToDosError(Status
);
2361 RtlInitUnicodeString(&ValueName
,
2362 (LPWSTR
)lpValueName
);
2364 Status
= NtDeleteValueKey(KeyHandle
,
2367 ClosePredefKey(KeyHandle
);
2369 if (!NT_SUCCESS(Status
))
2371 return RtlNtStatusToDosError(Status
);
2374 return ERROR_SUCCESS
;
2378 /************************************************************************
2384 RegEnumKeyA(HKEY hKey
,
2392 return RegEnumKeyExA(hKey
,
2403 /************************************************************************
2409 RegEnumKeyW(HKEY hKey
,
2417 return RegEnumKeyExW(hKey
,
2428 /************************************************************************
2434 RegEnumKeyExA(HKEY hKey
,
2441 PFILETIME lpftLastWriteTime
)
2445 KEY_NODE_INFORMATION Node
;
2446 KEY_BASIC_INFORMATION Basic
;
2449 UNICODE_STRING StringU
;
2450 ANSI_STRING StringA
;
2451 LONG ErrorCode
= ERROR_SUCCESS
;
2453 DWORD ClassLength
= 0;
2459 TRACE("RegEnumKeyExA(hKey 0x%x, dwIndex %d, lpName 0x%x, *lpcbName %d, lpClass 0x%x, lpcbClass %d)\n",
2460 hKey
, dwIndex
, lpName
, *lpcbName
, lpClass
, lpcbClass
? *lpcbClass
: 0);
2462 if ((lpClass
) && (!lpcbClass
))
2464 return ERROR_INVALID_PARAMETER
;
2467 Status
= MapDefaultKey(&KeyHandle
, hKey
);
2468 if (!NT_SUCCESS(Status
))
2470 return RtlNtStatusToDosError(Status
);
2475 NameLength
= min (*lpcbName
- 1 , REG_MAX_NAME_SIZE
) * sizeof (WCHAR
);
2486 ClassLength
= min (*lpcbClass
-1, REG_MAX_NAME_SIZE
) * sizeof(WCHAR
);
2493 /* The class name should start at a dword boundary */
2494 BufferSize
= ((sizeof(KEY_NODE_INFORMATION
) + NameLength
+ 3) & ~3) + ClassLength
;
2498 BufferSize
= sizeof(KEY_BASIC_INFORMATION
) + NameLength
;
2501 KeyInfo
= RtlAllocateHeap (ProcessHeap
, 0, BufferSize
);
2502 if (KeyInfo
== NULL
)
2504 ErrorCode
= ERROR_OUTOFMEMORY
;
2508 Status
= NtEnumerateKey(KeyHandle
,
2510 lpClass
== NULL
? KeyBasicInformation
: KeyNodeInformation
,
2514 TRACE("NtEnumerateKey() returned status 0x%X\n", Status
);
2515 if (!NT_SUCCESS(Status
))
2517 ErrorCode
= RtlNtStatusToDosError (Status
);
2521 if (lpClass
== NULL
)
2523 if (KeyInfo
->Basic
.NameLength
> NameLength
)
2525 ErrorCode
= ERROR_BUFFER_OVERFLOW
;
2529 StringU
.Buffer
= KeyInfo
->Basic
.Name
;
2530 StringU
.Length
= KeyInfo
->Basic
.NameLength
;
2531 StringU
.MaximumLength
= KeyInfo
->Basic
.NameLength
;
2536 if (KeyInfo
->Node
.NameLength
> NameLength
||
2537 KeyInfo
->Node
.ClassLength
> ClassLength
)
2539 ErrorCode
= ERROR_BUFFER_OVERFLOW
;
2543 StringA
.Buffer
= lpClass
;
2545 StringA
.MaximumLength
= *lpcbClass
;
2546 StringU
.Buffer
= (PWCHAR
)((ULONG_PTR
)KeyInfo
->Node
.Name
+ KeyInfo
->Node
.ClassOffset
);
2547 StringU
.Length
= KeyInfo
->Node
.ClassLength
;
2548 StringU
.MaximumLength
= KeyInfo
->Node
.ClassLength
;
2549 RtlUnicodeStringToAnsiString (&StringA
, &StringU
, FALSE
);
2550 lpClass
[StringA
.Length
] = 0;
2551 *lpcbClass
= StringA
.Length
;
2552 StringU
.Buffer
= KeyInfo
->Node
.Name
;
2553 StringU
.Length
= KeyInfo
->Node
.NameLength
;
2554 StringU
.MaximumLength
= KeyInfo
->Node
.NameLength
;
2558 if (ErrorCode
== ERROR_SUCCESS
)
2560 StringA
.Buffer
= lpName
;
2562 StringA
.MaximumLength
= *lpcbName
;
2563 RtlUnicodeStringToAnsiString (&StringA
, &StringU
, FALSE
);
2564 lpName
[StringA
.Length
] = 0;
2565 *lpcbName
= StringA
.Length
;
2566 if (lpftLastWriteTime
!= NULL
)
2568 if (lpClass
== NULL
)
2570 lpftLastWriteTime
->dwLowDateTime
= KeyInfo
->Basic
.LastWriteTime
.u
.LowPart
;
2571 lpftLastWriteTime
->dwHighDateTime
= KeyInfo
->Basic
.LastWriteTime
.u
.HighPart
;
2575 lpftLastWriteTime
->dwLowDateTime
= KeyInfo
->Node
.LastWriteTime
.u
.LowPart
;
2576 lpftLastWriteTime
->dwHighDateTime
= KeyInfo
->Node
.LastWriteTime
.u
.HighPart
;
2582 /*TRACE("Key Namea0 Length %d\n", StringU.Length);*/ /* BUGBUG could be uninitialized */
2583 TRACE("Key Name1 Length %d\n", NameLength
);
2584 TRACE("Key Name Length %d\n", *lpcbName
);
2585 TRACE("Key Name %s\n", lpName
);
2587 RtlFreeHeap(ProcessHeap
,
2592 ClosePredefKey(KeyHandle
);
2598 /************************************************************************
2604 RegEnumKeyExW(HKEY hKey
,
2611 PFILETIME lpftLastWriteTime
)
2615 KEY_NODE_INFORMATION Node
;
2616 KEY_BASIC_INFORMATION Basic
;
2622 ULONG ClassLength
= 0;
2624 LONG ErrorCode
= ERROR_SUCCESS
;
2627 Status
= MapDefaultKey(&KeyHandle
,
2629 if (!NT_SUCCESS(Status
))
2631 return RtlNtStatusToDosError(Status
);
2636 NameLength
= min (*lpcbName
- 1, REG_MAX_NAME_SIZE
) * sizeof (WCHAR
);
2647 ClassLength
= min (*lpcbClass
- 1, REG_MAX_NAME_SIZE
) * sizeof(WCHAR
);
2654 BufferSize
= ((sizeof(KEY_NODE_INFORMATION
) + NameLength
+ 3) & ~3) + ClassLength
;
2658 BufferSize
= sizeof(KEY_BASIC_INFORMATION
) + NameLength
;
2661 KeyInfo
= RtlAllocateHeap(ProcessHeap
,
2664 if (KeyInfo
== NULL
)
2666 ErrorCode
= ERROR_OUTOFMEMORY
;
2670 Status
= NtEnumerateKey(KeyHandle
,
2672 lpClass
? KeyNodeInformation
: KeyBasicInformation
,
2676 TRACE("NtEnumerateKey() returned status 0x%X\n", Status
);
2677 if (!NT_SUCCESS(Status
))
2679 ErrorCode
= RtlNtStatusToDosError (Status
);
2683 if (lpClass
== NULL
)
2685 if (KeyInfo
->Basic
.NameLength
> NameLength
)
2687 ErrorCode
= ERROR_BUFFER_OVERFLOW
;
2691 RtlCopyMemory(lpName
,
2692 KeyInfo
->Basic
.Name
,
2693 KeyInfo
->Basic
.NameLength
);
2694 *lpcbName
= (DWORD
)(KeyInfo
->Basic
.NameLength
/ sizeof(WCHAR
));
2695 lpName
[*lpcbName
] = 0;
2700 if (KeyInfo
->Node
.NameLength
> NameLength
||
2701 KeyInfo
->Node
.ClassLength
> ClassLength
)
2703 ErrorCode
= ERROR_BUFFER_OVERFLOW
;
2707 RtlCopyMemory(lpName
,
2709 KeyInfo
->Node
.NameLength
);
2710 *lpcbName
= KeyInfo
->Node
.NameLength
/ sizeof(WCHAR
);
2711 lpName
[*lpcbName
] = 0;
2712 RtlCopyMemory(lpClass
,
2713 (PVOID
)((ULONG_PTR
)KeyInfo
->Node
.Name
+ KeyInfo
->Node
.ClassOffset
),
2714 KeyInfo
->Node
.ClassLength
);
2715 *lpcbClass
= (DWORD
)(KeyInfo
->Node
.ClassLength
/ sizeof(WCHAR
));
2716 lpClass
[*lpcbClass
] = 0;
2720 if (ErrorCode
== ERROR_SUCCESS
&& lpftLastWriteTime
!= NULL
)
2722 if (lpClass
== NULL
)
2724 lpftLastWriteTime
->dwLowDateTime
= KeyInfo
->Basic
.LastWriteTime
.u
.LowPart
;
2725 lpftLastWriteTime
->dwHighDateTime
= KeyInfo
->Basic
.LastWriteTime
.u
.HighPart
;
2729 lpftLastWriteTime
->dwLowDateTime
= KeyInfo
->Node
.LastWriteTime
.u
.LowPart
;
2730 lpftLastWriteTime
->dwHighDateTime
= KeyInfo
->Node
.LastWriteTime
.u
.HighPart
;
2735 RtlFreeHeap(ProcessHeap
,
2740 ClosePredefKey(KeyHandle
);
2746 /************************************************************************
2752 RegEnumValueA(HKEY hKey
,
2764 char buffer
[256], *buf_ptr
= buffer
;
2765 KEY_VALUE_FULL_INFORMATION
*info
= (KEY_VALUE_FULL_INFORMATION
*)buffer
;
2766 static const int info_size
= FIELD_OFFSET( KEY_VALUE_FULL_INFORMATION
, Name
);
2768 //TRACE("(%p,%ld,%p,%p,%p,%p,%p,%p)\n",
2769 // hkey, index, value, val_count, reserved, type, data, count );
2771 /* NT only checks count, not val_count */
2772 if ((data
&& !count
) || reserved
)
2773 return ERROR_INVALID_PARAMETER
;
2775 status
= MapDefaultKey(&KeyHandle
, hKey
);
2776 if (!NT_SUCCESS(status
))
2778 return RtlNtStatusToDosError(status
);
2781 total_size
= info_size
+ (MAX_PATH
+ 1) * sizeof(WCHAR
);
2782 if (data
) total_size
+= *count
;
2783 total_size
= min( sizeof(buffer
), total_size
);
2785 status
= NtEnumerateValueKey( KeyHandle
, index
, KeyValueFullInformation
,
2786 buffer
, total_size
, &total_size
);
2787 if (status
&& status
!= STATUS_BUFFER_OVERFLOW
) goto done
;
2789 /* we need to fetch the contents for a string type even if not requested,
2790 * because we need to compute the length of the ASCII string. */
2791 if (value
|| data
|| is_string(info
->Type
))
2793 /* retry with a dynamically allocated buffer */
2794 while (status
== STATUS_BUFFER_OVERFLOW
)
2796 if (buf_ptr
!= buffer
) HeapFree( GetProcessHeap(), 0, buf_ptr
);
2797 if (!(buf_ptr
= HeapAlloc( GetProcessHeap(), 0, total_size
)))
2799 status
= STATUS_INSUFFICIENT_RESOURCES
;
2802 info
= (KEY_VALUE_FULL_INFORMATION
*)buf_ptr
;
2803 status
= NtEnumerateValueKey( KeyHandle
, index
, KeyValueFullInformation
,
2804 buf_ptr
, total_size
, &total_size
);
2807 if (status
) goto done
;
2809 if (is_string(info
->Type
))
2812 RtlUnicodeToMultiByteSize( &len
, (WCHAR
*)(buf_ptr
+ info
->DataOffset
),
2813 total_size
- info
->DataOffset
);
2816 if (len
> *count
) status
= STATUS_BUFFER_OVERFLOW
;
2819 RtlUnicodeToMultiByteN( (PCHAR
)data
, len
, NULL
, (WCHAR
*)(buf_ptr
+ info
->DataOffset
),
2820 total_size
- info
->DataOffset
);
2821 /* if the type is REG_SZ and data is not 0-terminated
2822 * and there is enough space in the buffer NT appends a \0 */
2823 if (len
< *count
&& data
[len
-1]) data
[len
] = 0;
2826 info
->DataLength
= len
;
2830 if (total_size
- info
->DataOffset
> *count
) status
= STATUS_BUFFER_OVERFLOW
;
2831 else memcpy( data
, buf_ptr
+ info
->DataOffset
, total_size
- info
->DataOffset
);
2834 if (value
&& !status
)
2838 RtlUnicodeToMultiByteSize( &len
, info
->Name
, info
->NameLength
);
2839 if (len
>= *val_count
)
2841 status
= STATUS_BUFFER_OVERFLOW
;
2844 len
= *val_count
- 1;
2845 RtlUnicodeToMultiByteN( value
, len
, NULL
, info
->Name
, info
->NameLength
);
2851 RtlUnicodeToMultiByteN( value
, len
, NULL
, info
->Name
, info
->NameLength
);
2857 else status
= STATUS_SUCCESS
;
2859 if (type
) *type
= info
->Type
;
2860 if (count
) *count
= info
->DataLength
;
2863 if (buf_ptr
!= buffer
) HeapFree( GetProcessHeap(), 0, buf_ptr
);
2864 ClosePredefKey(KeyHandle
);
2865 return RtlNtStatusToDosError(status
);
2869 /******************************************************************************
2870 * RegEnumValueW [ADVAPI32.@]
2874 * hkey [I] Handle to key to query
2875 * index [I] Index of value to query
2876 * value [O] Value string
2877 * val_count [I/O] Size of value buffer (in wchars)
2878 * reserved [I] Reserved
2879 * type [O] Type code
2880 * data [O] Value data
2881 * count [I/O] Size of data buffer (in bytes)
2884 * Success: ERROR_SUCCESS
2885 * Failure: nonzero error code from Winerror.h
2888 RegEnumValueW(HKEY hKey
,
2900 char buffer
[256], *buf_ptr
= buffer
;
2901 KEY_VALUE_FULL_INFORMATION
*info
= (KEY_VALUE_FULL_INFORMATION
*)buffer
;
2902 static const int info_size
= FIELD_OFFSET( KEY_VALUE_FULL_INFORMATION
, Name
);
2904 //TRACE("(%p,%ld,%p,%p,%p,%p,%p,%p)\n",
2905 // hkey, index, value, val_count, reserved, type, data, count );
2907 /* NT only checks count, not val_count */
2908 if ((data
&& !count
) || reserved
) return ERROR_INVALID_PARAMETER
;
2910 status
= MapDefaultKey(&KeyHandle
, hKey
);
2911 if (!NT_SUCCESS(status
))
2913 return RtlNtStatusToDosError(status
);
2916 total_size
= info_size
+ (MAX_PATH
+ 1) * sizeof(WCHAR
);
2917 if (data
) total_size
+= *count
;
2918 total_size
= min( sizeof(buffer
), total_size
);
2920 status
= NtEnumerateValueKey( KeyHandle
, index
, KeyValueFullInformation
,
2921 buffer
, total_size
, &total_size
);
2922 if (status
&& status
!= STATUS_BUFFER_OVERFLOW
) goto done
;
2926 /* retry with a dynamically allocated buffer */
2927 while (status
== STATUS_BUFFER_OVERFLOW
)
2929 if (buf_ptr
!= buffer
) HeapFree( GetProcessHeap(), 0, buf_ptr
);
2930 if (!(buf_ptr
= HeapAlloc( GetProcessHeap(), 0, total_size
)))
2932 status
= ERROR_NOT_ENOUGH_MEMORY
;
2935 info
= (KEY_VALUE_FULL_INFORMATION
*)buf_ptr
;
2936 status
= NtEnumerateValueKey( KeyHandle
, index
, KeyValueFullInformation
,
2937 buf_ptr
, total_size
, &total_size
);
2940 if (status
) goto done
;
2944 if (info
->NameLength
/sizeof(WCHAR
) >= *val_count
)
2946 status
= STATUS_BUFFER_OVERFLOW
;
2949 memcpy( value
, info
->Name
, info
->NameLength
);
2950 *val_count
= info
->NameLength
/ sizeof(WCHAR
);
2951 value
[*val_count
] = 0;
2956 if (total_size
- info
->DataOffset
> *count
)
2958 status
= STATUS_BUFFER_OVERFLOW
;
2961 memcpy( data
, buf_ptr
+ info
->DataOffset
, total_size
- info
->DataOffset
);
2962 if (total_size
- info
->DataOffset
<= *count
-sizeof(WCHAR
) && is_string(info
->Type
))
2964 /* if the type is REG_SZ and data is not 0-terminated
2965 * and there is enough space in the buffer NT appends a \0 */
2966 WCHAR
*ptr
= (WCHAR
*)(data
+ total_size
- info
->DataOffset
);
2967 if (ptr
> (WCHAR
*)data
&& ptr
[-1]) *ptr
= 0;
2971 else status
= STATUS_SUCCESS
;
2974 if (type
) *type
= info
->Type
;
2975 if (count
) *count
= info
->DataLength
;
2978 if (buf_ptr
!= buffer
) HeapFree( GetProcessHeap(), 0, buf_ptr
);
2979 ClosePredefKey(KeyHandle
);
2980 return RtlNtStatusToDosError(status
);
2984 /************************************************************************
2990 RegFlushKey(HKEY hKey
)
2995 if (hKey
== HKEY_PERFORMANCE_DATA
)
2997 return ERROR_SUCCESS
;
3000 Status
= MapDefaultKey(&KeyHandle
,
3002 if (!NT_SUCCESS(Status
))
3004 return RtlNtStatusToDosError(Status
);
3007 Status
= NtFlushKey(KeyHandle
);
3009 ClosePredefKey(KeyHandle
);
3011 if (!NT_SUCCESS(Status
))
3013 return RtlNtStatusToDosError(Status
);
3016 return ERROR_SUCCESS
;
3020 /************************************************************************
3026 RegGetKeySecurity(HKEY hKey
,
3027 SECURITY_INFORMATION SecurityInformation
,
3028 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
3029 LPDWORD lpcbSecurityDescriptor
)
3034 if (hKey
== HKEY_PERFORMANCE_DATA
)
3036 return ERROR_INVALID_HANDLE
;
3039 Status
= MapDefaultKey(&KeyHandle
,
3041 if (!NT_SUCCESS(Status
))
3043 TRACE("MapDefaultKey() failed (Status %lx)\n", Status
);
3044 return RtlNtStatusToDosError(Status
);
3048 Status
= NtQuerySecurityObject(KeyHandle
,
3049 SecurityInformation
,
3050 pSecurityDescriptor
,
3051 *lpcbSecurityDescriptor
,
3052 lpcbSecurityDescriptor
);
3055 ClosePredefKey(KeyHandle
);
3057 if (!NT_SUCCESS(Status
))
3059 WARN("NtQuerySecurityObject() failed (Status %lx)\n", Status
);
3060 return RtlNtStatusToDosError(Status
);
3063 return ERROR_SUCCESS
;
3067 /************************************************************************
3073 RegLoadKeyA(HKEY hKey
,
3077 UNICODE_STRING FileName
;
3078 UNICODE_STRING KeyName
;
3081 RtlCreateUnicodeStringFromAsciiz(&KeyName
,
3083 RtlCreateUnicodeStringFromAsciiz(&FileName
,
3086 ErrorCode
= RegLoadKeyW(hKey
,
3090 RtlFreeUnicodeString(&FileName
);
3091 RtlFreeUnicodeString(&KeyName
);
3097 /************************************************************************
3103 RegLoadKeyW(HKEY hKey
,
3107 OBJECT_ATTRIBUTES FileObjectAttributes
;
3108 OBJECT_ATTRIBUTES KeyObjectAttributes
;
3109 UNICODE_STRING FileName
;
3110 UNICODE_STRING KeyName
;
3113 LONG ErrorCode
= ERROR_SUCCESS
;
3115 if (hKey
== HKEY_PERFORMANCE_DATA
)
3117 return ERROR_INVALID_HANDLE
;
3120 Status
= MapDefaultKey(&KeyHandle
,
3122 if (!NT_SUCCESS(Status
))
3124 return RtlNtStatusToDosError(Status
);
3127 if (!RtlDosPathNameToNtPathName_U(lpFile
,
3132 ErrorCode
= ERROR_BAD_PATHNAME
;
3136 InitializeObjectAttributes(&FileObjectAttributes
,
3138 OBJ_CASE_INSENSITIVE
,
3142 RtlInitUnicodeString(&KeyName
,
3145 InitializeObjectAttributes(&KeyObjectAttributes
,
3147 OBJ_CASE_INSENSITIVE
,
3151 Status
= NtLoadKey(&KeyObjectAttributes
,
3152 &FileObjectAttributes
);
3154 RtlFreeHeap(RtlGetProcessHeap(),
3158 if (!NT_SUCCESS(Status
))
3160 ErrorCode
= RtlNtStatusToDosError(Status
);
3165 ClosePredefKey(KeyHandle
);
3171 /************************************************************************
3172 * RegNotifyChangeKeyValue
3177 RegNotifyChangeKeyValue(HKEY hKey
,
3179 DWORD dwNotifyFilter
,
3183 IO_STATUS_BLOCK IoStatusBlock
;
3186 LONG ErrorCode
= ERROR_SUCCESS
;
3188 if (hKey
== HKEY_PERFORMANCE_DATA
)
3190 return ERROR_INVALID_HANDLE
;
3193 if (fAsynchronous
== TRUE
&& hEvent
== NULL
)
3195 return ERROR_INVALID_PARAMETER
;
3198 Status
= MapDefaultKey(&KeyHandle
,
3200 if (!NT_SUCCESS(Status
))
3202 return RtlNtStatusToDosError(Status
);
3205 /* FIXME: Remote key handles must fail */
3207 Status
= NtNotifyChangeKey(KeyHandle
,
3217 if (!NT_SUCCESS(Status
) && Status
!= STATUS_TIMEOUT
)
3219 ErrorCode
= RtlNtStatusToDosError(Status
);
3222 ClosePredefKey(KeyHandle
);
3228 /************************************************************************
3229 * RegOpenCurrentUser
3234 RegOpenCurrentUser(IN REGSAM samDesired
,
3235 OUT PHKEY phkResult
)
3239 Status
= RtlOpenCurrentUser((ACCESS_MASK
)samDesired
,
3240 (PHANDLE
)phkResult
);
3241 if (!NT_SUCCESS(Status
))
3243 /* NOTE - don't set the last error code! just return the error! */
3244 return RtlNtStatusToDosError(Status
);
3247 return ERROR_SUCCESS
;
3251 /************************************************************************
3254 * 20050503 Fireball - imported from WINE
3259 RegOpenKeyA(HKEY hKey
,
3263 TRACE("RegOpenKeyA hKey 0x%x lpSubKey %s phkResult %p\n",
3264 hKey
, lpSubKey
, phkResult
);
3266 if (!hKey
&& lpSubKey
&& phkResult
)
3268 return ERROR_INVALID_HANDLE
;
3271 if (!lpSubKey
|| !*lpSubKey
)
3274 return ERROR_SUCCESS
;
3277 return RegOpenKeyExA(hKey
,
3285 /************************************************************************
3290 * 20050503 Fireball - imported from WINE
3295 RegOpenKeyW(HKEY hKey
,
3299 TRACE("RegOpenKeyW hKey 0x%x lpSubKey %S phkResult %p\n",
3300 hKey
, lpSubKey
, phkResult
);
3302 if (!hKey
&& lpSubKey
&& phkResult
)
3304 return ERROR_INVALID_HANDLE
;
3307 if (!lpSubKey
|| !*lpSubKey
)
3310 return ERROR_SUCCESS
;
3313 return RegOpenKeyExW(hKey
,
3321 /************************************************************************
3327 RegOpenKeyExA(HKEY hKey
,
3333 OBJECT_ATTRIBUTES ObjectAttributes
;
3334 UNICODE_STRING SubKeyString
;
3337 LONG ErrorCode
= ERROR_SUCCESS
;
3339 TRACE("RegOpenKeyExA hKey 0x%x lpSubKey %s ulOptions 0x%x samDesired 0x%x phkResult %p\n",
3340 hKey
, lpSubKey
, ulOptions
, samDesired
, phkResult
);
3343 return ERROR_INVALID_PARAMETER
;
3346 Status
= MapDefaultKey(&KeyHandle
,
3348 if (!NT_SUCCESS(Status
))
3350 return RtlNtStatusToDosError(Status
);
3353 RtlCreateUnicodeStringFromAsciiz(&SubKeyString
,
3355 InitializeObjectAttributes(&ObjectAttributes
,
3357 OBJ_CASE_INSENSITIVE
,
3361 Status
= NtOpenKey((PHANDLE
)phkResult
,
3364 RtlFreeUnicodeString(&SubKeyString
);
3365 if (!NT_SUCCESS(Status
))
3367 ErrorCode
= RtlNtStatusToDosError(Status
);
3370 ClosePredefKey(KeyHandle
);
3376 /************************************************************************
3382 RegOpenKeyExW(HKEY hKey
,
3388 OBJECT_ATTRIBUTES ObjectAttributes
;
3389 UNICODE_STRING SubKeyString
;
3392 LONG ErrorCode
= ERROR_SUCCESS
;
3394 TRACE("RegOpenKeyExW hKey 0x%x lpSubKey %S ulOptions 0x%x samDesired 0x%x phkResult %p\n",
3395 hKey
, lpSubKey
, ulOptions
, samDesired
, phkResult
);
3398 return ERROR_INVALID_PARAMETER
;
3401 Status
= MapDefaultKey(&KeyHandle
, hKey
);
3402 if (!NT_SUCCESS(Status
))
3404 return RtlNtStatusToDosError(Status
);
3407 if (lpSubKey
!= NULL
)
3408 RtlInitUnicodeString(&SubKeyString
, (LPWSTR
)lpSubKey
);
3410 RtlInitUnicodeString(&SubKeyString
, (LPWSTR
)L
"");
3412 InitializeObjectAttributes(&ObjectAttributes
,
3414 OBJ_CASE_INSENSITIVE
,
3418 Status
= NtOpenKey((PHANDLE
)phkResult
,
3421 if (!NT_SUCCESS(Status
))
3423 ErrorCode
= RtlNtStatusToDosError(Status
);
3426 ClosePredefKey(KeyHandle
);
3432 /************************************************************************
3433 * RegOpenUserClassesRoot
3438 RegOpenUserClassesRoot(IN HANDLE hToken
,
3440 IN REGSAM samDesired
,
3441 OUT PHKEY phkResult
)
3443 const WCHAR UserClassesKeyPrefix
[] = L
"\\Registry\\User\\";
3444 const WCHAR UserClassesKeySuffix
[] = L
"_Classes";
3445 PTOKEN_USER TokenUserData
;
3446 ULONG RequiredLength
;
3447 UNICODE_STRING UserSidString
, UserClassesKeyRoot
;
3448 OBJECT_ATTRIBUTES ObjectAttributes
;
3451 /* check parameters */
3452 if (hToken
== NULL
|| dwOptions
!= 0 || phkResult
== NULL
)
3454 return ERROR_INVALID_PARAMETER
;
3458 * Get the user sid from the token
3462 /* determine how much memory we need */
3463 Status
= NtQueryInformationToken(hToken
,
3468 if (!NT_SUCCESS(Status
) && (Status
!= STATUS_BUFFER_TOO_SMALL
))
3470 /* NOTE - as opposed to all other registry functions windows does indeed
3471 change the last error code in case the caller supplied a invalid
3472 handle for example! */
3473 return RtlNtStatusToDosError(Status
);
3476 TokenUserData
= RtlAllocateHeap(ProcessHeap
,
3479 if (TokenUserData
== NULL
)
3481 return ERROR_NOT_ENOUGH_MEMORY
;
3484 /* attempt to read the information */
3485 Status
= NtQueryInformationToken(hToken
,
3490 if (!NT_SUCCESS(Status
))
3492 RtlFreeHeap(ProcessHeap
,
3495 if (Status
== STATUS_BUFFER_TOO_SMALL
)
3497 /* the information appears to have changed?! try again */
3501 /* NOTE - as opposed to all other registry functions windows does indeed
3502 change the last error code in case the caller supplied a invalid
3503 handle for example! */
3504 return RtlNtStatusToDosError(Status
);
3508 * Build the absolute path for the user's registry in the form
3509 * "\Registry\User\<SID>_Classes"
3511 Status
= RtlConvertSidToUnicodeString(&UserSidString
,
3512 TokenUserData
->User
.Sid
,
3515 /* we don't need the user data anymore, free it */
3516 RtlFreeHeap(ProcessHeap
,
3520 if (!NT_SUCCESS(Status
))
3522 return RtlNtStatusToDosError(Status
);
3525 /* allocate enough memory for the entire key string */
3526 UserClassesKeyRoot
.Length
= 0;
3527 UserClassesKeyRoot
.MaximumLength
= UserSidString
.Length
+
3528 sizeof(UserClassesKeyPrefix
) +
3529 sizeof(UserClassesKeySuffix
);
3530 UserClassesKeyRoot
.Buffer
= RtlAllocateHeap(ProcessHeap
,
3532 UserClassesKeyRoot
.MaximumLength
);
3533 if (UserClassesKeyRoot
.Buffer
== NULL
)
3535 RtlFreeUnicodeString(&UserSidString
);
3536 return RtlNtStatusToDosError(Status
);
3539 /* build the string */
3540 RtlAppendUnicodeToString(&UserClassesKeyRoot
,
3541 UserClassesKeyPrefix
);
3542 RtlAppendUnicodeStringToString(&UserClassesKeyRoot
,
3544 RtlAppendUnicodeToString(&UserClassesKeyRoot
,
3545 UserClassesKeySuffix
);
3547 TRACE("RegOpenUserClassesRoot: Absolute path: %wZ\n", &UserClassesKeyRoot
);
3552 InitializeObjectAttributes(&ObjectAttributes
,
3553 &UserClassesKeyRoot
,
3554 OBJ_CASE_INSENSITIVE
,
3558 Status
= NtOpenKey((PHANDLE
)phkResult
,
3562 RtlFreeUnicodeString(&UserSidString
);
3563 RtlFreeUnicodeString(&UserClassesKeyRoot
);
3565 if (!NT_SUCCESS(Status
))
3567 return RtlNtStatusToDosError(Status
);
3570 return ERROR_SUCCESS
;
3574 /************************************************************************
3580 RegQueryInfoKeyA(HKEY hKey
,
3585 LPDWORD lpcbMaxSubKeyLen
,
3586 LPDWORD lpcbMaxClassLen
,
3588 LPDWORD lpcbMaxValueNameLen
,
3589 LPDWORD lpcbMaxValueLen
,
3590 LPDWORD lpcbSecurityDescriptor
,
3591 PFILETIME lpftLastWriteTime
)
3593 WCHAR ClassName
[MAX_PATH
];
3594 UNICODE_STRING UnicodeString
;
3595 ANSI_STRING AnsiString
;
3598 RtlInitUnicodeString(&UnicodeString
,
3600 if (lpClass
!= NULL
)
3602 UnicodeString
.Buffer
= &ClassName
[0];
3603 UnicodeString
.MaximumLength
= sizeof(ClassName
);
3604 AnsiString
.MaximumLength
= *lpcbClass
;
3607 ErrorCode
= RegQueryInfoKeyW(hKey
,
3608 UnicodeString
.Buffer
,
3615 lpcbMaxValueNameLen
,
3617 lpcbSecurityDescriptor
,
3619 if ((ErrorCode
== ERROR_SUCCESS
) && (lpClass
!= NULL
))
3621 AnsiString
.Buffer
= lpClass
;
3622 AnsiString
.Length
= 0;
3623 UnicodeString
.Length
= *lpcbClass
* sizeof(WCHAR
);
3624 RtlUnicodeStringToAnsiString(&AnsiString
,
3627 *lpcbClass
= AnsiString
.Length
;
3628 lpClass
[AnsiString
.Length
] = 0;
3635 /************************************************************************
3641 RegQueryInfoKeyW(HKEY hKey
,
3646 LPDWORD lpcbMaxSubKeyLen
,
3647 LPDWORD lpcbMaxClassLen
,
3649 LPDWORD lpcbMaxValueNameLen
,
3650 LPDWORD lpcbMaxValueLen
,
3651 LPDWORD lpcbSecurityDescriptor
,
3652 PFILETIME lpftLastWriteTime
)
3654 KEY_FULL_INFORMATION FullInfoBuffer
;
3655 PKEY_FULL_INFORMATION FullInfo
;
3657 ULONG ClassLength
= 0;
3661 LONG ErrorCode
= ERROR_SUCCESS
;
3663 if ((lpClass
) && (!lpcbClass
))
3665 return ERROR_INVALID_PARAMETER
;
3668 Status
= MapDefaultKey(&KeyHandle
,
3670 if (!NT_SUCCESS(Status
))
3672 return RtlNtStatusToDosError(Status
);
3675 if (lpClass
!= NULL
)
3679 ClassLength
= min(*lpcbClass
- 1, REG_MAX_NAME_SIZE
) * sizeof(WCHAR
);
3686 FullInfoSize
= sizeof(KEY_FULL_INFORMATION
) + ((ClassLength
+ 3) & ~3);
3687 FullInfo
= RtlAllocateHeap(ProcessHeap
,
3690 if (FullInfo
== NULL
)
3692 ErrorCode
= ERROR_OUTOFMEMORY
;
3696 FullInfo
->ClassLength
= ClassLength
;
3700 FullInfoSize
= sizeof(KEY_FULL_INFORMATION
);
3701 FullInfo
= &FullInfoBuffer
;
3702 FullInfo
->ClassLength
= 0;
3704 FullInfo
->ClassOffset
= FIELD_OFFSET(KEY_FULL_INFORMATION
, Class
);
3706 Status
= NtQueryKey(KeyHandle
,
3711 TRACE("NtQueryKey() returned status 0x%X\n", Status
);
3712 if (!NT_SUCCESS(Status
))
3714 if (lpClass
!= NULL
)
3716 RtlFreeHeap(ProcessHeap
,
3721 ErrorCode
= RtlNtStatusToDosError(Status
);
3725 TRACE("SubKeys %d\n", FullInfo
->SubKeys
);
3726 if (lpcSubKeys
!= NULL
)
3728 *lpcSubKeys
= FullInfo
->SubKeys
;
3731 TRACE("MaxNameLen %lu\n", FullInfo
->MaxNameLen
);
3732 if (lpcbMaxSubKeyLen
!= NULL
)
3734 *lpcbMaxSubKeyLen
= FullInfo
->MaxNameLen
/ sizeof(WCHAR
) + 1;
3737 TRACE("MaxClassLen %lu\n", FullInfo
->MaxClassLen
);
3738 if (lpcbMaxClassLen
!= NULL
)
3740 *lpcbMaxClassLen
= FullInfo
->MaxClassLen
/ sizeof(WCHAR
) + 1;
3743 TRACE("Values %lu\n", FullInfo
->Values
);
3744 if (lpcValues
!= NULL
)
3746 *lpcValues
= FullInfo
->Values
;
3749 TRACE("MaxValueNameLen %lu\n", FullInfo
->MaxValueNameLen
);
3750 if (lpcbMaxValueNameLen
!= NULL
)
3752 *lpcbMaxValueNameLen
= FullInfo
->MaxValueNameLen
/ sizeof(WCHAR
) + 1;
3755 TRACE("MaxValueDataLen %lu\n", FullInfo
->MaxValueDataLen
);
3756 if (lpcbMaxValueLen
!= NULL
)
3758 *lpcbMaxValueLen
= FullInfo
->MaxValueDataLen
;
3762 if (lpcbSecurityDescriptor
!= NULL
)
3764 Status
= NtQuerySecurityObject(KeyHandle
,
3765 OWNER_SECURITY_INFORMATION
|
3766 GROUP_SECURITY_INFORMATION
|
3767 DACL_SECURITY_INFORMATION
,
3770 lpcbSecurityDescriptor
);
3771 if (!NT_SUCCESS(Status
))
3773 if (lpClass
!= NULL
)
3775 RtlFreeHeap(ProcessHeap
,
3780 ErrorCode
= RtlNtStatusToDosError(Status
);
3786 if (lpftLastWriteTime
!= NULL
)
3788 lpftLastWriteTime
->dwLowDateTime
= FullInfo
->LastWriteTime
.u
.LowPart
;
3789 lpftLastWriteTime
->dwHighDateTime
= FullInfo
->LastWriteTime
.u
.HighPart
;
3792 if (lpClass
!= NULL
)
3794 if (FullInfo
->ClassLength
> ClassLength
)
3796 ErrorCode
= ERROR_BUFFER_OVERFLOW
;
3800 RtlCopyMemory(lpClass
,
3802 FullInfo
->ClassLength
);
3803 *lpcbClass
= FullInfo
->ClassLength
/ sizeof(WCHAR
);
3804 lpClass
[*lpcbClass
] = 0;
3807 RtlFreeHeap(ProcessHeap
,
3813 ClosePredefKey(KeyHandle
);
3819 /************************************************************************
3820 * RegQueryMultipleValuesA
3825 RegQueryMultipleValuesA(HKEY hKey
,
3832 DWORD maxBytes
= *ldwTotsize
;
3833 LPSTR bufptr
= (LPSTR
)lpValueBuf
;
3836 if (maxBytes
>= (1024*1024))
3837 return ERROR_TRANSFER_TOO_LONG
;
3841 TRACE("RegQueryMultipleValuesA(%p,%p,%ld,%p,%p=%ld)\n",
3842 hKey
, val_list
, num_vals
, lpValueBuf
, ldwTotsize
, *ldwTotsize
);
3844 for (i
= 0; i
< num_vals
; i
++)
3846 val_list
[i
].ve_valuelen
= 0;
3847 ErrorCode
= RegQueryValueExA(hKey
,
3848 val_list
[i
].ve_valuename
,
3852 &val_list
[i
].ve_valuelen
);
3853 if (ErrorCode
!= ERROR_SUCCESS
)
3858 if (lpValueBuf
!= NULL
&& *ldwTotsize
+ val_list
[i
].ve_valuelen
<= maxBytes
)
3860 ErrorCode
= RegQueryValueExA(hKey
,
3861 val_list
[i
].ve_valuename
,
3863 &val_list
[i
].ve_type
,
3865 &val_list
[i
].ve_valuelen
);
3866 if (ErrorCode
!= ERROR_SUCCESS
)
3871 val_list
[i
].ve_valueptr
= (DWORD_PTR
)bufptr
;
3873 bufptr
+= val_list
[i
].ve_valuelen
;
3876 *ldwTotsize
+= val_list
[i
].ve_valuelen
;
3879 return (lpValueBuf
!= NULL
&& *ldwTotsize
<= maxBytes
) ? ERROR_SUCCESS
: ERROR_MORE_DATA
;
3883 /************************************************************************
3884 * RegQueryMultipleValuesW
3889 RegQueryMultipleValuesW(HKEY hKey
,
3896 DWORD maxBytes
= *ldwTotsize
;
3897 LPSTR bufptr
= (LPSTR
)lpValueBuf
;
3900 if (maxBytes
>= (1024*1024))
3901 return ERROR_TRANSFER_TOO_LONG
;
3905 TRACE("RegQueryMultipleValuesW(%p,%p,%ld,%p,%p=%ld)\n",
3906 hKey
, val_list
, num_vals
, lpValueBuf
, ldwTotsize
, *ldwTotsize
);
3908 for (i
= 0; i
< num_vals
; i
++)
3910 val_list
[i
].ve_valuelen
= 0;
3911 ErrorCode
= RegQueryValueExW(hKey
,
3912 val_list
[i
].ve_valuename
,
3916 &val_list
[i
].ve_valuelen
);
3917 if (ErrorCode
!= ERROR_SUCCESS
)
3922 if (lpValueBuf
!= NULL
&& *ldwTotsize
+ val_list
[i
].ve_valuelen
<= maxBytes
)
3924 ErrorCode
= RegQueryValueExW(hKey
,
3925 val_list
[i
].ve_valuename
,
3927 &val_list
[i
].ve_type
,
3929 &val_list
[i
].ve_valuelen
);
3930 if (ErrorCode
!= ERROR_SUCCESS
)
3935 val_list
[i
].ve_valueptr
= (DWORD_PTR
)bufptr
;
3937 bufptr
+= val_list
[i
].ve_valuelen
;
3940 *ldwTotsize
+= val_list
[i
].ve_valuelen
;
3943 return (lpValueBuf
!= NULL
&& *ldwTotsize
<= maxBytes
) ? ERROR_SUCCESS
: ERROR_MORE_DATA
;
3947 /************************************************************************
3948 * RegQueryReflectionKey
3953 RegQueryReflectionKey(IN HKEY hBase
,
3954 OUT BOOL
* bIsReflectionDisabled
)
3956 FIXME("RegQueryReflectionKey(0x%p, 0x%p) UNIMPLEMENTED!\n",
3957 hBase
, bIsReflectionDisabled
);
3958 return ERROR_CALL_NOT_IMPLEMENTED
;
3962 /************************************************************************
3968 RegQueryValueExA(HKEY hKey
,
3975 UNICODE_STRING ValueName
;
3976 UNICODE_STRING ValueData
;
3977 ANSI_STRING AnsiString
;
3982 TRACE("hKey 0x%X lpValueName %s lpData 0x%X lpcbData %d\n",
3983 hKey
, lpValueName
, lpData
, lpcbData
? *lpcbData
: 0);
3985 if (lpData
!= NULL
&& lpcbData
== NULL
)
3987 return ERROR_INVALID_PARAMETER
;
3992 ValueData
.Length
= 0;
3993 ValueData
.MaximumLength
= (*lpcbData
+ 1) * sizeof(WCHAR
);
3994 ValueData
.Buffer
= RtlAllocateHeap(ProcessHeap
,
3996 ValueData
.MaximumLength
);
3997 if (!ValueData
.Buffer
)
3999 return ERROR_OUTOFMEMORY
;
4004 ValueData
.Buffer
= NULL
;
4005 ValueData
.Length
= 0;
4006 ValueData
.MaximumLength
= 0;
4012 RtlCreateUnicodeStringFromAsciiz(&ValueName
,
4013 (LPSTR
)lpValueName
);
4015 Length
= (lpcbData
== NULL
) ? 0 : *lpcbData
* sizeof(WCHAR
);
4016 ErrorCode
= RegQueryValueExW(hKey
,
4020 (lpData
== NULL
) ? NULL
: (LPBYTE
)ValueData
.Buffer
,
4022 TRACE("ErrorCode %lu\n", ErrorCode
);
4023 RtlFreeUnicodeString(&ValueName
);
4025 if (ErrorCode
== ERROR_SUCCESS
||
4026 ErrorCode
== ERROR_MORE_DATA
)
4029 if ((Type
== REG_SZ
) || (Type
== REG_MULTI_SZ
) || (Type
== REG_EXPAND_SZ
))
4031 if (ErrorCode
== ERROR_SUCCESS
&& ValueData
.Buffer
!= NULL
)
4033 RtlInitAnsiString(&AnsiString
, NULL
);
4034 AnsiString
.Buffer
= (LPSTR
)lpData
;
4035 AnsiString
.MaximumLength
= *lpcbData
;
4036 ValueData
.Length
= Length
;
4037 ValueData
.MaximumLength
= ValueData
.Length
+ sizeof(WCHAR
);
4038 RtlUnicodeStringToAnsiString(&AnsiString
, &ValueData
, FALSE
);
4041 Length
= Length
/ sizeof(WCHAR
);
4043 else if (ErrorCode
== ERROR_SUCCESS
&& ValueData
.Buffer
!= NULL
)
4045 if (*lpcbData
< Length
)
4047 ErrorCode
= ERROR_MORE_DATA
;
4051 RtlMoveMemory(lpData
, ValueData
.Buffer
, Length
);
4055 if (lpcbData
!= NULL
)
4066 if (ValueData
.Buffer
!= NULL
)
4068 RtlFreeHeap(ProcessHeap
, 0, ValueData
.Buffer
);
4075 /************************************************************************
4082 RegQueryValueExW(HKEY hkeyorg
,
4091 UNICODE_STRING name_str
;
4093 char buffer
[256], *buf_ptr
= buffer
;
4094 KEY_VALUE_PARTIAL_INFORMATION
*info
= (KEY_VALUE_PARTIAL_INFORMATION
*)buffer
;
4095 static const int info_size
= offsetof( KEY_VALUE_PARTIAL_INFORMATION
, Data
);
4097 TRACE("(%p,%s,%p,%p,%p,%p=%d)\n",
4098 hkey
, debugstr_w(name
), reserved
, type
, data
, count
,
4099 (count
&& data
) ? *count
: 0 );
4101 if ((data
&& !count
) || reserved
) return ERROR_INVALID_PARAMETER
;
4103 status
= MapDefaultKey(&hkey
, hkeyorg
);
4104 if (!NT_SUCCESS(status
))
4106 return RtlNtStatusToDosError(status
);
4109 RtlInitUnicodeString( &name_str
, name
);
4111 if (data
) total_size
= min( sizeof(buffer
), *count
+ info_size
);
4114 total_size
= info_size
;
4115 if (count
) *count
= 0;
4118 /* this matches Win9x behaviour - NT sets *type to a random value */
4119 if (type
) *type
= REG_NONE
;
4121 status
= NtQueryValueKey( hkey
, &name_str
, KeyValuePartialInformation
,
4122 buffer
, total_size
, &total_size
);
4123 if (status
&& status
!= STATUS_BUFFER_OVERFLOW
) goto done
;
4127 /* retry with a dynamically allocated buffer */
4128 while (status
== STATUS_BUFFER_OVERFLOW
&& total_size
- info_size
<= *count
)
4130 if (buf_ptr
!= buffer
) HeapFree( GetProcessHeap(), 0, buf_ptr
);
4131 if (!(buf_ptr
= HeapAlloc( GetProcessHeap(), 0, total_size
)))
4133 ClosePredefKey(hkey
);
4134 return ERROR_NOT_ENOUGH_MEMORY
;
4136 info
= (KEY_VALUE_PARTIAL_INFORMATION
*)buf_ptr
;
4137 status
= NtQueryValueKey( hkey
, &name_str
, KeyValuePartialInformation
,
4138 buf_ptr
, total_size
, &total_size
);
4143 memcpy( data
, buf_ptr
+ info_size
, total_size
- info_size
);
4144 /* if the type is REG_SZ and data is not 0-terminated
4145 * and there is enough space in the buffer NT appends a \0 */
4146 if (total_size
- info_size
<= *count
-sizeof(WCHAR
) && is_string(info
->Type
))
4148 WCHAR
*ptr
= (WCHAR
*)(data
+ total_size
- info_size
);
4149 if (ptr
> (WCHAR
*)data
&& ptr
[-1]) *ptr
= 0;
4152 else if (status
!= STATUS_BUFFER_OVERFLOW
) goto done
;
4154 else status
= STATUS_SUCCESS
;
4156 if (type
) *type
= info
->Type
;
4157 if (count
) *count
= total_size
- info_size
;
4160 if (buf_ptr
!= buffer
) HeapFree( GetProcessHeap(), 0, buf_ptr
);
4161 ClosePredefKey(hkey
);
4162 return RtlNtStatusToDosError(status
);
4166 /************************************************************************
4171 LSTATUS WINAPI
RegQueryValueA( HKEY hkey
, LPCSTR name
, LPSTR data
, LPLONG count
)
4176 TRACE("(%p,%s,%p,%d)\n", hkey
, debugstr_a(name
), data
, count
? *count
: 0 );
4178 if (name
&& name
[0])
4180 if ((ret
= RegOpenKeyA( hkey
, name
, &subkey
)) != ERROR_SUCCESS
) return ret
;
4182 ret
= RegQueryValueExA( subkey
, NULL
, NULL
, NULL
, (LPBYTE
)data
, (LPDWORD
)count
);
4183 if (subkey
!= hkey
) RegCloseKey( subkey
);
4184 if (ret
== ERROR_FILE_NOT_FOUND
)
4186 /* return empty string if default value not found */
4187 if (data
) *data
= 0;
4188 if (count
) *count
= 1;
4189 ret
= ERROR_SUCCESS
;
4195 /************************************************************************
4200 LSTATUS WINAPI
RegQueryValueW( HKEY hkey
, LPCWSTR name
, LPWSTR data
, LPLONG count
)
4205 TRACE("(%p,%s,%p,%d)\n", hkey
, debugstr_w(name
), data
, count
? *count
: 0 );
4208 return ERROR_INVALID_HANDLE
;
4210 if (name
&& name
[0])
4212 ret
= RegOpenKeyW( hkey
, name
, &subkey
);
4213 if (ret
!= ERROR_SUCCESS
)
4219 ret
= RegQueryValueExW( subkey
, NULL
, NULL
, NULL
, (LPBYTE
)data
, (LPDWORD
)count
);
4223 RegCloseKey( subkey
);
4226 if (ret
== ERROR_FILE_NOT_FOUND
)
4228 /* return empty string if default value not found */
4232 *count
= sizeof(WCHAR
);
4233 ret
= ERROR_SUCCESS
;
4239 /************************************************************************
4245 RegReplaceKeyA(HKEY hKey
,
4250 UNICODE_STRING SubKey
;
4251 UNICODE_STRING NewFile
;
4252 UNICODE_STRING OldFile
;
4255 RtlCreateUnicodeStringFromAsciiz(&SubKey
,
4257 RtlCreateUnicodeStringFromAsciiz(&OldFile
,
4259 RtlCreateUnicodeStringFromAsciiz(&NewFile
,
4262 ErrorCode
= RegReplaceKeyW(hKey
,
4267 RtlFreeUnicodeString(&OldFile
);
4268 RtlFreeUnicodeString(&NewFile
);
4269 RtlFreeUnicodeString(&SubKey
);
4275 /************************************************************************
4281 RegReplaceKeyW(HKEY hKey
,
4286 OBJECT_ATTRIBUTES KeyObjectAttributes
;
4287 OBJECT_ATTRIBUTES NewObjectAttributes
;
4288 OBJECT_ATTRIBUTES OldObjectAttributes
;
4289 UNICODE_STRING SubKeyName
;
4290 UNICODE_STRING NewFileName
;
4291 UNICODE_STRING OldFileName
;
4292 BOOLEAN CloseRealKey
;
4293 HANDLE RealKeyHandle
;
4296 LONG ErrorCode
= ERROR_SUCCESS
;
4298 if (hKey
== HKEY_PERFORMANCE_DATA
)
4300 return ERROR_INVALID_HANDLE
;
4303 Status
= MapDefaultKey(&KeyHandle
,
4305 if (!NT_SUCCESS(Status
))
4307 return RtlNtStatusToDosError(Status
);
4310 /* Open the real key */
4311 if (lpSubKey
!= NULL
&& *lpSubKey
!= (WCHAR
)0)
4313 RtlInitUnicodeString(&SubKeyName
,
4315 InitializeObjectAttributes(&KeyObjectAttributes
,
4317 OBJ_CASE_INSENSITIVE
,
4320 Status
= NtOpenKey(&RealKeyHandle
,
4322 &KeyObjectAttributes
);
4323 if (!NT_SUCCESS(Status
))
4325 ErrorCode
= RtlNtStatusToDosError(Status
);
4329 CloseRealKey
= TRUE
;
4333 RealKeyHandle
= KeyHandle
;
4334 CloseRealKey
= FALSE
;
4337 /* Convert new file name */
4338 if (!RtlDosPathNameToNtPathName_U(lpNewFile
,
4345 NtClose(RealKeyHandle
);
4348 ErrorCode
= ERROR_INVALID_PARAMETER
;
4352 InitializeObjectAttributes(&NewObjectAttributes
,
4354 OBJ_CASE_INSENSITIVE
,
4358 /* Convert old file name */
4359 if (!RtlDosPathNameToNtPathName_U(lpOldFile
,
4364 RtlFreeHeap(RtlGetProcessHeap (),
4366 NewFileName
.Buffer
);
4369 NtClose(RealKeyHandle
);
4372 ErrorCode
= ERROR_INVALID_PARAMETER
;
4376 InitializeObjectAttributes(&OldObjectAttributes
,
4378 OBJ_CASE_INSENSITIVE
,
4382 Status
= NtReplaceKey(&NewObjectAttributes
,
4384 &OldObjectAttributes
);
4386 RtlFreeHeap(RtlGetProcessHeap(),
4388 OldFileName
.Buffer
);
4389 RtlFreeHeap(RtlGetProcessHeap(),
4391 NewFileName
.Buffer
);
4395 NtClose(RealKeyHandle
);
4398 if (!NT_SUCCESS(Status
))
4400 return RtlNtStatusToDosError(Status
);
4404 ClosePredefKey(KeyHandle
);
4410 /************************************************************************
4416 RegRestoreKeyA(HKEY hKey
,
4420 UNICODE_STRING FileName
;
4423 RtlCreateUnicodeStringFromAsciiz(&FileName
,
4426 ErrorCode
= RegRestoreKeyW(hKey
,
4430 RtlFreeUnicodeString(&FileName
);
4436 /************************************************************************
4442 RegRestoreKeyW(HKEY hKey
,
4446 OBJECT_ATTRIBUTES ObjectAttributes
;
4447 IO_STATUS_BLOCK IoStatusBlock
;
4448 UNICODE_STRING FileName
;
4453 if (hKey
== HKEY_PERFORMANCE_DATA
)
4455 return ERROR_INVALID_HANDLE
;
4458 Status
= MapDefaultKey(&KeyHandle
,
4460 if (!NT_SUCCESS(Status
))
4462 return RtlNtStatusToDosError(Status
);
4465 if (!RtlDosPathNameToNtPathName_U(lpFile
,
4470 Status
= STATUS_INVALID_PARAMETER
;
4474 InitializeObjectAttributes(&ObjectAttributes
,
4476 OBJ_CASE_INSENSITIVE
,
4480 Status
= NtOpenFile(&FileHandle
,
4485 FILE_SYNCHRONOUS_IO_NONALERT
);
4486 RtlFreeHeap(RtlGetProcessHeap(),
4489 if (!NT_SUCCESS(Status
))
4494 Status
= NtRestoreKey(KeyHandle
,
4497 NtClose (FileHandle
);
4500 ClosePredefKey(KeyHandle
);
4502 if (!NT_SUCCESS(Status
))
4504 return RtlNtStatusToDosError(Status
);
4507 return ERROR_SUCCESS
;
4511 /************************************************************************
4517 RegSaveKeyA(HKEY hKey
,
4519 LPSECURITY_ATTRIBUTES lpSecurityAttributes
)
4521 UNICODE_STRING FileName
;
4524 RtlCreateUnicodeStringFromAsciiz(&FileName
,
4526 ErrorCode
= RegSaveKeyW(hKey
,
4528 lpSecurityAttributes
);
4529 RtlFreeUnicodeString(&FileName
);
4535 /************************************************************************
4541 RegSaveKeyW(HKEY hKey
,
4543 LPSECURITY_ATTRIBUTES lpSecurityAttributes
)
4545 PSECURITY_DESCRIPTOR SecurityDescriptor
= NULL
;
4546 OBJECT_ATTRIBUTES ObjectAttributes
;
4547 UNICODE_STRING FileName
;
4548 IO_STATUS_BLOCK IoStatusBlock
;
4553 Status
= MapDefaultKey(&KeyHandle
,
4555 if (!NT_SUCCESS(Status
))
4557 return RtlNtStatusToDosError(Status
);
4560 if (!RtlDosPathNameToNtPathName_U(lpFile
,
4565 Status
= STATUS_INVALID_PARAMETER
;
4569 if (lpSecurityAttributes
!= NULL
)
4571 SecurityDescriptor
= lpSecurityAttributes
->lpSecurityDescriptor
;
4574 InitializeObjectAttributes(&ObjectAttributes
,
4576 OBJ_CASE_INSENSITIVE
,
4578 SecurityDescriptor
);
4579 Status
= NtCreateFile(&FileHandle
,
4580 GENERIC_WRITE
| SYNCHRONIZE
,
4584 FILE_ATTRIBUTE_NORMAL
,
4587 FILE_OPEN_FOR_BACKUP_INTENT
| FILE_SYNCHRONOUS_IO_NONALERT
,
4590 RtlFreeHeap(RtlGetProcessHeap(),
4593 if (!NT_SUCCESS(Status
))
4598 Status
= NtSaveKey(KeyHandle
,
4600 NtClose (FileHandle
);
4603 ClosePredefKey(KeyHandle
);
4605 if (!NT_SUCCESS(Status
))
4607 return RtlNtStatusToDosError(Status
);
4610 return ERROR_SUCCESS
;
4614 /************************************************************************
4621 RegSaveKeyExA(HKEY hKey
,
4623 LPSECURITY_ATTRIBUTES lpSecurityAttributes
,
4626 UNICODE_STRING FileName
;
4629 RtlCreateUnicodeStringFromAsciiz(&FileName
,
4631 ErrorCode
= RegSaveKeyExW(hKey
,
4633 lpSecurityAttributes
,
4635 RtlFreeUnicodeString(&FileName
);
4641 /************************************************************************
4648 RegSaveKeyExW(HKEY hKey
,
4650 LPSECURITY_ATTRIBUTES lpSecurityAttributes
,
4655 case REG_STANDARD_FORMAT
:
4656 case REG_LATEST_FORMAT
:
4657 case REG_NO_COMPRESSION
:
4660 return ERROR_INVALID_PARAMETER
;
4663 FIXME("RegSaveKeyExW(): Flags ignored!\n");
4665 return RegSaveKeyW(hKey
,
4667 lpSecurityAttributes
);
4671 /************************************************************************
4677 RegSetKeySecurity(HKEY hKey
,
4678 SECURITY_INFORMATION SecurityInformation
,
4679 PSECURITY_DESCRIPTOR pSecurityDescriptor
)
4684 if (hKey
== HKEY_PERFORMANCE_DATA
)
4686 return ERROR_INVALID_HANDLE
;
4689 Status
= MapDefaultKey(&KeyHandle
,
4691 if (!NT_SUCCESS(Status
))
4693 return RtlNtStatusToDosError(Status
);
4696 Status
= NtSetSecurityObject(KeyHandle
,
4697 SecurityInformation
,
4698 pSecurityDescriptor
);
4700 ClosePredefKey(KeyHandle
);
4702 if (!NT_SUCCESS(Status
))
4704 return RtlNtStatusToDosError(Status
);
4707 return ERROR_SUCCESS
;
4711 /************************************************************************
4717 RegSetValueExA(HKEY hKey
,
4724 UNICODE_STRING ValueName
;
4726 ANSI_STRING AnsiString
;
4727 UNICODE_STRING Data
;
4732 if (lpValueName
!= NULL
&&
4733 strlen(lpValueName
) != 0)
4735 RtlCreateUnicodeStringFromAsciiz(&ValueName
,
4740 ValueName
.Buffer
= NULL
;
4743 pValueName
= (LPWSTR
)ValueName
.Buffer
;
4745 if (((dwType
== REG_SZ
) ||
4746 (dwType
== REG_MULTI_SZ
) ||
4747 (dwType
== REG_EXPAND_SZ
)) &&
4750 /* NT adds one if the caller forgot the NULL-termination character */
4751 if (lpData
[cbData
- 1] != '\0')
4756 RtlInitAnsiString(&AnsiString
,
4758 AnsiString
.Buffer
= (PSTR
)lpData
;
4759 AnsiString
.Length
= cbData
- 1;
4760 AnsiString
.MaximumLength
= cbData
;
4761 RtlAnsiStringToUnicodeString(&Data
,
4764 pData
= (LPBYTE
)Data
.Buffer
;
4765 DataSize
= cbData
* sizeof(WCHAR
);
4769 RtlInitUnicodeString(&Data
,
4771 pData
= (LPBYTE
)lpData
;
4775 ErrorCode
= RegSetValueExW(hKey
,
4781 if (pValueName
!= NULL
)
4783 RtlFreeHeap(ProcessHeap
,
4788 if (Data
.Buffer
!= NULL
)
4790 RtlFreeHeap(ProcessHeap
,
4799 /************************************************************************
4805 RegSetValueExW(HKEY hKey
,
4806 LPCWSTR lpValueName
,
4812 UNICODE_STRING ValueName
;
4813 PUNICODE_STRING pValueName
;
4817 Status
= MapDefaultKey(&KeyHandle
,
4819 if (!NT_SUCCESS(Status
))
4821 return RtlNtStatusToDosError(Status
);
4824 if (lpValueName
!= NULL
)
4826 RtlInitUnicodeString(&ValueName
,
4831 RtlInitUnicodeString(&ValueName
, L
"");
4833 pValueName
= &ValueName
;
4835 if (((dwType
== REG_SZ
) ||
4836 (dwType
== REG_MULTI_SZ
) ||
4837 (dwType
== REG_EXPAND_SZ
)) &&
4838 (cbData
!= 0) && (*(((PWCHAR
)lpData
) + (cbData
/ sizeof(WCHAR
)) - 1) != L
'\0'))
4840 /* NT adds one if the caller forgot the NULL-termination character */
4841 cbData
+= sizeof(WCHAR
);
4844 Status
= NtSetValueKey(KeyHandle
,
4851 ClosePredefKey(KeyHandle
);
4853 if (!NT_SUCCESS(Status
))
4855 return RtlNtStatusToDosError(Status
);
4858 return ERROR_SUCCESS
;
4862 /************************************************************************
4868 RegSetValueA(HKEY hKeyOriginal
,
4879 TRACE("(%p,%s,%d,%s,%d)\n", hKey
, debugstr_a(lpSubKey
), dwType
, debugstr_a(lpData
), cbData
);
4881 if (dwType
!= REG_SZ
|| !lpData
) return ERROR_INVALID_PARAMETER
;
4883 Status
= MapDefaultKey(&hKey
, hKeyOriginal
);
4884 if (!NT_SUCCESS(Status
))
4886 return RtlNtStatusToDosError (Status
);
4890 if (lpSubKey
&& lpSubKey
[0]) /* need to create the subkey */
4892 ret
= RegCreateKeyA(hKey
, lpSubKey
, &subkey
);
4893 if (ret
!= ERROR_SUCCESS
)
4897 ret
= RegSetValueExA( subkey
, NULL
, 0, REG_SZ
, (const BYTE
*)lpData
, strlen(lpData
)+1 );
4899 RegCloseKey(subkey
);
4902 ClosePredefKey(hKey
);
4908 /************************************************************************
4914 RegSetValueW(HKEY hKeyOriginal
,
4925 TRACE("(%p,%s,%d,%s,%d)\n", hKeyOriginal
, debugstr_w(lpSubKey
), dwType
, debugstr_w(lpData
), cbData
);
4927 if (dwType
!= REG_SZ
|| !lpData
)
4928 return ERROR_INVALID_PARAMETER
;
4930 Status
= MapDefaultKey(&hKey
,
4932 if (!NT_SUCCESS(Status
))
4934 return RtlNtStatusToDosError(Status
);
4938 if (lpSubKey
&& lpSubKey
[0]) /* need to create the subkey */
4940 ret
= RegCreateKeyW(hKey
, lpSubKey
, &subkey
);
4941 if (ret
!= ERROR_SUCCESS
)
4945 ret
= RegSetValueExW( subkey
, NULL
, 0, REG_SZ
, (const BYTE
*)lpData
,
4946 (wcslen( lpData
) + 1) * sizeof(WCHAR
) );
4948 RegCloseKey(subkey
);
4951 ClosePredefKey(hKey
);
4957 /************************************************************************
4963 RegUnLoadKeyA(HKEY hKey
,
4966 UNICODE_STRING KeyName
;
4969 RtlCreateUnicodeStringFromAsciiz(&KeyName
,
4972 ErrorCode
= RegUnLoadKeyW(hKey
,
4975 RtlFreeUnicodeString (&KeyName
);
4981 /************************************************************************
4987 RegUnLoadKeyW(HKEY hKey
,
4990 OBJECT_ATTRIBUTES ObjectAttributes
;
4991 UNICODE_STRING KeyName
;
4995 if (hKey
== HKEY_PERFORMANCE_DATA
)
4997 return ERROR_INVALID_HANDLE
;
5000 Status
= MapDefaultKey(&KeyHandle
, hKey
);
5001 if (!NT_SUCCESS(Status
))
5003 return RtlNtStatusToDosError(Status
);
5006 RtlInitUnicodeString(&KeyName
,
5009 InitializeObjectAttributes(&ObjectAttributes
,
5011 OBJ_CASE_INSENSITIVE
,
5015 Status
= NtUnloadKey(&ObjectAttributes
);
5017 ClosePredefKey(KeyHandle
);
5019 if (!NT_SUCCESS(Status
))
5021 return RtlNtStatusToDosError(Status
);
5024 return ERROR_SUCCESS
;
5028 /******************************************************************************
5029 * load_string [Internal]
5031 * This is basically a copy of user32/resource.c's LoadStringW. Necessary to
5032 * avoid importing user32, which is higher level than advapi32. Helper for
5035 static int load_string(HINSTANCE hModule
, UINT resId
, LPWSTR pwszBuffer
, INT cMaxChars
)
5042 /* Negative values have to be inverted. */
5043 if (HIWORD(resId
) == 0xffff)
5044 resId
= (UINT
)(-((INT
)resId
));
5046 /* Load the resource into memory and get a pointer to it. */
5047 hResource
= FindResourceW(hModule
, MAKEINTRESOURCEW(LOWORD(resId
>> 4) + 1), (LPWSTR
)RT_STRING
);
5048 if (!hResource
) return 0;
5049 hMemory
= LoadResource(hModule
, hResource
);
5050 if (!hMemory
) return 0;
5051 pString
= LockResource(hMemory
);
5053 /* Strings are length-prefixed. Lowest nibble of resId is an index. */
5054 idxString
= resId
& 0xf;
5055 while (idxString
--) pString
+= *pString
+ 1;
5057 /* If no buffer is given, return length of the string. */
5058 if (!pwszBuffer
) return *pString
;
5060 /* Else copy over the string, respecting the buffer size. */
5061 cMaxChars
= (*pString
< cMaxChars
) ? *pString
: (cMaxChars
- 1);
5064 memcpy(pwszBuffer
, pString
+1, cMaxChars
* sizeof(WCHAR
));
5065 pwszBuffer
[cMaxChars
] = L
'\0';
5072 /************************************************************************
5078 RegLoadMUIStringW(IN HKEY hKey
,
5079 IN LPCWSTR pszValue OPTIONAL
,
5080 OUT LPWSTR pszOutBuf
,
5082 OUT LPDWORD pcbData OPTIONAL
,
5084 IN LPCWSTR pszDirectory OPTIONAL
)
5086 DWORD dwValueType
, cbData
;
5087 LPWSTR pwszTempBuffer
= NULL
, pwszExpandedBuffer
= NULL
;
5090 /* Parameter sanity checks. */
5091 if (!hKey
|| !pszOutBuf
)
5092 return ERROR_INVALID_PARAMETER
;
5094 if (pszDirectory
&& *pszDirectory
)
5096 FIXME("BaseDir parameter not yet supported!\n");
5097 return ERROR_INVALID_PARAMETER
;
5100 /* Check for value existence and correctness of it's type, allocate a buffer and load it. */
5101 result
= RegQueryValueExW(hKey
, pszValue
, NULL
, &dwValueType
, NULL
, &cbData
);
5102 if (result
!= ERROR_SUCCESS
) goto cleanup
;
5103 if (!(dwValueType
== REG_SZ
|| dwValueType
== REG_EXPAND_SZ
) || !cbData
)
5105 result
= ERROR_FILE_NOT_FOUND
;
5108 pwszTempBuffer
= HeapAlloc(GetProcessHeap(), 0, cbData
);
5109 if (!pwszTempBuffer
)
5111 result
= ERROR_NOT_ENOUGH_MEMORY
;
5114 result
= RegQueryValueExW(hKey
, pszValue
, NULL
, &dwValueType
, (LPBYTE
)pwszTempBuffer
, &cbData
);
5115 if (result
!= ERROR_SUCCESS
) goto cleanup
;
5117 /* Expand environment variables, if appropriate, or copy the original string over. */
5118 if (dwValueType
== REG_EXPAND_SZ
)
5120 cbData
= ExpandEnvironmentStringsW(pwszTempBuffer
, NULL
, 0) * sizeof(WCHAR
);
5121 if (!cbData
) goto cleanup
;
5122 pwszExpandedBuffer
= HeapAlloc(GetProcessHeap(), 0, cbData
);
5123 if (!pwszExpandedBuffer
)
5125 result
= ERROR_NOT_ENOUGH_MEMORY
;
5128 ExpandEnvironmentStringsW(pwszTempBuffer
, pwszExpandedBuffer
, cbData
);
5132 pwszExpandedBuffer
= HeapAlloc(GetProcessHeap(), 0, cbData
);
5133 memcpy(pwszExpandedBuffer
, pwszTempBuffer
, cbData
);
5136 /* If the value references a resource based string, parse the value and load the string.
5137 * Else just copy over the original value. */
5138 result
= ERROR_SUCCESS
;
5139 if (*pwszExpandedBuffer
!= L
'@') /* '@' is the prefix for resource based string entries. */
5141 lstrcpynW(pszOutBuf
, pwszExpandedBuffer
, cbOutBuf
/ sizeof(WCHAR
));
5145 WCHAR
*pComma
= wcsrchr(pwszExpandedBuffer
, L
',');
5149 /* Format of the expanded value is 'path_to_dll,-resId' */
5150 if (!pComma
|| pComma
[1] != L
'-')
5152 result
= ERROR_BADKEY
;
5156 uiStringId
= _wtoi(pComma
+2);
5159 hModule
= LoadLibraryExW(pwszExpandedBuffer
+ 1, NULL
, LOAD_LIBRARY_AS_DATAFILE
);
5160 if (!hModule
|| !load_string(hModule
, uiStringId
, pszOutBuf
, cbOutBuf
/ sizeof(WCHAR
)))
5161 result
= ERROR_BADKEY
;
5162 FreeLibrary(hModule
);
5166 HeapFree(GetProcessHeap(), 0, pwszTempBuffer
);
5167 HeapFree(GetProcessHeap(), 0, pwszExpandedBuffer
);
5172 /************************************************************************
5178 RegLoadMUIStringA(IN HKEY hKey
,
5179 IN LPCSTR pszValue OPTIONAL
,
5180 OUT LPSTR pszOutBuf
,
5182 OUT LPDWORD pcbData OPTIONAL
,
5184 IN LPCSTR pszDirectory OPTIONAL
)
5186 UNICODE_STRING valueW
, baseDirW
;
5188 DWORD cbData
= cbOutBuf
* sizeof(WCHAR
);
5191 valueW
.Buffer
= baseDirW
.Buffer
= pwszBuffer
= NULL
;
5192 if (!RtlCreateUnicodeStringFromAsciiz(&valueW
, pszValue
) ||
5193 !RtlCreateUnicodeStringFromAsciiz(&baseDirW
, pszDirectory
) ||
5194 !(pwszBuffer
= HeapAlloc(GetProcessHeap(), 0, cbData
)))
5196 result
= ERROR_NOT_ENOUGH_MEMORY
;
5200 result
= RegLoadMUIStringW(hKey
, valueW
.Buffer
, pwszBuffer
, cbData
, NULL
, Flags
,
5203 if (result
== ERROR_SUCCESS
)
5205 cbData
= WideCharToMultiByte(CP_ACP
, 0, pwszBuffer
, -1, pszOutBuf
, cbOutBuf
, NULL
, NULL
);
5211 HeapFree(GetProcessHeap(), 0, pwszBuffer
);
5212 RtlFreeUnicodeString(&baseDirW
);
5213 RtlFreeUnicodeString(&valueW
);