quartz: Make dwSamplesProcessed a longlong.
[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 /* ========================================================================= */
215
216 /* This code exists for the following reasons:
217  *
218  * - It makes working with the registry easier
219  * - By storing a mini cache of the registry, we can more easily implement
220  *   cancel/revert and apply. The 'settings list' is an overlay on top of
221  *   the actual registry data that we can write out at will.
222  *
223  * Rather than model a tree in memory, we simply store each absolute (rooted
224  * at the config key) path.
225  *
226  */
227
228 struct setting
229 {
230     struct list entry;
231     HKEY root;    /* the key on which path is rooted */
232     WCHAR *path;   /* path in the registry rooted at root  */
233     WCHAR *name;   /* name of the registry value. if null, this means delete the key  */
234     WCHAR *value;  /* contents of the registry value. if null, this means delete the value  */
235     DWORD type;   /* type of registry value. REG_SZ or REG_DWORD for now */
236 };
237
238 struct list *settings;
239
240 static void free_setting(struct setting *setting)
241 {
242     assert( setting != NULL );
243     assert( setting->path );
244
245     WINE_TRACE("destroying %p: %s\n", setting,
246                wine_dbgstr_w(setting->path));
247
248     HeapFree(GetProcessHeap(), 0, setting->path);
249     HeapFree(GetProcessHeap(), 0, setting->name);
250     HeapFree(GetProcessHeap(), 0, setting->value);
251
252     list_remove(&setting->entry);
253
254     HeapFree(GetProcessHeap(), 0, setting);
255 }
256
257 /**
258  * Returns the contents of the value at path. If not in the settings
259  * list, it will be fetched from the registry - failing that, the
260  * default will be used.
261  *
262  * If already in the list, the contents as given there will be
263  * returned. You are expected to HeapFree the result.
264  */
265 WCHAR *get_reg_keyW(HKEY root, const WCHAR *path, const WCHAR *name, const WCHAR *def)
266 {
267     struct list *cursor;
268     struct setting *s;
269     WCHAR *val;
270
271     WINE_TRACE("path=%s, name=%s, def=%s\n", wine_dbgstr_w(path),
272                wine_dbgstr_w(name), wine_dbgstr_w(def));
273
274     /* check if it's in the list */
275     LIST_FOR_EACH( cursor, settings )
276     {
277         s = LIST_ENTRY(cursor, struct setting, entry);
278
279         if (root != s->root) continue;
280         if (lstrcmpiW(path, s->path) != 0) continue;
281         if (!s->name) continue;
282         if (lstrcmpiW(name, s->name) != 0) continue;
283
284         WINE_TRACE("found %s:%s in settings list, returning %s\n",
285                    wine_dbgstr_w(path), wine_dbgstr_w(name),
286                    wine_dbgstr_w(s->value));
287         return s->value ? strdupW(s->value) : NULL;
288     }
289
290     /* no, so get from the registry */
291     val = get_config_key(root, path, name, def);
292
293     WINE_TRACE("returning %s\n", wine_dbgstr_w(val));
294
295     return val;
296 }
297
298 char *get_reg_key(HKEY root, const char *path, const char *name, const char *def)
299 {
300     WCHAR *wpath, *wname, *wdef = NULL, *wRet = NULL;
301     char *szRet = NULL;
302     int len;
303
304     WINE_TRACE("path=%s, name=%s, def=%s\n", path, name, def);
305
306     wpath = HeapAlloc(GetProcessHeap(), 0, (strlen(path)+1)*sizeof(WCHAR));
307     wname = HeapAlloc(GetProcessHeap(), 0, (strlen(name)+1)*sizeof(WCHAR));
308
309     MultiByteToWideChar(CP_ACP, 0, path, -1, wpath, strlen(path)+1);
310     MultiByteToWideChar(CP_ACP, 0, name, -1, wname, strlen(name)+1);
311
312     if (def)
313     {
314         wdef = HeapAlloc(GetProcessHeap(), 0, (strlen(def)+1)*sizeof(WCHAR));
315         MultiByteToWideChar(CP_ACP, 0, def, -1, wdef, strlen(def)+1);
316     }
317
318     wRet = get_reg_keyW(root, wpath, wname, wdef);
319
320     len = WideCharToMultiByte(CP_ACP, 0, wRet, -1, NULL, 0, NULL, NULL);
321     if (len)
322     {
323         szRet = HeapAlloc(GetProcessHeap(), 0, len);
324         WideCharToMultiByte(CP_ACP, 0, wRet, -1, szRet, len, NULL, NULL);
325     }
326
327     HeapFree(GetProcessHeap(), 0, wpath);
328     HeapFree(GetProcessHeap(), 0, wname);
329     HeapFree(GetProcessHeap(), 0, wdef);
330     HeapFree(GetProcessHeap(), 0, wRet);
331
332     return szRet;
333 }
334
335 /**
336  * Used to set a registry key.
337  *
338  * path is rooted at the config key, ie use "Version" or
339  * "AppDefaults\\fooapp.exe\\Version". You can use keypath()
340  * to get such a string.
341  *
342  * name is the value name, or NULL to delete the path.
343  *
344  * value is what to set the value to, or NULL to delete it.
345  *
346  * type is REG_SZ or REG_DWORD.
347  *
348  * These values will be copied when necessary.
349  */
350 static void set_reg_key_ex(HKEY root, const WCHAR *path, const WCHAR *name, const void *value, DWORD type)
351 {
352     struct list *cursor;
353     struct setting *s;
354
355     assert( path != NULL );
356
357     WINE_TRACE("path=%s, name=%s, value=%s\n", wine_dbgstr_w(path),
358                wine_dbgstr_w(name), wine_dbgstr_w(value));
359
360     /* firstly, see if we already set this setting  */
361     LIST_FOR_EACH( cursor, settings )
362     {
363         struct setting *s = LIST_ENTRY(cursor, struct setting, entry);
364
365         if (root != s->root) continue;
366         if (lstrcmpiW(s->path, path) != 0) continue;
367         if ((s->name && name) && lstrcmpiW(s->name, name) != 0) continue;
368
369         /* are we attempting a double delete? */
370         if (!s->name && !name) return;
371
372         /* do we want to undelete this key? */
373         if (!s->name && name) s->name = strdupW(name);
374
375         /* yes, we have already set it, so just replace the content and return  */
376         HeapFree(GetProcessHeap(), 0, s->value);
377         s->type = type;
378         switch (type)
379         {
380             case REG_SZ:
381                 s->value = value ? strdupW(value) : NULL;
382                 break;
383             case REG_DWORD:
384                 s->value = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD));
385                 memcpy( s->value, value, sizeof(DWORD) );
386                 break;
387         }
388
389         /* are we deleting this key? this won't remove any of the
390          * children from the overlay so if the user adds it again in
391          * that session it will appear to undelete the settings, but
392          * in reality only the settings actually modified by the user
393          * in that session will be restored. we might want to fix this
394          * corner case in future by actually deleting all the children
395          * here so that once it's gone, it's gone.
396          */
397         if (!name) s->name = NULL;
398
399         return;
400     }
401
402     /* otherwise add a new setting for it  */
403     s = HeapAlloc(GetProcessHeap(), 0, sizeof(struct setting));
404     s->root  = root;
405     s->path  = strdupW(path);
406     s->name  = name  ? strdupW(name)  : NULL;
407     s->type  = type;
408     switch (type)
409     {
410         case REG_SZ:
411             s->value = value ? strdupW(value) : NULL;
412             break;
413         case REG_DWORD:
414             s->value = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD));
415             memcpy( s->value, value, sizeof(DWORD) );
416             break;
417     }
418
419     list_add_tail(settings, &s->entry);
420 }
421
422 void set_reg_key(HKEY root, const char *path, const char *name, const char *value)
423 {
424     WCHAR *wpath, *wname = NULL, *wvalue = NULL;
425
426     wpath = HeapAlloc(GetProcessHeap(), 0, (strlen(path)+1)*sizeof(WCHAR));
427     MultiByteToWideChar(CP_ACP, 0, path, -1, wpath, strlen(path)+1);
428
429     if (name)
430     {
431         wname = HeapAlloc(GetProcessHeap(), 0, (strlen(name)+1)*sizeof(WCHAR));
432         MultiByteToWideChar(CP_ACP, 0, name, -1, wname, strlen(name)+1);
433     }
434
435     if (value)
436     {
437         wvalue = HeapAlloc(GetProcessHeap(), 0, (strlen(value)+1)*sizeof(WCHAR));
438         MultiByteToWideChar(CP_ACP, 0, value, -1, wvalue, strlen(value)+1);
439     }
440
441     set_reg_key_ex(root, wpath, wname, wvalue, REG_SZ);
442
443     HeapFree(GetProcessHeap(), 0, wpath);
444     HeapFree(GetProcessHeap(), 0, wname);
445     HeapFree(GetProcessHeap(), 0, wvalue);
446 }
447
448 void set_reg_key_dword(HKEY root, const char *path, const char *name, DWORD value)
449 {
450     WCHAR *wpath, *wname;
451
452     wpath = HeapAlloc(GetProcessHeap(), 0, (strlen(path)+1)*sizeof(WCHAR));
453     wname = HeapAlloc(GetProcessHeap(), 0, (strlen(name)+1)*sizeof(WCHAR));
454
455     MultiByteToWideChar(CP_ACP, 0, path, -1, wpath, strlen(path)+1);
456     MultiByteToWideChar(CP_ACP, 0, name, -1, wname, strlen(name)+1);
457
458     set_reg_key_ex(root, wpath, wname, &value, REG_DWORD);
459
460     HeapFree(GetProcessHeap(), 0, wpath);
461     HeapFree(GetProcessHeap(), 0, wname);
462 }
463
464 void set_reg_keyW(HKEY root, const WCHAR *path, const WCHAR *name, const WCHAR *value)
465 {
466     set_reg_key_ex(root, path, name, value, REG_SZ);
467 }
468
469 void set_reg_key_dwordW(HKEY root, const WCHAR *path, const WCHAR *name, DWORD value)
470 {
471     set_reg_key_ex(root, path, name, &value, REG_DWORD);
472 }
473
474 /**
475  * enumerates the value names at the given path, taking into account
476  * the changes in the settings list.
477  *
478  * you are expected to HeapFree each element of the array, which is null
479  * terminated, as well as the array itself.
480  */
481 WCHAR **enumerate_valuesW(HKEY root, WCHAR *path)
482 {
483     HKEY key;
484     DWORD res, i = 0;
485     WCHAR **values = NULL;
486     int valueslen = 0;
487     struct list *cursor;
488
489     res = RegOpenKeyW(root, path, &key);
490     if (res == ERROR_SUCCESS)
491     {
492         while (TRUE)
493         {
494             WCHAR name[1024];
495             DWORD namesize = sizeof(name);
496             BOOL removed = FALSE;
497
498             /* find out the needed size, allocate a buffer, read the value  */
499             if ((res = RegEnumValueW(key, i, name, &namesize, NULL, NULL, NULL, NULL)) != ERROR_SUCCESS)
500                 break;
501
502             WINE_TRACE("name=%s\n", wine_dbgstr_w(name));
503
504             /* check if this value name has been removed in the settings list  */
505             LIST_FOR_EACH( cursor, settings )
506             {
507                 struct setting *s = LIST_ENTRY(cursor, struct setting, entry);
508                 if (lstrcmpiW(s->path, path) != 0) continue;
509                 if (lstrcmpiW(s->name, name) != 0) continue;
510
511                 if (!s->value)
512                 {
513                     WINE_TRACE("this key has been removed, so skipping\n");
514                     removed = TRUE;
515                     break;
516                 }
517             }
518
519             if (removed)            /* this value was deleted by the user, so don't include it */
520             {
521                 HeapFree(GetProcessHeap(), 0, name);
522                 i++;
523                 continue;
524             }
525
526             /* grow the array if necessary, add buffer to it, iterate  */
527             if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(WCHAR*) * (valueslen + 1));
528             else values = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR*));
529
530             values[valueslen++] = strdupW(name);
531             WINE_TRACE("valueslen is now %d\n", valueslen);
532             i++;
533         }
534     }
535     else
536     {
537         WINE_WARN("failed opening registry key %s, res=0x%x\n",
538                   wine_dbgstr_w(path), res);
539     }
540
541     WINE_TRACE("adding settings in list but not registry\n");
542
543     /* now we have to add the values that aren't in the registry but are in the settings list */
544     LIST_FOR_EACH( cursor, settings )
545     {
546         struct setting *setting = LIST_ENTRY(cursor, struct setting, entry);
547         BOOL found = FALSE;
548
549         if (lstrcmpiW(setting->path, path) != 0) continue;
550
551         if (!setting->value) continue;
552
553         for (i = 0; i < valueslen; i++)
554         {
555             if (lstrcmpiW(setting->name, values[i]) == 0)
556             {
557                 found = TRUE;
558                 break;
559             }
560         }
561
562         if (found) continue;
563
564         WINE_TRACE("%s in list but not registry\n", wine_dbgstr_w(setting->name));
565
566         /* otherwise it's been set by the user but isn't in the registry */
567         if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(WCHAR*) * (valueslen + 1));
568         else values = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR*));
569
570         values[valueslen++] = strdupW(setting->name);
571     }
572
573     WINE_TRACE("adding null terminator\n");
574     if (values)
575     {
576         values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(WCHAR*) * (valueslen + 1));
577         values[valueslen] = NULL;
578     }
579
580     RegCloseKey(key);
581
582     return values;
583 }
584
585 char **enumerate_values(HKEY root, char *path)
586 {
587     WCHAR *wpath;
588     WCHAR **wret;
589     char **ret=NULL;
590     int i=0, len=0;
591
592     wpath = HeapAlloc(GetProcessHeap(), 0, (strlen(path)+1)*sizeof(WCHAR));
593     MultiByteToWideChar(CP_ACP, 0, path, -1, wpath, strlen(path)+1);
594
595     wret = enumerate_valuesW(root, wpath);
596
597     if (wret)
598     {
599         for(len=0; wret[len]; len++);
600         ret = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(char*));
601
602         /* convert WCHAR ** to char ** and HeapFree each WCHAR * element on our way */
603         for (i=0; i<len; i++)
604         {
605             ret[i] = HeapAlloc(GetProcessHeap(), 0,
606                                (lstrlenW(wret[i]) + 1) * sizeof(char));
607             WideCharToMultiByte(CP_ACP, 0, wret[i], -1, ret[i],
608                                 lstrlenW(wret[i]) + 1, NULL, NULL);
609             HeapFree(GetProcessHeap(), 0, wret[i]);
610         }
611         ret[len] = NULL;
612     }
613
614     HeapFree(GetProcessHeap(), 0, wpath);
615     HeapFree(GetProcessHeap(), 0, wret);
616
617     return ret;
618 }
619
620 /**
621  * returns true if the given key/value pair exists in the registry or
622  * has been written to.
623  */
624 BOOL reg_key_exists(HKEY root, const char *path, const char *name)
625 {
626     char *val = get_reg_key(root, path, name, NULL);
627
628     if (val)
629     {
630         HeapFree(GetProcessHeap(), 0, val);
631         return TRUE;
632     }
633
634     return FALSE;
635 }
636
637 static void process_setting(struct setting *s)
638 {
639     if (s->value)
640     {
641         WINE_TRACE("Setting %s:%s to '%s'\n", wine_dbgstr_w(s->path),
642                    wine_dbgstr_w(s->name), wine_dbgstr_w(s->value));
643         set_config_key(s->root, s->path, s->name, s->value, s->type);
644     }
645     else
646     {
647         /* NULL name means remove that path/section entirely */
648         if (s->path && s->name) remove_value(s->root, s->path, s->name);
649         else if (s->path && !s->name) RegDeleteTreeW(s->root, s->path);
650     }
651 }
652
653 void apply(void)
654 {
655     if (list_empty(settings)) return; /* we will be called for each page when the user clicks OK */
656
657     WINE_TRACE("()\n");
658
659     while (!list_empty(settings))
660     {
661         struct setting *s = (struct setting *) list_head(settings);
662         process_setting(s);
663         free_setting(s);
664     }
665 }
666
667 /* ================================== utility functions ============================ */
668
669 WCHAR* current_app = NULL; /* the app we are currently editing, or NULL if editing global */
670
671 /* returns a registry key path suitable for passing to addTransaction  */
672 char *keypath(const char *section)
673 {
674     static char *result = NULL;
675
676     HeapFree(GetProcessHeap(), 0, result);
677
678     if (current_app)
679     {
680         result = HeapAlloc(GetProcessHeap(), 0, strlen("AppDefaults\\") + lstrlenW(current_app)*2 + 2 /* \\ */ + strlen(section) + 1 /* terminator */);
681         wsprintf(result, "AppDefaults\\%ls", current_app);
682         if (section[0]) sprintf( result + strlen(result), "\\%s", section );
683     }
684     else
685     {
686         result = strdupA(section);
687     }
688
689     return result;
690 }
691
692 WCHAR *keypathW(const WCHAR *section)
693 {
694     static const WCHAR appdefaultsW[] = {'A','p','p','D','e','f','a','u','l','t','s','\\',0};
695     static WCHAR *result = NULL;
696
697     HeapFree(GetProcessHeap(), 0, result);
698
699     if (current_app)
700     {
701         DWORD len = sizeof(appdefaultsW) + (lstrlenW(current_app) + lstrlenW(section) + 1) * sizeof(WCHAR);
702         result = HeapAlloc(GetProcessHeap(), 0, len );
703         lstrcpyW( result, appdefaultsW );
704         lstrcatW( result, current_app );
705         if (section[0])
706         {
707             len = lstrlenW(result);
708             result[len++] = '\\';
709             lstrcpyW( result + len, section );
710         }
711     }
712     else
713     {
714         result = strdupW(section);
715     }
716
717     return result;
718 }
719
720 void PRINTERROR(void)
721 {
722         LPSTR msg;
723
724         FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
725                        0, GetLastError(), MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
726                        (LPSTR)&msg, 0, NULL);
727
728         /* eliminate trailing newline, is this a Wine bug? */
729         *(strrchr(msg, '\r')) = '\0';
730         
731         WINE_TRACE("error: '%s'\n", msg);
732 }
733
734 int initialize(HINSTANCE hInstance)
735 {
736     DWORD res = RegCreateKey(HKEY_CURRENT_USER, WINE_KEY_ROOT, &config_key);
737
738     if (res != ERROR_SUCCESS) {
739         WINE_ERR("RegOpenKey failed on wine config key (%d)\n", res);
740         return 1;
741     }
742
743     /* load any menus */
744     hPopupMenus = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_WINECFG));
745
746     /* we could probably just have the list as static data  */
747     settings = HeapAlloc(GetProcessHeap(), 0, sizeof(struct list));
748     list_init(settings);
749
750     return 0;
751 }