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"
32 #include "wine/unicode.h"
35 static const WCHAR
* editValueName
;
36 static WCHAR
* stringValueData
;
37 static BOOL isDecimal
;
42 LPCWSTR lpszValueName
;
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
, isDecimal
? IDC_DWORD_DEC
: IDC_DWORD_HEX
);
112 switch (LOWORD(wParam
)) {
114 if (isDecimal
&& change_dword_base(hwndDlg
, TRUE
)) isDecimal
= FALSE
;
117 if (!isDecimal
&& change_dword_base(hwndDlg
, FALSE
)) isDecimal
= TRUE
;
120 if ((hwndValue
= GetDlgItem(hwndDlg
, IDC_VALUE_DATA
))) {
121 len
= GetWindowTextLengthW(hwndValue
);
122 stringValueData
= heap_xrealloc(stringValueData
, (len
+ 1) * sizeof(WCHAR
));
123 if (!GetWindowTextW(hwndValue
, stringValueData
, len
+ 1))
124 *stringValueData
= 0;
128 EndDialog(hwndDlg
, wParam
);
135 static INT_PTR CALLBACK
bin_modify_dlgproc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
137 struct edit_params
*params
;
144 params
= (struct edit_params
*)lParam
;
145 SetWindowLongPtrW(hwndDlg
, DWLP_USER
, (ULONG_PTR
)params
);
146 if (params
->lpszValueName
)
147 SetDlgItemTextW(hwndDlg
, IDC_VALUE_NAME
, params
->lpszValueName
);
149 SetDlgItemTextW(hwndDlg
, IDC_VALUE_NAME
, g_pszDefaultValueName
);
150 SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, HEM_SETDATA
, (WPARAM
)params
->cbData
, (LPARAM
)params
->pData
);
151 SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, WM_SETFONT
, (WPARAM
) GetStockObject(ANSI_FIXED_FONT
), TRUE
);
154 switch (LOWORD(wParam
)) {
156 params
= (struct edit_params
*)GetWindowLongPtrW(hwndDlg
, DWLP_USER
);
157 cbData
= SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, HEM_GETDATA
, 0, 0);
158 pData
= heap_xalloc(cbData
);
162 SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, HEM_GETDATA
, (WPARAM
)cbData
, (LPARAM
)pData
);
163 lRet
= RegSetValueExW(params
->hKey
, params
->lpszValueName
, 0, REG_BINARY
, pData
, cbData
);
167 lRet
= ERROR_OUTOFMEMORY
;
169 if (lRet
== ERROR_SUCCESS
)
170 EndDialog(hwndDlg
, 1);
173 error_code_messagebox(hwndDlg
, IDS_SET_VALUE_FAILED
);
174 EndDialog(hwndDlg
, 0);
178 EndDialog(hwndDlg
, 0);
185 static BOOL
value_exists(HWND hwnd
, HKEY hKey
, const WCHAR
*value_name
)
187 return !RegQueryValueExW(hKey
, value_name
, NULL
, NULL
, NULL
, NULL
);
190 static LPWSTR
read_value(HWND hwnd
, HKEY hKey
, LPCWSTR valueName
, DWORD
*lpType
, LONG
*len
)
193 LPWSTR buffer
= NULL
;
196 lRet
= RegQueryValueExW(hKey
, valueName
, NULL
, lpType
, NULL
, &valueDataLen
);
198 if (lRet
== ERROR_FILE_NOT_FOUND
&& !valueName
) { /* no default value here, make it up */
200 if (lpType
) *lpType
= REG_SZ
;
201 buffer
= heap_xalloc(sizeof(WCHAR
));
205 error_code_messagebox(hwnd
, IDS_BAD_VALUE
, valueName
);
208 if ( *lpType
== REG_DWORD
) valueDataLen
= sizeof(DWORD
);
209 buffer
= heap_xalloc(valueDataLen
+ sizeof(WCHAR
));
210 lRet
= RegQueryValueExW(hKey
, valueName
, 0, 0, (LPBYTE
)buffer
, &valueDataLen
);
212 error_code_messagebox(hwnd
, IDS_BAD_VALUE
, valueName
);
215 if((valueDataLen
% sizeof(WCHAR
)) == 0)
216 buffer
[valueDataLen
/ sizeof(WCHAR
)] = 0;
217 if(len
) *len
= valueDataLen
;
225 BOOL
CreateKey(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPWSTR keyName
)
228 LONG lRet
= ERROR_SUCCESS
;
230 WCHAR newKey
[MAX_NEW_KEY_LEN
- 4];
234 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_CREATE_SUB_KEY
, &hKey
);
236 error_code_messagebox(hwnd
, IDS_CREATE_KEY_FAILED
);
240 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWKEY
, newKey
, ARRAY_SIZE(newKey
))) goto done
;
242 /* try to find a name for the key being created (maximum = 100 attempts) */
243 for (keyNum
= 1; keyNum
< 100; keyNum
++) {
244 wsprintfW(keyName
, newKey
, keyNum
);
245 lRet
= RegOpenKeyW(hKey
, keyName
, &retKey
);
249 if (lRet
== ERROR_SUCCESS
) goto done
;
251 lRet
= RegCreateKeyW(hKey
, keyName
, &retKey
);
253 error_code_messagebox(hwnd
, IDS_CREATE_KEY_FAILED
);
264 BOOL
ModifyValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR valueName
)
272 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
274 error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
278 editValueName
= valueName
? valueName
: g_pszDefaultValueName
;
279 if(!(stringValueData
= read_value(hwnd
, hKey
, valueName
, &type
, &len
))) goto done
;
281 if ( (type
== REG_SZ
) || (type
== REG_EXPAND_SZ
) ) {
282 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_STRING
), hwnd
, modify_dlgproc
) == IDOK
) {
283 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (LPBYTE
)stringValueData
, (lstrlenW(stringValueData
) + 1) * sizeof(WCHAR
));
284 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
285 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
287 } else if ( type
== REG_DWORD
) {
288 const WCHAR u
[] = {'%','u',0};
289 const WCHAR x
[] = {'%','x',0};
290 wsprintfW(stringValueData
, isDecimal
? u
: x
, *((DWORD
*)stringValueData
));
291 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_DWORD
), hwnd
, modify_dlgproc
) == IDOK
) {
293 CHAR
* valueA
= GetMultiByteString(stringValueData
);
294 if (sscanf(valueA
, isDecimal
? "%u" : "%x", &val
)) {
295 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (BYTE
*)&val
, sizeof(val
));
296 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
297 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
301 } else if ( type
== REG_MULTI_SZ
) {
302 WCHAR char1
= '\r', char2
= '\n';
303 WCHAR
*tmpValueData
= NULL
;
306 for ( i
= 0, count
= 0; i
< len
- 1; i
++)
307 if ( !stringValueData
[i
] && stringValueData
[i
+ 1] )
309 tmpValueData
= heap_xalloc((len
+ count
) * sizeof(WCHAR
));
311 for ( i
= 0, j
= 0; i
< len
- 1; i
++)
313 if ( !stringValueData
[i
] && stringValueData
[i
+ 1])
315 tmpValueData
[j
++] = char1
;
316 tmpValueData
[j
++] = char2
;
319 tmpValueData
[j
++] = stringValueData
[i
];
321 tmpValueData
[j
] = stringValueData
[i
];
322 heap_free(stringValueData
);
323 stringValueData
= tmpValueData
;
326 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_MULTI_STRING
), hwnd
, modify_dlgproc
) == IDOK
)
328 len
= lstrlenW( stringValueData
);
329 tmpValueData
= heap_xalloc((len
+ 2) * sizeof(WCHAR
));
331 for ( i
= 0, j
= 0; i
< len
- 1; i
++)
333 if ( stringValueData
[i
] == char1
&& stringValueData
[i
+ 1] == char2
)
335 if ( tmpValueData
[j
- 1] != 0)
336 tmpValueData
[j
++] = 0;
340 tmpValueData
[j
++] = stringValueData
[i
];
342 tmpValueData
[j
++] = stringValueData
[i
];
343 tmpValueData
[j
++] = 0;
344 tmpValueData
[j
++] = 0;
345 heap_free(stringValueData
);
346 stringValueData
= tmpValueData
;
348 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (LPBYTE
)stringValueData
, j
* sizeof(WCHAR
));
349 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
350 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
353 else /* hex data types */
355 struct edit_params params
;
358 params
.lpszValueName
= valueName
;
359 params
.pData
= stringValueData
;
361 result
= DialogBoxParamW(NULL
, MAKEINTRESOURCEW(IDD_EDIT_BINARY
), hwnd
,
362 bin_modify_dlgproc
, (LPARAM
)¶ms
);
365 /* Update the listview item with the new data string */
368 int index
= SendMessageW(g_pChildWnd
->hListWnd
, LVM_GETNEXTITEM
, -1,
369 MAKELPARAM(LVNI_FOCUSED
| LVNI_SELECTED
, 0));
370 heap_free(stringValueData
);
371 stringValueData
= read_value(hwnd
, hKey
, valueName
, &type
, &len
);
372 format_value_data(g_pChildWnd
->hListWnd
, index
, type
, stringValueData
, len
);
376 heap_free(stringValueData
);
377 stringValueData
= NULL
;
382 BOOL
DeleteKey(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
)
388 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
|KEY_SET_VALUE
, &hKey
);
390 error_code_messagebox(hwnd
, IDS_DELETE_KEY_FAILED
);
394 if (messagebox(hwnd
, MB_YESNO
| MB_ICONEXCLAMATION
, IDS_DELETE_KEY_TITLE
,
395 IDS_DELETE_KEY_TEXT
) != IDYES
)
398 lRet
= SHDeleteKeyW(hKeyRoot
, keyPath
);
400 error_code_messagebox(hwnd
, IDS_BAD_KEY
, keyPath
);
410 BOOL
DeleteValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR valueName
)
416 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
417 if (lRet
) return FALSE
;
419 lRet
= RegDeleteValueW(hKey
, valueName
);
420 if (lRet
&& valueName
) {
421 error_code_messagebox(hwnd
, IDS_BAD_VALUE
, valueName
);
431 BOOL
CreateValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, DWORD valueType
, LPWSTR valueName
)
433 LONG lRet
= ERROR_SUCCESS
;
435 DWORD valueDword
= 0;
441 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
443 error_code_messagebox(hwnd
, IDS_CREATE_VALUE_FAILED
);
447 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWVALUE
, newValue
, ARRAY_SIZE(newValue
))) goto done
;
449 /* try to find a name for the value being created (maximum = 100 attempts) */
450 for (valueNum
= 1; valueNum
< 100; valueNum
++) {
451 wsprintfW(valueName
, newValue
, valueNum
);
452 lRet
= RegQueryValueExW(hKey
, valueName
, 0, 0, 0, 0);
453 if (lRet
== ERROR_FILE_NOT_FOUND
) break;
455 if (lRet
!= ERROR_FILE_NOT_FOUND
) {
456 error_code_messagebox(hwnd
, IDS_CREATE_VALUE_FAILED
);
460 lRet
= RegSetValueExW(hKey
, valueName
, 0, valueType
, (BYTE
*)&valueDword
, sizeof(DWORD
));
462 error_code_messagebox(hwnd
, IDS_CREATE_VALUE_FAILED
);
466 /* Add the new item to the listview */
467 index
= AddEntryToList(g_pChildWnd
->hListWnd
, valueName
, valueType
,
468 (BYTE
*)&valueDword
, sizeof(DWORD
), -1);
469 item
.state
= LVIS_FOCUSED
| LVIS_SELECTED
;
470 item
.stateMask
= LVIS_FOCUSED
| LVIS_SELECTED
;
471 SendMessageW(g_pChildWnd
->hListWnd
, LVM_SETITEMSTATE
, index
, (LPARAM
)&item
);
480 BOOL
RenameValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR oldName
, LPCWSTR newName
)
488 if (!oldName
) return FALSE
;
489 if (!newName
) return FALSE
;
491 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
493 error_code_messagebox(hwnd
, IDS_RENAME_VALUE_FAILED
);
497 if (value_exists(hwnd
, hKey
, newName
)) {
498 error_code_messagebox(hwnd
, IDS_VALUE_EXISTS
, oldName
);
501 value
= read_value(hwnd
, hKey
, oldName
, &type
, &len
);
502 if(!value
) goto done
;
503 lRet
= RegSetValueExW(hKey
, newName
, 0, type
, (BYTE
*)value
, len
);
505 error_code_messagebox(hwnd
, IDS_RENAME_VALUE_FAILED
);
508 lRet
= RegDeleteValueW(hKey
, oldName
);
510 RegDeleteValueW(hKey
, newName
);
511 error_code_messagebox(hwnd
, IDS_RENAME_VALUE_FAILED
);
523 BOOL
RenameKey(HWND hwnd
, HKEY hRootKey
, LPCWSTR keyPath
, LPCWSTR newName
)
525 LPWSTR parentPath
= 0;
526 LPCWSTR srcSubKey
= 0;
533 if (!keyPath
|| !newName
) return FALSE
;
535 if (!strrchrW(keyPath
, '\\')) {
536 parentKey
= hRootKey
;
539 LPWSTR srcSubKey_copy
;
541 parentPath
= heap_xalloc((lstrlenW(keyPath
) + 1) * sizeof(WCHAR
));
542 lstrcpyW(parentPath
, keyPath
);
543 srcSubKey_copy
= strrchrW(parentPath
, '\\');
545 srcSubKey
= srcSubKey_copy
+ 1;
546 lRet
= RegOpenKeyExW(hRootKey
, parentPath
, 0, KEY_READ
| KEY_CREATE_SUB_KEY
, &parentKey
);
548 error_code_messagebox(hwnd
, IDS_RENAME_KEY_FAILED
);
553 /* The following fails if the old name is the same as the new name. */
554 if (!lstrcmpW(srcSubKey
, newName
)) goto done
;
556 lRet
= RegCreateKeyExW(parentKey
, newName
, 0, NULL
, REG_OPTION_NON_VOLATILE
,
557 KEY_WRITE
, NULL
/* FIXME */, &destKey
, &disposition
);
558 if (disposition
== REG_OPENED_EXISTING_KEY
)
559 lRet
= ERROR_FILE_EXISTS
;
561 error_code_messagebox(hwnd
, IDS_KEY_EXISTS
, srcSubKey
);
565 /* FIXME: SHCopyKey does not copy the security attributes */
566 lRet
= SHCopyKeyW(parentKey
, srcSubKey
, destKey
, 0);
568 RegCloseKey(destKey
);
569 RegDeleteKeyW(parentKey
, newName
);
570 error_code_messagebox(hwnd
, IDS_RENAME_KEY_FAILED
);
574 lRet
= SHDeleteKeyW(hRootKey
, keyPath
);
576 error_code_messagebox(hwnd
, IDS_RENAME_KEY_FAILED
);
583 RegCloseKey(destKey
);
585 RegCloseKey(parentKey
);
586 heap_free(parentPath
);