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/unicode.h"
37 static const WCHAR
* editValueName
;
38 static WCHAR
* stringValueData
;
39 static BOOL isDecimal
;
44 LPCWSTR lpszValueName
;
49 static int vmessagebox(HWND hwnd
, int buttons
, int titleId
, int resId
, __ms_va_list va_args
)
56 LoadStringW(hInst
, titleId
, title
, COUNT_OF(title
));
57 LoadStringW(hInst
, resId
, fmt
, COUNT_OF(fmt
));
59 FormatMessageW(FORMAT_MESSAGE_FROM_STRING
|FORMAT_MESSAGE_ALLOCATE_BUFFER
,
60 fmt
, 0, 0, (WCHAR
*)&str
, 0, &va_args
);
61 ret
= MessageBoxW(hwnd
, str
, title
, buttons
);
67 int __cdecl
messagebox(HWND hwnd
, int buttons
, int titleId
, int resId
, ...)
72 __ms_va_start(ap
, resId
);
73 result
= vmessagebox(hwnd
, buttons
, titleId
, resId
, ap
);
79 static void __cdecl
error_code_messagebox(HWND hwnd
, unsigned int msg_id
, ...)
83 __ms_va_start(ap
, msg_id
);
84 vmessagebox(hwnd
, MB_OK
|MB_ICONERROR
, IDS_ERROR
, msg_id
, ap
);
88 static BOOL
change_dword_base(HWND hwndDlg
, BOOL toHex
)
90 static const WCHAR percent_u
[] = {'%','u',0};
91 static const WCHAR percent_x
[] = {'%','x',0};
96 if (!GetDlgItemTextW(hwndDlg
, IDC_VALUE_DATA
, buf
, COUNT_OF(buf
))) return FALSE
;
97 if (!swscanf(buf
, toHex
? percent_u
: percent_x
, &val
)) return FALSE
;
98 wsprintfW(buf
, toHex
? percent_x
: percent_u
, val
);
99 return SetDlgItemTextW(hwndDlg
, IDC_VALUE_DATA
, buf
);
102 static INT_PTR CALLBACK
modify_dlgproc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
109 SetDlgItemTextW(hwndDlg
, IDC_VALUE_NAME
, editValueName
);
110 SetDlgItemTextW(hwndDlg
, IDC_VALUE_DATA
, stringValueData
);
111 CheckRadioButton(hwndDlg
, IDC_DWORD_HEX
, IDC_DWORD_DEC
, isDecimal
? IDC_DWORD_DEC
: IDC_DWORD_HEX
);
114 switch (LOWORD(wParam
)) {
116 if (isDecimal
&& change_dword_base(hwndDlg
, TRUE
)) isDecimal
= FALSE
;
119 if (!isDecimal
&& change_dword_base(hwndDlg
, FALSE
)) isDecimal
= TRUE
;
122 if ((hwndValue
= GetDlgItem(hwndDlg
, IDC_VALUE_DATA
))) {
123 len
= GetWindowTextLengthW(hwndValue
);
124 stringValueData
= heap_xrealloc(stringValueData
, (len
+ 1) * sizeof(WCHAR
));
125 if (!GetWindowTextW(hwndValue
, stringValueData
, len
+ 1))
126 *stringValueData
= 0;
130 EndDialog(hwndDlg
, wParam
);
137 static INT_PTR CALLBACK
bin_modify_dlgproc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
139 struct edit_params
*params
;
146 params
= (struct edit_params
*)lParam
;
147 SetWindowLongPtrW(hwndDlg
, DWLP_USER
, (ULONG_PTR
)params
);
148 if (params
->lpszValueName
)
149 SetDlgItemTextW(hwndDlg
, IDC_VALUE_NAME
, params
->lpszValueName
);
151 SetDlgItemTextW(hwndDlg
, IDC_VALUE_NAME
, g_pszDefaultValueName
);
152 SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, HEM_SETDATA
, (WPARAM
)params
->cbData
, (LPARAM
)params
->pData
);
155 switch (LOWORD(wParam
)) {
157 params
= (struct edit_params
*)GetWindowLongPtrW(hwndDlg
, DWLP_USER
);
158 cbData
= SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, HEM_GETDATA
, 0, 0);
159 pData
= heap_xalloc(cbData
);
163 SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, HEM_GETDATA
, (WPARAM
)cbData
, (LPARAM
)pData
);
164 lRet
= RegSetValueExW(params
->hKey
, params
->lpszValueName
, 0, REG_BINARY
, pData
, cbData
);
168 lRet
= ERROR_OUTOFMEMORY
;
170 if (lRet
== ERROR_SUCCESS
)
171 EndDialog(hwndDlg
, 1);
174 error_code_messagebox(hwndDlg
, IDS_SET_VALUE_FAILED
);
175 EndDialog(hwndDlg
, 0);
179 EndDialog(hwndDlg
, 0);
186 static BOOL
check_value(HWND hwnd
, HKEY hKey
, LPCWSTR valueName
)
189 LONG lRet
= RegQueryValueExW(hKey
, valueName
? valueName
: &empty
, 0, NULL
, 0, NULL
);
190 if(lRet
!= ERROR_SUCCESS
) return FALSE
;
194 static LPWSTR
read_value(HWND hwnd
, HKEY hKey
, LPCWSTR valueName
, DWORD
*lpType
, LONG
*len
)
197 LPWSTR buffer
= NULL
;
201 lRet
= RegQueryValueExW(hKey
, valueName
? valueName
: &empty
, 0, lpType
, 0, &valueDataLen
);
202 if (lRet
!= ERROR_SUCCESS
) {
203 if (lRet
== ERROR_FILE_NOT_FOUND
&& !valueName
) { /* no default value here, make it up */
205 if (lpType
) *lpType
= REG_SZ
;
206 buffer
= heap_xalloc(sizeof(WCHAR
));
210 error_code_messagebox(hwnd
, IDS_BAD_VALUE
, valueName
);
213 if ( *lpType
== REG_DWORD
) valueDataLen
= sizeof(DWORD
);
214 buffer
= heap_xalloc(valueDataLen
+ sizeof(WCHAR
));
215 lRet
= RegQueryValueExW(hKey
, valueName
, 0, 0, (LPBYTE
)buffer
, &valueDataLen
);
216 if (lRet
!= ERROR_SUCCESS
) {
217 error_code_messagebox(hwnd
, IDS_BAD_VALUE
, valueName
);
220 if((valueDataLen
% sizeof(WCHAR
)) == 0)
221 buffer
[valueDataLen
/ sizeof(WCHAR
)] = 0;
222 if(len
) *len
= valueDataLen
;
230 BOOL
CreateKey(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPWSTR keyName
)
233 LONG lRet
= ERROR_SUCCESS
;
235 WCHAR newKey
[MAX_NEW_KEY_LEN
- 4];
239 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_CREATE_SUB_KEY
, &hKey
);
240 if (lRet
!= ERROR_SUCCESS
) {
241 error_code_messagebox(hwnd
, IDS_CREATE_KEY_FAILED
);
245 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWKEY
, newKey
, COUNT_OF(newKey
))) goto done
;
247 /* try to find a name for the key being created (maximum = 100 attempts) */
248 for (keyNum
= 1; keyNum
< 100; keyNum
++) {
249 wsprintfW(keyName
, newKey
, keyNum
);
250 lRet
= RegOpenKeyW(hKey
, keyName
, &retKey
);
251 if (lRet
!= ERROR_SUCCESS
) break;
254 if (lRet
== ERROR_SUCCESS
) goto done
;
256 lRet
= RegCreateKeyW(hKey
, keyName
, &retKey
);
257 if (lRet
!= ERROR_SUCCESS
) {
258 error_code_messagebox(hwnd
, IDS_CREATE_KEY_FAILED
);
269 BOOL
ModifyValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR valueName
)
277 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
278 if (lRet
!= ERROR_SUCCESS
) {
279 error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
283 editValueName
= valueName
? valueName
: g_pszDefaultValueName
;
284 if(!(stringValueData
= read_value(hwnd
, hKey
, valueName
, &type
, &len
))) goto done
;
286 if ( (type
== REG_SZ
) || (type
== REG_EXPAND_SZ
) ) {
287 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_STRING
), hwnd
, modify_dlgproc
) == IDOK
) {
288 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (LPBYTE
)stringValueData
, (lstrlenW(stringValueData
) + 1) * sizeof(WCHAR
));
289 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
290 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
292 } else if ( type
== REG_DWORD
) {
293 const WCHAR u
[] = {'%','u',0};
294 const WCHAR x
[] = {'%','x',0};
295 wsprintfW(stringValueData
, isDecimal
? u
: x
, *((DWORD
*)stringValueData
));
296 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_DWORD
), hwnd
, modify_dlgproc
) == IDOK
) {
298 CHAR
* valueA
= GetMultiByteString(stringValueData
);
299 if (sscanf(valueA
, isDecimal
? "%u" : "%x", &val
)) {
300 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (BYTE
*)&val
, sizeof(val
));
301 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
302 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
306 } else if ( type
== REG_MULTI_SZ
) {
307 WCHAR char1
= '\r', char2
= '\n';
308 WCHAR
*tmpValueData
= NULL
;
311 for ( i
= 0, count
= 0; i
< len
- 1; i
++)
312 if ( !stringValueData
[i
] && stringValueData
[i
+ 1] )
314 tmpValueData
= heap_xalloc((len
+ count
) * sizeof(WCHAR
));
316 for ( i
= 0, j
= 0; i
< len
- 1; i
++)
318 if ( !stringValueData
[i
] && stringValueData
[i
+ 1])
320 tmpValueData
[j
++] = char1
;
321 tmpValueData
[j
++] = char2
;
324 tmpValueData
[j
++] = stringValueData
[i
];
326 tmpValueData
[j
] = stringValueData
[i
];
327 heap_free(stringValueData
);
328 stringValueData
= tmpValueData
;
331 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_MULTI_STRING
), hwnd
, modify_dlgproc
) == IDOK
)
333 len
= lstrlenW( stringValueData
);
334 tmpValueData
= heap_xalloc((len
+ 2) * sizeof(WCHAR
));
336 for ( i
= 0, j
= 0; i
< len
- 1; i
++)
338 if ( stringValueData
[i
] == char1
&& stringValueData
[i
+ 1] == char2
)
340 if ( tmpValueData
[j
- 1] != 0)
341 tmpValueData
[j
++] = 0;
345 tmpValueData
[j
++] = stringValueData
[i
];
347 tmpValueData
[j
++] = stringValueData
[i
];
348 tmpValueData
[j
++] = 0;
349 tmpValueData
[j
++] = 0;
350 heap_free(stringValueData
);
351 stringValueData
= tmpValueData
;
353 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (LPBYTE
)stringValueData
, j
* sizeof(WCHAR
));
354 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
355 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
358 else /* hex data types */
360 struct edit_params params
;
363 params
.lpszValueName
= valueName
;
364 params
.pData
= 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
);
394 if (lRet
!= ERROR_SUCCESS
) {
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
);
404 if (lRet
!= ERROR_SUCCESS
) {
405 error_code_messagebox(hwnd
, IDS_BAD_KEY
, keyPath
);
415 BOOL
DeleteValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR valueName
, BOOL showMessageBox
)
420 LPCWSTR visibleValueName
= valueName
? valueName
: g_pszDefaultValueName
;
423 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
424 if (lRet
!= ERROR_SUCCESS
) return FALSE
;
428 if (messagebox(hwnd
, MB_YESNO
| MB_ICONEXCLAMATION
, IDS_DELETE_VALUE_TITLE
, IDS_DELETE_VALUE_TEXT
,
429 visibleValueName
) != IDYES
)
433 lRet
= RegDeleteValueW(hKey
, valueName
? valueName
: &empty
);
434 if (lRet
!= ERROR_SUCCESS
&& valueName
) {
435 error_code_messagebox(hwnd
, IDS_BAD_VALUE
, valueName
);
437 if (lRet
!= ERROR_SUCCESS
) goto done
;
445 BOOL
CreateValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, DWORD valueType
, LPWSTR valueName
)
447 LONG lRet
= ERROR_SUCCESS
;
449 DWORD valueDword
= 0;
455 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
456 if (lRet
!= ERROR_SUCCESS
) {
457 error_code_messagebox(hwnd
, IDS_CREATE_VALUE_FAILED
);
461 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWVALUE
, newValue
, COUNT_OF(newValue
))) goto done
;
463 /* try to find a name for the value being created (maximum = 100 attempts) */
464 for (valueNum
= 1; valueNum
< 100; valueNum
++) {
465 wsprintfW(valueName
, newValue
, valueNum
);
466 lRet
= RegQueryValueExW(hKey
, valueName
, 0, 0, 0, 0);
467 if (lRet
== ERROR_FILE_NOT_FOUND
) break;
469 if (lRet
!= ERROR_FILE_NOT_FOUND
) {
470 error_code_messagebox(hwnd
, IDS_CREATE_VALUE_FAILED
);
474 lRet
= RegSetValueExW(hKey
, valueName
, 0, valueType
, (BYTE
*)&valueDword
, sizeof(DWORD
));
475 if (lRet
!= ERROR_SUCCESS
) {
476 error_code_messagebox(hwnd
, IDS_CREATE_VALUE_FAILED
);
480 /* Add the new item to the listview */
481 index
= AddEntryToList(g_pChildWnd
->hListWnd
, valueName
, valueType
,
482 (BYTE
*)&valueDword
, sizeof(DWORD
), -1);
483 item
.state
= LVIS_FOCUSED
| LVIS_SELECTED
;
484 item
.stateMask
= LVIS_FOCUSED
| LVIS_SELECTED
;
485 SendMessageW(g_pChildWnd
->hListWnd
, LVM_SETITEMSTATE
, index
, (LPARAM
)&item
);
494 BOOL
RenameValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR oldName
, LPCWSTR newName
)
502 if (!oldName
) return FALSE
;
503 if (!newName
) return FALSE
;
505 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
506 if (lRet
!= ERROR_SUCCESS
) {
507 error_code_messagebox(hwnd
, IDS_RENAME_VALUE_FAILED
);
510 /* check if the value already exists */
511 if (check_value(hwnd
, hKey
, newName
)) {
512 error_code_messagebox(hwnd
, IDS_VALUE_EXISTS
, oldName
);
515 value
= read_value(hwnd
, hKey
, oldName
, &type
, &len
);
516 if(!value
) goto done
;
517 lRet
= RegSetValueExW(hKey
, newName
, 0, type
, (BYTE
*)value
, len
);
518 if (lRet
!= ERROR_SUCCESS
) {
519 error_code_messagebox(hwnd
, IDS_RENAME_VALUE_FAILED
);
522 lRet
= RegDeleteValueW(hKey
, oldName
);
523 if (lRet
!= ERROR_SUCCESS
) {
524 RegDeleteValueW(hKey
, newName
);
525 error_code_messagebox(hwnd
, IDS_RENAME_VALUE_FAILED
);
537 BOOL
RenameKey(HWND hwnd
, HKEY hRootKey
, LPCWSTR keyPath
, LPCWSTR newName
)
539 LPWSTR parentPath
= 0;
540 LPCWSTR srcSubKey
= 0;
547 if (!keyPath
|| !newName
) return FALSE
;
549 if (!strrchrW(keyPath
, '\\')) {
550 parentKey
= hRootKey
;
553 LPWSTR srcSubKey_copy
;
555 parentPath
= heap_xalloc((lstrlenW(keyPath
) + 1) * sizeof(WCHAR
));
556 lstrcpyW(parentPath
, keyPath
);
557 srcSubKey_copy
= strrchrW(parentPath
, '\\');
559 srcSubKey
= srcSubKey_copy
+ 1;
560 lRet
= RegOpenKeyExW(hRootKey
, parentPath
, 0, KEY_READ
| KEY_CREATE_SUB_KEY
, &parentKey
);
561 if (lRet
!= ERROR_SUCCESS
) {
562 error_code_messagebox(hwnd
, IDS_RENAME_KEY_FAILED
);
567 /* The following fails if the old name is the same as the new name. */
568 if (!lstrcmpW(srcSubKey
, newName
)) goto done
;
570 lRet
= RegCreateKeyExW(parentKey
, newName
, 0, NULL
, REG_OPTION_NON_VOLATILE
,
571 KEY_WRITE
, NULL
/* FIXME */, &destKey
, &disposition
);
572 if (disposition
== REG_OPENED_EXISTING_KEY
)
573 lRet
= ERROR_FILE_EXISTS
;
574 if (lRet
!= ERROR_SUCCESS
) {
575 error_code_messagebox(hwnd
, IDS_KEY_EXISTS
, srcSubKey
);
579 /* FIXME: SHCopyKey does not copy the security attributes */
580 lRet
= SHCopyKeyW(parentKey
, srcSubKey
, destKey
, 0);
581 if (lRet
!= ERROR_SUCCESS
) {
582 RegCloseKey(destKey
);
583 RegDeleteKeyW(parentKey
, newName
);
584 error_code_messagebox(hwnd
, IDS_RENAME_KEY_FAILED
);
588 lRet
= SHDeleteKeyW(hRootKey
, keyPath
);
589 if (lRet
!= ERROR_SUCCESS
) {
590 error_code_messagebox(hwnd
, IDS_RENAME_KEY_FAILED
);
597 RegCloseKey(destKey
);
599 RegCloseKey(parentKey
);
600 heap_free(parentPath
);