winecfg.c 23 KB
Newer Older
1 2 3 4 5
/*
 * WineCfg configuration management
 *
 * Copyright 2002 Jaco Greeff
 * Copyright 2003 Dimitrie O. Paun
6
 * Copyright 2003-2004 Mike Hearn
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
 *
Mike Hearn's avatar
Mike Hearn committed
22 23 24 25 26 27
 * TODO:
 *  - Use unicode
 *  - Icons in listviews/icons
 *  - Better add app dialog, scan c: for EXE files and add to list in background
 *  - Use [GNOME] HIG style groupboxes rather than win32 style (looks nicer, imho)
 *
28 29
 */

30 31
#define WIN32_LEAN_AND_MEAN

32
#include <assert.h>
33 34 35
#include <stdio.h>
#include <limits.h>
#include <windows.h>
36
#include <winreg.h>
37
#include <wine/unicode.h>
38
#include <wine/debug.h>
39
#include <wine/list.h>
40 41

WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
42 43

#include "winecfg.h"
44
#include "resource.h"
45

46
static const BOOL is_win64 = (sizeof(void *) > sizeof(int));
47

48
HKEY config_key = NULL;
49
HMENU hPopupMenus = 0;
50

51 52 53 54 55 56 57 58

/* this is called from the WM_SHOWWINDOW handlers of each tab page.
 *
 * it's a nasty hack, necessary because the property sheet insists on resetting the window title
 * to the title of the tab, which is utterly useless. dropping the property sheet is on the todo list.
 */
void set_window_title(HWND dialog)
{
59
    WCHAR newtitle[256];
60 61

    /* update the window title  */
Mike Hearn's avatar
Mike Hearn committed
62
    if (current_app)
63
    {
64
        WCHAR apptitle[256];
65
        LoadStringW(GetModuleHandleW(NULL), IDS_WINECFG_TITLE_APP, apptitle, ARRAY_SIZE(apptitle));
66
        wsprintfW (newtitle, apptitle, current_app);
67
    }
68 69
    else
    {
70
        LoadStringW(GetModuleHandleW(NULL), IDS_WINECFG_TITLE, newtitle, ARRAY_SIZE(newtitle));
71 72
    }

73 74
    WINE_TRACE("setting title to %s\n", wine_dbgstr_w (newtitle));
    SendMessageW (GetParent(dialog), PSM_SETTITLEW, 0, (LPARAM) newtitle);
75 76
}

77

78 79
WCHAR* load_string (UINT id)
{
80
    WCHAR buf[1024];
81 82 83
    int len;
    WCHAR* newStr;

84
    LoadStringW(GetModuleHandleW(NULL), id, buf, ARRAY_SIZE(buf));
85 86 87 88 89 90 91 92

    len = lstrlenW (buf);
    newStr = HeapAlloc (GetProcessHeap(), 0, (len + 1) * sizeof (WCHAR));
    memcpy (newStr, buf, len * sizeof (WCHAR));
    newStr[len] = 0;
    return newStr;
}

93
/**
94
 * get_config_key: Retrieves a configuration value from the registry
95
 *
96 97 98
 * char *subkey : the name of the config section
 * char *name : the name of the config value
 * char *default : if the key isn't found, return this value instead
99
 *
100 101
 * Returns a buffer holding the value if successful, NULL if
 * not. Caller is responsible for releasing the result.
102
 *
103
 */
104
static WCHAR *get_config_key (HKEY root, const WCHAR *subkey, const WCHAR *name, const WCHAR *def)
105
{
106
    LPWSTR buffer = NULL;
107
    DWORD len;
108
    HKEY hSubKey = NULL;
109
    DWORD res;
110

111 112
    WINE_TRACE("subkey=%s, name=%s, def=%s\n", wine_dbgstr_w(subkey),
               wine_dbgstr_w(name), wine_dbgstr_w(def));
113

114
    res = RegOpenKeyExW(root, subkey, 0, MAXIMUM_ALLOWED, &hSubKey);
115 116 117
    if (res != ERROR_SUCCESS)
    {
        if (res == ERROR_FILE_NOT_FOUND)
118
        {
119
            WINE_TRACE("Section key not present - using default\n");
120
            return def ? strdupW(def) : NULL;
121 122 123
        }
        else
        {
124
            WINE_ERR("RegOpenKey failed on wine config key (res=%d)\n", res);
125 126 127
        }
        goto end;
    }
128

129
    res = RegQueryValueExW(hSubKey, name, NULL, NULL, NULL, &len);
130 131
    if (res == ERROR_FILE_NOT_FOUND)
    {
132
        WINE_TRACE("Value not present - using default\n");
133
        buffer = def ? strdupW(def) : NULL;
134
	goto end;
135
    } else if (res != ERROR_SUCCESS)
136
    {
137
        WINE_ERR("Couldn't query value's length (res=%d)\n", res);
138 139
        goto end;
    }
140

141
    buffer = HeapAlloc(GetProcessHeap(), 0, len + sizeof(WCHAR));
142

143
    RegQueryValueExW(hSubKey, name, NULL, NULL, (LPBYTE) buffer, &len);
144

145
    WINE_TRACE("buffer=%s\n", wine_dbgstr_w(buffer));
146
end:
147
    RegCloseKey(hSubKey);
148

Mike McCormack's avatar
Mike McCormack committed
149
    return buffer;
150 151
}

152
/**
153
 * set_config_key: convenience wrapper to set a key/value pair
154
 *
155 156 157
 * const char *subKey : the name of the config section
 * const char *valueName : the name of the config value
 * const char *value : the value to set the configuration key to
158 159
 *
 * Returns 0 on success, non-zero otherwise
160 161
 *
 * If valueName or value is NULL, an empty section will be created
162
 */
163 164
static int set_config_key(HKEY root, const WCHAR *subkey, REGSAM access, const WCHAR *name, const void *value, DWORD type)
{
165 166 167
    DWORD res = 1;
    HKEY key = NULL;

168 169
    WINE_TRACE("subkey=%s: name=%s, value=%p, type=%d\n", wine_dbgstr_w(subkey),
               wine_dbgstr_w(name), value, type);
170 171

    assert( subkey != NULL );
172

173 174
    if (subkey[0])
    {
175 176
        res = RegCreateKeyExW( root, subkey, 0, NULL, REG_OPTION_NON_VOLATILE,
                               access, NULL, &key, NULL );
177 178
        if (res != ERROR_SUCCESS) goto end;
    }
179
    else key = root;
180
    if (name == NULL || value == NULL) goto end;
181

182 183
    switch (type)
    {
184 185
        case REG_SZ: res = RegSetValueExW(key, name, 0, REG_SZ, value, (lstrlenW(value)+1)*sizeof(WCHAR)); break;
        case REG_DWORD: res = RegSetValueExW(key, name, 0, REG_DWORD, value, sizeof(DWORD)); break;
186
    }
187 188 189 190
    if (res != ERROR_SUCCESS) goto end;

    res = 0;
end:
191
    if (key && key != root) RegCloseKey(key);
192 193 194
    if (res != 0)
        WINE_ERR("Unable to set configuration key %s in section %s, res=%d\n",
                 wine_dbgstr_w(name), wine_dbgstr_w(subkey), res);
195 196 197
    return res;
}

198
/* ========================================================================= */
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

/* This code exists for the following reasons:
 *
 * - It makes working with the registry easier
 * - By storing a mini cache of the registry, we can more easily implement
 *   cancel/revert and apply. The 'settings list' is an overlay on top of
 *   the actual registry data that we can write out at will.
 *
 * Rather than model a tree in memory, we simply store each absolute (rooted
 * at the config key) path.
 *
 */

struct setting
{
    struct list entry;
215
    HKEY root;    /* the key on which path is rooted */
216 217 218
    WCHAR *path;   /* path in the registry rooted at root  */
    WCHAR *name;   /* name of the registry value. if null, this means delete the key  */
    WCHAR *value;  /* contents of the registry value. if null, this means delete the value  */
219
    DWORD type;   /* type of registry value. REG_SZ or REG_DWORD for now */
220 221
};

222
static struct list settings = LIST_INIT(settings);
223 224 225 226

static void free_setting(struct setting *setting)
{
    assert( setting != NULL );
227
    assert( setting->path );
228

229 230 231
    WINE_TRACE("destroying %p: %s\n", setting,
               wine_dbgstr_w(setting->path));

232 233
    HeapFree(GetProcessHeap(), 0, setting->path);
    HeapFree(GetProcessHeap(), 0, setting->name);
234
    HeapFree(GetProcessHeap(), 0, setting->value);
235 236 237 238 239 240 241 242 243 244 245 246 247 248

    list_remove(&setting->entry);

    HeapFree(GetProcessHeap(), 0, setting);
}

/**
 * Returns the contents of the value at path. If not in the settings
 * list, it will be fetched from the registry - failing that, the
 * default will be used.
 *
 * If already in the list, the contents as given there will be
 * returned. You are expected to HeapFree the result.
 */
249
WCHAR *get_reg_keyW(HKEY root, const WCHAR *path, const WCHAR *name, const WCHAR *def)
250 251 252
{
    struct list *cursor;
    struct setting *s;
253
    WCHAR *val;
254

255 256
    WINE_TRACE("path=%s, name=%s, def=%s\n", wine_dbgstr_w(path),
               wine_dbgstr_w(name), wine_dbgstr_w(def));
257 258

    /* check if it's in the list */
259
    LIST_FOR_EACH( cursor, &settings )
260 261 262
    {
        s = LIST_ENTRY(cursor, struct setting, entry);

263
        if (root != s->root) continue;
264
        if (lstrcmpiW(path, s->path) != 0) continue;
265
        if (!s->name) continue;
266
        if (lstrcmpiW(name, s->name) != 0) continue;
267

268 269 270 271
        WINE_TRACE("found %s:%s in settings list, returning %s\n",
                   wine_dbgstr_w(path), wine_dbgstr_w(name),
                   wine_dbgstr_w(s->value));
        return s->value ? strdupW(s->value) : NULL;
272 273 274
    }

    /* no, so get from the registry */
275
    val = get_config_key(root, path, name, def);
276

277
    WINE_TRACE("returning %s\n", wine_dbgstr_w(val));
278 279 280 281

    return val;
}

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
char *get_reg_key(HKEY root, const char *path, const char *name, const char *def)
{
    WCHAR *wpath, *wname, *wdef = NULL, *wRet = NULL;
    char *szRet = NULL;
    int len;

    WINE_TRACE("path=%s, name=%s, def=%s\n", path, name, def);

    wpath = HeapAlloc(GetProcessHeap(), 0, (strlen(path)+1)*sizeof(WCHAR));
    wname = HeapAlloc(GetProcessHeap(), 0, (strlen(name)+1)*sizeof(WCHAR));

    MultiByteToWideChar(CP_ACP, 0, path, -1, wpath, strlen(path)+1);
    MultiByteToWideChar(CP_ACP, 0, name, -1, wname, strlen(name)+1);

    if (def)
    {
        wdef = HeapAlloc(GetProcessHeap(), 0, (strlen(def)+1)*sizeof(WCHAR));
        MultiByteToWideChar(CP_ACP, 0, def, -1, wdef, strlen(def)+1);
    }

    wRet = get_reg_keyW(root, wpath, wname, wdef);

304 305 306 307 308 309
    len = WideCharToMultiByte(CP_ACP, 0, wRet, -1, NULL, 0, NULL, NULL);
    if (len)
    {
        szRet = HeapAlloc(GetProcessHeap(), 0, len);
        WideCharToMultiByte(CP_ACP, 0, wRet, -1, szRet, len, NULL, NULL);
    }
310 311 312 313 314 315 316 317 318

    HeapFree(GetProcessHeap(), 0, wpath);
    HeapFree(GetProcessHeap(), 0, wname);
    HeapFree(GetProcessHeap(), 0, wdef);
    HeapFree(GetProcessHeap(), 0, wRet);

    return szRet;
}

319 320 321 322 323 324 325
/**
 * Used to set a registry key.
 *
 * path is rooted at the config key, ie use "Version" or
 * "AppDefaults\\fooapp.exe\\Version". You can use keypath()
 * to get such a string.
 *
326
 * name is the value name, or NULL to delete the path.
327 328 329
 *
 * value is what to set the value to, or NULL to delete it.
 *
330 331
 * type is REG_SZ or REG_DWORD.
 *
332 333
 * These values will be copied when necessary.
 */
334
static void set_reg_key_ex(HKEY root, const WCHAR *path, const WCHAR *name, const void *value, DWORD type)
335 336 337 338 339 340
{
    struct list *cursor;
    struct setting *s;

    assert( path != NULL );

341
    WINE_TRACE("path=%s, name=%s, value=%s\n", wine_dbgstr_w(path),
342
               wine_dbgstr_w(name), wine_dbgstr_w(value));
343 344

    /* firstly, see if we already set this setting  */
345
    LIST_FOR_EACH( cursor, &settings )
346 347 348
    {
        struct setting *s = LIST_ENTRY(cursor, struct setting, entry);

349
        if (root != s->root) continue;
350 351
        if (lstrcmpiW(s->path, path) != 0) continue;
        if ((s->name && name) && lstrcmpiW(s->name, name) != 0) continue;
352 353 354 355 356

        /* are we attempting a double delete? */
        if (!s->name && !name) return;

        /* do we want to undelete this key? */
357
        if (!s->name && name) s->name = strdupW(name);
358 359

        /* yes, we have already set it, so just replace the content and return  */
360
        HeapFree(GetProcessHeap(), 0, s->value);
361 362 363 364
        s->type = type;
        switch (type)
        {
            case REG_SZ:
365
                s->value = value ? strdupW(value) : NULL;
366 367 368 369 370 371
                break;
            case REG_DWORD:
                s->value = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD));
                memcpy( s->value, value, sizeof(DWORD) );
                break;
        }
372

373 374 375 376 377 378 379 380 381 382
        /* are we deleting this key? this won't remove any of the
         * children from the overlay so if the user adds it again in
         * that session it will appear to undelete the settings, but
         * in reality only the settings actually modified by the user
         * in that session will be restored. we might want to fix this
         * corner case in future by actually deleting all the children
         * here so that once it's gone, it's gone.
         */
        if (!name) s->name = NULL;

383 384 385 386 387
        return;
    }

    /* otherwise add a new setting for it  */
    s = HeapAlloc(GetProcessHeap(), 0, sizeof(struct setting));
388
    s->root  = root;
389 390
    s->path  = strdupW(path);
    s->name  = name  ? strdupW(name)  : NULL;
391 392 393 394
    s->type  = type;
    switch (type)
    {
        case REG_SZ:
395
            s->value = value ? strdupW(value) : NULL;
396 397 398 399 400 401
            break;
        case REG_DWORD:
            s->value = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD));
            memcpy( s->value, value, sizeof(DWORD) );
            break;
    }
402

403
    list_add_tail(&settings, &s->entry);
404
}
405

406 407
void set_reg_key(HKEY root, const char *path, const char *name, const char *value)
{
408
    WCHAR *wpath, *wname = NULL, *wvalue = NULL;
409 410 411

    wpath = HeapAlloc(GetProcessHeap(), 0, (strlen(path)+1)*sizeof(WCHAR));
    MultiByteToWideChar(CP_ACP, 0, path, -1, wpath, strlen(path)+1);
412 413 414 415 416 417

    if (name)
    {
        wname = HeapAlloc(GetProcessHeap(), 0, (strlen(name)+1)*sizeof(WCHAR));
        MultiByteToWideChar(CP_ACP, 0, name, -1, wname, strlen(name)+1);
    }
418 419 420 421 422 423

    if (value)
    {
        wvalue = HeapAlloc(GetProcessHeap(), 0, (strlen(value)+1)*sizeof(WCHAR));
        MultiByteToWideChar(CP_ACP, 0, value, -1, wvalue, strlen(value)+1);
    }
424 425 426 427 428 429

    set_reg_key_ex(root, wpath, wname, wvalue, REG_SZ);

    HeapFree(GetProcessHeap(), 0, wpath);
    HeapFree(GetProcessHeap(), 0, wname);
    HeapFree(GetProcessHeap(), 0, wvalue);
430 431 432 433
}

void set_reg_key_dword(HKEY root, const char *path, const char *name, DWORD value)
{
434 435 436 437 438 439 440 441 442 443 444 445
    WCHAR *wpath, *wname;

    wpath = HeapAlloc(GetProcessHeap(), 0, (strlen(path)+1)*sizeof(WCHAR));
    wname = HeapAlloc(GetProcessHeap(), 0, (strlen(name)+1)*sizeof(WCHAR));

    MultiByteToWideChar(CP_ACP, 0, path, -1, wpath, strlen(path)+1);
    MultiByteToWideChar(CP_ACP, 0, name, -1, wname, strlen(name)+1);

    set_reg_key_ex(root, wpath, wname, &value, REG_DWORD);

    HeapFree(GetProcessHeap(), 0, wpath);
    HeapFree(GetProcessHeap(), 0, wname);
446 447
}

448 449 450 451 452
void set_reg_keyW(HKEY root, const WCHAR *path, const WCHAR *name, const WCHAR *value)
{
    set_reg_key_ex(root, path, name, value, REG_SZ);
}

453 454 455 456 457
void set_reg_key_dwordW(HKEY root, const WCHAR *path, const WCHAR *name, DWORD value)
{
    set_reg_key_ex(root, path, name, &value, REG_DWORD);
}

458 459 460 461 462 463 464
/**
 * enumerates the value names at the given path, taking into account
 * the changes in the settings list.
 *
 * you are expected to HeapFree each element of the array, which is null
 * terminated, as well as the array itself.
 */
465
static WCHAR **enumerate_valuesW(HKEY root, WCHAR *path)
466 467
{
    HKEY key;
468
    DWORD res, i = 0, valueslen = 0;
469
    WCHAR **values = NULL;
470 471
    struct list *cursor;

472
    res = RegOpenKeyExW(root, path, 0, MAXIMUM_ALLOWED, &key);
473 474 475 476
    if (res == ERROR_SUCCESS)
    {
        while (TRUE)
        {
477
            WCHAR name[1024];
478
            DWORD namesize = ARRAY_SIZE(name);
479 480 481
            BOOL removed = FALSE;

            /* find out the needed size, allocate a buffer, read the value  */
482
            if ((res = RegEnumValueW(key, i, name, &namesize, NULL, NULL, NULL, NULL)) != ERROR_SUCCESS)
483 484
                break;

485
            WINE_TRACE("name=%s\n", wine_dbgstr_w(name));
486 487

            /* check if this value name has been removed in the settings list  */
488
            LIST_FOR_EACH( cursor, &settings )
489 490
            {
                struct setting *s = LIST_ENTRY(cursor, struct setting, entry);
491 492
                if (lstrcmpiW(s->path, path) != 0) continue;
                if (lstrcmpiW(s->name, name) != 0) continue;
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508

                if (!s->value)
                {
                    WINE_TRACE("this key has been removed, so skipping\n");
                    removed = TRUE;
                    break;
                }
            }

            if (removed)            /* this value was deleted by the user, so don't include it */
            {
                i++;
                continue;
            }

            /* grow the array if necessary, add buffer to it, iterate  */
509 510
            if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(WCHAR*) * (valueslen + 1));
            else values = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR*));
511

512
            values[valueslen++] = strdupW(name);
513 514 515 516 517 518
            WINE_TRACE("valueslen is now %d\n", valueslen);
            i++;
        }
    }
    else
    {
519 520
        WINE_WARN("failed opening registry key %s, res=0x%x\n",
                  wine_dbgstr_w(path), res);
521 522 523 524 525
    }

    WINE_TRACE("adding settings in list but not registry\n");

    /* now we have to add the values that aren't in the registry but are in the settings list */
526
    LIST_FOR_EACH( cursor, &settings )
527 528 529 530
    {
        struct setting *setting = LIST_ENTRY(cursor, struct setting, entry);
        BOOL found = FALSE;

531
        if (lstrcmpiW(setting->path, path) != 0) continue;
532 533 534 535 536

        if (!setting->value) continue;

        for (i = 0; i < valueslen; i++)
        {
537
            if (lstrcmpiW(setting->name, values[i]) == 0)
538 539 540 541 542 543 544 545
            {
                found = TRUE;
                break;
            }
        }

        if (found) continue;

546
        WINE_TRACE("%s in list but not registry\n", wine_dbgstr_w(setting->name));
547 548

        /* otherwise it's been set by the user but isn't in the registry */
549 550
        if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(WCHAR*) * (valueslen + 1));
        else values = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR*));
551

552
        values[valueslen++] = strdupW(setting->name);
553 554
    }

555 556 557
    WINE_TRACE("adding null terminator\n");
    if (values)
    {
558
        values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(WCHAR*) * (valueslen + 1));
559
        values[valueslen] = NULL;
560
    }
561 562 563 564

    RegCloseKey(key);

    return values;
565 566
}

567 568 569 570 571
char **enumerate_values(HKEY root, char *path)
{
    WCHAR *wpath;
    WCHAR **wret;
    char **ret=NULL;
572
    int i=0, len=0, size;
573 574 575 576 577 578 579 580 581 582 583 584 585 586

    wpath = HeapAlloc(GetProcessHeap(), 0, (strlen(path)+1)*sizeof(WCHAR));
    MultiByteToWideChar(CP_ACP, 0, path, -1, wpath, strlen(path)+1);

    wret = enumerate_valuesW(root, wpath);

    if (wret)
    {
        for(len=0; wret[len]; len++);
        ret = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(char*));

        /* convert WCHAR ** to char ** and HeapFree each WCHAR * element on our way */
        for (i=0; i<len; i++)
        {
587 588 589 590 591 592 593
            size = WideCharToMultiByte(CP_ACP, 0, wret[i], -1, NULL, 0, NULL, NULL);
            if(size)
            {
                ret[i] = HeapAlloc(GetProcessHeap(), 0, size);
                WideCharToMultiByte(CP_ACP, 0, wret[i], -1, ret[i], size, NULL, NULL);
                HeapFree(GetProcessHeap(), 0, wret[i]);
            }
594
        }
595
        ret[len] = NULL;
596 597 598 599 600 601 602 603
    }

    HeapFree(GetProcessHeap(), 0, wpath);
    HeapFree(GetProcessHeap(), 0, wret);

    return ret;
}

604 605 606 607
/**
 * returns true if the given key/value pair exists in the registry or
 * has been written to.
 */
608
BOOL reg_key_exists(HKEY root, const char *path, const char *name)
609
{
610
    char *val = get_reg_key(root, path, name, NULL);
611 612 613 614 615

    if (val)
    {
        HeapFree(GetProcessHeap(), 0, val);
        return TRUE;
616
    }
617 618

    return FALSE;
619 620
}

621
static void process_setting(struct setting *s)
622
{
623 624 625
    static const WCHAR softwareW[] = {'S','o','f','t','w','a','r','e','\\'};
    HKEY key;
    BOOL needs_wow64 = (is_win64 && s->root == HKEY_LOCAL_MACHINE && s->path &&
626
                        !strncmpiW(s->path, softwareW, ARRAY_SIZE(softwareW)));
627

628 629
    if (s->value)
    {
630 631
	WINE_TRACE("Setting %s:%s to '%s'\n", wine_dbgstr_w(s->path),
                   wine_dbgstr_w(s->name), wine_dbgstr_w(s->value));
632 633 634 635 636 637 638
        set_config_key(s->root, s->path, MAXIMUM_ALLOWED, s->name, s->value, s->type);
        if (needs_wow64)
        {
            WINE_TRACE("Setting 32-bit %s:%s to '%s'\n", wine_dbgstr_w(s->path),
                       wine_dbgstr_w(s->name), wine_dbgstr_w(s->value));
            set_config_key(s->root, s->path, MAXIMUM_ALLOWED | KEY_WOW64_32KEY, s->name, s->value, s->type);
        }
639 640 641
    }
    else
    {
642
	WINE_TRACE("Removing %s:%s\n", wine_dbgstr_w(s->path), wine_dbgstr_w(s->name));
643
        if (!RegOpenKeyExW( s->root, s->path, 0, MAXIMUM_ALLOWED, &key ))
644 645 646
        {
            /* NULL name means remove that path/section entirely */
            if (s->name) RegDeleteValueW( key, s->name );
647 648 649 650 651
            else
            {
                RegDeleteTreeW( key, NULL );
                RegDeleteKeyW( s->root, s->path );
            }
652 653 654 655 656 657 658 659
            RegCloseKey( key );
        }
        if (needs_wow64)
        {
            WINE_TRACE("Removing 32-bit %s:%s\n", wine_dbgstr_w(s->path), wine_dbgstr_w(s->name));
            if (!RegOpenKeyExW( s->root, s->path, 0, MAXIMUM_ALLOWED | KEY_WOW64_32KEY, &key ))
            {
                if (s->name) RegDeleteValueW( key, s->name );
660 661 662 663 664
                else
                {
                    RegDeleteTreeW( key, NULL );
                    RegDeleteKeyExW( s->root, s->path, KEY_WOW64_32KEY, 0 );
                }
665 666 667
                RegCloseKey( key );
            }
        }
668 669 670 671 672
    }
}

void apply(void)
{
673
    if (list_empty(&settings)) return; /* we will be called for each page when the user clicks OK */
674 675 676

    WINE_TRACE("()\n");

677
    while (!list_empty(&settings))
678
    {
679
        struct setting *s = (struct setting *) list_head(&settings);
680 681
        process_setting(s);
        free_setting(s);
682
    }
683
}
684 685 686

/* ================================== utility functions ============================ */

687
WCHAR* current_app = NULL; /* the app we are currently editing, or NULL if editing global */
688 689

/* returns a registry key path suitable for passing to addTransaction  */
690
char *keypath(const char *section)
691 692
{
    static char *result = NULL;
693

694
    HeapFree(GetProcessHeap(), 0, result);
695

Mike Hearn's avatar
Mike Hearn committed
696
    if (current_app)
697
    {
698
        result = HeapAlloc(GetProcessHeap(), 0, strlen("AppDefaults\\") + lstrlenW(current_app)*2 + 2 /* \\ */ + strlen(section) + 1 /* terminator */);
699
        wsprintfA(result, "AppDefaults\\%ls", current_app);
700
        if (section[0]) sprintf( result + strlen(result), "\\%s", section );
701 702 703 704 705
    }
    else
    {
        result = strdupA(section);
    }
706

707 708 709
    return result;
}

710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
WCHAR *keypathW(const WCHAR *section)
{
    static const WCHAR appdefaultsW[] = {'A','p','p','D','e','f','a','u','l','t','s','\\',0};
    static WCHAR *result = NULL;

    HeapFree(GetProcessHeap(), 0, result);

    if (current_app)
    {
        DWORD len = sizeof(appdefaultsW) + (lstrlenW(current_app) + lstrlenW(section) + 1) * sizeof(WCHAR);
        result = HeapAlloc(GetProcessHeap(), 0, len );
        lstrcpyW( result, appdefaultsW );
        lstrcatW( result, current_app );
        if (section[0])
        {
            len = lstrlenW(result);
            result[len++] = '\\';
            lstrcpyW( result + len, section );
        }
    }
    else
    {
        result = strdupW(section);
    }

    return result;
}

738 739 740 741 742 743 744
void PRINTERROR(void)
{
        LPSTR msg;

        FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
                       0, GetLastError(), MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
                       (LPSTR)&msg, 0, NULL);
Mike Hearn's avatar
Mike Hearn committed
745 746

        /* eliminate trailing newline, is this a Wine bug? */
747
        *(strrchr(msg, '\r')) = '\0';
Mike Hearn's avatar
Mike Hearn committed
748
        
749 750
        WINE_TRACE("error: '%s'\n", msg);
}
751

752
BOOL initialize(HINSTANCE hInstance)
753
{
754
    DWORD res = RegCreateKeyA(HKEY_CURRENT_USER, WINE_KEY_ROOT, &config_key);
755 756

    if (res != ERROR_SUCCESS) {
757
	WINE_ERR("RegOpenKey failed on wine config key (%d)\n", res);
758
        return TRUE;
759 760
    }

761
    return FALSE;
762
}