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 */
32 #include "wine/heap.h"
33 #include "wine/unicode.h"
38 static const WCHAR
* editValueName
;
39 static WCHAR
* stringValueData
;
40 static BOOL isDecimal
;
45 LPCWSTR lpszValueName
;
50 static int vmessagebox(HWND hwnd
, int buttons
, int titleId
, int resId
, __ms_va_list va_args
)
57 LoadStringW(hInst
, titleId
, title
, COUNT_OF(title
));
58 LoadStringW(hInst
, resId
, fmt
, COUNT_OF(fmt
));
60 FormatMessageW(FORMAT_MESSAGE_FROM_STRING
|FORMAT_MESSAGE_ALLOCATE_BUFFER
,
61 fmt
, 0, 0, (WCHAR
*)&str
, 0, &va_args
);
62 ret
= MessageBoxW(hwnd
, str
, title
, buttons
);
68 int WINAPIV
messagebox(HWND hwnd
, int buttons
, int titleId
, int resId
, ...)
73 __ms_va_start(ap
, resId
);
74 result
= vmessagebox(hwnd
, buttons
, titleId
, resId
, ap
);
80 static void WINAPIV
error_code_messagebox(HWND hwnd
, unsigned int msg_id
, ...)
84 __ms_va_start(ap
, msg_id
);
85 vmessagebox(hwnd
, MB_OK
|MB_ICONERROR
, IDS_ERROR
, msg_id
, ap
);
89 static BOOL
change_dword_base(HWND hwndDlg
, BOOL toHex
)
91 static const WCHAR percent_u
[] = {'%','u',0};
92 static const WCHAR percent_x
[] = {'%','x',0};
97 if (!GetDlgItemTextW(hwndDlg
, IDC_VALUE_DATA
, buf
, COUNT_OF(buf
))) return FALSE
;
98 if (!swscanf(buf
, toHex
? percent_u
: percent_x
, &val
)) return FALSE
;
99 wsprintfW(buf
, toHex
? percent_x
: percent_u
, val
);
100 return SetDlgItemTextW(hwndDlg
, IDC_VALUE_DATA
, buf
);
103 static INT_PTR CALLBACK
modify_dlgproc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
110 SetDlgItemTextW(hwndDlg
, IDC_VALUE_NAME
, editValueName
);
111 SetDlgItemTextW(hwndDlg
, IDC_VALUE_DATA
, stringValueData
);
112 CheckRadioButton(hwndDlg
, IDC_DWORD_HEX
, IDC_DWORD_DEC
, isDecimal
? IDC_DWORD_DEC
: IDC_DWORD_HEX
);
115 switch (LOWORD(wParam
)) {
117 if (isDecimal
&& change_dword_base(hwndDlg
, TRUE
)) isDecimal
= FALSE
;
120 if (!isDecimal
&& change_dword_base(hwndDlg
, FALSE
)) isDecimal
= TRUE
;
123 if ((hwndValue
= GetDlgItem(hwndDlg
, IDC_VALUE_DATA
))) {
124 len
= GetWindowTextLengthW(hwndValue
);
125 stringValueData
= heap_xrealloc(stringValueData
, (len
+ 1) * sizeof(WCHAR
));
126 if (!GetWindowTextW(hwndValue
, stringValueData
, len
+ 1))
127 *stringValueData
= 0;
131 EndDialog(hwndDlg
, wParam
);
138 static INT_PTR CALLBACK
bin_modify_dlgproc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
140 struct edit_params
*params
;
147 params
= (struct edit_params
*)lParam
;
148 SetWindowLongPtrW(hwndDlg
, DWLP_USER
, (ULONG_PTR
)params
);
149 if (params
->lpszValueName
)
150 SetDlgItemTextW(hwndDlg
, IDC_VALUE_NAME
, params
->lpszValueName
);
152 SetDlgItemTextW(hwndDlg
, IDC_VALUE_NAME
, g_pszDefaultValueName
);
153 SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, HEM_SETDATA
, (WPARAM
)params
->cbData
, (LPARAM
)params
->pData
);
154 SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, WM_SETFONT
, (WPARAM
) GetStockObject(ANSI_FIXED_FONT
), TRUE
);
157 switch (LOWORD(wParam
)) {
159 params
= (struct edit_params
*)GetWindowLongPtrW(hwndDlg
, DWLP_USER
);
160 cbData
= SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, HEM_GETDATA
, 0, 0);
161 pData
= heap_xalloc(cbData
);
165 SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, HEM_GETDATA
, (WPARAM
)cbData
, (LPARAM
)pData
);
166 lRet
= RegSetValueExW(params
->hKey
, params
->lpszValueName
, 0, REG_BINARY
, pData
, cbData
);
170 lRet
= ERROR_OUTOFMEMORY
;
172 if (lRet
== ERROR_SUCCESS
)
173 EndDialog(hwndDlg
, 1);
176 error_code_messagebox(hwndDlg
, IDS_SET_VALUE_FAILED
);
177 EndDialog(hwndDlg
, 0);
181 EndDialog(hwndDlg
, 0);
188 static BOOL
check_value(HWND hwnd
, HKEY hKey
, LPCWSTR valueName
)
191 LONG lRet
= RegQueryValueExW(hKey
, valueName
? valueName
: &empty
, 0, NULL
, 0, NULL
);
192 if(lRet
!= ERROR_SUCCESS
) return FALSE
;
196 static LPWSTR
read_value(HWND hwnd
, HKEY hKey
, LPCWSTR valueName
, DWORD
*lpType
, LONG
*len
)
199 LPWSTR buffer
= NULL
;
203 lRet
= RegQueryValueExW(hKey
, valueName
? valueName
: &empty
, 0, lpType
, 0, &valueDataLen
);
204 if (lRet
!= ERROR_SUCCESS
) {
205 if (lRet
== ERROR_FILE_NOT_FOUND
&& !valueName
) { /* no default value here, make it up */
207 if (lpType
) *lpType
= REG_SZ
;
208 buffer
= heap_xalloc(sizeof(WCHAR
));
212 error_code_messagebox(hwnd
, IDS_BAD_VALUE
, valueName
);
215 if ( *lpType
== REG_DWORD
) valueDataLen
= sizeof(DWORD
);
216 buffer
= heap_xalloc(valueDataLen
+ sizeof(WCHAR
));
217 lRet
= RegQueryValueExW(hKey
, valueName
, 0, 0, (LPBYTE
)buffer
, &valueDataLen
);
218 if (lRet
!= ERROR_SUCCESS
) {
219 error_code_messagebox(hwnd
, IDS_BAD_VALUE
, valueName
);
222 if((valueDataLen
% sizeof(WCHAR
)) == 0)
223 buffer
[valueDataLen
/ sizeof(WCHAR
)] = 0;
224 if(len
) *len
= valueDataLen
;
232 BOOL
CreateKey(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPWSTR keyName
)
235 LONG lRet
= ERROR_SUCCESS
;
237 WCHAR newKey
[MAX_NEW_KEY_LEN
- 4];
241 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_CREATE_SUB_KEY
, &hKey
);
242 if (lRet
!= ERROR_SUCCESS
) {
243 error_code_messagebox(hwnd
, IDS_CREATE_KEY_FAILED
);
247 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWKEY
, newKey
, COUNT_OF(newKey
))) goto done
;
249 /* try to find a name for the key being created (maximum = 100 attempts) */
250 for (keyNum
= 1; keyNum
< 100; keyNum
++) {
251 wsprintfW(keyName
, newKey
, keyNum
);
252 lRet
= RegOpenKeyW(hKey
, keyName
, &retKey
);
253 if (lRet
!= ERROR_SUCCESS
) break;
256 if (lRet
== ERROR_SUCCESS
) goto done
;
258 lRet
= RegCreateKeyW(hKey
, keyName
, &retKey
);
259 if (lRet
!= ERROR_SUCCESS
) {
260 error_code_messagebox(hwnd
, IDS_CREATE_KEY_FAILED
);
271 BOOL
ModifyValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR valueName
)
279 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
280 if (lRet
!= ERROR_SUCCESS
) {
281 error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
285 editValueName
= valueName
? valueName
: g_pszDefaultValueName
;
286 if(!(stringValueData
= read_value(hwnd
, hKey
, valueName
, &type
, &len
))) goto done
;
288 if ( (type
== REG_SZ
) || (type
== REG_EXPAND_SZ
) ) {
289 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_STRING
), hwnd
, modify_dlgproc
) == IDOK
) {
290 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (LPBYTE
)stringValueData
, (lstrlenW(stringValueData
) + 1) * sizeof(WCHAR
));
291 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
292 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
294 } else if ( type
== REG_DWORD
) {
295 const WCHAR u
[] = {'%','u',0};
296 const WCHAR x
[] = {'%','x',0};
297 wsprintfW(stringValueData
, isDecimal
? u
: x
, *((DWORD
*)stringValueData
));
298 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_DWORD
), hwnd
, modify_dlgproc
) == IDOK
) {
300 CHAR
* valueA
= GetMultiByteString(stringValueData
);
301 if (sscanf(valueA
, isDecimal
? "%u" : "%x", &val
)) {
302 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (BYTE
*)&val
, sizeof(val
));
303 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
304 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
308 } else if ( type
== REG_MULTI_SZ
) {
309 WCHAR char1
= '\r', char2
= '\n';
310 WCHAR
*tmpValueData
= NULL
;
313 for ( i
= 0, count
= 0; i
< len
- 1; i
++)
314 if ( !stringValueData
[i
] && stringValueData
[i
+ 1] )
316 tmpValueData
= heap_xalloc((len
+ count
) * sizeof(WCHAR
));
318 for ( i
= 0, j
= 0; i
< len
- 1; i
++)
320 if ( !stringValueData
[i
] && stringValueData
[i
+ 1])
322 tmpValueData
[j
++] = char1
;
323 tmpValueData
[j
++] = char2
;
326 tmpValueData
[j
++] = stringValueData
[i
];
328 tmpValueData
[j
] = stringValueData
[i
];
329 heap_free(stringValueData
);
330 stringValueData
= tmpValueData
;
333 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_MULTI_STRING
), hwnd
, modify_dlgproc
) == IDOK
)
335 len
= lstrlenW( stringValueData
);
336 tmpValueData
= heap_xalloc((len
+ 2) * sizeof(WCHAR
));
338 for ( i
= 0, j
= 0; i
< len
- 1; i
++)
340 if ( stringValueData
[i
] == char1
&& stringValueData
[i
+ 1] == char2
)
342 if ( tmpValueData
[j
- 1] != 0)
343 tmpValueData
[j
++] = 0;
347 tmpValueData
[j
++] = stringValueData
[i
];
349 tmpValueData
[j
++] = stringValueData
[i
];
350 tmpValueData
[j
++] = 0;
351 tmpValueData
[j
++] = 0;
352 heap_free(stringValueData
);
353 stringValueData
= tmpValueData
;
355 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (LPBYTE
)stringValueData
, j
* sizeof(WCHAR
));
356 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
357 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
360 else /* hex data types */
362 struct edit_params params
;
365 params
.lpszValueName
= valueName
;
366 params
.pData
= stringValueData
;
368 result
= DialogBoxParamW(NULL
, MAKEINTRESOURCEW(IDD_EDIT_BINARY
), hwnd
,
369 bin_modify_dlgproc
, (LPARAM
)¶ms
);
372 /* Update the listview item with the new data string */
375 int index
= SendMessageW(g_pChildWnd
->hListWnd
, LVM_GETNEXTITEM
, -1,
376 MAKELPARAM(LVNI_FOCUSED
| LVNI_SELECTED
, 0));
377 heap_free(stringValueData
);
378 stringValueData
= read_value(hwnd
, hKey
, valueName
, &type
, &len
);
379 format_value_data(g_pChildWnd
->hListWnd
, index
, type
, stringValueData
, len
);
383 heap_free(stringValueData
);
384 stringValueData
= NULL
;
389 BOOL
DeleteKey(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
)
395 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
|KEY_SET_VALUE
, &hKey
);
396 if (lRet
!= ERROR_SUCCESS
) {
397 error_code_messagebox(hwnd
, IDS_DELETE_KEY_FAILED
);
401 if (messagebox(hwnd
, MB_YESNO
| MB_ICONEXCLAMATION
, IDS_DELETE_KEY_TITLE
,
402 IDS_DELETE_KEY_TEXT
) != IDYES
)
405 lRet
= SHDeleteKeyW(hKeyRoot
, keyPath
);
406 if (lRet
!= ERROR_SUCCESS
) {
407 error_code_messagebox(hwnd
, IDS_BAD_KEY
, keyPath
);
417 BOOL
DeleteValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR valueName
, BOOL showMessageBox
)
422 LPCWSTR visibleValueName
= valueName
? valueName
: g_pszDefaultValueName
;
425 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
426 if (lRet
!= ERROR_SUCCESS
) return FALSE
;
430 if (messagebox(hwnd
, MB_YESNO
| MB_ICONEXCLAMATION
, IDS_DELETE_VALUE_TITLE
, IDS_DELETE_VALUE_TEXT
,
431 visibleValueName
) != IDYES
)
435 lRet
= RegDeleteValueW(hKey
, valueName
? valueName
: &empty
);
436 if (lRet
!= ERROR_SUCCESS
&& valueName
) {
437 error_code_messagebox(hwnd
, IDS_BAD_VALUE
, valueName
);
439 if (lRet
!= ERROR_SUCCESS
) goto done
;
447 BOOL
CreateValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, DWORD valueType
, LPWSTR valueName
)
449 LONG lRet
= ERROR_SUCCESS
;
451 DWORD valueDword
= 0;
457 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
458 if (lRet
!= ERROR_SUCCESS
) {
459 error_code_messagebox(hwnd
, IDS_CREATE_VALUE_FAILED
);
463 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWVALUE
, newValue
, COUNT_OF(newValue
))) goto done
;
465 /* try to find a name for the value being created (maximum = 100 attempts) */
466 for (valueNum
= 1; valueNum
< 100; valueNum
++) {
467 wsprintfW(valueName
, newValue
, valueNum
);
468 lRet
= RegQueryValueExW(hKey
, valueName
, 0, 0, 0, 0);
469 if (lRet
== ERROR_FILE_NOT_FOUND
) break;
471 if (lRet
!= ERROR_FILE_NOT_FOUND
) {
472 error_code_messagebox(hwnd
, IDS_CREATE_VALUE_FAILED
);
476 lRet
= RegSetValueExW(hKey
, valueName
, 0, valueType
, (BYTE
*)&valueDword
, sizeof(DWORD
));
477 if (lRet
!= ERROR_SUCCESS
) {
478 error_code_messagebox(hwnd
, IDS_CREATE_VALUE_FAILED
);
482 /* Add the new item to the listview */
483 index
= AddEntryToList(g_pChildWnd
->hListWnd
, valueName
, valueType
,
484 (BYTE
*)&valueDword
, sizeof(DWORD
), -1);
485 item
.state
= LVIS_FOCUSED
| LVIS_SELECTED
;
486 item
.stateMask
= LVIS_FOCUSED
| LVIS_SELECTED
;
487 SendMessageW(g_pChildWnd
->hListWnd
, LVM_SETITEMSTATE
, index
, (LPARAM
)&item
);
496 BOOL
RenameValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR oldName
, LPCWSTR newName
)
504 if (!oldName
) return FALSE
;
505 if (!newName
) return FALSE
;
507 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
508 if (lRet
!= ERROR_SUCCESS
) {
509 error_code_messagebox(hwnd
, IDS_RENAME_VALUE_FAILED
);
512 /* check if the value already exists */
513 if (check_value(hwnd
, hKey
, newName
)) {
514 error_code_messagebox(hwnd
, IDS_VALUE_EXISTS
, oldName
);
517 value
= read_value(hwnd
, hKey
, oldName
, &type
, &len
);
518 if(!value
) goto done
;
519 lRet
= RegSetValueExW(hKey
, newName
, 0, type
, (BYTE
*)value
, len
);
520 if (lRet
!= ERROR_SUCCESS
) {
521 error_code_messagebox(hwnd
, IDS_RENAME_VALUE_FAILED
);
524 lRet
= RegDeleteValueW(hKey
, oldName
);
525 if (lRet
!= ERROR_SUCCESS
) {
526 RegDeleteValueW(hKey
, newName
);
527 error_code_messagebox(hwnd
, IDS_RENAME_VALUE_FAILED
);
539 BOOL
RenameKey(HWND hwnd
, HKEY hRootKey
, LPCWSTR keyPath
, LPCWSTR newName
)
541 LPWSTR parentPath
= 0;
542 LPCWSTR srcSubKey
= 0;
549 if (!keyPath
|| !newName
) return FALSE
;
551 if (!strrchrW(keyPath
, '\\')) {
552 parentKey
= hRootKey
;
555 LPWSTR srcSubKey_copy
;
557 parentPath
= heap_xalloc((lstrlenW(keyPath
) + 1) * sizeof(WCHAR
));
558 lstrcpyW(parentPath
, keyPath
);
559 srcSubKey_copy
= strrchrW(parentPath
, '\\');
561 srcSubKey
= srcSubKey_copy
+ 1;
562 lRet
= RegOpenKeyExW(hRootKey
, parentPath
, 0, KEY_READ
| KEY_CREATE_SUB_KEY
, &parentKey
);
563 if (lRet
!= ERROR_SUCCESS
) {
564 error_code_messagebox(hwnd
, IDS_RENAME_KEY_FAILED
);
569 /* The following fails if the old name is the same as the new name. */
570 if (!lstrcmpW(srcSubKey
, newName
)) goto done
;
572 lRet
= RegCreateKeyExW(parentKey
, newName
, 0, NULL
, REG_OPTION_NON_VOLATILE
,
573 KEY_WRITE
, NULL
/* FIXME */, &destKey
, &disposition
);
574 if (disposition
== REG_OPENED_EXISTING_KEY
)
575 lRet
= ERROR_FILE_EXISTS
;
576 if (lRet
!= ERROR_SUCCESS
) {
577 error_code_messagebox(hwnd
, IDS_KEY_EXISTS
, srcSubKey
);
581 /* FIXME: SHCopyKey does not copy the security attributes */
582 lRet
= SHCopyKeyW(parentKey
, srcSubKey
, destKey
, 0);
583 if (lRet
!= ERROR_SUCCESS
) {
584 RegCloseKey(destKey
);
585 RegDeleteKeyW(parentKey
, newName
);
586 error_code_messagebox(hwnd
, IDS_RENAME_KEY_FAILED
);
590 lRet
= SHDeleteKeyW(hRootKey
, keyPath
);
591 if (lRet
!= ERROR_SUCCESS
) {
592 error_code_messagebox(hwnd
, IDS_RENAME_KEY_FAILED
);
599 RegCloseKey(destKey
);
601 RegCloseKey(parentKey
);
602 heap_free(parentPath
);