ReactOS 0.4.15-dev-7788-g1ad9096
registry.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Service Host
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: base/services/svchost/registry.c
5 * PURPOSE: Helper functions for accessing the registry
6 * PROGRAMMERS: ReactOS Portable Systems Group
7 */
8
9/* INCLUDES ******************************************************************/
10
11#include "svchost.h"
12
13/* FUNCTIONS *****************************************************************/
14
19 _In_ LPCWSTR pszValueName,
20 _In_ DWORD dwExpectedType,
21 _Out_ PBYTE* ppbData,
22 _Out_ PDWORD pdwSize
23 )
24{
25 DWORD dwError, dwType, dwBytes;
27 ASSERT(hKey);
28 ASSERT(pszValueName);
29 ASSERT(ppbData);
30 ASSERT(pdwSize);
31
32 /* Assume failure */
33 *ppbData = NULL;
34 *pdwSize = 0;
35
36 /* Query how big and what type the registry data is */
37 dwBytes = 0;
38 dwError = RegQueryValueExW(hKey,
39 pszValueName,
40 NULL,
41 &dwType,
42 NULL,
43 &dwBytes);
44 if (dwError != ERROR_SUCCESS) return dwError;
45
46 /* It if's not the right type, or it's sero bytes, fail*/
47 if ((dwType != dwExpectedType) || (dwBytes == 0)) return ERROR_INVALID_DATA;
48
49 /* Allocate space to hold the data */
50 pbData = MemAlloc(0, dwBytes);
51 if (pbData == NULL) return ERROR_OUTOFMEMORY;
52
53 /* Now get the real registry data */
54 dwError = RegQueryValueExW(hKey,
55 pszValueName,
56 NULL,
57 &dwType,
58 pbData,
59 &dwBytes);
60 if (dwError != ERROR_SUCCESS)
61 {
62 /* We failed, free the data since it won't be needed */
64 }
65 else
66 {
67 /* It worked, return the data and size back to the caller */
68 *ppbData = pbData;
69 *pdwSize = dwBytes;
70 }
71
72 /* All done */
73 return dwError;
74}
75
80 _In_ LPCWSTR pszValueName,
81 _Out_ PDWORD pdwValue
82 )
83{
84 DWORD dwError, cbData, dwType;
85 ASSERT(hKey);
86 ASSERT(pszValueName);
87 ASSERT(pdwValue);
88
89 /* Attempt to read 4 bytes */
90 cbData = sizeof(DWORD);
91 dwError = RegQueryValueExW(hKey, pszValueName, 0, &dwType, 0, &cbData);
92
93 /* If we didn't get back a DWORD... */
94 if ((dwError == ERROR_SUCCESS) && (dwType != REG_DWORD))
95 {
96 /* Zero out the output and fail */
97 *pdwValue = 0;
98 dwError = ERROR_INVALID_DATATYPE;
99 }
100
101 /* All done! */
102 return dwError;
103}
104
105DWORD
106WINAPI
108 _In_ HKEY hKey,
109 _In_ LPCWSTR pszValueName,
110 _In_ DWORD dwExpectedType,
111 _Out_ PBYTE* ppbData
112 )
113{
115 ASSERT(hKey);
116 ASSERT(pszValueName);
117
118 /* Call the helper function */
120 pszValueName,
121 dwExpectedType,
122 ppbData,
123 &dwSize);
124}
125
126DWORD
127WINAPI
129 _In_ HKEY hKey,
130 _In_ LPCWSTR pszValueName,
131 _In_ DWORD dwExpectedType,
132 _Out_ LPCSTR* ppszData
133 )
134{
135 DWORD dwError;
136 LPWSTR pbLocalData;
137 DWORD cchValueName, cbMultiByte;
138 LPSTR pszData;
139 ASSERT(hKey);
140 ASSERT(pszValueName);
141 ASSERT(ppszData);
142
143 /* Assume failure */
144 *ppszData = NULL;
145
146 /* Query the string in Unicode first */
147 dwError = RegQueryString(hKey,
148 pszValueName,
149 dwExpectedType,
150 (PBYTE*)&pbLocalData);
151 if (dwError != ERROR_SUCCESS) return dwError;
152
153 /* Get the length of the Unicode string */
154 cchValueName = lstrlenW(pbLocalData);
155
156 /* See how much space it would take to convert to ANSI */
157 cbMultiByte = WideCharToMultiByte(CP_ACP,
158 0,
159 pbLocalData,
160 cchValueName + 1,
161 NULL,
162 0,
163 NULL,
164 NULL);
165 if (cbMultiByte != 0)
166 {
167 /* Allocate the space, assuming failure */
168 dwError = ERROR_OUTOFMEMORY;
169 pszData = MemAlloc(0, cbMultiByte);
170 if (pszData != NULL)
171 {
172 /* What do you know, it worked! */
173 dwError = ERROR_SUCCESS;
174
175 /* Now do the real conversion */
177 0,
178 pbLocalData,
179 cchValueName + 1,
180 pszData,
181 cbMultiByte,
182 NULL,
183 NULL) != 0)
184 {
185 /* It worked, return the data back to the caller */
186 *ppszData = pszData;
187 }
188 else
189 {
190 /* It failed, free our buffer and get the error code */
191 MemFree(pszData);
192 dwError = GetLastError();
193 }
194 }
195 }
196
197 /* Free the original Unicode string and return the error */
198 MemFree(pbLocalData);
199 return dwError;
200}
201
BOOL MemFree(IN PVOID lpMem)
Definition: utils.c:26
PVOID MemAlloc(IN DWORD dwFlags, IN SIZE_T dwBytes)
Definition: utils.c:33
DWORD WINAPI RegQueryStringA(_In_ HKEY hKey, _In_ LPCWSTR pszValueName, _In_ DWORD dwExpectedType, _Out_ LPCSTR *ppszData)
Definition: registry.c:128
DWORD WINAPI RegQueryValueWithAlloc(_In_ HKEY hKey, _In_ LPCWSTR pszValueName, _In_ DWORD dwExpectedType, _Out_ PBYTE *ppbData, _Out_ PDWORD pdwSize)
Definition: registry.c:17
DWORD WINAPI RegQueryString(_In_ HKEY hKey, _In_ LPCWSTR pszValueName, _In_ DWORD dwExpectedType, _Out_ PBYTE *ppbData)
Definition: registry.c:107
DWORD WINAPI RegQueryDword(_In_ HKEY hKey, _In_ LPCWSTR pszValueName, _Out_ PDWORD pdwValue)
Definition: registry.c:78
#define ERROR_OUTOFMEMORY
Definition: deptool.c:13
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
LONG WINAPI RegQueryValueExW(_In_ HKEY hkeyorg, _In_ LPCWSTR name, _In_ LPDWORD reserved, _In_ LPDWORD type, _In_ LPBYTE data, _In_ LPDWORD count)
Definition: reg.c:4132
#define CP_ACP
Definition: compat.h:109
#define WideCharToMultiByte
Definition: compat.h:111
#define lstrlenW
Definition: compat.h:750
unsigned long DWORD
Definition: ntddk_ex.h:95
FxAutoRegKey hKey
#define ASSERT(a)
Definition: mode.c:44
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:56
#define _Out_
Definition: ms_sal.h:345
#define _In_
Definition: ms_sal.h:308
#define DWORD
Definition: nt_native.h:44
BYTE * PBYTE
Definition: pedump.c:66
DWORD * PDWORD
Definition: pedump.c:68
#define REG_DWORD
Definition: sdbapi.c:596
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
_In_ HCRYPTHASH _In_ BOOL _In_ DWORD _Inout_updates_bytes_to_ pdwDataLen BYTE * pbData
Definition: wincrypt.h:4201
#define WINAPI
Definition: msvc.h:6
#define ERROR_INVALID_DATATYPE
Definition: winerror.h:1111
#define ERROR_INVALID_DATA
Definition: winerror.h:116
const char * LPCSTR
Definition: xmlstorage.h:183
char * LPSTR
Definition: xmlstorage.h:182
WCHAR * LPWSTR
Definition: xmlstorage.h:184
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185