winecfg: Fix crash caused by calling set_reg_key with NULL value.
[wine] / programs / winecfg / winecfg.c
1 /*
2  * WineCfg configuration management
3  *
4  * Copyright 2002 Jaco Greeff
5  * Copyright 2003 Dimitrie O. Paun
6  * Copyright 2003-2004 Mike Hearn
7  *
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.
12  *
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.
17  *
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
21  *
22  * TODO:
23  *  - Use unicode
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)
27  *
28  */
29
30 #define WIN32_LEAN_AND_MEAN
31
32 #include <assert.h>
33 #include <stdio.h>
34 #include <limits.h>
35 #include <windows.h>
36 #include <winreg.h>
37 #include <wine/debug.h>
38 #include <wine/list.h>
39
40 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
41
42 #include "winecfg.h"
43 #include "resource.h"
44
45 HKEY config_key = NULL;
46 HMENU hPopupMenus = 0;
47
48
49 /* this is called from the WM_SHOWWINDOW handlers of each tab page.
50  *
51  * it's a nasty hack, necessary because the property sheet insists on resetting the window title
52  * to the title of the tab, which is utterly useless. dropping the property sheet is on the todo list.
53  */
54 void set_window_title(HWND dialog)
55 {
56     WCHAR newtitle[256];
57
58     /* update the window title  */
59     if (current_app)
60     {
61         WCHAR apptitle[256];
62         LoadStringW (GetModuleHandle(NULL), IDS_WINECFG_TITLE_APP, apptitle,
63             sizeof(apptitle)/sizeof(apptitle[0]));
64         wsprintfW (newtitle, apptitle, current_app);
65     }
66     else
67     {
68         LoadStringW (GetModuleHandle(NULL), IDS_WINECFG_TITLE, newtitle,
69             sizeof(newtitle)/sizeof(newtitle[0]));
70     }
71
72     WINE_TRACE("setting title to %s\n", wine_dbgstr_w (newtitle));
73     SendMessageW (GetParent(dialog), PSM_SETTITLEW, 0, (LPARAM) newtitle);
74 }
75
76
77 WCHAR* load_string (UINT id)
78 {
79     WCHAR buf[1024];
80     int len;
81     WCHAR* newStr;
82
83     LoadStringW (GetModuleHandle (NULL), id, buf, sizeof(buf)/sizeof(buf[0]));
84
85     len = lstrlenW (buf);
86     newStr = HeapAlloc (GetProcessHeap(), 0, (len + 1) * sizeof (WCHAR));
87     memcpy (newStr, buf, len * sizeof (WCHAR));
88     newStr[len] = 0;
89     return newStr;
90 }
91
92 /**
93  * get_config_key: Retrieves a configuration value from the registry
94  *
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
98  *
99  * Returns a buffer holding the value if successful, NULL if
100  * not. Caller is responsible for releasing the result.
101  *
102  */
103 static WCHAR *get_config_key (HKEY root, const WCHAR *subkey, const WCHAR *name, const WCHAR *def)
104 {
105     LPWSTR buffer = NULL;
106     DWORD len;
107     HKEY hSubKey = NULL;
108     DWORD res;
109
110     WINE_TRACE("subkey=%s, name=%s, def=%s\n", wine_dbgstr_w(subkey),
111                wine_dbgstr_w(name), wine_dbgstr_w(def));
112
113     res = RegOpenKeyW(root, subkey, &hSubKey);
114     if (res != ERROR_SUCCESS)
115     {
116         if (res == ERROR_FILE_NOT_FOUND)
117         {
118             WINE_TRACE("Section key not present - using default\n");
119             return def ? strdupW(def) : NULL;
120         }
121         else
122         {
123             WINE_ERR("RegOpenKey failed on wine config key (res=%d)\n", res);
124         }
125         goto end;
126     }
127
128     res = RegQueryValueExW(hSubKey, name, NULL, NULL, NULL, &len);
129     if (res == ERROR_FILE_NOT_FOUND)
130     {
131         WINE_TRACE("Value not present - using default\n");
132         buffer = def ? strdupW(def) : NULL;
133         goto end;
134     } else if (res != ERROR_SUCCESS)
135     {
136         WINE_ERR("Couldn't query value's length (res=%d)\n", res);
137         goto end;
138     }
139
140     buffer = HeapAlloc(GetProcessHeap(), 0, len + sizeof(WCHAR));
141
142     RegQueryValueExW(hSubKey, name, NULL, NULL, (LPBYTE) buffer, &len);
143
144     WINE_TRACE("buffer=%s\n", wine_dbgstr_w(buffer));
145 end:
146     if (hSubKey && hSubKey != root) RegCloseKey(hSubKey);
147
148     return buffer;
149 }
150
151 /**
152  * set_config_key: convenience wrapper to set a key/value pair
153  *
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
157  *
158  * Returns 0 on success, non-zero otherwise
159  *
160  * If valueName or value is NULL, an empty section will be created
161  */
162 static int set_config_key(HKEY root, const WCHAR *subkey, const WCHAR *name, const void *value, DWORD type) {
163     DWORD res = 1;
164     HKEY key = NULL;
165
166     WINE_TRACE("subkey=%s: name=%s, value=%p, type=%d\n", wine_dbgstr_w(subkey),
167                wine_dbgstr_w(name), value, type);
168
169     assert( subkey != NULL );
170
171     if (subkey[0])
172     {
173         res = RegCreateKeyW(root, subkey, &key);
174         if (res != ERROR_SUCCESS) goto end;
175     }
176     else key = root;
177     if (name == NULL || value == NULL) goto end;
178
179     switch (type)
180     {
181         case REG_SZ: res = RegSetValueExW(key, name, 0, REG_SZ, value, (lstrlenW(value)+1)*sizeof(WCHAR)); break;
182         case REG_DWORD: res = RegSetValueExW(key, name, 0, REG_DWORD, value, sizeof(DWORD)); break;
183     }
184     if (res != ERROR_SUCCESS) goto end;
185
186     res = 0;
187 end:
188     if (key && key != root) RegCloseKey(key);
189     if (res != 0)
190         WINE_ERR("Unable to set configuration key %s in section %s, res=%d\n",
191                  wine_dbgstr_w(name), wine_dbgstr_w(subkey), res);
192     return res;
193 }
194
195 /* removes the requested value from the registry, however, does not
196  * remove the section if empty. Returns S_OK (0) on success.
197  */
198 static HRESULT remove_value(HKEY root, const WCHAR *subkey, const WCHAR *name)
199 {
200     HRESULT hr;
201     HKEY key;
202
203     WINE_TRACE("subkey=%s, name=%s\n", wine_dbgstr_w(subkey), wine_dbgstr_w(name));
204
205     hr = RegOpenKeyW(root, subkey, &key);
206     if (hr != S_OK) return hr;
207
208     hr = RegDeleteValueW(key, name);
209     if (hr != ERROR_SUCCESS) return hr;
210
211     return S_OK;
212 }
213
214 /* removes the requested subkey from the registry, assuming it exists */
215 static LONG remove_path(HKEY root, WCHAR *section) {
216     HKEY branch_key;
217     DWORD max_sub_key_len;
218     DWORD subkeys;
219     DWORD curr_len;
220     LONG ret = ERROR_SUCCESS;
221     long int i;
222     WCHAR *buffer;
223
224     WINE_TRACE("section=%s\n", wine_dbgstr_w(section));
225
226     if ((ret = RegOpenKeyW(root, section, &branch_key)) != ERROR_SUCCESS)
227         return ret;
228
229     /* get size information and resize the buffers if necessary */
230     if ((ret = RegQueryInfoKeyW(branch_key, NULL, NULL, NULL,
231                                &subkeys, &max_sub_key_len,
232                                NULL, NULL, NULL, NULL, NULL, NULL
233                               )) != ERROR_SUCCESS)
234         return ret;
235
236     curr_len = lstrlenW(section);
237     buffer = HeapAlloc(GetProcessHeap(), 0, (max_sub_key_len + curr_len + 1)*sizeof(WCHAR));
238     lstrcpyW(buffer, section);
239
240     buffer[curr_len] = '\\';
241     for (i = subkeys - 1; i >= 0; i--)
242     {
243         DWORD buf_len = max_sub_key_len - curr_len - 1;
244
245         ret = RegEnumKeyExW(branch_key, i, buffer + curr_len + 1,
246                            &buf_len, NULL, NULL, NULL, NULL);
247         if (ret != ERROR_SUCCESS && ret != ERROR_MORE_DATA &&
248             ret != ERROR_NO_MORE_ITEMS)
249             break;
250         else
251             remove_path(root, buffer);
252     }
253     HeapFree(GetProcessHeap(), 0, buffer);
254     RegCloseKey(branch_key);
255
256     return RegDeleteKeyW(root, section);
257 }
258
259
260 /* ========================================================================= */
261
262 /* This code exists for the following reasons:
263  *
264  * - It makes working with the registry easier
265  * - By storing a mini cache of the registry, we can more easily implement
266  *   cancel/revert and apply. The 'settings list' is an overlay on top of
267  *   the actual registry data that we can write out at will.
268  *
269  * Rather than model a tree in memory, we simply store each absolute (rooted
270  * at the config key) path.
271  *
272  */
273
274 struct setting
275 {
276     struct list entry;
277     HKEY root;    /* the key on which path is rooted */
278     WCHAR *path;   /* path in the registry rooted at root  */
279     WCHAR *name;   /* name of the registry value. if null, this means delete the key  */
280     WCHAR *value;  /* contents of the registry value. if null, this means delete the value  */
281     DWORD type;   /* type of registry value. REG_SZ or REG_DWORD for now */
282 };
283
284 struct list *settings;
285
286 static void free_setting(struct setting *setting)
287 {
288     assert( setting != NULL );
289     assert( setting->path );
290
291     WINE_TRACE("destroying %p: %s\n", setting,
292                wine_dbgstr_w(setting->path));
293
294     HeapFree(GetProcessHeap(), 0, setting->path);
295     HeapFree(GetProcessHeap(), 0, setting->name);
296     HeapFree(GetProcessHeap(), 0, setting->value);
297
298     list_remove(&setting->entry);
299
300     HeapFree(GetProcessHeap(), 0, setting);
301 }
302
303 /**
304  * Returns the contents of the value at path. If not in the settings
305  * list, it will be fetched from the registry - failing that, the
306  * default will be used.
307  *
308  * If already in the list, the contents as given there will be
309  * returned. You are expected to HeapFree the result.
310  */
311 WCHAR *get_reg_keyW(HKEY root, const WCHAR *path, const WCHAR *name, const WCHAR *def)
312 {
313     struct list *cursor;
314     struct setting *s;
315     WCHAR *val;
316
317     WINE_TRACE("path=%s, name=%s, def=%s\n", wine_dbgstr_w(path),
318                wine_dbgstr_w(name), wine_dbgstr_w(def));
319
320     /* check if it's in the list */
321     LIST_FOR_EACH( cursor, settings )
322     {
323         s = LIST_ENTRY(cursor, struct setting, entry);
324
325         if (root != s->root) continue;
326         if (lstrcmpiW(path, s->path) != 0) continue;
327         if (!s->name) continue;
328         if (lstrcmpiW(name, s->name) != 0) continue;
329
330         WINE_TRACE("found %s:%s in settings list, returning %s\n",
331                    wine_dbgstr_w(path), wine_dbgstr_w(name),
332                    wine_dbgstr_w(s->value));
333         return s->value ? strdupW(s->value) : NULL;
334     }
335
336     /* no, so get from the registry */
337     val = get_config_key(root, path, name, def);
338
339     WINE_TRACE("returning %s\n", wine_dbgstr_w(val));
340
341     return val;
342 }
343
344 char *get_reg_key(HKEY root, const char *path, const char *name, const char *def)
345 {
346     WCHAR *wpath, *wname, *wdef = NULL, *wRet = NULL;
347     char *szRet = NULL;
348     int len;
349
350     WINE_TRACE("path=%s, name=%s, def=%s\n", path, name, def);
351
352     wpath = HeapAlloc(GetProcessHeap(), 0, (strlen(path)+1)*sizeof(WCHAR));
353     wname = HeapAlloc(GetProcessHeap(), 0, (strlen(name)+1)*sizeof(WCHAR));
354
355     MultiByteToWideChar(CP_ACP, 0, path, -1, wpath, strlen(path)+1);
356     MultiByteToWideChar(CP_ACP, 0, name, -1, wname, strlen(name)+1);
357
358     if (def)
359     {
360         wdef = HeapAlloc(GetProcessHeap(), 0, (strlen(def)+1)*sizeof(WCHAR));
361         MultiByteToWideChar(CP_ACP, 0, def, -1, wdef, strlen(def)+1);
362     }
363
364     wRet = get_reg_keyW(root, wpath, wname, wdef);
365
366     len = WideCharToMultiByte(CP_ACP, 0, wRet, -1, szRet, 0, NULL, NULL);
367     szRet = HeapAlloc(GetProcessHeap(), 0, len);
368     WideCharToMultiByte(CP_ACP, 0, wRet, -1, szRet, len, NULL, NULL);
369
370     HeapFree(GetProcessHeap(), 0, wpath);
371     HeapFree(GetProcessHeap(), 0, wname);
372     HeapFree(GetProcessHeap(), 0, wdef);
373     HeapFree(GetProcessHeap(), 0, wRet);
374
375     return szRet;
376 }
377
378 /**
379  * Used to set a registry key.
380  *
381  * path is rooted at the config key, ie use "Version" or
382  * "AppDefaults\\fooapp.exe\\Version". You can use keypath()
383  * to get such a string.
384  *
385  * name is the value name, or NULL to delete the path.
386  *
387  * value is what to set the value to, or NULL to delete it.
388  *
389  * type is REG_SZ or REG_DWORD.
390  *
391  * These values will be copied when necessary.
392  */
393 static void set_reg_key_ex(HKEY root, const WCHAR *path, const WCHAR *name, const void *value, DWORD type)
394 {
395     struct list *cursor;
396     struct setting *s;
397
398     assert( path != NULL );
399
400     WINE_TRACE("path=%s, name=%s, value=%p\n", wine_dbgstr_w(path),
401                wine_dbgstr_w(name), wine_dbgstr_w(value));
402
403     /* firstly, see if we already set this setting  */
404     LIST_FOR_EACH( cursor, settings )
405     {
406         struct setting *s = LIST_ENTRY(cursor, struct setting, entry);
407
408         if (root != s->root) continue;
409         if (lstrcmpiW(s->path, path) != 0) continue;
410         if ((s->name && name) && lstrcmpiW(s->name, name) != 0) continue;
411
412         /* are we attempting a double delete? */
413         if (!s->name && !name) return;
414
415         /* do we want to undelete this key? */
416         if (!s->name && name) s->name = strdupW(name);
417
418         /* yes, we have already set it, so just replace the content and return  */
419         HeapFree(GetProcessHeap(), 0, s->value);
420         s->type = type;
421         switch (type)
422         {
423             case REG_SZ:
424                 s->value = value ? strdupW(value) : NULL;
425                 break;
426             case REG_DWORD:
427                 s->value = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD));
428                 memcpy( s->value, value, sizeof(DWORD) );
429                 break;
430         }
431
432         /* are we deleting this key? this won't remove any of the
433          * children from the overlay so if the user adds it again in
434          * that session it will appear to undelete the settings, but
435          * in reality only the settings actually modified by the user
436          * in that session will be restored. we might want to fix this
437          * corner case in future by actually deleting all the children
438          * here so that once it's gone, it's gone.
439          */
440         if (!name) s->name = NULL;
441
442         return;
443     }
444
445     /* otherwise add a new setting for it  */
446     s = HeapAlloc(GetProcessHeap(), 0, sizeof(struct setting));
447     s->root  = root;
448     s->path  = strdupW(path);
449     s->name  = name  ? strdupW(name)  : NULL;
450     s->type  = type;
451     switch (type)
452     {
453         case REG_SZ:
454             s->value = value ? strdupW(value) : NULL;
455             break;
456         case REG_DWORD:
457             s->value = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD));
458             memcpy( s->value, value, sizeof(DWORD) );
459             break;
460     }
461
462     list_add_tail(settings, &s->entry);
463 }
464
465 void set_reg_key(HKEY root, const char *path, const char *name, const char *value)
466 {
467     WCHAR *wpath, *wname, *wvalue = NULL;
468
469     wpath = HeapAlloc(GetProcessHeap(), 0, (strlen(path)+1)*sizeof(WCHAR));
470     wname = HeapAlloc(GetProcessHeap(), 0, (strlen(name)+1)*sizeof(WCHAR));
471
472     MultiByteToWideChar(CP_ACP, 0, path, -1, wpath, strlen(path)+1);
473     MultiByteToWideChar(CP_ACP, 0, name, -1, wname, strlen(name)+1);
474
475     if (value)
476     {
477         wvalue = HeapAlloc(GetProcessHeap(), 0, (strlen(value)+1)*sizeof(WCHAR));
478         MultiByteToWideChar(CP_ACP, 0, value, -1, wvalue, strlen(value)+1);
479     }
480
481     set_reg_key_ex(root, wpath, wname, wvalue, REG_SZ);
482
483     HeapFree(GetProcessHeap(), 0, wpath);
484     HeapFree(GetProcessHeap(), 0, wname);
485     HeapFree(GetProcessHeap(), 0, wvalue);
486 }
487
488 void set_reg_key_dword(HKEY root, const char *path, const char *name, DWORD value)
489 {
490     WCHAR *wpath, *wname;
491
492     wpath = HeapAlloc(GetProcessHeap(), 0, (strlen(path)+1)*sizeof(WCHAR));
493     wname = HeapAlloc(GetProcessHeap(), 0, (strlen(name)+1)*sizeof(WCHAR));
494
495     MultiByteToWideChar(CP_ACP, 0, path, -1, wpath, strlen(path)+1);
496     MultiByteToWideChar(CP_ACP, 0, name, -1, wname, strlen(name)+1);
497
498     set_reg_key_ex(root, wpath, wname, &value, REG_DWORD);
499
500     HeapFree(GetProcessHeap(), 0, wpath);
501     HeapFree(GetProcessHeap(), 0, wname);
502 }
503
504 /**
505  * enumerates the value names at the given path, taking into account
506  * the changes in the settings list.
507  *
508  * you are expected to HeapFree each element of the array, which is null
509  * terminated, as well as the array itself.
510  */
511 WCHAR **enumerate_valuesW(HKEY root, WCHAR *path)
512 {
513     HKEY key;
514     DWORD res, i = 0;
515     WCHAR **values = NULL;
516     int valueslen = 0;
517     struct list *cursor;
518
519     res = RegOpenKeyW(root, path, &key);
520     if (res == ERROR_SUCCESS)
521     {
522         while (TRUE)
523         {
524             WCHAR name[1024];
525             DWORD namesize = sizeof(name);
526             BOOL removed = FALSE;
527
528             /* find out the needed size, allocate a buffer, read the value  */
529             if ((res = RegEnumValueW(key, i, name, &namesize, NULL, NULL, NULL, NULL)) != ERROR_SUCCESS)
530                 break;
531
532             WINE_TRACE("name=%s\n", wine_dbgstr_w(name));
533
534             /* check if this value name has been removed in the settings list  */
535             LIST_FOR_EACH( cursor, settings )
536             {
537                 struct setting *s = LIST_ENTRY(cursor, struct setting, entry);
538                 if (lstrcmpiW(s->path, path) != 0) continue;
539                 if (lstrcmpiW(s->name, name) != 0) continue;
540
541                 if (!s->value)
542                 {
543                     WINE_TRACE("this key has been removed, so skipping\n");
544                     removed = TRUE;
545                     break;
546                 }
547             }
548
549             if (removed)            /* this value was deleted by the user, so don't include it */
550             {
551                 HeapFree(GetProcessHeap(), 0, name);
552                 i++;
553                 continue;
554             }
555
556             /* grow the array if necessary, add buffer to it, iterate  */
557             if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(WCHAR*) * (valueslen + 1));
558             else values = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR*));
559
560             values[valueslen++] = strdupW(name);
561             WINE_TRACE("valueslen is now %d\n", valueslen);
562             i++;
563         }
564     }
565     else
566     {
567         WINE_WARN("failed opening registry key %s, res=0x%x\n",
568                   wine_dbgstr_w(path), res);
569     }
570
571     WINE_TRACE("adding settings in list but not registry\n");
572
573     /* now we have to add the values that aren't in the registry but are in the settings list */
574     LIST_FOR_EACH( cursor, settings )
575     {
576         struct setting *setting = LIST_ENTRY(cursor, struct setting, entry);
577         BOOL found = FALSE;
578
579         if (lstrcmpiW(setting->path, path) != 0) continue;
580
581         if (!setting->value) continue;
582
583         for (i = 0; i < valueslen; i++)
584         {
585             if (lstrcmpiW(setting->name, values[i]) == 0)
586             {
587                 found = TRUE;
588                 break;
589             }
590         }
591
592         if (found) continue;
593
594         WINE_TRACE("%s in list but not registry\n", wine_dbgstr_w(setting->name));
595
596         /* otherwise it's been set by the user but isn't in the registry */
597         if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(WCHAR*) * (valueslen + 1));
598         else values = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR*));
599
600         values[valueslen++] = strdupW(setting->name);
601     }
602
603     WINE_TRACE("adding null terminator\n");
604     if (values)
605     {
606         values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(WCHAR*) * (valueslen + 1));
607         values[valueslen] = NULL;
608     }
609
610     RegCloseKey(key);
611
612     return values;
613 }
614
615 char **enumerate_values(HKEY root, char *path)
616 {
617     WCHAR *wpath;
618     WCHAR **wret;
619     char **ret=NULL;
620     int i=0, len=0;
621
622     wpath = HeapAlloc(GetProcessHeap(), 0, (strlen(path)+1)*sizeof(WCHAR));
623     MultiByteToWideChar(CP_ACP, 0, path, -1, wpath, strlen(path)+1);
624
625     wret = enumerate_valuesW(root, wpath);
626
627     if (wret)
628     {
629         for(len=0; wret[len]; len++);
630         ret = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(char*));
631
632         /* convert WCHAR ** to char ** and HeapFree each WCHAR * element on our way */
633         for (i=0; i<len; i++)
634         {
635             ret[i] = HeapAlloc(GetProcessHeap(), 0,
636                                (lstrlenW(wret[i]) + 1) * sizeof(char));
637             WideCharToMultiByte(CP_ACP, 0, wret[i], -1, ret[i],
638                                 lstrlenW(wret[i]) + 1, NULL, NULL);
639             HeapFree(GetProcessHeap(), 0, wret[i]);
640         }
641     }
642
643     HeapFree(GetProcessHeap(), 0, wpath);
644     HeapFree(GetProcessHeap(), 0, wret);
645
646     return ret;
647 }
648
649 /**
650  * returns true if the given key/value pair exists in the registry or
651  * has been written to.
652  */
653 BOOL reg_key_exists(HKEY root, const char *path, const char *name)
654 {
655     char *val = get_reg_key(root, path, name, NULL);
656
657     if (val)
658     {
659         HeapFree(GetProcessHeap(), 0, val);
660         return TRUE;
661     }
662
663     return FALSE;
664 }
665
666 static void process_setting(struct setting *s)
667 {
668     if (s->value)
669     {
670         WINE_TRACE("Setting %s:%s to '%s'\n", wine_dbgstr_w(s->path),
671                    wine_dbgstr_w(s->name), wine_dbgstr_w(s->value));
672         set_config_key(s->root, s->path, s->name, s->value, s->type);
673     }
674     else
675     {
676         /* NULL name means remove that path/section entirely */
677         if (s->path && s->name) remove_value(s->root, s->path, s->name);
678         else if (s->path && !s->name) remove_path(s->root, s->path);
679     }
680 }
681
682 void apply(void)
683 {
684     if (list_empty(settings)) return; /* we will be called for each page when the user clicks OK */
685
686     WINE_TRACE("()\n");
687
688     while (!list_empty(settings))
689     {
690         struct setting *s = (struct setting *) list_head(settings);
691         process_setting(s);
692         free_setting(s);
693     }
694 }
695
696 /* ================================== utility functions ============================ */
697
698 WCHAR* current_app = NULL; /* the app we are currently editing, or NULL if editing global */
699
700 /* returns a registry key path suitable for passing to addTransaction  */
701 char *keypath(const char *section)
702 {
703     static char *result = NULL;
704
705     HeapFree(GetProcessHeap(), 0, result);
706
707     if (current_app)
708     {
709         result = HeapAlloc(GetProcessHeap(), 0, strlen("AppDefaults\\") + lstrlenW(current_app)*2 + 2 /* \\ */ + strlen(section) + 1 /* terminator */);
710         wsprintf(result, "AppDefaults\\%ls", current_app);
711         if (section[0]) sprintf( result + strlen(result), "\\%s", section );
712     }
713     else
714     {
715         result = strdupA(section);
716     }
717
718     return result;
719 }
720
721 void PRINTERROR(void)
722 {
723         LPSTR msg;
724
725         FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
726                        0, GetLastError(), MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
727                        (LPSTR)&msg, 0, NULL);
728
729         /* eliminate trailing newline, is this a Wine bug? */
730         *(strrchr(msg, '\r')) = '\0';
731         
732         WINE_TRACE("error: '%s'\n", msg);
733 }
734
735 int initialize(HINSTANCE hInstance)
736 {
737     DWORD res = RegCreateKey(HKEY_CURRENT_USER, WINE_KEY_ROOT, &config_key);
738
739     if (res != ERROR_SUCCESS) {
740         WINE_ERR("RegOpenKey failed on wine config key (%d)\n", res);
741         return 1;
742     }
743
744     /* load any menus */
745     hPopupMenus = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_WINECFG));
746
747     /* we could probably just have the list as static data  */
748     settings = HeapAlloc(GetProcessHeap(), 0, sizeof(struct list));
749     list_init(settings);
750
751     return 0;
752 }