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
);
156 switch (LOWORD(wParam
)) {
158 params
= (struct edit_params
*)GetWindowLongPtrW(hwndDlg
, DWLP_USER
);
159 cbData
= SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, HEM_GETDATA
, 0, 0);
160 pData
= heap_xalloc(cbData
);
164 SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, HEM_GETDATA
, (WPARAM
)cbData
, (LPARAM
)pData
);
165 lRet
= RegSetValueExW(params
->hKey
, params
->lpszValueName
, 0, REG_BINARY
, pData
, cbData
);
169 lRet
= ERROR_OUTOFMEMORY
;
171 if (lRet
== ERROR_SUCCESS
)
172 EndDialog(hwndDlg
, 1);
175 error_code_messagebox(hwndDlg
, IDS_SET_VALUE_FAILED
);
176 EndDialog(hwndDlg
, 0);
180 EndDialog(hwndDlg
, 0);
187 static BOOL
check_value(HWND hwnd
, HKEY hKey
, LPCWSTR valueName
)
190 LONG lRet
= RegQueryValueExW(hKey
, valueName
? valueName
: &empty
, 0, NULL
, 0, NULL
);
191 if(lRet
!= ERROR_SUCCESS
) return FALSE
;
195 static LPWSTR
read_value(HWND hwnd
, HKEY hKey
, LPCWSTR valueName
, DWORD
*lpType
, LONG
*len
)
198 LPWSTR buffer
= NULL
;
202 lRet
= RegQueryValueExW(hKey
, valueName
? valueName
: &empty
, 0, lpType
, 0, &valueDataLen
);
203 if (lRet
!= ERROR_SUCCESS
) {
204 if (lRet
== ERROR_FILE_NOT_FOUND
&& !valueName
) { /* no default value here, make it up */
206 if (lpType
) *lpType
= REG_SZ
;
207 buffer
= heap_xalloc(sizeof(WCHAR
));
211 error_code_messagebox(hwnd
, IDS_BAD_VALUE
, valueName
);
214 if ( *lpType
== REG_DWORD
) valueDataLen
= sizeof(DWORD
);
215 buffer
= heap_xalloc(valueDataLen
+ sizeof(WCHAR
));
216 lRet
= RegQueryValueExW(hKey
, valueName
, 0, 0, (LPBYTE
)buffer
, &valueDataLen
);
217 if (lRet
!= ERROR_SUCCESS
) {
218 error_code_messagebox(hwnd
, IDS_BAD_VALUE
, valueName
);
221 if((valueDataLen
% sizeof(WCHAR
)) == 0)
222 buffer
[valueDataLen
/ sizeof(WCHAR
)] = 0;
223 if(len
) *len
= valueDataLen
;
231 BOOL
CreateKey(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPWSTR keyName
)
234 LONG lRet
= ERROR_SUCCESS
;
236 WCHAR newKey
[MAX_NEW_KEY_LEN
- 4];
240 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_CREATE_SUB_KEY
, &hKey
);
241 if (lRet
!= ERROR_SUCCESS
) {
242 error_code_messagebox(hwnd
, IDS_CREATE_KEY_FAILED
);
246 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWKEY
, newKey
, COUNT_OF(newKey
))) goto done
;
248 /* try to find a name for the key being created (maximum = 100 attempts) */
249 for (keyNum
= 1; keyNum
< 100; keyNum
++) {
250 wsprintfW(keyName
, newKey
, keyNum
);
251 lRet
= RegOpenKeyW(hKey
, keyName
, &retKey
);
252 if (lRet
!= ERROR_SUCCESS
) break;
255 if (lRet
== ERROR_SUCCESS
) goto done
;
257 lRet
= RegCreateKeyW(hKey
, keyName
, &retKey
);
258 if (lRet
!= ERROR_SUCCESS
) {
259 error_code_messagebox(hwnd
, IDS_CREATE_KEY_FAILED
);
270 BOOL
ModifyValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR valueName
)
278 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
279 if (lRet
!= ERROR_SUCCESS
) {
280 error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
284 editValueName
= valueName
? valueName
: g_pszDefaultValueName
;
285 if(!(stringValueData
= read_value(hwnd
, hKey
, valueName
, &type
, &len
))) goto done
;
287 if ( (type
== REG_SZ
) || (type
== REG_EXPAND_SZ
) ) {
288 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_STRING
), hwnd
, modify_dlgproc
) == IDOK
) {
289 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (LPBYTE
)stringValueData
, (lstrlenW(stringValueData
) + 1) * sizeof(WCHAR
));
290 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
291 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
293 } else if ( type
== REG_DWORD
) {
294 const WCHAR u
[] = {'%','u',0};
295 const WCHAR x
[] = {'%','x',0};
296 wsprintfW(stringValueData
, isDecimal
? u
: x
, *((DWORD
*)stringValueData
));
297 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_DWORD
), hwnd
, modify_dlgproc
) == IDOK
) {
299 CHAR
* valueA
= GetMultiByteString(stringValueData
);
300 if (sscanf(valueA
, isDecimal
? "%u" : "%x", &val
)) {
301 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (BYTE
*)&val
, sizeof(val
));
302 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
303 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
307 } else if ( type
== REG_MULTI_SZ
) {
308 WCHAR char1
= '\r', char2
= '\n';
309 WCHAR
*tmpValueData
= NULL
;
312 for ( i
= 0, count
= 0; i
< len
- 1; i
++)
313 if ( !stringValueData
[i
] && stringValueData
[i
+ 1] )
315 tmpValueData
= heap_xalloc((len
+ count
) * sizeof(WCHAR
));
317 for ( i
= 0, j
= 0; i
< len
- 1; i
++)
319 if ( !stringValueData
[i
] && stringValueData
[i
+ 1])
321 tmpValueData
[j
++] = char1
;
322 tmpValueData
[j
++] = char2
;
325 tmpValueData
[j
++] = stringValueData
[i
];
327 tmpValueData
[j
] = stringValueData
[i
];
328 heap_free(stringValueData
);
329 stringValueData
= tmpValueData
;
332 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_MULTI_STRING
), hwnd
, modify_dlgproc
) == IDOK
)
334 len
= lstrlenW( stringValueData
);
335 tmpValueData
= heap_xalloc((len
+ 2) * sizeof(WCHAR
));
337 for ( i
= 0, j
= 0; i
< len
- 1; i
++)
339 if ( stringValueData
[i
] == char1
&& stringValueData
[i
+ 1] == char2
)
341 if ( tmpValueData
[j
- 1] != 0)
342 tmpValueData
[j
++] = 0;
346 tmpValueData
[j
++] = stringValueData
[i
];
348 tmpValueData
[j
++] = stringValueData
[i
];
349 tmpValueData
[j
++] = 0;
350 tmpValueData
[j
++] = 0;
351 heap_free(stringValueData
);
352 stringValueData
= tmpValueData
;
354 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (LPBYTE
)stringValueData
, j
* sizeof(WCHAR
));
355 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
356 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
359 else /* hex data types */
361 struct edit_params params
;
364 params
.lpszValueName
= valueName
;
365 params
.pData
= stringValueData
;
367 result
= DialogBoxParamW(NULL
, MAKEINTRESOURCEW(IDD_EDIT_BINARY
), hwnd
,
368 bin_modify_dlgproc
, (LPARAM
)¶ms
);
371 /* Update the listview item with the new data string */
374 int index
= SendMessageW(g_pChildWnd
->hListWnd
, LVM_GETNEXTITEM
, -1,
375 MAKELPARAM(LVNI_FOCUSED
| LVNI_SELECTED
, 0));
376 heap_free(stringValueData
);
377 stringValueData
= read_value(hwnd
, hKey
, valueName
, &type
, &len
);
378 format_value_data(g_pChildWnd
->hListWnd
, index
, type
, stringValueData
, len
);
382 heap_free(stringValueData
);
383 stringValueData
= NULL
;
388 BOOL
DeleteKey(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
)
394 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
|KEY_SET_VALUE
, &hKey
);
395 if (lRet
!= ERROR_SUCCESS
) {
396 error_code_messagebox(hwnd
, IDS_DELETE_KEY_FAILED
);
400 if (messagebox(hwnd
, MB_YESNO
| MB_ICONEXCLAMATION
, IDS_DELETE_KEY_TITLE
,
401 IDS_DELETE_KEY_TEXT
) != IDYES
)
404 lRet
= SHDeleteKeyW(hKeyRoot
, keyPath
);
405 if (lRet
!= ERROR_SUCCESS
) {
406 error_code_messagebox(hwnd
, IDS_BAD_KEY
, keyPath
);
416 BOOL
DeleteValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR valueName
, BOOL showMessageBox
)
421 LPCWSTR visibleValueName
= valueName
? valueName
: g_pszDefaultValueName
;
424 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
425 if (lRet
!= ERROR_SUCCESS
) return FALSE
;
429 if (messagebox(hwnd
, MB_YESNO
| MB_ICONEXCLAMATION
, IDS_DELETE_VALUE_TITLE
, IDS_DELETE_VALUE_TEXT
,
430 visibleValueName
) != IDYES
)
434 lRet
= RegDeleteValueW(hKey
, valueName
? valueName
: &empty
);
435 if (lRet
!= ERROR_SUCCESS
&& valueName
) {
436 error_code_messagebox(hwnd
, IDS_BAD_VALUE
, valueName
);
438 if (lRet
!= ERROR_SUCCESS
) goto done
;
446 BOOL
CreateValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, DWORD valueType
, LPWSTR valueName
)
448 LONG lRet
= ERROR_SUCCESS
;
450 DWORD valueDword
= 0;
456 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
457 if (lRet
!= ERROR_SUCCESS
) {
458 error_code_messagebox(hwnd
, IDS_CREATE_VALUE_FAILED
);
462 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWVALUE
, newValue
, COUNT_OF(newValue
))) goto done
;
464 /* try to find a name for the value being created (maximum = 100 attempts) */
465 for (valueNum
= 1; valueNum
< 100; valueNum
++) {
466 wsprintfW(valueName
, newValue
, valueNum
);
467 lRet
= RegQueryValueExW(hKey
, valueName
, 0, 0, 0, 0);
468 if (lRet
== ERROR_FILE_NOT_FOUND
) break;
470 if (lRet
!= ERROR_FILE_NOT_FOUND
) {
471 error_code_messagebox(hwnd
, IDS_CREATE_VALUE_FAILED
);
475 lRet
= RegSetValueExW(hKey
, valueName
, 0, valueType
, (BYTE
*)&valueDword
, sizeof(DWORD
));
476 if (lRet
!= ERROR_SUCCESS
) {
477 error_code_messagebox(hwnd
, IDS_CREATE_VALUE_FAILED
);
481 /* Add the new item to the listview */
482 index
= AddEntryToList(g_pChildWnd
->hListWnd
, valueName
, valueType
,
483 (BYTE
*)&valueDword
, sizeof(DWORD
), -1);
484 item
.state
= LVIS_FOCUSED
| LVIS_SELECTED
;
485 item
.stateMask
= LVIS_FOCUSED
| LVIS_SELECTED
;
486 SendMessageW(g_pChildWnd
->hListWnd
, LVM_SETITEMSTATE
, index
, (LPARAM
)&item
);
495 BOOL
RenameValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR oldName
, LPCWSTR newName
)
503 if (!oldName
) return FALSE
;
504 if (!newName
) return FALSE
;
506 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
507 if (lRet
!= ERROR_SUCCESS
) {
508 error_code_messagebox(hwnd
, IDS_RENAME_VALUE_FAILED
);
511 /* check if the value already exists */
512 if (check_value(hwnd
, hKey
, newName
)) {
513 error_code_messagebox(hwnd
, IDS_VALUE_EXISTS
, oldName
);
516 value
= read_value(hwnd
, hKey
, oldName
, &type
, &len
);
517 if(!value
) goto done
;
518 lRet
= RegSetValueExW(hKey
, newName
, 0, type
, (BYTE
*)value
, len
);
519 if (lRet
!= ERROR_SUCCESS
) {
520 error_code_messagebox(hwnd
, IDS_RENAME_VALUE_FAILED
);
523 lRet
= RegDeleteValueW(hKey
, oldName
);
524 if (lRet
!= ERROR_SUCCESS
) {
525 RegDeleteValueW(hKey
, newName
);
526 error_code_messagebox(hwnd
, IDS_RENAME_VALUE_FAILED
);
538 BOOL
RenameKey(HWND hwnd
, HKEY hRootKey
, LPCWSTR keyPath
, LPCWSTR newName
)
540 LPWSTR parentPath
= 0;
541 LPCWSTR srcSubKey
= 0;
548 if (!keyPath
|| !newName
) return FALSE
;
550 if (!strrchrW(keyPath
, '\\')) {
551 parentKey
= hRootKey
;
554 LPWSTR srcSubKey_copy
;
556 parentPath
= heap_xalloc((lstrlenW(keyPath
) + 1) * sizeof(WCHAR
));
557 lstrcpyW(parentPath
, keyPath
);
558 srcSubKey_copy
= strrchrW(parentPath
, '\\');
560 srcSubKey
= srcSubKey_copy
+ 1;
561 lRet
= RegOpenKeyExW(hRootKey
, parentPath
, 0, KEY_READ
| KEY_CREATE_SUB_KEY
, &parentKey
);
562 if (lRet
!= ERROR_SUCCESS
) {
563 error_code_messagebox(hwnd
, IDS_RENAME_KEY_FAILED
);
568 /* The following fails if the old name is the same as the new name. */
569 if (!lstrcmpW(srcSubKey
, newName
)) goto done
;
571 lRet
= RegCreateKeyExW(parentKey
, newName
, 0, NULL
, REG_OPTION_NON_VOLATILE
,
572 KEY_WRITE
, NULL
/* FIXME */, &destKey
, &disposition
);
573 if (disposition
== REG_OPENED_EXISTING_KEY
)
574 lRet
= ERROR_FILE_EXISTS
;
575 if (lRet
!= ERROR_SUCCESS
) {
576 error_code_messagebox(hwnd
, IDS_KEY_EXISTS
, srcSubKey
);
580 /* FIXME: SHCopyKey does not copy the security attributes */
581 lRet
= SHCopyKeyW(parentKey
, srcSubKey
, destKey
, 0);
582 if (lRet
!= ERROR_SUCCESS
) {
583 RegCloseKey(destKey
);
584 RegDeleteKeyW(parentKey
, newName
);
585 error_code_messagebox(hwnd
, IDS_RENAME_KEY_FAILED
);
589 lRet
= SHDeleteKeyW(hRootKey
, keyPath
);
590 if (lRet
!= ERROR_SUCCESS
) {
591 error_code_messagebox(hwnd
, IDS_RENAME_KEY_FAILED
);
598 RegCloseKey(destKey
);
600 RegCloseKey(parentKey
);
601 heap_free(parentPath
);