ReactOS  0.4.14-dev-376-gaedba84
regtools.cpp
Go to the documentation of this file.
1 // Copyright (C) Alexander Telyatnikov, Ivan Keliukh, Yegor Anchishkin, SKIF Software, 1999-2013. Kiev, Ukraine
3 // All rights reserved
4 // This file was released under the GPLv2 on June 2015.
6 
7 #include "regtools.h"
8 
9 #ifndef WIN_32_MODE
10 
13  IN HKEY hRootKey,
15  OUT HKEY* hKey
16  )
17 {
19  UNICODE_STRING NameString;
21 
22  //UDFPrint(("RegTGetKeyHandle: h=%x, %S\n", hRootKey, KeyName));
23 
24  RtlInitUnicodeString(&NameString, KeyName);
25 
28  &NameString,
30  hRootKey,
31  NULL
32  );
33 
34  status = ZwOpenKey(
35  hKey,
38  );
39 
40  if(!NT_SUCCESS(status)) {
41  //UDFPrint((" status %x\n", status));
42  *hKey = NULL;
43  }
44 
45  return status;
46 } // end RegTGetKeyHandle()
47 
48 VOID
50  IN HKEY hKey
51 )
52 {
53  ZwClose(hKey);
54 } // end RegTCloseKeyHandle()
55 
56 #else //WIN_32_MODE
57 
60  IN HKEY hRootKey,
62  OUT HKEY* hKey
63  )
64 {
65  LONG status;
66 
67  if(!hRootKey)
68  hRootKey = HKEY_LOCAL_MACHINE;
69 
71  hRootKey,
72  KeyName,
73  0,
75  hKey
76  );
77 
78  if(status != ERROR_SUCCESS) {
79  *hKey = NULL;
80  }
81 
82  return status;
83 } // end RegTGetKeyHandle()
84 
85 VOID
87  IN HKEY hKey
88 )
89 {
90  if(!hKey) {
91  return;
92  }
93  RegCloseKey(hKey);
94 } // end RegTCloseKeyHandle()
95 
96 #endif //WIN_32_MODE
97 
98 BOOLEAN
100  IN HKEY hRootKey,
102  IN PCWSTR Name,
103  IN PULONG pUlong
104  )
105 {
106 #ifndef WIN_32_MODE
107  UNICODE_STRING NameString;
109 #endif //WIN_32_MODE
110  ULONG len;
112  HKEY hKey;
113  BOOLEAN retval = FALSE;
114  BOOLEAN free_h = FALSE;
115 
116 #ifdef WIN_32_MODE
117  if(!hRootKey)
118  hRootKey = HKEY_LOCAL_MACHINE;
119 #endif //WIN_32_MODE
120 
121  if(RegistryPath && RegistryPath[0]) {
122  status = RegTGetKeyHandle(hRootKey, RegistryPath, &hKey);
123 #ifdef WIN_32_MODE
124  if(status != ERROR_SUCCESS)
125 #else //WIN_32_MODE
126  if(!NT_SUCCESS(status))
127 #endif //WIN_32_MODE
128  return FALSE;
129  free_h = TRUE;
130  } else {
131  hKey = hRootKey;
132  }
133  if(!hKey)
134  return FALSE;
135 
136 #ifndef WIN_32_MODE
137 /*
138  UDFPrint(("h=%x|%S, %S (%x)\n",
139  hRootKey, RegistryPath, Name, *pUlong));
140 */
141  len = sizeof(KEY_VALUE_PARTIAL_INFORMATION) + sizeof(ULONG) + 0x20;
144  if(!ValInfo) {
145  if(free_h) {
146  RegTCloseKeyHandle(hKey);
147  }
148  return FALSE;
149  }
150 
151  RtlInitUnicodeString(&NameString, Name);
152 
153  status = ZwQueryValueKey(hKey,
154  &NameString,
156  ValInfo,
157  len,
158  &len);
159  if(NT_SUCCESS(status) &&
160  ValInfo->DataLength == sizeof(ULONG)) {
161  RtlCopyMemory(pUlong, ValInfo->Data, sizeof(ULONG));
162  retval = TRUE;
163  //UDFPrint((" -> %x\n",*pUlong));
164  } else {
165  //UDFPrint((" err %x\n",status));
166  }
167 
168  MyFreePool__(ValInfo);
169 #else //WIN_32_MODE
170  len = sizeof(ULONG);
172  hKey, // handle of key to query
173  Name, // address of name of value to query
174  0, // reserved
175  NULL, // address of buffer for value type
176  (BYTE *)pUlong, // address of data buffer
177  &len // address of data buffer size
178  ) && len == sizeof(ULONG)) {
179  retval = TRUE;
180  }
181 #endif //WIN_32_MODE
182  if(free_h) {
183  RegTCloseKeyHandle(hKey);
184  }
185  return retval;
186 } // end RegTGetDwordValue()
187 
188 BOOLEAN
190  IN HKEY hRootKey,
192  IN PCWSTR Name,
193  IN PWCHAR pStr,
194  IN ULONG MaxLen
195  )
196 {
197 #ifndef WIN_32_MODE
198  UNICODE_STRING NameString;
200 #endif //USER_MODE
201  ULONG len;
203  HKEY hKey;
204  BOOLEAN retval = FALSE;
205  BOOLEAN free_h = FALSE;
206 
207 #ifdef WIN_32_MODE
208  if(!hRootKey)
209  hRootKey = HKEY_LOCAL_MACHINE;
210 #endif //WIN_32_MODE
211 
212  if(RegistryPath && RegistryPath[0]) {
213  status = RegTGetKeyHandle(hRootKey, RegistryPath, &hKey);
214 #ifdef WIN_32_MODE
215  if(status != ERROR_SUCCESS)
216 #else //WIN_32_MODE
217  if(!NT_SUCCESS(status))
218 #endif //WIN_32_MODE
219  return FALSE;
220  free_h = TRUE;
221  } else {
222  hKey = hRootKey;
223  }
224  if(!hKey)
225  return FALSE;
226 
227  pStr[0] = 0;
228 #ifndef WIN_32_MODE
229  len = sizeof(KEY_VALUE_PARTIAL_INFORMATION) + MaxLen + 0x20;
232  if(!ValInfo) {
233  if(free_h) {
234  RegTCloseKeyHandle(hKey);
235  }
236  return FALSE;
237  }
238 
239  RtlInitUnicodeString(&NameString, Name);
240 
241  status = ZwQueryValueKey(hKey,
242  &NameString,
244  ValInfo,
245  len,
246  &len);
247  if(NT_SUCCESS(status) &&
248  ValInfo->DataLength) {
249  RtlCopyMemory(pStr, ValInfo->Data, min(ValInfo->DataLength, MaxLen) );
250  if(pStr[(ValInfo->DataLength)/sizeof(WCHAR)-1]) {
251  pStr[(ValInfo->DataLength)/sizeof(WCHAR)-1] = 0;
252  }
253  retval = TRUE;
254  }
255 
256  MyFreePool__(ValInfo);
257 #else //WIN_32_MODE
258  len = MaxLen;
260  hKey, // handle of key to query
261  Name, // address of name of value to query
262  0, // reserved
263  NULL, // address of buffer for value type
264  (BYTE *)pStr, // address of data buffer
265  &len // address of data buffer size
266  ) && len) {
267  if(pStr[len-1]) {
268  pStr[len-1] = 0;
269  }
270  retval = TRUE;
271  }
272 #endif //WIN_32_MODE
273 
274  if(free_h) {
275  RegTCloseKeyHandle(hKey);
276  }
277  return retval;
278 } // end RegTGetStringValue()
279 
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
_Must_inspect_result_ _Out_ PNDIS_STATUS _In_ NDIS_HANDLE _In_ ULONG _Out_ PNDIS_STRING KeyName
Definition: ndis.h:4711
const uint16_t * PCWSTR
Definition: typedefs.h:55
#define IN
Definition: typedefs.h:38
#define TRUE
Definition: types.h:120
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
#define ERROR_SUCCESS
Definition: deptool.c:10
#define KEY_READ
Definition: nt_native.h:1023
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
BOOLEAN RegTGetDwordValue(IN HKEY hRootKey, IN PCWSTR RegistryPath, IN PCWSTR Name, IN PULONG pUlong)
Definition: regtools.cpp:99
LONG NTSTATUS
Definition: precomp.h:26
uint16_t * PWCHAR
Definition: typedefs.h:54
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
long LONG
Definition: pedump.c:60
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
#define MyAllocatePool__(type, size)
Definition: mem_tools.h:149
#define KEY_WRITE
Definition: nt_native.h:1031
LONG WINAPI RegQueryValueExW(_In_ HKEY hkeyorg, _In_ LPCWSTR name, _In_ LPDWORD reserved, _In_ LPDWORD type, _In_ LPBYTE data, _In_ LPDWORD count)
Definition: reg.c:4116
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
struct _KEY_VALUE_PARTIAL_INFORMATION * PKEY_VALUE_PARTIAL_INFORMATION
#define MyFreePool__(addr)
Definition: mem_tools.h:152
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
VOID RegTCloseKeyHandle(IN HKEY hKey)
Definition: regtools.cpp:86
GLenum GLsizei len
Definition: glext.h:6722
unsigned char BYTE
Definition: mem.h:68
struct _KEY_VALUE_PARTIAL_INFORMATION KEY_VALUE_PARTIAL_INFORMATION
unsigned int * PULONG
Definition: retypes.h:1
#define min(a, b)
Definition: monoChain.cc:55
#define OUT
Definition: typedefs.h:39
unsigned int ULONG
Definition: retypes.h:1
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3366
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
_In_ PUNICODE_STRING RegistryPath
Definition: wmip.h:27
BOOLEAN RegTGetStringValue(IN HKEY hRootKey, IN PCWSTR RegistryPath, IN PCWSTR Name, IN PWCHAR pStr, IN ULONG MaxLen)
Definition: regtools.cpp:189
NTSTATUS RegTGetKeyHandle(IN HKEY hRootKey, IN PWCHAR KeyName, OUT HKEY *hKey)
Definition: regtools.cpp:59
static SERVICE_STATUS status
Definition: service.c:31
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12
Definition: ps.c:97