File: edit.c

package info (click to toggle)
wine 10.0~repack-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 325,920 kB
  • sloc: ansic: 4,156,003; perl: 23,800; yacc: 22,031; javascript: 15,872; makefile: 12,346; pascal: 9,519; objc: 6,923; lex: 5,273; xml: 3,219; python: 2,673; cpp: 1,741; sh: 893; java: 750; asm: 299; cs: 62
file content (685 lines) | stat: -rw-r--r-- 19,356 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/*
 * Registry editing UI functions.
 *
 * Copyright (C) 2003 Dimitrie O. Paun
 *
 * 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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#define WIN32_LEAN_AND_MEAN     /* Exclude rarely-used stuff from Windows headers */

#include <windows.h>
#include <commctrl.h>
#include <commdlg.h>
#include <cderr.h>
#include <shellapi.h>
#include <shlwapi.h>

#include "main.h"

static BOOL isDecimal;

struct edit_params
{
    HKEY         hkey;
    const WCHAR *value_name;
    DWORD        type;
    void        *data;
    DWORD        size;
};

static int vmessagebox(HWND hwnd, int buttons, int titleId, int resId, va_list va_args)
{
    WCHAR title[256];
    WCHAR fmt[1024];
    WCHAR *str;
    int ret;

    LoadStringW(hInst, titleId, title, ARRAY_SIZE(title));
    LoadStringW(hInst, resId, fmt, ARRAY_SIZE(fmt));

    FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
                   fmt, 0, 0, (WCHAR *)&str, 0, &va_args);
    ret = MessageBoxW(hwnd, str, title, buttons);
    LocalFree(str);

    return ret;
}

int WINAPIV messagebox(HWND hwnd, int buttons, int titleId, int resId, ...)
{
    va_list ap;
    INT result;

    va_start(ap, resId);
    result = vmessagebox(hwnd, buttons, titleId, resId, ap);
    va_end(ap);

    return result;
}

static void WINAPIV error_code_messagebox(HWND hwnd, unsigned int msg_id, ...)
{
    va_list ap;

    va_start(ap, msg_id);
    vmessagebox(hwnd, MB_OK|MB_ICONERROR, IDS_ERROR, msg_id, ap);
    va_end(ap);
}

static BOOL update_registry_value(HWND hwndDlg, struct edit_params *params)
{
    HWND hwndValue;
    unsigned int len;
    WCHAR *buf;
    LONG ret;

    hwndValue = GetDlgItem(hwndDlg, IDC_VALUE_DATA);
    len = GetWindowTextLengthW(hwndValue);
    buf = malloc((len + 1) * sizeof(WCHAR));
    len = GetWindowTextW(hwndValue, buf, len + 1);

    free(params->data);

    switch (params->type)
    {
    case REG_SZ:
    case REG_EXPAND_SZ:
        params->data = buf;
        params->size = (len + 1) * sizeof(WCHAR);
        break;
    case REG_DWORD:
        params->size = sizeof(DWORD);
        params->data = malloc(params->size);
        swscanf(buf, isDecimal ? L"%lu" : L"%lx", params->data);
        free(buf);
        break;
    case REG_QWORD:
        params->size = sizeof(UINT64);
        params->data = malloc(params->size);
        swscanf(buf, isDecimal ? L"%I64u" : L"%I64x", params->data);
        free(buf);
        break;
    case REG_MULTI_SZ:
    {
        WCHAR *tmp = malloc((len + 2) * sizeof(WCHAR));
        unsigned int i, j;

        for (i = 0, j = 0; i < len; i++)
        {
            if (buf[i] == '\r' && buf[i + 1] == '\n')
            {
                if (j && tmp[j - 1]) tmp[j++] = 0;
                i++;
            }
            else tmp[j++] = buf[i];
        }

        tmp[j++] = 0;
        tmp[j++] = 0;

        free(buf);
        params->data = tmp;
        params->size = j * sizeof(WCHAR);
        break;
    }
    default: /* hex data types */
        free(buf);
        params->size = SendMessageW(hwndValue, HEM_GETDATA, 0, 0);
        params->data = malloc(params->size);
        SendMessageW(hwndValue, HEM_GETDATA, (WPARAM)params->size, (LPARAM)params->data);
    }

    ret = RegSetValueExW(params->hkey, params->value_name, 0, params->type, (BYTE *)params->data, params->size);
    if (ret) error_code_messagebox(hwndDlg, IDS_SET_VALUE_FAILED);

    return !ret;
}

static void set_dlgproc_value_name(HWND hwndDlg, struct edit_params *params)
{
    if (params->value_name)
        SetDlgItemTextW(hwndDlg, IDC_VALUE_NAME, params->value_name);
    else
        SetDlgItemTextW(hwndDlg, IDC_VALUE_NAME, g_pszDefaultValueName);
}

static INT_PTR CALLBACK modify_string_dlgproc(HWND hwndDlg, UINT msg, WPARAM wparam, LPARAM lparam)
{
    struct edit_params *params;
    int ret = 0;

    switch (msg)
    {
    case WM_INITDIALOG:
        params = (struct edit_params *)lparam;
        SetWindowLongPtrW(hwndDlg, DWLP_USER, (ULONG_PTR)params);
        set_dlgproc_value_name(hwndDlg, params);
        SetDlgItemTextW(hwndDlg, IDC_VALUE_DATA, params->data);
        return TRUE;
    case WM_COMMAND:
        switch (LOWORD(wparam))
        {
        case IDOK:
            params = (struct edit_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
            ret = update_registry_value(hwndDlg, params);
            /* fall through */
        case IDCANCEL:
            EndDialog(hwndDlg, ret);
            return TRUE;
        }
    }
    return FALSE;
}

static void set_dword_edit_limit(HWND hwndDlg, DWORD type)
{
    if (isDecimal)
        SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, EM_SETLIMITTEXT, type == REG_DWORD ? 10 : 20, 0);
    else
        SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, EM_SETLIMITTEXT, type == REG_DWORD ? 8 : 16, 0);
}

static void change_dword_base(HWND hwndDlg, BOOL toHex, DWORD type)
{
    WCHAR buf[64];
    unsigned int len;
    UINT64 val;

    len = GetDlgItemTextW(hwndDlg, IDC_VALUE_DATA, buf, ARRAY_SIZE(buf));
    if (!len) SetDlgItemTextW(hwndDlg, IDC_VALUE_DATA, L"0");

    if ((isDecimal && !toHex) || (!isDecimal && toHex))
        return;

    if (len)
    {
        swscanf(buf, toHex ? L"%I64u" : L"%I64x", &val);
        swprintf(buf, ARRAY_SIZE(buf), toHex ? L"%I64x" : L"%I64u", val);
        SetDlgItemTextW(hwndDlg, IDC_VALUE_DATA, buf);
    }

    isDecimal = !toHex;

    set_dword_edit_limit(hwndDlg, type);
}

static INT_PTR CALLBACK modify_dword_dlgproc(HWND hwndDlg, UINT msg, WPARAM wparam, LPARAM lparam)
{
    static struct edit_params *params;
    WCHAR buf[64];
    int ret = 0;

    switch (msg)
    {
    case WM_INITDIALOG:
        params = (struct edit_params *)lparam;
        SetWindowLongPtrW(hwndDlg, DWLP_USER, (ULONG_PTR)params);
        set_dlgproc_value_name(hwndDlg, params);
        SetDlgItemTextW(hwndDlg, IDC_VALUE_DATA, params->data);
        CheckRadioButton(hwndDlg, IDC_DWORD_HEX, IDC_DWORD_DEC, IDC_DWORD_HEX);
        isDecimal = FALSE;
        if (params->type == REG_QWORD && LoadStringW(GetModuleHandleW(0), IDS_EDIT_QWORD, buf, ARRAY_SIZE(buf)))
            SetWindowTextW(hwndDlg, buf);
        set_dword_edit_limit(hwndDlg, params->type);
        return TRUE;
    case WM_COMMAND:
        switch (LOWORD(wparam))
        {
        case IDC_DWORD_HEX:
	    change_dword_base(hwndDlg, TRUE, params->type);
            break;
        case IDC_DWORD_DEC:
	    change_dword_base(hwndDlg, FALSE, params->type);
            break;
        case IDOK:
            params = (struct edit_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
            if (!SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, EM_LINELENGTH, 0, 0))
                SetDlgItemTextW(hwndDlg, IDC_VALUE_DATA, L"0");
            ret = update_registry_value(hwndDlg, params);
            /* fall through */
        case IDCANCEL:
            EndDialog(hwndDlg, ret);
            return TRUE;
        }
    }
    return FALSE;
}

static INT_PTR CALLBACK modify_binary_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    struct edit_params *params;
    int ret = 0;

    switch(uMsg) {
    case WM_INITDIALOG:
        params = (struct edit_params *)lParam;
        SetWindowLongPtrW(hwndDlg, DWLP_USER, (ULONG_PTR)params);
        set_dlgproc_value_name(hwndDlg, params);
        SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, HEM_SETDATA, (WPARAM)params->size, (LPARAM)params->data);
        SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, WM_SETFONT, (WPARAM) GetStockObject(ANSI_FIXED_FONT), TRUE);
        return TRUE;
    case WM_COMMAND:
        switch (LOWORD(wParam)) {
        case IDOK:
            params = (struct edit_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
            ret = update_registry_value(hwndDlg, params);
            /* fall through */
        case IDCANCEL:
            EndDialog(hwndDlg, ret);
            return TRUE;
        }
    }
    return FALSE;
}

static BOOL read_value(HWND hwnd, struct edit_params *params)
{
    LONG ret;
    WCHAR *buf = NULL;

    if ((ret = RegQueryValueExW(params->hkey, params->value_name, NULL, &params->type, NULL, &params->size)))
    {
        if (ret == ERROR_FILE_NOT_FOUND && !params->value_name)
        {
            params->type = REG_SZ;
            params->size = sizeof(WCHAR);
            params->data = malloc(params->size);
            *(WCHAR *)params->data = 0;
            return TRUE;
        }

        goto error;
    }

    buf = malloc(params->size + sizeof(WCHAR));

    if (RegQueryValueExW(params->hkey, params->value_name, NULL, &params->type, (BYTE *)buf, &params->size))
        goto error;

    if (params->size % sizeof(WCHAR) == 0)
        buf[params->size / sizeof(WCHAR)] = 0;

    params->data = buf;

    return TRUE;

error:
    error_code_messagebox(hwnd, IDS_BAD_VALUE, params->value_name);
    free(buf);
    params->data = NULL;
    return FALSE;
}

BOOL CreateKey(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPWSTR keyName)
{
    BOOL result = FALSE;
    LONG lRet = ERROR_SUCCESS;
    HKEY retKey = NULL;
    WCHAR newKey[MAX_NEW_KEY_LEN - 4];
    int keyNum;
    HKEY hKey;
         
    lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_CREATE_SUB_KEY, &hKey);
    if (lRet) {
        error_code_messagebox(hwnd, IDS_CREATE_KEY_FAILED);
	goto done;
    }

    if (!LoadStringW(GetModuleHandleW(0), IDS_NEWKEY, newKey, ARRAY_SIZE(newKey))) goto done;

    /* try to find a name for the key being created (maximum = 100 attempts) */
    for (keyNum = 1; keyNum < 100; keyNum++) {
	wsprintfW(keyName, newKey, keyNum);
	lRet = RegOpenKeyW(hKey, keyName, &retKey);
	if (lRet) break;
	RegCloseKey(retKey);
    }
    if (lRet == ERROR_SUCCESS) goto done;
    
    lRet = RegCreateKeyW(hKey, keyName, &retKey);
    if (lRet) {
        error_code_messagebox(hwnd, IDS_CREATE_KEY_FAILED);
	goto done;
    }

    result = TRUE;

done:
    RegCloseKey(retKey);
    return result;
}

static void format_dlgproc_string(struct edit_params *params)
{
    int i, j, count, len;
    WCHAR *str, *buf;

    if (params->type == REG_DWORD || params->type == REG_QWORD)
    {
        UINT64 value = *((UINT64 *)params->data);

        params->data = realloc(params->data, 32 * sizeof(WCHAR));
        swprintf(params->data, 32, params->type == REG_DWORD ? L"%lx" : L"%I64x", value);
        return;
    }

    /* REG_MULTI_SZ */
    len = params->size / sizeof(WCHAR);
    str = params->data;

    for (i = 0, count = 0; i < len; i++)
    {
        if (!str[i] && str[i + 1]) count++;
    }

    buf = malloc(params->size + (count * sizeof(WCHAR)));

    for (i = 0, j = 0; i < len; i++)
    {
        if (!str[i] && str[i + 1])
        {
            buf[j++] = '\r';
            buf[j++] = '\n';
        }
        else buf[j++] = str[i];
    }

    free(params->data);
    str = NULL;
    params->data = buf;
}

BOOL ModifyValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR valueName)
{
    struct edit_params params;
    BOOL result = FALSE;

    if (RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &params.hkey))
    {
        error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
        return FALSE;
    }

    params.value_name = valueName;

    if (!read_value(hwnd, &params))
    {
        RegCloseKey(params.hkey);
        return FALSE;
    }

    switch (params.type)
    {
    case REG_SZ:
    case REG_EXPAND_SZ:
        result = DialogBoxParamW(NULL, MAKEINTRESOURCEW(IDD_EDIT_STRING), hwnd,
                                 modify_string_dlgproc, (LPARAM)&params);
        break;
    case REG_DWORD:
    case REG_QWORD:
        format_dlgproc_string(&params);
        result = DialogBoxParamW(NULL, MAKEINTRESOURCEW(IDD_EDIT_DWORD), hwnd,
                                 modify_dword_dlgproc, (LPARAM)&params);
        break;
    case REG_MULTI_SZ:
        format_dlgproc_string(&params);
        result = DialogBoxParamW(NULL, MAKEINTRESOURCEW(IDD_EDIT_MULTI_STRING), hwnd,
                                 modify_string_dlgproc, (LPARAM)&params);
        break;
    default: /* hex data types */
        result = DialogBoxParamW(NULL, MAKEINTRESOURCEW(IDD_EDIT_BINARY), hwnd,
                                 modify_binary_dlgproc, (LPARAM)&params);
    }

    /* Update the listview item with the new data string */
    if (result)
    {
        int index = SendMessageW(g_pChildWnd->hListWnd, LVM_GETNEXTITEM, -1,
                                 MAKELPARAM(LVNI_FOCUSED | LVNI_SELECTED, 0));

        format_value_data(g_pChildWnd->hListWnd, index, params.type, params.data, params.size);
    }

    free(params.data);
    RegCloseKey(params.hkey);
    return result;
}

BOOL DeleteKey(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath)
{
    BOOL result = FALSE;
    LONG lRet;
    HKEY hKey;

    lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ|KEY_SET_VALUE, &hKey);
    if (lRet) {
        error_code_messagebox(hwnd, IDS_DELETE_KEY_FAILED);
	return FALSE;
    }
    
    if (messagebox(hwnd, MB_YESNO | MB_ICONEXCLAMATION, IDS_DELETE_KEY_TITLE,
                   IDS_DELETE_KEY_TEXT) != IDYES)
	goto done;
	
    lRet = SHDeleteKeyW(hKeyRoot, keyPath);
    if (lRet) {
        error_code_messagebox(hwnd, IDS_BAD_KEY, keyPath);
	goto done;
    }
    result = TRUE;
    
done:
    RegCloseKey(hKey);
    return result;
}

BOOL DeleteValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR valueName)
{
    BOOL result = FALSE;
    LONG lRet;
    HKEY hKey;

    lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
    if (lRet) return FALSE;

    lRet = RegDeleteValueW(hKey, valueName);
    if (lRet && valueName) {
        error_code_messagebox(hwnd, IDS_BAD_VALUE, valueName);
    }
    if (lRet) goto done;
    result = TRUE;

done:
    RegCloseKey(hKey);
    return result;
}

BOOL CreateValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, DWORD valueType, LPWSTR valueName)
{
    LONG lRet = ERROR_SUCCESS;
    WCHAR newValue[256];
    UINT64 value = 0;
    DWORD size_bytes;
    BOOL result = FALSE;
    int valueNum, index;
    HKEY hKey;
    LVITEMW item;
         
    lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
    if (lRet) {
        error_code_messagebox(hwnd, IDS_CREATE_VALUE_FAILED);
	return FALSE;
    }

    if (!LoadStringW(GetModuleHandleW(0), IDS_NEWVALUE, newValue, ARRAY_SIZE(newValue))) goto done;

    /* try to find a name for the value being created (maximum = 100 attempts) */
    for (valueNum = 1; valueNum < 100; valueNum++) {
	wsprintfW(valueName, newValue, valueNum);
	lRet = RegQueryValueExW(hKey, valueName, 0, 0, 0, 0);
	if (lRet == ERROR_FILE_NOT_FOUND) break;
    }
    if (lRet != ERROR_FILE_NOT_FOUND) {
        error_code_messagebox(hwnd, IDS_CREATE_VALUE_FAILED);
	goto done;
    }

    switch (valueType)
    {
        case REG_DWORD:
        case REG_DWORD_BIG_ENDIAN:
            size_bytes = sizeof(DWORD);
            break;
        case REG_QWORD:
            size_bytes = sizeof(UINT64);
            break;
        case REG_BINARY:
            size_bytes = 0;
            break;
        case REG_MULTI_SZ:
            size_bytes = 2 * sizeof(WCHAR);
            break;
        default: /* REG_NONE, REG_SZ, REG_EXPAND_SZ */
            size_bytes = sizeof(WCHAR);
    }

    lRet = RegSetValueExW(hKey, valueName, 0, valueType, (BYTE *)&value, size_bytes);
    if (lRet) {
        error_code_messagebox(hwnd, IDS_CREATE_VALUE_FAILED);
	goto done;
    }

    /* Add the new item to the listview */
    index = AddEntryToList(g_pChildWnd->hListWnd, valueName, valueType, (BYTE *)&value, size_bytes, -1);
    item.state = LVIS_FOCUSED | LVIS_SELECTED;
    item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
    SendMessageW(g_pChildWnd->hListWnd, LVM_SETITEMSTATE, index, (LPARAM)&item);

    result = TRUE;

done:
    RegCloseKey(hKey);
    return result;
}

BOOL RenameValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR oldName, LPCWSTR newName)
{
    struct edit_params params;
    BOOL result = FALSE;

    if (!oldName) return FALSE;
    if (!newName) return FALSE;

    if (RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &params.hkey))
    {
        error_code_messagebox(hwnd, IDS_RENAME_VALUE_FAILED);
        return FALSE;
    }

    if (!RegQueryValueExW(params.hkey, newName, NULL, NULL, NULL, NULL))
    {
        error_code_messagebox(hwnd, IDS_VALUE_EXISTS, oldName);
        goto done;
    }

    params.value_name = oldName;
    if (!read_value(hwnd, &params)) goto done;

    if (RegSetValueExW(params.hkey, newName, 0, params.type, (BYTE *)params.data, params.size))
    {
        error_code_messagebox(hwnd, IDS_RENAME_VALUE_FAILED);
        goto done;
    }

    if (RegDeleteValueW(params.hkey, oldName))
    {
        RegDeleteValueW(params.hkey, newName);
        error_code_messagebox(hwnd, IDS_RENAME_VALUE_FAILED);
        goto done;
    }

    result = TRUE;

done:
    free(params.data);
    RegCloseKey(params.hkey);
    return result;
}

BOOL RenameKey(HWND hwnd, HKEY hRootKey, LPCWSTR keyPath, LPCWSTR newName)
{
    LPWSTR parentPath = 0;
    LPCWSTR srcSubKey = 0;
    HKEY parentKey = 0;
    HKEY destKey = 0;
    BOOL result = FALSE;
    LONG lRet;
    DWORD disposition;

    if (!keyPath || !newName) return FALSE;

    if (!wcsrchr(keyPath, '\\')) {
	parentKey = hRootKey;
	srcSubKey = keyPath;
    } else {
	LPWSTR srcSubKey_copy;

	parentPath = wcsdup(keyPath);
	srcSubKey_copy = wcsrchr(parentPath, '\\');
	*srcSubKey_copy = 0;
	srcSubKey = srcSubKey_copy + 1;
	lRet = RegOpenKeyExW(hRootKey, parentPath, 0, KEY_READ | KEY_CREATE_SUB_KEY, &parentKey);
	if (lRet) {
            error_code_messagebox(hwnd, IDS_RENAME_KEY_FAILED);
	    goto done;
	}
    }

    /* The following fails if the old name is the same as the new name. */
    if (!lstrcmpW(srcSubKey, newName)) goto done;

    lRet = RegCreateKeyExW(parentKey, newName, 0, NULL, REG_OPTION_NON_VOLATILE,
        KEY_WRITE, NULL /* FIXME */, &destKey, &disposition);
    if (disposition == REG_OPENED_EXISTING_KEY)
        lRet = ERROR_FILE_EXISTS;
    if (lRet) {
        error_code_messagebox(hwnd, IDS_KEY_EXISTS, srcSubKey);
        goto done;
    }

    /* FIXME: SHCopyKey does not copy the security attributes */
    lRet = SHCopyKeyW(parentKey, srcSubKey, destKey, 0);
    if (lRet) {
        RegCloseKey(destKey);
        RegDeleteKeyW(parentKey, newName);
        error_code_messagebox(hwnd, IDS_RENAME_KEY_FAILED);
        goto done;
    }

    lRet = SHDeleteKeyW(hRootKey, keyPath);
    if (lRet) {
        error_code_messagebox(hwnd, IDS_RENAME_KEY_FAILED);
        goto done;
    }

    result = TRUE;

done:
    RegCloseKey(destKey);
    RegCloseKey(parentKey);
    free(parentPath);
    return result;
}