1 /* registry.cc: registry interface
3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008, 2009 Red Hat, Inc.
6 This file is part of Cygwin.
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
22 reg_key::reg_key (HKEY top, REGSAM access, ...): _disposition (0)
25 va_start (av, access);
26 build_reg (top, access, av);
30 /* Opens a key under the appropriate Cygwin key.
31 Do not use HKCU per MS KB 199190 */
33 reg_key::reg_key (bool isHKLM, REGSAM access, ...): _disposition (0)
39 top = HKEY_LOCAL_MACHINE;
43 const char *names[2] = {cygheap->user.get_windows_id (name), ".DEFAULT"};
44 for (int i = 0; i < 2; i++)
46 key_is_invalid = RegOpenKeyEx (HKEY_USERS, names[i], 0, access, &top);
47 if (key_is_invalid == ERROR_SUCCESS)
49 debug_printf ("HKU\\%s failed, Win32 error %ld", names[i], key_is_invalid);
54 new (this) reg_key (top, access, "SOFTWARE",
55 CYGWIN_INFO_CYGWIN_REGISTRY_NAME, NULL);
56 if (top != HKEY_LOCAL_MACHINE)
62 va_start (av, access);
63 build_reg (top, access, av);
70 reg_key::build_reg (HKEY top, REGSAM access, va_list av)
76 /* FIXME: Most of the time a valid mount area should exist. Perhaps
77 we should just try an open of the correct key first and only resort
78 to this method in the unlikely situation that it's the first time
79 the current mount areas are being used. */
81 while ((name = va_arg (av, char *)) != NULL)
83 int res = RegCreateKeyExA (r,
87 REG_OPTION_NON_VOLATILE,
95 if (res != ERROR_SUCCESS)
98 debug_printf ("failed to create key %s in the registry", name);
104 /* Given the current registry key, return the specific int value
105 requested. Return def on failure. */
108 reg_key::get_int (const char *name, int def)
112 DWORD size = sizeof (dst);
117 LONG res = RegQueryValueExA (key, name, 0, &type, (LPBYTE) &dst, &size);
119 if (type != REG_DWORD || res != ERROR_SUCCESS)
126 reg_key::get_int (const PWCHAR name, int def)
130 DWORD size = sizeof (dst);
135 LONG res = RegQueryValueExW (key, name, 0, &type, (LPBYTE) &dst, &size);
137 if (type != REG_DWORD || res != ERROR_SUCCESS)
143 /* Given the current registry key, set a specific int value. */
146 reg_key::set_int (const char *name, int val)
150 return key_is_invalid;
152 return (int) RegSetValueExA (key, name, 0, REG_DWORD,
153 (const BYTE *) &value, sizeof (value));
157 reg_key::set_int (const PWCHAR name, int val)
161 return key_is_invalid;
163 return (int) RegSetValueExW (key, name, 0, REG_DWORD,
164 (const BYTE *) &value, sizeof (value));
167 /* Given the current registry key, return the specific string value
168 requested. Return zero on success, non-zero on failure. */
171 reg_key::get_string (const char *name, char *dst, size_t max, const char *def)
178 res = key_is_invalid;
180 res = RegQueryValueExA (key, name, 0, &type, (LPBYTE) dst, &size);
182 if ((def != 0) && ((type != REG_SZ) || (res != ERROR_SUCCESS)))
188 reg_key::get_string (const PWCHAR name, PWCHAR dst, size_t max, const PWCHAR def)
195 res = key_is_invalid;
197 res = RegQueryValueExW (key, name, 0, &type, (LPBYTE) dst, &size);
199 if ((def != 0) && ((type != REG_SZ) || (res != ERROR_SUCCESS)))
204 /* Given the current registry key, set a specific string value. */
207 reg_key::set_string (const char *name, const char *src)
210 return key_is_invalid;
211 return (int) RegSetValueExA (key, name, 0, REG_SZ, (const BYTE*) src,
216 reg_key::set_string (const PWCHAR name, const PWCHAR src)
219 return key_is_invalid;
220 return (int) RegSetValueExW (key, name, 0, REG_SZ, (const BYTE*) src,
221 (wcslen (src) + 1) * sizeof (WCHAR));
224 /* Return the handle to key. */
232 /* Delete subkey of current key. Returns the error code from the
233 RegDeleteKeyA invocation. */
236 reg_key::kill (const char *name)
239 return key_is_invalid;
240 return RegDeleteKeyA (key, name);
243 /* Delete the value specified by name of current key. Returns the error code
244 from the RegDeleteValueA invocation. */
247 reg_key::killvalue (const char *name)
250 return key_is_invalid;
251 return RegDeleteValueA (key, name);
262 get_registry_hive_path (const PWCHAR name, PWCHAR path)
264 WCHAR key[256], *kend;
269 kend = wcpcpy (key, L"SOFTWARE\\Microsoft\\WindowsNT\\CurrentVersion\\ProfileList\\");
271 if (!RegOpenKeyExW (HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hkey))
274 PWCHAR buf = tp.w_get ();
278 if (!RegQueryValueExW (hkey, L"ProfileImagePath", 0, &type,
279 (BYTE *)buf, (siz = NT_MAX_PATH, &siz)))
280 ExpandEnvironmentStringsW (buf, path, NT_MAX_PATH);
285 debug_printf ("HKLM\\%W not found", key);
290 load_registry_hive (const PWCHAR name)
293 PWCHAR path = tp.w_get ();
299 /* Check if user hive is already loaded. */
300 if (!RegOpenKeyExW (HKEY_USERS, name, 0, KEY_READ, &hkey))
302 debug_printf ("User registry hive for %W already exists", name);
306 if (get_registry_hive_path (name, path))
308 wcscat (path, L"\\NTUSER.DAT");
309 if ((ret = RegLoadKeyW (HKEY_USERS, name, path)) != ERROR_SUCCESS)
310 debug_printf ("Loading user registry hive for %W failed: %d", name, ret);