2 * COPYRIGHT: GPL, see COPYING in the top level directory
3 * PROJECT: ReactOS win32 kernel mode subsystem server
4 * PURPOSE: Registry loading and storing
5 * FILE: subsystem/win32/win32k/misc/registry.c
6 * PROGRAMER: Timo Kreuzer (timo.kreuzer@reactos.org)
21 OBJECT_ATTRIBUTES ObjectAttributes
;
22 UNICODE_STRING ustrKeyName
;
25 /* Initialize the key name */
26 RtlInitUnicodeString(&ustrKeyName
, pwszKeyName
);
28 /* Initialize object attributes */
29 InitializeObjectAttributes(&ObjectAttributes
,
36 Status
= ZwOpenKey(&hkey
, KEY_READ
, &ObjectAttributes
);
37 if (NT_SUCCESS(Status
))
49 IN PCWSTR pwszValueName
,
52 IN OUT PULONG pcbValue
)
55 UNICODE_STRING ustrValueName
;
57 PKEY_VALUE_PARTIAL_INFORMATION pInfo
;
58 ULONG cbInfoSize
, cbDataSize
;
60 /* Check if the local buffer is sufficient */
61 cbInfoSize
= sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + *pcbValue
;
62 if (cbInfoSize
<= sizeof(ajBuffer
))
64 pInfo
= (PVOID
)ajBuffer
;
68 /* It's not, allocate a sufficient buffer */
69 pInfo
= ExAllocatePoolWithTag(PagedPool
, cbInfoSize
, TAG_TEMP
);
72 return STATUS_INSUFFICIENT_RESOURCES
;
77 RtlInitUnicodeString(&ustrValueName
, pwszValueName
);
78 Status
= ZwQueryValueKey(hkey
,
80 KeyValuePartialInformation
,
85 cbDataSize
= cbInfoSize
- FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION
, Data
);
87 if (NT_SUCCESS(Status
))
89 /* Did we get the right type */
90 if (pInfo
->Type
== ulType
)
92 /* Copy the contents to the caller */
93 RtlCopyMemory(pvData
, pInfo
->Data
, min(*pcbValue
, cbDataSize
));
96 Status
= STATUS_OBJECT_TYPE_MISMATCH
;
99 /* Return the data size to the caller */
100 *pcbValue
= cbDataSize
;
103 if (pInfo
!= (PVOID
)ajBuffer
)
104 ExFreePoolWithTag(pInfo
, TAG_TEMP
);
112 RegWriteSZ(HKEY hkey
, PWSTR pwszValue
, PWSTR pwszData
)
114 UNICODE_STRING ustrValue
;
115 UNICODE_STRING ustrData
;
117 RtlInitUnicodeString(&ustrValue
, pwszValue
);
118 RtlInitUnicodeString(&ustrData
, pwszData
);
119 ZwSetValueKey(hkey
, &ustrValue
, 0, REG_SZ
, &ustrData
, ustrData
.Length
+ sizeof(WCHAR
));
124 RegWriteDWORD(HKEY hkey
, PWSTR pwszValue
, DWORD dwData
)
126 UNICODE_STRING ustrValue
;
128 RtlInitUnicodeString(&ustrValue
, pwszValue
);
129 ZwSetValueKey(hkey
, &ustrValue
, 0, REG_DWORD
, &dwData
, sizeof(DWORD
));
134 RegReadDWORD(HKEY hkey
, PWSTR pwszValue
, PDWORD pdwData
)
137 ULONG cbSize
= sizeof(DWORD
);
138 Status
= RegQueryValue(hkey
, pwszValue
, REG_DWORD
, pdwData
, &cbSize
);
139 return NT_SUCCESS(Status
);
145 IN PCWSTR pwszKeyName
,
146 IN PCWSTR pwszValueName
,
152 OBJECT_ATTRIBUTES ObjectAttributes
;
153 UNICODE_STRING usCurrentUserKey
, usKeyName
, usValueName
;
154 WCHAR awcBuffer
[MAX_PATH
];
156 PKEY_VALUE_PARTIAL_INFORMATION pInfo
;
157 ULONG cbInfoSize
, cbReqSize
;
159 /* Get the path of the current user's profile */
160 Status
= RtlFormatCurrentUserKeyPath(&usCurrentUserKey
);
161 if (!NT_SUCCESS(Status
))
166 /* Initialize empty key name */
167 RtlInitEmptyUnicodeString(&usKeyName
, awcBuffer
, sizeof(awcBuffer
));
169 /* Append the current user key name */
170 Status
= RtlAppendUnicodeStringToString(&usKeyName
, &usCurrentUserKey
);
172 /* Free the current user key name */
173 RtlFreeUnicodeString(&usCurrentUserKey
);
175 /* Check for success */
176 if (!NT_SUCCESS(Status
))
181 /* Append a '\', we can trust in enough space left. */
182 usKeyName
.Buffer
[usKeyName
.Length
/ sizeof(WCHAR
)] = '\\';
183 usKeyName
.Length
+= sizeof(WCHAR
);
185 /* Append the subkey name */
186 Status
= RtlAppendUnicodeToString(&usKeyName
, pwszKeyName
);
187 if (!NT_SUCCESS(Status
))
192 /* Initialize object attributes */
193 InitializeObjectAttributes(&ObjectAttributes
,
195 OBJ_CASE_INSENSITIVE
,
200 Status
= ZwOpenKey(&hkey
, KEY_READ
, &ObjectAttributes
);
201 if (!NT_SUCCESS(Status
))
206 /* Check if the local buffer is sufficient */
207 cbInfoSize
= sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + cbDataSize
;
208 if (cbInfoSize
<= sizeof(awcBuffer
))
210 pInfo
= (PVOID
)awcBuffer
;
214 /* It's not, allocate a sufficient buffer */
215 pInfo
= ExAllocatePoolWithTag(PagedPool
, cbInfoSize
, TAG_TEMP
);
223 /* Query the value */
224 RtlInitUnicodeString(&usValueName
, pwszValueName
);
225 Status
= ZwQueryValueKey(hkey
,
227 KeyValuePartialInformation
,
231 if (NT_SUCCESS(Status
))
233 /* Did we get the right type */
234 if (pInfo
->Type
== ulType
)
236 /* Copy the contents to the caller */
237 RtlCopyMemory(pvData
, pInfo
->Data
, cbDataSize
);
243 if (pInfo
!= (PVOID
)awcBuffer
)
244 ExFreePoolWithTag(pInfo
, TAG_TEMP
);
246 return NT_SUCCESS(Status
);
252 IN PCWSTR pwszKeyName
,
253 IN PCWSTR pwszValueName
,
259 OBJECT_ATTRIBUTES ObjectAttributes
;
260 UNICODE_STRING usCurrentUserKey
, usKeyName
, usValueName
;
261 WCHAR awcBuffer
[MAX_PATH
];
264 // FIXME: logged in user versus current process user?
265 /* Get the path of the current user's profile */
266 Status
= RtlFormatCurrentUserKeyPath(&usCurrentUserKey
);
267 if (!NT_SUCCESS(Status
))
269 DPRINT1("RtlFormatCurrentUserKeyPath failed\n");
273 /* Initialize empty key name */
274 RtlInitEmptyUnicodeString(&usKeyName
, awcBuffer
, sizeof(awcBuffer
));
276 /* Append the current user key name */
277 Status
= RtlAppendUnicodeStringToString(&usKeyName
, &usCurrentUserKey
);
278 if (!NT_SUCCESS(Status
))
283 /* Free the current user key name */
284 RtlFreeUnicodeString(&usCurrentUserKey
);
286 /* Append a '\', we can trust in enough space left. */
287 usKeyName
.Buffer
[usKeyName
.Length
/ sizeof(WCHAR
)] = '\\';
288 usKeyName
.Length
+= sizeof(WCHAR
);
290 /* Append the subkey name */
291 Status
= RtlAppendUnicodeToString(&usKeyName
, pwszKeyName
);
292 if (!NT_SUCCESS(Status
))
294 DPRINT1("RtlAppendUnicodeToString failed with Status=%lx, buf:%d,%d\n",
295 Status
, usKeyName
.Length
, usKeyName
.MaximumLength
);
299 /* Initialize object attributes */
300 InitializeObjectAttributes(&ObjectAttributes
,
302 OBJ_CASE_INSENSITIVE
,
306 /* Open or create the key */
307 Status
= ZwCreateKey(&hkey
,
308 KEY_READ
| KEY_WRITE
,
314 if(!NT_SUCCESS(Status
))
316 DPRINT1("Failed to create key: 0x%x\n", Status
);
320 /* Initialize the value name string */
321 RtlInitUnicodeString(&usValueName
, pwszValueName
);
323 Status
= ZwSetValueKey(hkey
, &usValueName
, 0, ulType
, pvData
, cbDataSize
);
324 if(!NT_SUCCESS(Status
))
326 DPRINT1("Failed to write reg key '%S' value '%S', Status = %lx\n",
327 pwszKeyName
, pwszValueName
, Status
);
333 return NT_SUCCESS(Status
);