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
, __ms_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
, ...)
70 __ms_va_start(ap
, resId
);
71 result
= vmessagebox(hwnd
, buttons
, titleId
, resId
, ap
);
77 static void WINAPIV
error_code_messagebox(HWND hwnd
, unsigned int msg_id
, ...)
81 __ms_va_start(ap
, msg_id
);
82 vmessagebox(hwnd
, MB_OK
|MB_ICONERROR
, IDS_ERROR
, msg_id
, ap
);
86 static BOOL
change_dword_base(HWND hwndDlg
, BOOL toHex
)
88 static const WCHAR percent_u
[] = {'%','u',0};
89 static const WCHAR percent_x
[] = {'%','x',0};
94 if (!GetDlgItemTextW(hwndDlg
, IDC_VALUE_DATA
, buf
, ARRAY_SIZE(buf
))) return FALSE
;
95 if (!swscanf(buf
, toHex
? percent_u
: percent_x
, &val
)) return FALSE
;
96 wsprintfW(buf
, toHex
? percent_x
: percent_u
, val
);
97 return SetDlgItemTextW(hwndDlg
, IDC_VALUE_DATA
, buf
);
100 static INT_PTR CALLBACK
modify_dlgproc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
107 SetDlgItemTextW(hwndDlg
, IDC_VALUE_NAME
, editValueName
);
108 SetDlgItemTextW(hwndDlg
, IDC_VALUE_DATA
, stringValueData
);
109 CheckRadioButton(hwndDlg
, IDC_DWORD_HEX
, IDC_DWORD_DEC
, IDC_DWORD_HEX
);
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 BOOL
value_exists(HWND hwnd
, HKEY hKey
, const WCHAR
*value_name
)
183 return !RegQueryValueExW(hKey
, value_name
, NULL
, NULL
, NULL
, NULL
);
186 static LPWSTR
read_value(HWND hwnd
, HKEY hKey
, LPCWSTR valueName
, DWORD
*lpType
, LONG
*len
)
189 LPWSTR buffer
= NULL
;
192 lRet
= RegQueryValueExW(hKey
, valueName
, NULL
, lpType
, NULL
, &valueDataLen
);
194 if (lRet
== ERROR_FILE_NOT_FOUND
&& !valueName
) { /* no default value here, make it up */
196 if (lpType
) *lpType
= REG_SZ
;
197 buffer
= heap_xalloc(sizeof(WCHAR
));
201 error_code_messagebox(hwnd
, IDS_BAD_VALUE
, valueName
);
204 if ( *lpType
== REG_DWORD
) valueDataLen
= sizeof(DWORD
);
205 buffer
= heap_xalloc(valueDataLen
+ sizeof(WCHAR
));
206 lRet
= RegQueryValueExW(hKey
, valueName
, 0, 0, (LPBYTE
)buffer
, &valueDataLen
);
208 error_code_messagebox(hwnd
, IDS_BAD_VALUE
, valueName
);
211 if((valueDataLen
% sizeof(WCHAR
)) == 0)
212 buffer
[valueDataLen
/ sizeof(WCHAR
)] = 0;
213 if(len
) *len
= valueDataLen
;
221 BOOL
CreateKey(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPWSTR keyName
)
224 LONG lRet
= ERROR_SUCCESS
;
226 WCHAR newKey
[MAX_NEW_KEY_LEN
- 4];
230 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_CREATE_SUB_KEY
, &hKey
);
232 error_code_messagebox(hwnd
, IDS_CREATE_KEY_FAILED
);
236 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWKEY
, newKey
, ARRAY_SIZE(newKey
))) goto done
;
238 /* try to find a name for the key being created (maximum = 100 attempts) */
239 for (keyNum
= 1; keyNum
< 100; keyNum
++) {
240 wsprintfW(keyName
, newKey
, keyNum
);
241 lRet
= RegOpenKeyW(hKey
, keyName
, &retKey
);
245 if (lRet
== ERROR_SUCCESS
) goto done
;
247 lRet
= RegCreateKeyW(hKey
, keyName
, &retKey
);
249 error_code_messagebox(hwnd
, IDS_CREATE_KEY_FAILED
);
260 BOOL
ModifyValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR valueName
)
268 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
270 error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
274 editValueName
= valueName
? valueName
: g_pszDefaultValueName
;
275 if(!(stringValueData
= read_value(hwnd
, hKey
, valueName
, &type
, &len
))) goto done
;
277 if ( (type
== REG_SZ
) || (type
== REG_EXPAND_SZ
) ) {
278 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_STRING
), hwnd
, modify_dlgproc
) == IDOK
) {
279 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (LPBYTE
)stringValueData
, (lstrlenW(stringValueData
) + 1) * sizeof(WCHAR
));
280 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
281 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
283 } else if ( type
== REG_DWORD
) {
284 static const WCHAR x
[] = {'%','x',0};
285 DWORD value
= *((DWORD
*)stringValueData
);
286 stringValueData
= heap_xrealloc(stringValueData
, 64);
287 wsprintfW(stringValueData
, x
, value
);
288 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_DWORD
), hwnd
, modify_dlgproc
) == IDOK
) {
290 CHAR
* valueA
= GetMultiByteString(stringValueData
);
291 if (sscanf(valueA
, isDecimal
? "%u" : "%x", &val
)) {
292 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (BYTE
*)&val
, sizeof(val
));
293 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
294 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
298 } else if ( type
== REG_MULTI_SZ
) {
299 WCHAR char1
= '\r', char2
= '\n';
300 WCHAR
*tmpValueData
= NULL
;
303 for ( i
= 0, count
= 0; i
< len
- 1; i
++)
304 if ( !stringValueData
[i
] && stringValueData
[i
+ 1] )
306 tmpValueData
= heap_xalloc((len
+ count
) * sizeof(WCHAR
));
308 for ( i
= 0, j
= 0; i
< len
- 1; i
++)
310 if ( !stringValueData
[i
] && stringValueData
[i
+ 1])
312 tmpValueData
[j
++] = char1
;
313 tmpValueData
[j
++] = char2
;
316 tmpValueData
[j
++] = stringValueData
[i
];
318 tmpValueData
[j
] = stringValueData
[i
];
319 heap_free(stringValueData
);
320 stringValueData
= tmpValueData
;
323 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_MULTI_STRING
), hwnd
, modify_dlgproc
) == IDOK
)
325 len
= lstrlenW( stringValueData
);
326 tmpValueData
= heap_xalloc((len
+ 2) * sizeof(WCHAR
));
328 for ( i
= 0, j
= 0; i
< len
- 1; i
++)
330 if ( stringValueData
[i
] == char1
&& stringValueData
[i
+ 1] == char2
)
332 if ( tmpValueData
[j
- 1] != 0)
333 tmpValueData
[j
++] = 0;
337 tmpValueData
[j
++] = stringValueData
[i
];
339 tmpValueData
[j
++] = stringValueData
[i
];
340 tmpValueData
[j
++] = 0;
341 tmpValueData
[j
++] = 0;
342 heap_free(stringValueData
);
343 stringValueData
= tmpValueData
;
345 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (LPBYTE
)stringValueData
, j
* sizeof(WCHAR
));
346 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
347 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
350 else /* hex data types */
352 struct edit_params params
;
355 params
.value_name
= valueName
;
357 params
.data
= stringValueData
;
359 result
= DialogBoxParamW(NULL
, MAKEINTRESOURCEW(IDD_EDIT_BINARY
), hwnd
,
360 bin_modify_dlgproc
, (LPARAM
)¶ms
);
363 /* Update the listview item with the new data string */
366 int index
= SendMessageW(g_pChildWnd
->hListWnd
, LVM_GETNEXTITEM
, -1,
367 MAKELPARAM(LVNI_FOCUSED
| LVNI_SELECTED
, 0));
368 heap_free(stringValueData
);
369 stringValueData
= read_value(hwnd
, hKey
, valueName
, &type
, &len
);
370 format_value_data(g_pChildWnd
->hListWnd
, index
, type
, stringValueData
, len
);
374 heap_free(stringValueData
);
375 stringValueData
= NULL
;
380 BOOL
DeleteKey(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
)
386 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
|KEY_SET_VALUE
, &hKey
);
388 error_code_messagebox(hwnd
, IDS_DELETE_KEY_FAILED
);
392 if (messagebox(hwnd
, MB_YESNO
| MB_ICONEXCLAMATION
, IDS_DELETE_KEY_TITLE
,
393 IDS_DELETE_KEY_TEXT
) != IDYES
)
396 lRet
= SHDeleteKeyW(hKeyRoot
, keyPath
);
398 error_code_messagebox(hwnd
, IDS_BAD_KEY
, keyPath
);
408 BOOL
DeleteValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR valueName
)
414 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
415 if (lRet
) return FALSE
;
417 lRet
= RegDeleteValueW(hKey
, valueName
);
418 if (lRet
&& valueName
) {
419 error_code_messagebox(hwnd
, IDS_BAD_VALUE
, valueName
);
429 BOOL
CreateValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, DWORD valueType
, LPWSTR valueName
)
431 LONG lRet
= ERROR_SUCCESS
;
433 DWORD valueDword
= 0;
439 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
441 error_code_messagebox(hwnd
, IDS_CREATE_VALUE_FAILED
);
445 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWVALUE
, newValue
, ARRAY_SIZE(newValue
))) goto done
;
447 /* try to find a name for the value being created (maximum = 100 attempts) */
448 for (valueNum
= 1; valueNum
< 100; valueNum
++) {
449 wsprintfW(valueName
, newValue
, valueNum
);
450 lRet
= RegQueryValueExW(hKey
, valueName
, 0, 0, 0, 0);
451 if (lRet
== ERROR_FILE_NOT_FOUND
) break;
453 if (lRet
!= ERROR_FILE_NOT_FOUND
) {
454 error_code_messagebox(hwnd
, IDS_CREATE_VALUE_FAILED
);
458 lRet
= RegSetValueExW(hKey
, valueName
, 0, valueType
, (BYTE
*)&valueDword
, sizeof(DWORD
));
460 error_code_messagebox(hwnd
, IDS_CREATE_VALUE_FAILED
);
464 /* Add the new item to the listview */
465 index
= AddEntryToList(g_pChildWnd
->hListWnd
, valueName
, valueType
,
466 (BYTE
*)&valueDword
, sizeof(DWORD
), -1);
467 item
.state
= LVIS_FOCUSED
| LVIS_SELECTED
;
468 item
.stateMask
= LVIS_FOCUSED
| LVIS_SELECTED
;
469 SendMessageW(g_pChildWnd
->hListWnd
, LVM_SETITEMSTATE
, index
, (LPARAM
)&item
);
478 BOOL
RenameValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR oldName
, LPCWSTR newName
)
486 if (!oldName
) return FALSE
;
487 if (!newName
) return FALSE
;
489 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
491 error_code_messagebox(hwnd
, IDS_RENAME_VALUE_FAILED
);
495 if (value_exists(hwnd
, hKey
, newName
)) {
496 error_code_messagebox(hwnd
, IDS_VALUE_EXISTS
, oldName
);
499 value
= read_value(hwnd
, hKey
, oldName
, &type
, &len
);
500 if(!value
) goto done
;
501 lRet
= RegSetValueExW(hKey
, newName
, 0, type
, (BYTE
*)value
, len
);
503 error_code_messagebox(hwnd
, IDS_RENAME_VALUE_FAILED
);
506 lRet
= RegDeleteValueW(hKey
, oldName
);
508 RegDeleteValueW(hKey
, newName
);
509 error_code_messagebox(hwnd
, IDS_RENAME_VALUE_FAILED
);
521 BOOL
RenameKey(HWND hwnd
, HKEY hRootKey
, LPCWSTR keyPath
, LPCWSTR newName
)
523 LPWSTR parentPath
= 0;
524 LPCWSTR srcSubKey
= 0;
531 if (!keyPath
|| !newName
) return FALSE
;
533 if (!wcsrchr(keyPath
, '\\')) {
534 parentKey
= hRootKey
;
537 LPWSTR srcSubKey_copy
;
539 parentPath
= heap_xalloc((lstrlenW(keyPath
) + 1) * sizeof(WCHAR
));
540 lstrcpyW(parentPath
, keyPath
);
541 srcSubKey_copy
= wcsrchr(parentPath
, '\\');
543 srcSubKey
= srcSubKey_copy
+ 1;
544 lRet
= RegOpenKeyExW(hRootKey
, parentPath
, 0, KEY_READ
| KEY_CREATE_SUB_KEY
, &parentKey
);
546 error_code_messagebox(hwnd
, IDS_RENAME_KEY_FAILED
);
551 /* The following fails if the old name is the same as the new name. */
552 if (!lstrcmpW(srcSubKey
, newName
)) goto done
;
554 lRet
= RegCreateKeyExW(parentKey
, newName
, 0, NULL
, REG_OPTION_NON_VOLATILE
,
555 KEY_WRITE
, NULL
/* FIXME */, &destKey
, &disposition
);
556 if (disposition
== REG_OPENED_EXISTING_KEY
)
557 lRet
= ERROR_FILE_EXISTS
;
559 error_code_messagebox(hwnd
, IDS_KEY_EXISTS
, srcSubKey
);
563 /* FIXME: SHCopyKey does not copy the security attributes */
564 lRet
= SHCopyKeyW(parentKey
, srcSubKey
, destKey
, 0);
566 RegCloseKey(destKey
);
567 RegDeleteKeyW(parentKey
, newName
);
568 error_code_messagebox(hwnd
, IDS_RENAME_KEY_FAILED
);
572 lRet
= SHDeleteKeyW(hRootKey
, keyPath
);
574 error_code_messagebox(hwnd
, IDS_RENAME_KEY_FAILED
);
581 RegCloseKey(destKey
);
583 RegCloseKey(parentKey
);
584 heap_free(parentPath
);