2 * WineCfg configuration management
4 * Copyright 2002 Jaco Greeff
5 * Copyright 2003 Dimitrie O. Paun
6 * Copyright 2003-2004 Mike Hearn
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * - Icons in listviews/icons
25 * - Better add app dialog, scan c: for EXE files and add to list in background
26 * - Use [GNOME] HIG style groupboxes rather than win32 style (looks nicer, imho)
30 #define WIN32_LEAN_AND_MEAN
37 #include <wine/debug.h>
38 #include <wine/list.h>
40 WINE_DEFAULT_DEBUG_CHANNEL(winecfg
);
45 static const BOOL is_win64
= (sizeof(void *) > sizeof(int));
47 HKEY config_key
= NULL
;
48 HMENU hPopupMenus
= 0;
51 /* this is called from the WM_SHOWWINDOW handlers of each tab page.
53 * it's a nasty hack, necessary because the property sheet insists on resetting the window title
54 * to the title of the tab, which is utterly useless. dropping the property sheet is on the todo list.
56 void set_window_title(HWND dialog
)
60 /* update the window title */
64 LoadStringW(GetModuleHandleW(NULL
), IDS_WINECFG_TITLE_APP
, apptitle
, ARRAY_SIZE(apptitle
));
65 swprintf(newtitle
, ARRAY_SIZE(newtitle
), apptitle
, current_app
);
69 LoadStringW(GetModuleHandleW(NULL
), IDS_WINECFG_TITLE
, newtitle
, ARRAY_SIZE(newtitle
));
72 WINE_TRACE("setting title to %s\n", wine_dbgstr_w (newtitle
));
73 SendMessageW (GetParent(dialog
), PSM_SETTITLEW
, 0, (LPARAM
) newtitle
);
77 WCHAR
* load_string (UINT id
)
83 LoadStringW(GetModuleHandleW(NULL
), id
, buf
, ARRAY_SIZE(buf
));
86 newStr
= HeapAlloc (GetProcessHeap(), 0, (len
+ 1) * sizeof (WCHAR
));
87 memcpy (newStr
, buf
, len
* sizeof (WCHAR
));
93 * get_config_key: Retrieves a configuration value from the registry
95 * char *subkey : the name of the config section
96 * char *name : the name of the config value
97 * char *default : if the key isn't found, return this value instead
99 * Returns a buffer holding the value if successful, NULL if
100 * not. Caller is responsible for releasing the result.
103 static WCHAR
*get_config_key (HKEY root
, const WCHAR
*subkey
, const WCHAR
*name
, const WCHAR
*def
)
105 LPWSTR buffer
= NULL
;
110 WINE_TRACE("subkey=%s, name=%s, def=%s\n", wine_dbgstr_w(subkey
),
111 wine_dbgstr_w(name
), wine_dbgstr_w(def
));
113 res
= RegOpenKeyExW(root
, subkey
, 0, MAXIMUM_ALLOWED
, &hSubKey
);
114 if (res
!= ERROR_SUCCESS
)
116 if (res
== ERROR_FILE_NOT_FOUND
)
118 WINE_TRACE("Section key not present - using default\n");
119 return def
? strdupW(def
) : NULL
;
123 WINE_ERR("RegOpenKey failed on wine config key (res=%d)\n", res
);
128 res
= RegQueryValueExW(hSubKey
, name
, NULL
, NULL
, NULL
, &len
);
129 if (res
== ERROR_FILE_NOT_FOUND
)
131 WINE_TRACE("Value not present - using default\n");
132 buffer
= def
? strdupW(def
) : NULL
;
134 } else if (res
!= ERROR_SUCCESS
)
136 WINE_ERR("Couldn't query value's length (res=%d)\n", res
);
140 buffer
= HeapAlloc(GetProcessHeap(), 0, len
+ sizeof(WCHAR
));
142 RegQueryValueExW(hSubKey
, name
, NULL
, NULL
, (LPBYTE
) buffer
, &len
);
144 WINE_TRACE("buffer=%s\n", wine_dbgstr_w(buffer
));
146 RegCloseKey(hSubKey
);
152 * set_config_key: convenience wrapper to set a key/value pair
154 * const char *subKey : the name of the config section
155 * const char *valueName : the name of the config value
156 * const char *value : the value to set the configuration key to
158 * Returns 0 on success, non-zero otherwise
160 * If valueName or value is NULL, an empty section will be created
162 static int set_config_key(HKEY root
, const WCHAR
*subkey
, REGSAM access
, const WCHAR
*name
, const void *value
, DWORD type
)
167 WINE_TRACE("subkey=%s: name=%s, value=%p, type=%d\n", wine_dbgstr_w(subkey
),
168 wine_dbgstr_w(name
), value
, type
);
170 assert( subkey
!= NULL
);
174 res
= RegCreateKeyExW( root
, subkey
, 0, NULL
, REG_OPTION_NON_VOLATILE
,
175 access
, NULL
, &key
, NULL
);
176 if (res
!= ERROR_SUCCESS
) goto end
;
179 if (name
== NULL
|| value
== NULL
) goto end
;
183 case REG_SZ
: res
= RegSetValueExW(key
, name
, 0, REG_SZ
, value
, (lstrlenW(value
)+1)*sizeof(WCHAR
)); break;
184 case REG_DWORD
: res
= RegSetValueExW(key
, name
, 0, REG_DWORD
, value
, sizeof(DWORD
)); break;
186 if (res
!= ERROR_SUCCESS
) goto end
;
190 if (key
&& key
!= root
) RegCloseKey(key
);
192 WINE_ERR("Unable to set configuration key %s in section %s, res=%d\n",
193 wine_dbgstr_w(name
), wine_dbgstr_w(subkey
), res
);
197 /* ========================================================================= */
199 /* This code exists for the following reasons:
201 * - It makes working with the registry easier
202 * - By storing a mini cache of the registry, we can more easily implement
203 * cancel/revert and apply. The 'settings list' is an overlay on top of
204 * the actual registry data that we can write out at will.
206 * Rather than model a tree in memory, we simply store each absolute (rooted
207 * at the config key) path.
214 HKEY root
; /* the key on which path is rooted */
215 WCHAR
*path
; /* path in the registry rooted at root */
216 WCHAR
*name
; /* name of the registry value. if null, this means delete the key */
217 WCHAR
*value
; /* contents of the registry value. if null, this means delete the value */
218 DWORD type
; /* type of registry value. REG_SZ or REG_DWORD for now */
221 static struct list settings
= LIST_INIT(settings
);
223 static void free_setting(struct setting
*setting
)
225 assert( setting
!= NULL
);
226 assert( setting
->path
);
228 WINE_TRACE("destroying %p: %s\n", setting
,
229 wine_dbgstr_w(setting
->path
));
231 HeapFree(GetProcessHeap(), 0, setting
->path
);
232 HeapFree(GetProcessHeap(), 0, setting
->name
);
233 HeapFree(GetProcessHeap(), 0, setting
->value
);
235 list_remove(&setting
->entry
);
237 HeapFree(GetProcessHeap(), 0, setting
);
241 * Returns the contents of the value at path. If not in the settings
242 * list, it will be fetched from the registry - failing that, the
243 * default will be used.
245 * If already in the list, the contents as given there will be
246 * returned. You are expected to HeapFree the result.
248 WCHAR
*get_reg_key(HKEY root
, const WCHAR
*path
, const WCHAR
*name
, const WCHAR
*def
)
254 WINE_TRACE("path=%s, name=%s, def=%s\n", wine_dbgstr_w(path
),
255 wine_dbgstr_w(name
), wine_dbgstr_w(def
));
257 /* check if it's in the list */
258 LIST_FOR_EACH( cursor
, &settings
)
260 s
= LIST_ENTRY(cursor
, struct setting
, entry
);
262 if (root
!= s
->root
) continue;
263 if (lstrcmpiW(path
, s
->path
) != 0) continue;
264 if (!s
->name
) continue;
265 if (lstrcmpiW(name
, s
->name
) != 0) continue;
267 WINE_TRACE("found %s:%s in settings list, returning %s\n",
268 wine_dbgstr_w(path
), wine_dbgstr_w(name
),
269 wine_dbgstr_w(s
->value
));
270 return s
->value
? strdupW(s
->value
) : NULL
;
273 /* no, so get from the registry */
274 val
= get_config_key(root
, path
, name
, def
);
276 WINE_TRACE("returning %s\n", wine_dbgstr_w(val
));
282 * Used to set a registry key.
284 * path is rooted at the config key, ie use "Version" or
285 * "AppDefaults\\fooapp.exe\\Version". You can use keypath()
286 * to get such a string.
288 * name is the value name, or NULL to delete the path.
290 * value is what to set the value to, or NULL to delete it.
292 * type is REG_SZ or REG_DWORD.
294 * These values will be copied when necessary.
296 static void set_reg_key_ex(HKEY root
, const WCHAR
*path
, const WCHAR
*name
, const void *value
, DWORD type
)
301 assert( path
!= NULL
);
303 WINE_TRACE("path=%s, name=%s, value=%s\n", wine_dbgstr_w(path
),
304 wine_dbgstr_w(name
), wine_dbgstr_w(value
));
306 /* firstly, see if we already set this setting */
307 LIST_FOR_EACH( cursor
, &settings
)
309 struct setting
*s
= LIST_ENTRY(cursor
, struct setting
, entry
);
311 if (root
!= s
->root
) continue;
312 if (lstrcmpiW(s
->path
, path
) != 0) continue;
313 if ((s
->name
&& name
) && lstrcmpiW(s
->name
, name
) != 0) continue;
315 /* are we attempting a double delete? */
316 if (!s
->name
&& !name
) return;
318 /* do we want to undelete this key? */
319 if (!s
->name
&& name
) s
->name
= strdupW(name
);
321 /* yes, we have already set it, so just replace the content and return */
322 HeapFree(GetProcessHeap(), 0, s
->value
);
327 s
->value
= value
? strdupW(value
) : NULL
;
330 s
->value
= HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD
));
331 memcpy( s
->value
, value
, sizeof(DWORD
) );
335 /* are we deleting this key? this won't remove any of the
336 * children from the overlay so if the user adds it again in
337 * that session it will appear to undelete the settings, but
338 * in reality only the settings actually modified by the user
339 * in that session will be restored. we might want to fix this
340 * corner case in future by actually deleting all the children
341 * here so that once it's gone, it's gone.
343 if (!name
) s
->name
= NULL
;
348 /* otherwise add a new setting for it */
349 s
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct setting
));
351 s
->path
= strdupW(path
);
352 s
->name
= name
? strdupW(name
) : NULL
;
357 s
->value
= value
? strdupW(value
) : NULL
;
360 s
->value
= HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD
));
361 memcpy( s
->value
, value
, sizeof(DWORD
) );
365 list_add_tail(&settings
, &s
->entry
);
368 void set_reg_key(HKEY root
, const WCHAR
*path
, const WCHAR
*name
, const WCHAR
*value
)
370 set_reg_key_ex(root
, path
, name
, value
, REG_SZ
);
373 void set_reg_key_dword(HKEY root
, const WCHAR
*path
, const WCHAR
*name
, DWORD value
)
375 set_reg_key_ex(root
, path
, name
, &value
, REG_DWORD
);
379 * enumerates the value names at the given path, taking into account
380 * the changes in the settings list.
382 * you are expected to HeapFree each element of the array, which is null
383 * terminated, as well as the array itself.
385 WCHAR
**enumerate_values(HKEY root
, const WCHAR
*path
)
388 DWORD res
, i
= 0, valueslen
= 0;
389 WCHAR
**values
= NULL
;
392 res
= RegOpenKeyExW(root
, path
, 0, MAXIMUM_ALLOWED
, &key
);
393 if (res
== ERROR_SUCCESS
)
398 DWORD namesize
= ARRAY_SIZE(name
);
399 BOOL removed
= FALSE
;
401 /* find out the needed size, allocate a buffer, read the value */
402 if ((res
= RegEnumValueW(key
, i
, name
, &namesize
, NULL
, NULL
, NULL
, NULL
)) != ERROR_SUCCESS
)
405 WINE_TRACE("name=%s\n", wine_dbgstr_w(name
));
407 /* check if this value name has been removed in the settings list */
408 LIST_FOR_EACH( cursor
, &settings
)
410 struct setting
*s
= LIST_ENTRY(cursor
, struct setting
, entry
);
411 if (lstrcmpiW(s
->path
, path
) != 0) continue;
412 if (lstrcmpiW(s
->name
, name
) != 0) continue;
416 WINE_TRACE("this key has been removed, so skipping\n");
422 if (removed
) /* this value was deleted by the user, so don't include it */
428 /* grow the array if necessary, add buffer to it, iterate */
429 if (values
) values
= HeapReAlloc(GetProcessHeap(), 0, values
, sizeof(WCHAR
*) * (valueslen
+ 1));
430 else values
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
*));
432 values
[valueslen
++] = strdupW(name
);
433 WINE_TRACE("valueslen is now %d\n", valueslen
);
439 WINE_WARN("failed opening registry key %s, res=0x%x\n",
440 wine_dbgstr_w(path
), res
);
443 WINE_TRACE("adding settings in list but not registry\n");
445 /* now we have to add the values that aren't in the registry but are in the settings list */
446 LIST_FOR_EACH( cursor
, &settings
)
448 struct setting
*setting
= LIST_ENTRY(cursor
, struct setting
, entry
);
451 if (lstrcmpiW(setting
->path
, path
) != 0) continue;
453 if (!setting
->value
) continue;
455 for (i
= 0; i
< valueslen
; i
++)
457 if (lstrcmpiW(setting
->name
, values
[i
]) == 0)
466 WINE_TRACE("%s in list but not registry\n", wine_dbgstr_w(setting
->name
));
468 /* otherwise it's been set by the user but isn't in the registry */
469 if (values
) values
= HeapReAlloc(GetProcessHeap(), 0, values
, sizeof(WCHAR
*) * (valueslen
+ 1));
470 else values
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
*));
472 values
[valueslen
++] = strdupW(setting
->name
);
475 WINE_TRACE("adding null terminator\n");
478 values
= HeapReAlloc(GetProcessHeap(), 0, values
, sizeof(WCHAR
*) * (valueslen
+ 1));
479 values
[valueslen
] = NULL
;
488 * returns true if the given key/value pair exists in the registry or
489 * has been written to.
491 BOOL
reg_key_exists(HKEY root
, const WCHAR
*path
, const WCHAR
*name
)
493 WCHAR
*val
= get_reg_key(root
, path
, name
, NULL
);
495 HeapFree(GetProcessHeap(), 0, val
);
499 static void process_setting(struct setting
*s
)
502 BOOL needs_wow64
= (is_win64
&& s
->root
== HKEY_LOCAL_MACHINE
&& s
->path
&&
503 !wcsnicmp(s
->path
, L
"Software\\", wcslen(L
"Software\\")));
507 WINE_TRACE("Setting %s:%s to '%s'\n", wine_dbgstr_w(s
->path
),
508 wine_dbgstr_w(s
->name
), wine_dbgstr_w(s
->value
));
509 set_config_key(s
->root
, s
->path
, MAXIMUM_ALLOWED
, s
->name
, s
->value
, s
->type
);
512 WINE_TRACE("Setting 32-bit %s:%s to '%s'\n", wine_dbgstr_w(s
->path
),
513 wine_dbgstr_w(s
->name
), wine_dbgstr_w(s
->value
));
514 set_config_key(s
->root
, s
->path
, MAXIMUM_ALLOWED
| KEY_WOW64_32KEY
, s
->name
, s
->value
, s
->type
);
519 WINE_TRACE("Removing %s:%s\n", wine_dbgstr_w(s
->path
), wine_dbgstr_w(s
->name
));
520 if (!RegOpenKeyExW( s
->root
, s
->path
, 0, MAXIMUM_ALLOWED
, &key
))
522 /* NULL name means remove that path/section entirely */
523 if (s
->name
) RegDeleteValueW( key
, s
->name
);
526 RegDeleteTreeW( key
, NULL
);
527 RegDeleteKeyW( s
->root
, s
->path
);
533 WINE_TRACE("Removing 32-bit %s:%s\n", wine_dbgstr_w(s
->path
), wine_dbgstr_w(s
->name
));
534 if (!RegOpenKeyExW( s
->root
, s
->path
, 0, MAXIMUM_ALLOWED
| KEY_WOW64_32KEY
, &key
))
536 if (s
->name
) RegDeleteValueW( key
, s
->name
);
539 RegDeleteTreeW( key
, NULL
);
540 RegDeleteKeyExW( s
->root
, s
->path
, KEY_WOW64_32KEY
, 0 );
550 if (list_empty(&settings
)) return; /* we will be called for each page when the user clicks OK */
554 while (!list_empty(&settings
))
556 struct setting
*s
= (struct setting
*) list_head(&settings
);
562 /* ================================== utility functions ============================ */
564 WCHAR
* current_app
= NULL
; /* the app we are currently editing, or NULL if editing global */
566 /* returns a registry key path suitable for passing to addTransaction */
567 WCHAR
*keypath(const WCHAR
*section
)
569 static WCHAR
*result
= NULL
;
571 HeapFree(GetProcessHeap(), 0, result
);
575 DWORD len
= sizeof(L
"AppDefaults\\") + (lstrlenW(current_app
) + lstrlenW(section
) + 1) * sizeof(WCHAR
);
576 result
= HeapAlloc(GetProcessHeap(), 0, len
);
577 lstrcpyW( result
, L
"AppDefaults\\" );
578 lstrcatW( result
, current_app
);
581 len
= lstrlenW(result
);
582 result
[len
++] = '\\';
583 lstrcpyW( result
+ len
, section
);
588 result
= strdupW(section
);
594 void PRINTERROR(void)
598 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER
|FORMAT_MESSAGE_FROM_SYSTEM
,
599 0, GetLastError(), MAKELANGID(LANG_NEUTRAL
,SUBLANG_DEFAULT
),
600 (LPSTR
)&msg
, 0, NULL
);
602 /* eliminate trailing newline, is this a Wine bug? */
603 *(strrchr(msg
, '\r')) = '\0';
605 WINE_TRACE("error: '%s'\n", msg
);
608 BOOL
initialize(HINSTANCE hInstance
)
610 DWORD res
= RegCreateKeyW(HKEY_CURRENT_USER
, WINE_KEY_ROOT
, &config_key
);
612 if (res
!= ERROR_SUCCESS
) {
613 WINE_ERR("RegOpenKey failed on wine config key (%d)\n", res
);