1 /* registry.cc: registry interface
3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008 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
13 #include "shared_info.h"
16 #include <cygwin/version.h>
24 static const char cygnus_class[] = "cygnus";
26 reg_key::reg_key (HKEY top, REGSAM access, ...): _disposition (0)
29 va_start (av, access);
30 build_reg (top, access, av);
34 /* Opens a key under the appropriate Cygwin key.
35 Do not use HKCU per MS KB 199190 */
37 reg_key::reg_key (bool isHKLM, REGSAM access, ...): _disposition (0)
43 top = HKEY_LOCAL_MACHINE;
47 const char *names[2] = {cygheap->user.get_windows_id (name), ".DEFAULT"};
48 for (int i = 0; i < 2; i++)
50 key_is_invalid = RegOpenKeyEx (HKEY_USERS, names[i], 0, access, &top);
51 if (key_is_invalid == ERROR_SUCCESS)
53 debug_printf ("HKU\\%s failed, Win32 error %ld", names[i], key_is_invalid);
58 new (this) reg_key (top, access, "SOFTWARE",
59 CYGWIN_INFO_CYGNUS_REGISTRY_NAME,
60 CYGWIN_INFO_CYGWIN_REGISTRY_NAME, NULL);
61 if (top != HKEY_LOCAL_MACHINE)
67 va_start (av, access);
68 build_reg (top, access, av);
75 reg_key::build_reg (HKEY top, REGSAM access, va_list av)
81 /* FIXME: Most of the time a valid mount area should exist. Perhaps
82 we should just try an open of the correct key first and only resort
83 to this method in the unlikely situation that it's the first time
84 the current mount areas are being used. */
86 while ((name = va_arg (av, char *)) != NULL)
88 int res = RegCreateKeyExA (r,
91 (char *) cygnus_class,
92 REG_OPTION_NON_VOLATILE,
100 if (res != ERROR_SUCCESS)
102 key_is_invalid = res;
103 debug_printf ("failed to create key %s in the registry", name);
109 /* Given the current registry key, return the specific int value
110 requested. Return def on failure. */
113 reg_key::get_int (const char *name, int def)
117 DWORD size = sizeof (dst);
122 LONG res = RegQueryValueExA (key, name, 0, &type, (unsigned char *) &dst,
125 if (type != REG_DWORD || res != ERROR_SUCCESS)
131 /* Given the current registry key, set a specific int value. */
134 reg_key::set_int (const char *name, int val)
138 return key_is_invalid;
140 return (int) RegSetValueExA (key, name, 0, REG_DWORD,
141 (unsigned char *) &value, sizeof (value));
144 /* Given the current registry key, return the specific string value
145 requested. Return zero on success, non-zero on failure. */
148 reg_key::get_string (const char *name, char *dst, size_t max, const char * def)
155 res = key_is_invalid;
157 res = RegQueryValueExA (key, name, 0, &type, (unsigned char *) dst, &size);
159 if ((def != 0) && ((type != REG_SZ) || (res != ERROR_SUCCESS)))
164 /* Given the current registry key, set a specific string value. */
167 reg_key::set_string (const char *name, const char *src)
170 return key_is_invalid;
171 return (int) RegSetValueExA (key, name, 0, REG_SZ, (unsigned char*) src,
175 /* Return the handle to key. */
183 /* Delete subkey of current key. Returns the error code from the
184 RegDeleteKeyA invocation. */
187 reg_key::kill (const char *name)
190 return key_is_invalid;
191 return RegDeleteKeyA (key, name);
194 /* Delete the value specified by name of current key. Returns the error code
195 from the RegDeleteValueA invocation. */
198 reg_key::killvalue (const char *name)
201 return key_is_invalid;
202 return RegDeleteValueA (key, name);
213 get_registry_hive_path (const PWCHAR name, PWCHAR path)
215 WCHAR key[256], *kend;
220 kend = wcpcpy (key, L"SOFTWARE\\Microsoft\\WindowsNT\\CurrentVersion\\ProfileList\\");
222 if (!RegOpenKeyExW (HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hkey))
225 PWCHAR buf = tp.w_get ();
229 if (!RegQueryValueExW (hkey, L"ProfileImagePath", 0, &type,
230 (BYTE *)buf, (siz = NT_MAX_PATH, &siz)))
231 ExpandEnvironmentStringsW (buf, path, NT_MAX_PATH);
236 debug_printf ("HKLM\\%W not found", key);
241 load_registry_hive (const PWCHAR name)
244 PWCHAR path = tp.w_get ();
250 /* Check if user hive is already loaded. */
251 if (!RegOpenKeyExW (HKEY_USERS, name, 0, KEY_READ, &hkey))
253 debug_printf ("User registry hive for %W already exists", name);
257 if (get_registry_hive_path (name, path))
259 wcscat (path, L"\\NTUSER.DAT");
260 if ((ret = RegLoadKeyW (HKEY_USERS, name, path)) != ERROR_SUCCESS)
261 debug_printf ("Loading user registry hive for %W failed: %d", name, ret);