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
9 /* INCLUDES ******************************************************************/
13 /* FUNCTIONS *****************************************************************/
17 RegQueryValueWithAlloc (
19 _In_ LPCWSTR pszValueName
,
20 _In_ DWORD dwExpectedType
,
25 DWORD dwError
, dwType
, dwBytes
;
36 /* Query how big and what type the registry data is */
38 dwError
= RegQueryValueExW(hKey
,
44 if (dwError
!= ERROR_SUCCESS
) return dwError
;
46 /* It if's not the right type, or it's sero bytes, fail*/
47 if ((dwType
!= dwExpectedType
) || (dwBytes
== 0)) return ERROR_INVALID_DATA
;
49 /* Allocate space to hold the data */
50 pbData
= MemAlloc(0, dwBytes
);
51 if (pbData
== NULL
) return ERROR_OUTOFMEMORY
;
53 /* Now get the real registry data */
54 dwError
= RegQueryValueExW(hKey
,
60 if (dwError
!= ERROR_SUCCESS
)
62 /* We failed, free the data since it won't be needed */
67 /* It worked, return the data and size back to the caller */
80 _In_ LPCWSTR pszValueName
,
84 DWORD dwError
, cbData
, dwType
;
89 /* Attempt to read 4 bytes */
90 cbData
= sizeof(DWORD
);
91 dwError
= RegQueryValueExW(hKey
, pszValueName
, 0, &dwType
, 0, &cbData
);
93 /* If we didn't get back a DWORD... */
94 if ((dwError
== ERROR_SUCCESS
) && (dwType
!= REG_DWORD
))
96 /* Zero out the output and fail */
98 dwError
= ERROR_INVALID_DATATYPE
;
109 _In_ LPCWSTR pszValueName
,
110 _In_ DWORD dwExpectedType
,
116 ASSERT(pszValueName
);
118 /* Call the helper function */
119 return RegQueryValueWithAlloc(hKey
,
130 _In_ LPCWSTR pszValueName
,
131 _In_ DWORD dwExpectedType
,
132 _Out_ LPCSTR
* ppszData
137 DWORD cchValueName
, cbMultiByte
;
140 ASSERT(pszValueName
);
146 /* Query the string in Unicode first */
147 dwError
= RegQueryString(hKey
,
150 (PBYTE
*)&pbLocalData
);
151 if (dwError
!= ERROR_SUCCESS
) return dwError
;
153 /* Get the length of the Unicode string */
154 cchValueName
= lstrlenW(pbLocalData
);
156 /* See how much space it would take to convert to ANSI */
157 cbMultiByte
= WideCharToMultiByte(CP_ACP
,
165 if (cbMultiByte
!= 0)
167 /* Allocate the space, assuming failure */
168 dwError
= ERROR_OUTOFMEMORY
;
169 pszData
= MemAlloc(0, cbMultiByte
);
172 /* What do you know, it worked! */
173 dwError
= ERROR_SUCCESS
;
175 /* Now do the real conversion */
176 if (WideCharToMultiByte(CP_ACP
,
185 /* It worked, return the data back to the caller */
190 /* It failed, free our buffer and get the error code */
192 dwError
= GetLastError();
197 /* Free the original Unicode string and return the error */
198 MemFree(pbLocalData
);