ReactOS 0.4.15-dev-7953-g1f49173
registry.c File Reference
#include <win32k.h>
#include <debug.h>
Include dependency graph for registry.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

NTSTATUS NTAPI RegOpenKey (LPCWSTR pwszKeyName, PHKEY phkey)
 
NTSTATUS NTAPI RegQueryValue (IN HKEY hkey, IN PCWSTR pwszValueName, IN ULONG ulType, OUT PVOID pvData, IN OUT PULONG pcbValue)
 
VOID NTAPI RegWriteSZ (HKEY hkey, PCWSTR pwszValue, PWSTR pwszData)
 
VOID NTAPI RegWriteDWORD (HKEY hkey, PCWSTR pwszValue, DWORD dwData)
 
BOOL NTAPI RegReadDWORD (HKEY hkey, PCWSTR pwszValue, PDWORD pdwData)
 
NTSTATUS NTAPI RegOpenSectionKey (LPCWSTR pszSection, PHKEY phkey)
 
DWORD NTAPI RegGetSectionDWORD (LPCWSTR pszSection, PCWSTR pszValue, DWORD dwDefault)
 
 _Success_ (return!=FALSE)
 
 _Success_ (return !=FALSE)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 11 of file registry.c.

Function Documentation

◆ _Success_() [1/2]

_Success_ ( return = FALSE)

Definition at line 295 of file registry.c.

304{
307 UNICODE_STRING usCurrentUserKey, usKeyName, usValueName;
308 WCHAR awcBuffer[MAX_PATH];
309 HKEY hkey;
310
311 // FIXME: Logged in user versus current process user?
312 /* Get the path of the current user's profile */
313 Status = RtlFormatCurrentUserKeyPath(&usCurrentUserKey);
314 if (!NT_SUCCESS(Status))
315 {
316 DPRINT1("RtlFormatCurrentUserKeyPath failed\n");
317 return FALSE;
318 }
319
320 /* Initialize empty key name */
321 RtlInitEmptyUnicodeString(&usKeyName, awcBuffer, sizeof(awcBuffer));
322
323 /* Append the current user key name */
324 Status = RtlAppendUnicodeStringToString(&usKeyName, &usCurrentUserKey);
325 if (!NT_SUCCESS(Status))
326 {
327 return FALSE;
328 }
329
330 /* Free the current user key name */
331 RtlFreeUnicodeString(&usCurrentUserKey);
332
333 /* Append a '\', we can trust in enough space left. */
334 usKeyName.Buffer[usKeyName.Length / sizeof(WCHAR)] = '\\';
335 usKeyName.Length += sizeof(WCHAR);
336
337 /* Append the subkey name */
338 Status = RtlAppendUnicodeToString(&usKeyName, pwszKeyName);
339 if (!NT_SUCCESS(Status))
340 {
341 DPRINT1("RtlAppendUnicodeToString failed with Status=0x%lx, buf:%u,%u\n",
342 Status, usKeyName.Length, usKeyName.MaximumLength);
343 return FALSE;
344 }
345
346 /* Initialize object attributes */
348 &usKeyName,
350 NULL,
351 NULL);
352
353 /* Open or create the key */
354 Status = ZwCreateKey((PHANDLE)&hkey,
357 0,
358 NULL,
359 0,
360 NULL);
361 if(!NT_SUCCESS(Status))
362 {
363 DPRINT1("Failed to create key: 0x%x\n", Status);
364 return FALSE;
365 }
366
367 /* Initialize the value name string */
368 RtlInitUnicodeString(&usValueName, pwszValueName);
369
370 Status = ZwSetValueKey(hkey, &usValueName, 0, ulType, (PVOID)pvData, cbDataSize);
371 if(!NT_SUCCESS(Status))
372 {
373 DPRINT1("Failed to write reg key '%S' value '%S', Status = %lx\n",
374 pwszKeyName, pwszValueName, Status);
375 }
376
377 /* Cleanup */
378 ZwClose(hkey);
379
380 return NT_SUCCESS(Status);
381}
LONG NTSTATUS
Definition: precomp.h:26
#define DPRINT1
Definition: precomp.h:8
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define MAX_PATH
Definition: compat.h:34
NTSTATUS RtlAppendUnicodeToString(IN PUNICODE_STRING Str1, IN PWSTR Str2)
Definition: string_lib.cpp:62
Status
Definition: gdiplustypes.h:25
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
NTSYSAPI NTSTATUS NTAPI RtlAppendUnicodeStringToString(PUNICODE_STRING Destination, PUNICODE_STRING Source)
#define KEY_READ
Definition: nt_native.h:1023
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
NTSYSAPI VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)
#define KEY_WRITE
Definition: nt_native.h:1031
PVOID *typedef PHANDLE
Definition: ntsecpkg.h:455
NTSTATUS NTAPI RtlFormatCurrentUserKeyPath(OUT PUNICODE_STRING KeyPath)
Definition: registry.c:742
USHORT MaximumLength
Definition: env_spec_w32.h:370
_In_z_ PCWSTR pwszValueName
Definition: ntuser.h:42
_In_z_ PCWSTR _In_ ULONG ulType
Definition: ntuser.h:43
_In_ ULONG _In_opt_ PVOID pvData
Definition: winddi.h:3749
__wchar_t WCHAR
Definition: xmlstorage.h:180

◆ _Success_() [2/2]

_Success_ ( return!  = FALSE)

Definition at line 187 of file registry.c.

196{
199 UNICODE_STRING usCurrentUserKey, usKeyName, usValueName;
200 WCHAR awcBuffer[MAX_PATH];
201 HKEY hkey;
203 ULONG cbInfoSize, cbReqSize;
204
205 /* Get the path of the current user's profile */
206 Status = RtlFormatCurrentUserKeyPath(&usCurrentUserKey);
207 if (!NT_SUCCESS(Status))
208 {
209 return FALSE;
210 }
211
212 /* Initialize empty key name */
213 RtlInitEmptyUnicodeString(&usKeyName, awcBuffer, sizeof(awcBuffer));
214
215 /* Append the current user key name */
216 Status = RtlAppendUnicodeStringToString(&usKeyName, &usCurrentUserKey);
217
218 /* Free the current user key name */
219 RtlFreeUnicodeString(&usCurrentUserKey);
220
221 /* Check for success */
222 if (!NT_SUCCESS(Status))
223 {
224 return FALSE;
225 }
226
227 /* Append a '\', we can trust in enough space left. */
228 usKeyName.Buffer[usKeyName.Length / sizeof(WCHAR)] = '\\';
229 usKeyName.Length += sizeof(WCHAR);
230
231 /* Append the subkey name */
232 Status = RtlAppendUnicodeToString(&usKeyName, pwszKeyName);
233 if (!NT_SUCCESS(Status))
234 {
235 return FALSE;
236 }
237
238 /* Initialize object attributes */
240 &usKeyName,
242 NULL,
243 NULL);
244
245 /* Open the key */
246 Status = ZwOpenKey((PHANDLE)&hkey, KEY_READ, &ObjectAttributes);
247 if (!NT_SUCCESS(Status))
248 {
249 return FALSE;
250 }
251
252 /* Check if the local buffer is sufficient */
253 cbInfoSize = sizeof(KEY_VALUE_PARTIAL_INFORMATION) + cbDataSize;
254 if (cbInfoSize <= sizeof(awcBuffer))
255 {
256 pInfo = (PVOID)awcBuffer;
257 }
258 else
259 {
260 /* It's not, allocate a sufficient buffer */
261 pInfo = ExAllocatePoolWithTag(PagedPool, cbInfoSize, TAG_TEMP);
262 if (!pInfo)
263 {
264 ZwClose(hkey);
265 return FALSE;
266 }
267 }
268
269 /* Query the value */
270 RtlInitUnicodeString(&usValueName, pwszValueName);
271 Status = ZwQueryValueKey(hkey,
272 &usValueName,
274 (PVOID)pInfo,
275 cbInfoSize,
276 &cbReqSize);
277 if (NT_SUCCESS(Status))
278 {
279 /* Did we get the right type */
280 if (pInfo->Type == ulType)
281 {
282 /* Copy the contents to the caller */
283 RtlCopyMemory(pvData, pInfo->Data, cbDataSize);
284 }
285 }
286
287 /* Cleanup */
288 ZwClose(hkey);
289 if (pInfo != (PVOID)awcBuffer)
291
292 return NT_SUCCESS(Status);
293}
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define PagedPool
Definition: env_spec_w32.h:308
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1109
@ KeyValuePartialInformation
Definition: nt_native.h:1182
struct _KEY_VALUE_PARTIAL_INFORMATION KEY_VALUE_PARTIAL_INFORMATION
void * PVOID
Definition: typedefs.h:50
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
uint32_t ULONG
Definition: typedefs.h:59
#define TAG_TEMP
Definition: tags.h:43

◆ RegGetSectionDWORD()

DWORD NTAPI RegGetSectionDWORD ( LPCWSTR  pszSection,
PCWSTR  pszValue,
DWORD  dwDefault 
)

Definition at line 172 of file registry.c.

173{
174 HKEY hKey;
175 DWORD dwValue;
176
177 if (NT_ERROR(RegOpenSectionKey(pszSection, &hKey)))
178 return dwDefault;
179
180 if (!RegReadDWORD(hKey, pszValue, &dwValue))
181 dwValue = dwDefault;
182
183 ZwClose(hKey);
184 return dwValue;
185}
unsigned long DWORD
Definition: ntddk_ex.h:95
FxAutoRegKey hKey
#define NT_ERROR(Status)
Definition: umtypes.h:106
NTSTATUS NTAPI RegOpenSectionKey(LPCWSTR pszSection, PHKEY phkey)
Definition: registry.c:159
BOOL NTAPI RegReadDWORD(HKEY hkey, PCWSTR pwszValue, PDWORD pdwData)
Definition: registry.c:149

Referenced by UserIsCiceroEnabled(), and UserIsIMMEnabled().

◆ RegOpenKey()

NTSTATUS NTAPI RegOpenKey ( LPCWSTR  pwszKeyName,
PHKEY  phkey 
)

Definition at line 16 of file registry.c.

19{
22 UNICODE_STRING ustrKeyName;
23 HKEY hkey;
24
25 /* Initialize the key name */
26 RtlInitUnicodeString(&ustrKeyName, pwszKeyName);
27
28 /* Initialize object attributes */
30 &ustrKeyName,
32 NULL,
33 NULL);
34
35 /* Open the key */
36 Status = ZwOpenKey((PHANDLE)&hkey, KEY_READ, &ObjectAttributes);
37 if (NT_SUCCESS(Status))
38 {
39 *phkey = hkey;
40 }
41
42 return Status;
43}

◆ RegOpenSectionKey()

NTSTATUS NTAPI RegOpenSectionKey ( LPCWSTR  pszSection,
PHKEY  phkey 
)

Definition at line 159 of file registry.c.

162{
163 WCHAR szKey[MAX_PATH] =
164 L"\\Registry\\Machine\\Software\\Microsoft\\Windows NT\\CurrentVersion\\";
165
166 RtlStringCchCatW(szKey, _countof(szKey), pszSection);
167 return RegOpenKey(szKey, phkey);
168}
NTSTRSAFEAPI RtlStringCchCatW(_Inout_updates_(cchDest) _Always_(_Post_z_) NTSTRSAFE_PWSTR pszDest, _In_ size_t cchDest, _In_ NTSTRSAFE_PCWSTR pszSrc)
Definition: ntstrsafe.h:601
#define L(x)
Definition: ntvdm.h:50
#define _countof(array)
Definition: sndvol32.h:68
#define RegOpenKey
Definition: winreg.h:519

Referenced by RegGetSectionDWORD().

◆ RegQueryValue()

NTSTATUS NTAPI RegQueryValue ( IN HKEY  hkey,
IN PCWSTR  pwszValueName,
IN ULONG  ulType,
OUT PVOID  pvData,
IN OUT PULONG  pcbValue 
)

Definition at line 47 of file registry.c.

53{
55 UNICODE_STRING ustrValueName;
56 BYTE ajBuffer[100];
58 ULONG cbInfoSize, cbDataSize;
59
60 /* Check if the local buffer is sufficient */
61 cbInfoSize = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data[*pcbValue]);
62 if (cbInfoSize <= sizeof(ajBuffer))
63 {
64 pInfo = (PVOID)ajBuffer;
65 }
66 else
67 {
68 /* It's not, allocate a sufficient buffer */
69 pInfo = ExAllocatePoolWithTag(PagedPool, cbInfoSize, TAG_TEMP);
70 if (!pInfo)
71 {
73 }
74 }
75
76 /* Query the value */
77 RtlInitUnicodeString(&ustrValueName, pwszValueName);
78 Status = ZwQueryValueKey(hkey,
79 &ustrValueName,
81 (PVOID)pInfo,
82 cbInfoSize,
83 &cbInfoSize);
84
85 /* Note: STATUS_BUFFER_OVERFLOW is not a success */
86 if (NT_SUCCESS(Status))
87 {
88 cbDataSize = pInfo->DataLength;
89
90 /* Did we get the right type */
91 if (pInfo->Type != ulType)
92 {
94 }
95 else if (cbDataSize > *pcbValue)
96 {
98 }
99 else
100 {
101 /* Copy the contents to the caller */
102 RtlCopyMemory(pvData, pInfo->Data, cbDataSize);
103 }
104 }
106 {
107 _PRAGMA_WARNING_SUPPRESS(6102); /* cbInfoSize is initialized here! */
108 cbDataSize = cbInfoSize - FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data);
109 }
110 else
111 {
112 cbDataSize = 0;
113 }
114
115 /* Return the data size to the caller */
116 *pcbValue = cbDataSize;
117
118 /* Cleanup */
119 if (pInfo != (PVOID)ajBuffer)
121
122 return Status;
123}
#define STATUS_OBJECT_TYPE_MISMATCH
Definition: ntstatus.h:273
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:69
#define STATUS_BUFFER_OVERFLOW
Definition: shellext.h:66
#define _PRAGMA_WARNING_SUPPRESS(x)
Definition: suppress.h:28
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:255
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
unsigned char BYTE
Definition: xxhash.c:193

◆ RegReadDWORD()

BOOL NTAPI RegReadDWORD ( HKEY  hkey,
PCWSTR  pwszValue,
PDWORD  pdwData 
)

Definition at line 149 of file registry.c.

150{
152 ULONG cbSize = sizeof(DWORD);
153 Status = RegQueryValue(hkey, pwszValue, REG_DWORD, pdwData, &cbSize);
154 return NT_SUCCESS(Status);
155}
#define DWORD
Definition: nt_native.h:44
#define REG_DWORD
Definition: sdbapi.c:596
#define RegQueryValue
Definition: winreg.h:523

Referenced by EngpUpdateGraphicsDeviceList(), InitMetrics(), InitUserImpl(), and RegGetSectionDWORD().

◆ RegWriteDWORD()

VOID NTAPI RegWriteDWORD ( HKEY  hkey,
PCWSTR  pwszValue,
DWORD  dwData 
)

Definition at line 139 of file registry.c.

140{
141 UNICODE_STRING ustrValue;
142
143 RtlInitUnicodeString(&ustrValue, pwszValue);
144 ZwSetValueKey(hkey, &ustrValue, 0, REG_DWORD, &dwData, sizeof(DWORD));
145}
static HANDLE ULONG_PTR dwData
Definition: file.c:35

Referenced by RegWriteDisplaySettings().

◆ RegWriteSZ()

VOID NTAPI RegWriteSZ ( HKEY  hkey,
PCWSTR  pwszValue,
PWSTR  pwszData 
)

Definition at line 127 of file registry.c.

128{
129 UNICODE_STRING ustrValue;
130 UNICODE_STRING ustrData;
131
132 RtlInitUnicodeString(&ustrValue, pwszValue);
133 RtlInitUnicodeString(&ustrData, pwszData);
134 ZwSetValueKey(hkey, &ustrValue, 0, REG_SZ, &ustrData, ustrData.Length + sizeof(WCHAR));
135}
#define REG_SZ
Definition: layer.c:22