1 /* registry.cc: registry interface
3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
5 This file is part of Cygwin.
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
12 #include "shared_info.h"
15 #include <cygwin/version.h>
17 static char NO_COPY cygnus_class[] = "cygnus";
19 reg_key::reg_key (HKEY top, REGSAM access, ...)
22 va_start (av, access);
23 build_reg (top, access, av);
27 reg_key::reg_key (REGSAM access, ...)
31 new (this) reg_key (HKEY_CURRENT_USER, access, "SOFTWARE",
32 CYGWIN_INFO_CYGNUS_REGISTRY_NAME,
33 CYGWIN_INFO_CYGWIN_REGISTRY_NAME, NULL);
36 va_start (av, access);
37 build_reg (top, KEY_READ, av);
43 reg_key::reg_key (REGSAM access)
45 new (this) reg_key (HKEY_CURRENT_USER, access, "SOFTWARE",
46 CYGWIN_INFO_CYGNUS_REGISTRY_NAME,
47 CYGWIN_INFO_CYGWIN_REGISTRY_NAME,
48 CYGWIN_INFO_CYGWIN_MOUNT_REGISTRY_NAME, NULL);
52 reg_key::build_reg (HKEY top, REGSAM access, va_list av)
58 /* FIXME: Most of the time a valid mount area should exist. Perhaps
59 we should just try an open of the correct key first and only resort
60 to this method in the unlikely situation that it's the first time
61 the current mount areas are being used. */
63 while ((name = va_arg (av, char *)) != NULL)
66 int res = RegCreateKeyExA (r,
70 REG_OPTION_NON_VOLATILE,
78 if (res != ERROR_SUCCESS)
81 debug_printf ("failed to create key %s in the registry", name);
85 /* If we're considering the mounts key, check if it had to
86 be created and set had_to_create appropriately. */
87 if (strcmp (name, CYGWIN_INFO_CYGWIN_MOUNT_REGISTRY_NAME) == 0)
88 if (disp == REG_CREATED_NEW_KEY)
89 mount_table->had_to_create_mount_areas++;
93 /* Given the current registry key, return the specific int value
94 requested. Return def on failure. */
97 reg_key::get_int (const char *name, int def)
101 DWORD size = sizeof (dst);
106 LONG res = RegQueryValueExA (key, name, 0, &type, (unsigned char *) &dst,
109 if (type != REG_DWORD || res != ERROR_SUCCESS)
115 /* Given the current registry key, set a specific int value. */
118 reg_key::set_int (const char *name, int val)
122 return key_is_invalid;
124 return (int) RegSetValueExA (key, name, 0, REG_DWORD,
125 (unsigned char *) &value, sizeof (value));
128 /* Given the current registry key, return the specific string value
129 requested. Return zero on success, non-zero on failure. */
132 reg_key::get_string (const char *name, char *dst, size_t max, const char * def)
139 res = key_is_invalid;
141 res = RegQueryValueExA (key, name, 0, &type, (unsigned char *) dst, &size);
143 if ((def != 0) && ((type != REG_SZ) || (res != ERROR_SUCCESS)))
148 /* Given the current registry key, set a specific string value. */
151 reg_key::set_string (const char *name, const char *src)
154 return key_is_invalid;
155 return (int) RegSetValueExA (key, name, 0, REG_SZ, (unsigned char*) src,
159 /* Return the handle to key. */
167 /* Delete subkey of current key. Returns the error code from the
168 RegDeleteKeyA invocation. */
171 reg_key::kill (const char *name)
174 return key_is_invalid;
175 return RegDeleteKeyA (key, name);
178 /* Delete the value specified by name of current key. Returns the error code
179 from the RegDeleteValueA invocation. */
182 reg_key::killvalue (const char *name)
185 return key_is_invalid;
186 return RegDeleteValueA (key, name);
197 get_registry_hive_path (const PSID psid, char *path)
207 strcpy (key,"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\");
209 if (!RegOpenKeyExA (HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hkey))
215 if (!RegQueryValueExA (hkey, "ProfileImagePath", 0, &type,
216 (BYTE *)buf, (siz = 256, &siz)))
217 ExpandEnvironmentStringsA (buf, key, 256);
220 return strcpy (path, key);
226 load_registry_hive (PSID psid)
229 char path[CYG_MAX_PATH + 1];
235 /* Check if user hive is already loaded. */
238 if (!RegOpenKeyExA (HKEY_USERS, sid, 0, KEY_READ, &hkey))
240 debug_printf ("User registry hive for %s already exists", sid);
244 enable_restore_privilege ();
245 if (get_registry_hive_path (psid, path))
247 strcat (path, "\\NTUSER.DAT");
248 if ((ret = RegLoadKeyA (HKEY_USERS, sid, path)) != ERROR_SUCCESS)
249 debug_printf ("Loading user registry hive for %s failed: %d", sid, ret);