comparison cinst/nssstore_win.c @ 841:216a65d7fc4b

(issue66) Implement is_system_install and use it This has completly different implementations for linux and Windows. The commit also moves some code into util.c for better reuse.
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 29 Jul 2014 18:12:57 +0200
parents 4aa33c408776
children 797aa8d9c785
comparison
equal deleted inserted replaced
840:c9a31544aaab 841:216a65d7fc4b
168 retval = false; 168 retval = false;
169 return false; 169 return false;
170 } 170 }
171 } 171 }
172 return true; 172 return true;
173 }
174
175 /**@brief Read (and expand if necessary) a registry string.
176 *
177 * Reads a registry string and calls ExpandEnvironmentString
178 * if necessary on it. Returns a newly allocated string array
179 * with the expanded registry value converted to UTF-8
180 *
181 * Caller has to free return value with free.
182 *
183 * @param [in] root the root key (e.g. HKEY_LOCAL_MACHINE)
184 * @param [in] key the key
185 * @param [in] name the name of the value to read.
186 *
187 * @returns the expanded, null terminated utf-8 string of the value.
188 * or NULL on error.
189 */
190 static char*
191 read_registry_string (const HKEY root, const wchar_t *key,
192 const wchar_t *name)
193 {
194 HKEY key_handle = NULL;
195 DWORD size = 0,
196 type = 0,
197 ex_size = 0,
198 dwRet = 0;
199 LONG ret = 0;
200 char *retval = NULL;
201 wchar_t *buf = NULL,
202 *ex_buf = NULL;
203 if (root == NULL || key == NULL || name == NULL)
204 {
205 ERRORPRINTF ("Invalid call to read_registry_string");
206 return NULL;
207 }
208
209 ret = RegOpenKeyExW (root, key, 0, KEY_READ, &key_handle);
210 if (ret != ERROR_SUCCESS)
211 {
212 ERRORPRINTF ("Failed to open key.");
213 return NULL;
214 }
215
216 /* Get the size */
217 ret = RegQueryValueExW (key_handle, name, 0, NULL, NULL, &size);
218 if (ret != ERROR_MORE_DATA && !(ret == ERROR_SUCCESS && size != 0))
219 {
220 ERRORPRINTF ("Failed to get required registry size.");
221 return retval;
222 }
223
224 /* Size is size in bytes not in characters */
225 buf = xmalloc (size + sizeof(wchar_t));
226
227 /* If the stored value is not zero terminated the returned value also
228 is not zero terminated. That's why we reserve more and ensure it's
229 initialized. */
230 memset (buf, 0, size + sizeof(wchar_t));
231
232 ret = RegQueryValueExW (key_handle, name, 0, &type, (LPBYTE) buf, &size);
233 if (ret != ERROR_SUCCESS)
234 {
235 ERRORPRINTF ("Failed get registry value.");
236 return retval;
237 }
238
239 if (type == REG_SZ || (type == REG_EXPAND_SZ && wcschr (buf, '%') == NULL))
240 {
241 /* Nothing to expand, we are done */
242 retval = wchar_to_utf8 (buf, wcslen (buf));
243 goto done;
244 }
245
246 if (type != REG_EXPAND_SZ)
247 {
248 ERRORPRINTF ("Unhandled registry type %i", type);
249 goto done;
250 }
251
252 /* Expand the registry string */
253 ex_size = ExpandEnvironmentStringsW (buf, NULL, 0);
254
255 if (ex_size == 0)
256 {
257 PRINTLASTERROR ("Failed to determine expanded environment size.");
258 goto done;
259 }
260
261 ex_buf = xmalloc ((ex_size + 1) * sizeof(wchar_t));
262
263 dwRet = ExpandEnvironmentStringsW (buf, ex_buf, ex_size);
264
265 ex_buf[ex_size] = '\0'; /* Make sure it's a string */
266
267 if (dwRet == 0 || dwRet != ex_size)
268 {
269 PRINTLASTERROR ("Failed to expand environment variables.");
270 goto done;
271 }
272
273 retval = wchar_to_utf8 (ex_buf, ex_size);
274
275 done:
276 xfree (ex_buf);
277 xfree (buf);
278
279 RegCloseKey (key_handle);
280 return retval;
281 } 173 }
282 /**@brief Get the path to all users default registry hive 174 /**@brief Get the path to all users default registry hive
283 * 175 *
284 * Enumerates the keys in #PROFILE_LIST and retuns a 176 * Enumerates the keys in #PROFILE_LIST and retuns a
285 * strv array with the utf-8 encoded paths to their suggested 177 * strv array with the utf-8 encoded paths to their suggested

http://wald.intevation.org/projects/trustbridge/