2 * Registry editing UI functions.
4 * Copyright (C) 2003 Dimitrie O. Paun
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
31 #include "wine/heap.h"
34 static const WCHAR
* editValueName
;
35 static WCHAR
* stringValueData
;
36 static BOOL isDecimal
;
41 const WCHAR
*value_name
;
47 static int vmessagebox(HWND hwnd
, int buttons
, int titleId
, int resId
, va_list va_args
)
54 LoadStringW(hInst
, titleId
, title
, ARRAY_SIZE(title
));
55 LoadStringW(hInst
, resId
, fmt
, ARRAY_SIZE(fmt
));
57 FormatMessageW(FORMAT_MESSAGE_FROM_STRING
|FORMAT_MESSAGE_ALLOCATE_BUFFER
,
58 fmt
, 0, 0, (WCHAR
*)&str
, 0, &va_args
);
59 ret
= MessageBoxW(hwnd
, str
, title
, buttons
);
65 int WINAPIV
messagebox(HWND hwnd
, int buttons
, int titleId
, int resId
, ...)
71 result
= vmessagebox(hwnd
, buttons
, titleId
, resId
, ap
);
77 static void WINAPIV
error_code_messagebox(HWND hwnd
, unsigned int msg_id
, ...)
82 vmessagebox(hwnd
, MB_OK
|MB_ICONERROR
, IDS_ERROR
, msg_id
, ap
);
86 static BOOL
change_dword_base(HWND hwndDlg
, BOOL toHex
)
91 if (!GetDlgItemTextW(hwndDlg
, IDC_VALUE_DATA
, buf
, ARRAY_SIZE(buf
))) return FALSE
;
92 if (!swscanf(buf
, toHex
? L
"%I64u" : L
"%I64x", &val
)) return FALSE
;
93 wsprintfW(buf
, toHex
? L
"%I64x" : L
"%I64u", val
);
94 return SetDlgItemTextW(hwndDlg
, IDC_VALUE_DATA
, buf
);
97 static INT_PTR CALLBACK
modify_dlgproc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
105 SetDlgItemTextW(hwndDlg
, IDC_VALUE_NAME
, editValueName
);
106 SetDlgItemTextW(hwndDlg
, IDC_VALUE_DATA
, stringValueData
);
107 CheckRadioButton(hwndDlg
, IDC_DWORD_HEX
, IDC_DWORD_DEC
, IDC_DWORD_HEX
);
109 if (lParam
== REG_QWORD
&& LoadStringW(GetModuleHandleW(0), IDS_EDIT_QWORD
, buff
, ARRAY_SIZE(buff
)))
110 SetWindowTextW(hwndDlg
, buff
);
113 switch (LOWORD(wParam
)) {
115 if (isDecimal
&& change_dword_base(hwndDlg
, TRUE
)) isDecimal
= FALSE
;
118 if (!isDecimal
&& change_dword_base(hwndDlg
, FALSE
)) isDecimal
= TRUE
;
121 if ((hwndValue
= GetDlgItem(hwndDlg
, IDC_VALUE_DATA
))) {
122 len
= GetWindowTextLengthW(hwndValue
);
123 stringValueData
= heap_xrealloc(stringValueData
, (len
+ 1) * sizeof(WCHAR
));
124 if (!GetWindowTextW(hwndValue
, stringValueData
, len
+ 1))
125 *stringValueData
= 0;
129 EndDialog(hwndDlg
, wParam
);
136 static INT_PTR CALLBACK
bin_modify_dlgproc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
138 struct edit_params
*params
;
145 params
= (struct edit_params
*)lParam
;
146 SetWindowLongPtrW(hwndDlg
, DWLP_USER
, (ULONG_PTR
)params
);
147 if (params
->value_name
)
148 SetDlgItemTextW(hwndDlg
, IDC_VALUE_NAME
, params
->value_name
);
150 SetDlgItemTextW(hwndDlg
, IDC_VALUE_NAME
, g_pszDefaultValueName
);
151 SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, HEM_SETDATA
, (WPARAM
)params
->size
, (LPARAM
)params
->data
);
152 SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, WM_SETFONT
, (WPARAM
) GetStockObject(ANSI_FIXED_FONT
), TRUE
);
155 switch (LOWORD(wParam
)) {
157 params
= (struct edit_params
*)GetWindowLongPtrW(hwndDlg
, DWLP_USER
);
158 size
= SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, HEM_GETDATA
, 0, 0);
159 data
= heap_xalloc(size
);
161 SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, HEM_GETDATA
, (WPARAM
)size
, (LPARAM
)data
);
162 lRet
= RegSetValueExW(params
->hkey
, params
->value_name
, 0, params
->type
, data
, size
);
165 if (lRet
== ERROR_SUCCESS
)
166 EndDialog(hwndDlg
, 1);
169 error_code_messagebox(hwndDlg
, IDS_SET_VALUE_FAILED
);
170 EndDialog(hwndDlg
, 0);
174 EndDialog(hwndDlg
, 0);
181 static LPWSTR
read_value(HWND hwnd
, HKEY hKey
, LPCWSTR valueName
, DWORD
*lpType
, LONG
*len
)
184 LPWSTR buffer
= NULL
;
187 lRet
= RegQueryValueExW(hKey
, valueName
, NULL
, lpType
, NULL
, &valueDataLen
);
189 if (lRet
== ERROR_FILE_NOT_FOUND
&& !valueName
) { /* no default value here, make it up */
191 if (lpType
) *lpType
= REG_SZ
;
192 buffer
= heap_xalloc(sizeof(WCHAR
));
196 error_code_messagebox(hwnd
, IDS_BAD_VALUE
, valueName
);
200 buffer
= heap_xalloc(valueDataLen
+ sizeof(WCHAR
));
201 lRet
= RegQueryValueExW(hKey
, valueName
, 0, 0, (LPBYTE
)buffer
, &valueDataLen
);
203 error_code_messagebox(hwnd
, IDS_BAD_VALUE
, valueName
);
206 if((valueDataLen
% sizeof(WCHAR
)) == 0)
207 buffer
[valueDataLen
/ sizeof(WCHAR
)] = 0;
208 if(len
) *len
= valueDataLen
;
216 BOOL
CreateKey(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPWSTR keyName
)
219 LONG lRet
= ERROR_SUCCESS
;
221 WCHAR newKey
[MAX_NEW_KEY_LEN
- 4];
225 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_CREATE_SUB_KEY
, &hKey
);
227 error_code_messagebox(hwnd
, IDS_CREATE_KEY_FAILED
);
231 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWKEY
, newKey
, ARRAY_SIZE(newKey
))) goto done
;
233 /* try to find a name for the key being created (maximum = 100 attempts) */
234 for (keyNum
= 1; keyNum
< 100; keyNum
++) {
235 wsprintfW(keyName
, newKey
, keyNum
);
236 lRet
= RegOpenKeyW(hKey
, keyName
, &retKey
);
240 if (lRet
== ERROR_SUCCESS
) goto done
;
242 lRet
= RegCreateKeyW(hKey
, keyName
, &retKey
);
244 error_code_messagebox(hwnd
, IDS_CREATE_KEY_FAILED
);
255 BOOL
ModifyValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR valueName
)
263 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
265 error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
269 editValueName
= valueName
? valueName
: g_pszDefaultValueName
;
270 if(!(stringValueData
= read_value(hwnd
, hKey
, valueName
, &type
, &len
))) goto done
;
272 if ( (type
== REG_SZ
) || (type
== REG_EXPAND_SZ
) ) {
273 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_STRING
), hwnd
, modify_dlgproc
) == IDOK
) {
274 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (LPBYTE
)stringValueData
, (lstrlenW(stringValueData
) + 1) * sizeof(WCHAR
));
275 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
276 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
278 } else if ( type
== REG_DWORD
) {
279 DWORD value
= *((DWORD
*)stringValueData
);
280 stringValueData
= heap_xrealloc(stringValueData
, 64);
281 wsprintfW(stringValueData
, L
"%x", value
);
282 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_DWORD
), hwnd
, modify_dlgproc
) == IDOK
)
285 if (swscanf(stringValueData
, isDecimal
? L
"%lu" : L
"%lx", &val
))
287 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (BYTE
*)&val
, sizeof(val
));
288 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
289 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
292 } else if ( type
== REG_QWORD
) {
293 UINT64 value
= *((UINT64
*)stringValueData
);
294 stringValueData
= heap_xrealloc(stringValueData
, 64);
295 swprintf(stringValueData
, 64, L
"%I64x", value
);
296 if (DialogBoxParamW(0, MAKEINTRESOURCEW(IDD_EDIT_DWORD
), hwnd
, modify_dlgproc
, type
) == IDOK
)
298 if (swscanf(stringValueData
, isDecimal
? L
"%I64u" : L
"%I64x", &value
))
300 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (BYTE
*)&value
, sizeof(value
));
301 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
302 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
305 } else if ( type
== REG_MULTI_SZ
) {
306 WCHAR char1
= '\r', char2
= '\n';
307 WCHAR
*tmpValueData
= NULL
;
310 for (i
= 0, count
= 0; i
< len
/ sizeof(WCHAR
); i
++)
311 if ( !stringValueData
[i
] && stringValueData
[i
+ 1] )
313 tmpValueData
= heap_xalloc(len
+ (count
* sizeof(WCHAR
)));
315 for ( i
= 0, j
= 0; i
< len
/ sizeof(WCHAR
); i
++)
317 if ( !stringValueData
[i
] && stringValueData
[i
+ 1])
319 tmpValueData
[j
++] = char1
;
320 tmpValueData
[j
++] = char2
;
323 tmpValueData
[j
++] = stringValueData
[i
];
326 heap_free(stringValueData
);
327 stringValueData
= tmpValueData
;
330 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_MULTI_STRING
), hwnd
, modify_dlgproc
) == IDOK
)
332 len
= lstrlenW( stringValueData
);
333 tmpValueData
= heap_xalloc((len
+ 2) * sizeof(WCHAR
));
335 for (i
= 0, j
= 0; i
< len
; i
++)
337 if ( stringValueData
[i
] == char1
&& stringValueData
[i
+ 1] == char2
)
339 if ( tmpValueData
[j
- 1] != 0)
340 tmpValueData
[j
++] = 0;
344 tmpValueData
[j
++] = stringValueData
[i
];
347 tmpValueData
[j
++] = 0;
348 tmpValueData
[j
++] = 0;
349 heap_free(stringValueData
);
350 stringValueData
= tmpValueData
;
352 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (LPBYTE
)stringValueData
, j
* sizeof(WCHAR
));
353 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
354 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
357 else /* hex data types */
359 struct edit_params params
;
362 params
.value_name
= valueName
;
364 params
.data
= stringValueData
;
366 result
= DialogBoxParamW(NULL
, MAKEINTRESOURCEW(IDD_EDIT_BINARY
), hwnd
,
367 bin_modify_dlgproc
, (LPARAM
)¶ms
);
370 /* Update the listview item with the new data string */
373 int index
= SendMessageW(g_pChildWnd
->hListWnd
, LVM_GETNEXTITEM
, -1,
374 MAKELPARAM(LVNI_FOCUSED
| LVNI_SELECTED
, 0));
375 heap_free(stringValueData
);
376 stringValueData
= read_value(hwnd
, hKey
, valueName
, &type
, &len
);
377 format_value_data(g_pChildWnd
->hListWnd
, index
, type
, stringValueData
, len
);
381 heap_free(stringValueData
);
382 stringValueData
= NULL
;
387 BOOL
DeleteKey(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
)
393 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
|KEY_SET_VALUE
, &hKey
);
395 error_code_messagebox(hwnd
, IDS_DELETE_KEY_FAILED
);
399 if (messagebox(hwnd
, MB_YESNO
| MB_ICONEXCLAMATION
, IDS_DELETE_KEY_TITLE
,
400 IDS_DELETE_KEY_TEXT
) != IDYES
)
403 lRet
= SHDeleteKeyW(hKeyRoot
, keyPath
);
405 error_code_messagebox(hwnd
, IDS_BAD_KEY
, keyPath
);
415 BOOL
DeleteValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR valueName
)
421 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
422 if (lRet
) return FALSE
;
424 lRet
= RegDeleteValueW(hKey
, valueName
);
425 if (lRet
&& valueName
) {
426 error_code_messagebox(hwnd
, IDS_BAD_VALUE
, valueName
);
436 BOOL
CreateValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, DWORD valueType
, LPWSTR valueName
)
438 LONG lRet
= ERROR_SUCCESS
;
447 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
449 error_code_messagebox(hwnd
, IDS_CREATE_VALUE_FAILED
);
453 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWVALUE
, newValue
, ARRAY_SIZE(newValue
))) goto done
;
455 /* try to find a name for the value being created (maximum = 100 attempts) */
456 for (valueNum
= 1; valueNum
< 100; valueNum
++) {
457 wsprintfW(valueName
, newValue
, valueNum
);
458 lRet
= RegQueryValueExW(hKey
, valueName
, 0, 0, 0, 0);
459 if (lRet
== ERROR_FILE_NOT_FOUND
) break;
461 if (lRet
!= ERROR_FILE_NOT_FOUND
) {
462 error_code_messagebox(hwnd
, IDS_CREATE_VALUE_FAILED
);
469 case REG_DWORD_BIG_ENDIAN
:
470 size_bytes
= sizeof(DWORD
);
473 size_bytes
= sizeof(UINT64
);
479 size_bytes
= 2 * sizeof(WCHAR
);
481 default: /* REG_NONE, REG_SZ, REG_EXPAND_SZ */
482 size_bytes
= sizeof(WCHAR
);
485 lRet
= RegSetValueExW(hKey
, valueName
, 0, valueType
, (BYTE
*)&value
, size_bytes
);
487 error_code_messagebox(hwnd
, IDS_CREATE_VALUE_FAILED
);
491 /* Add the new item to the listview */
492 index
= AddEntryToList(g_pChildWnd
->hListWnd
, valueName
, valueType
, (BYTE
*)&value
, size_bytes
, -1);
493 item
.state
= LVIS_FOCUSED
| LVIS_SELECTED
;
494 item
.stateMask
= LVIS_FOCUSED
| LVIS_SELECTED
;
495 SendMessageW(g_pChildWnd
->hListWnd
, LVM_SETITEMSTATE
, index
, (LPARAM
)&item
);
504 BOOL
RenameValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR oldName
, LPCWSTR newName
)
512 if (!oldName
) return FALSE
;
513 if (!newName
) return FALSE
;
515 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
517 error_code_messagebox(hwnd
, IDS_RENAME_VALUE_FAILED
);
521 if (!RegQueryValueExW(hKey
, newName
, NULL
, NULL
, NULL
, NULL
)) {
522 error_code_messagebox(hwnd
, IDS_VALUE_EXISTS
, oldName
);
525 value
= read_value(hwnd
, hKey
, oldName
, &type
, &len
);
526 if(!value
) goto done
;
527 lRet
= RegSetValueExW(hKey
, newName
, 0, type
, (BYTE
*)value
, len
);
529 error_code_messagebox(hwnd
, IDS_RENAME_VALUE_FAILED
);
532 lRet
= RegDeleteValueW(hKey
, oldName
);
534 RegDeleteValueW(hKey
, newName
);
535 error_code_messagebox(hwnd
, IDS_RENAME_VALUE_FAILED
);
547 BOOL
RenameKey(HWND hwnd
, HKEY hRootKey
, LPCWSTR keyPath
, LPCWSTR newName
)
549 LPWSTR parentPath
= 0;
550 LPCWSTR srcSubKey
= 0;
557 if (!keyPath
|| !newName
) return FALSE
;
559 if (!wcsrchr(keyPath
, '\\')) {
560 parentKey
= hRootKey
;
563 LPWSTR srcSubKey_copy
;
565 parentPath
= heap_xalloc((lstrlenW(keyPath
) + 1) * sizeof(WCHAR
));
566 lstrcpyW(parentPath
, keyPath
);
567 srcSubKey_copy
= wcsrchr(parentPath
, '\\');
569 srcSubKey
= srcSubKey_copy
+ 1;
570 lRet
= RegOpenKeyExW(hRootKey
, parentPath
, 0, KEY_READ
| KEY_CREATE_SUB_KEY
, &parentKey
);
572 error_code_messagebox(hwnd
, IDS_RENAME_KEY_FAILED
);
577 /* The following fails if the old name is the same as the new name. */
578 if (!lstrcmpW(srcSubKey
, newName
)) goto done
;
580 lRet
= RegCreateKeyExW(parentKey
, newName
, 0, NULL
, REG_OPTION_NON_VOLATILE
,
581 KEY_WRITE
, NULL
/* FIXME */, &destKey
, &disposition
);
582 if (disposition
== REG_OPENED_EXISTING_KEY
)
583 lRet
= ERROR_FILE_EXISTS
;
585 error_code_messagebox(hwnd
, IDS_KEY_EXISTS
, srcSubKey
);
589 /* FIXME: SHCopyKey does not copy the security attributes */
590 lRet
= SHCopyKeyW(parentKey
, srcSubKey
, destKey
, 0);
592 RegCloseKey(destKey
);
593 RegDeleteKeyW(parentKey
, newName
);
594 error_code_messagebox(hwnd
, IDS_RENAME_KEY_FAILED
);
598 lRet
= SHDeleteKeyW(hRootKey
, keyPath
);
600 error_code_messagebox(hwnd
, IDS_RENAME_KEY_FAILED
);
607 RegCloseKey(destKey
);
609 RegCloseKey(parentKey
);
610 heap_free(parentPath
);