1 /* registry.cc: registry interface
3 This file is part of Cygwin.
5 This software is a copyrighted work licensed under the terms of the
6 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
20 /* Opens a key under the appropriate Cygwin key.
21 Do not use HKCU per MS KB 199190 */
23 top_key (bool isHKLM
, REGSAM access
, PHANDLE top
)
25 WCHAR rbuf
[PATH_MAX
], *p
;
27 OBJECT_ATTRIBUTES attr
;
30 InitializeObjectAttributes (&attr
, &rpath
, OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
33 wcpcpy (rbuf
, L
"\\Registry\\Machine");
34 RtlInitUnicodeString (&rpath
, rbuf
);
35 status
= NtOpenKey (top
, access
, &attr
);
40 PCWSTR names
[2] = {cygheap
->user
.get_windows_id (name
),
43 p
= wcpcpy (rbuf
, L
"\\Registry\\User\\");
44 for (int i
= 0; i
< 2; i
++)
47 RtlInitUnicodeString (&rpath
, rbuf
);
48 status
= NtOpenKey (top
, access
, &attr
);
49 if (NT_SUCCESS (status
))
56 reg_key::reg_key (HKEY top
, REGSAM access
, ...): _disposition (0)
59 va_start (av
, access
);
60 build_reg (top
, access
, av
);
64 reg_key::reg_key (bool isHKLM
, REGSAM access
, ...): _disposition (0)
69 key_is_invalid
= top_key (isHKLM
, access
, &top
);
70 if (NT_SUCCESS (key_is_invalid
))
72 new (this) reg_key ((HKEY
) top
, access
, L
"SOFTWARE",
73 _WIDE (CYGWIN_INFO_CYGWIN_REGISTRY_NAME
), NULL
);
78 va_start (av
, access
);
79 build_reg ((HKEY
) top
, access
, av
);
87 reg_key::build_reg (HKEY top
, REGSAM access
, va_list av
)
92 OBJECT_ATTRIBUTES attr
;
95 if (top
!= HKEY_LOCAL_MACHINE
&& top
!= HKEY_CURRENT_USER
)
97 else if (!NT_SUCCESS (top_key (top
== HKEY_LOCAL_MACHINE
, access
, &r
)))
100 while ((name
= va_arg (av
, PWCHAR
)) != NULL
)
102 RtlInitUnicodeString (&uname
, name
);
103 InitializeObjectAttributes (&attr
, &uname
,
104 OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
, r
, NULL
);
106 status
= NtCreateKey (&key
, access
, &attr
, 0, NULL
,
107 REG_OPTION_NON_VOLATILE
, &_disposition
);
108 if (r
!= (HANDLE
) top
)
111 if (!NT_SUCCESS (status
))
113 key_is_invalid
= status
;
114 debug_printf ("failed to create key %S in the registry", &uname
);
120 /* Given the current registry key, return the specific DWORD value
121 requested. Return def on failure. */
124 reg_key::get_dword (PCWSTR name
, DWORD def
)
130 UNICODE_STRING uname
;
131 ULONG size
= sizeof (KEY_VALUE_PARTIAL_INFORMATION
) + sizeof (DWORD
);
133 PKEY_VALUE_PARTIAL_INFORMATION vbuf
= (PKEY_VALUE_PARTIAL_INFORMATION
)
136 RtlInitUnicodeString (&uname
, name
);
137 status
= NtQueryValueKey (key
, &uname
, KeyValuePartialInformation
, vbuf
,
139 if (status
!= STATUS_SUCCESS
|| vbuf
->Type
!= REG_DWORD
)
141 DWORD
*dst
= (DWORD
*) vbuf
->Data
;
145 /* Given the current registry key, set a specific DWORD value. */
148 reg_key::set_dword (PCWSTR name
, DWORD val
)
151 return key_is_invalid
;
153 DWORD value
= (DWORD
) val
;
154 UNICODE_STRING uname
;
155 RtlInitUnicodeString (&uname
, name
);
156 return NtSetValueKey (key
, &uname
, 0, REG_DWORD
, &value
, sizeof (value
));
159 /* Given the current registry key, return the specific string value
160 requested. Return zero on success, non-zero on failure. */
163 reg_key::get_string (PCWSTR name
, PWCHAR dst
, size_t max
, PCWSTR def
)
169 status
= key_is_invalid
;
171 wcpncpy (dst
, def
, max
);
175 UNICODE_STRING uname
;
176 ULONG size
= sizeof (KEY_VALUE_PARTIAL_INFORMATION
) + max
* sizeof (WCHAR
);
178 PKEY_VALUE_PARTIAL_INFORMATION vbuf
= (PKEY_VALUE_PARTIAL_INFORMATION
)
181 RtlInitUnicodeString (&uname
, name
);
182 status
= NtQueryValueKey (key
, &uname
, KeyValuePartialInformation
, vbuf
,
184 if (status
!= STATUS_SUCCESS
|| vbuf
->Type
!= REG_SZ
)
185 wcpncpy (dst
, def
, max
);
187 wcpncpy (dst
, (PWCHAR
) vbuf
->Data
, max
);
192 /* Given the current registry key, set a specific string value. */
195 reg_key::set_string (PCWSTR name
, PCWSTR src
)
198 return key_is_invalid
;
200 UNICODE_STRING uname
;
201 RtlInitUnicodeString (&uname
, name
);
202 return NtSetValueKey (key
, &uname
, 0, REG_SZ
, (PVOID
) src
,
203 (wcslen (src
) + 1) * sizeof (WCHAR
));