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 const WCHAR
*value_name
;
48 static int vmessagebox(HWND hwnd
, int buttons
, int titleId
, int resId
, __ms_va_list va_args
)
55 LoadStringW(hInst
, titleId
, title
, ARRAY_SIZE(title
));
56 LoadStringW(hInst
, resId
, fmt
, ARRAY_SIZE(fmt
));
58 FormatMessageW(FORMAT_MESSAGE_FROM_STRING
|FORMAT_MESSAGE_ALLOCATE_BUFFER
,
59 fmt
, 0, 0, (WCHAR
*)&str
, 0, &va_args
);
60 ret
= MessageBoxW(hwnd
, str
, title
, buttons
);
66 int WINAPIV
messagebox(HWND hwnd
, int buttons
, int titleId
, int resId
, ...)
71 __ms_va_start(ap
, resId
);
72 result
= vmessagebox(hwnd
, buttons
, titleId
, resId
, ap
);
78 static void WINAPIV
error_code_messagebox(HWND hwnd
, unsigned int msg_id
, ...)
82 __ms_va_start(ap
, msg_id
);
83 vmessagebox(hwnd
, MB_OK
|MB_ICONERROR
, IDS_ERROR
, msg_id
, ap
);
87 static BOOL
change_dword_base(HWND hwndDlg
, BOOL toHex
)
89 static const WCHAR percent_u
[] = {'%','u',0};
90 static const WCHAR percent_x
[] = {'%','x',0};
95 if (!GetDlgItemTextW(hwndDlg
, IDC_VALUE_DATA
, buf
, ARRAY_SIZE(buf
))) return FALSE
;
96 if (!swscanf(buf
, toHex
? percent_u
: percent_x
, &val
)) return FALSE
;
97 wsprintfW(buf
, toHex
? percent_x
: percent_u
, val
);
98 return SetDlgItemTextW(hwndDlg
, IDC_VALUE_DATA
, buf
);
101 static INT_PTR CALLBACK
modify_dlgproc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
108 SetDlgItemTextW(hwndDlg
, IDC_VALUE_NAME
, editValueName
);
109 SetDlgItemTextW(hwndDlg
, IDC_VALUE_DATA
, stringValueData
);
110 CheckRadioButton(hwndDlg
, IDC_DWORD_HEX
, 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
->value_name
)
149 SetDlgItemTextW(hwndDlg
, IDC_VALUE_NAME
, params
->value_name
);
151 SetDlgItemTextW(hwndDlg
, IDC_VALUE_NAME
, g_pszDefaultValueName
);
152 SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, HEM_SETDATA
, (WPARAM
)params
->size
, (LPARAM
)params
->data
);
153 SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, WM_SETFONT
, (WPARAM
) GetStockObject(ANSI_FIXED_FONT
), TRUE
);
156 switch (LOWORD(wParam
)) {
158 params
= (struct edit_params
*)GetWindowLongPtrW(hwndDlg
, DWLP_USER
);
159 size
= SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, HEM_GETDATA
, 0, 0);
160 data
= heap_xalloc(size
);
162 SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, HEM_GETDATA
, (WPARAM
)size
, (LPARAM
)data
);
163 lRet
= RegSetValueExW(params
->hkey
, params
->value_name
, 0, params
->type
, data
, size
);
166 if (lRet
== ERROR_SUCCESS
)
167 EndDialog(hwndDlg
, 1);
170 error_code_messagebox(hwndDlg
, IDS_SET_VALUE_FAILED
);
171 EndDialog(hwndDlg
, 0);
175 EndDialog(hwndDlg
, 0);
182 static BOOL
value_exists(HWND hwnd
, HKEY hKey
, const WCHAR
*value_name
)
184 return !RegQueryValueExW(hKey
, value_name
, NULL
, NULL
, NULL
, NULL
);
187 static LPWSTR
read_value(HWND hwnd
, HKEY hKey
, LPCWSTR valueName
, DWORD
*lpType
, LONG
*len
)
190 LPWSTR buffer
= NULL
;
193 lRet
= RegQueryValueExW(hKey
, valueName
, NULL
, lpType
, NULL
, &valueDataLen
);
195 if (lRet
== ERROR_FILE_NOT_FOUND
&& !valueName
) { /* no default value here, make it up */
197 if (lpType
) *lpType
= REG_SZ
;
198 buffer
= heap_xalloc(sizeof(WCHAR
));
202 error_code_messagebox(hwnd
, IDS_BAD_VALUE
, valueName
);
205 if ( *lpType
== REG_DWORD
) valueDataLen
= sizeof(DWORD
);
206 buffer
= heap_xalloc(valueDataLen
+ sizeof(WCHAR
));
207 lRet
= RegQueryValueExW(hKey
, valueName
, 0, 0, (LPBYTE
)buffer
, &valueDataLen
);
209 error_code_messagebox(hwnd
, IDS_BAD_VALUE
, valueName
);
212 if((valueDataLen
% sizeof(WCHAR
)) == 0)
213 buffer
[valueDataLen
/ sizeof(WCHAR
)] = 0;
214 if(len
) *len
= valueDataLen
;
222 BOOL
CreateKey(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPWSTR keyName
)
225 LONG lRet
= ERROR_SUCCESS
;
227 WCHAR newKey
[MAX_NEW_KEY_LEN
- 4];
231 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_CREATE_SUB_KEY
, &hKey
);
233 error_code_messagebox(hwnd
, IDS_CREATE_KEY_FAILED
);
237 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWKEY
, newKey
, ARRAY_SIZE(newKey
))) goto done
;
239 /* try to find a name for the key being created (maximum = 100 attempts) */
240 for (keyNum
= 1; keyNum
< 100; keyNum
++) {
241 wsprintfW(keyName
, newKey
, keyNum
);
242 lRet
= RegOpenKeyW(hKey
, keyName
, &retKey
);
246 if (lRet
== ERROR_SUCCESS
) goto done
;
248 lRet
= RegCreateKeyW(hKey
, keyName
, &retKey
);
250 error_code_messagebox(hwnd
, IDS_CREATE_KEY_FAILED
);
261 BOOL
ModifyValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR valueName
)
269 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
271 error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
275 editValueName
= valueName
? valueName
: g_pszDefaultValueName
;
276 if(!(stringValueData
= read_value(hwnd
, hKey
, valueName
, &type
, &len
))) goto done
;
278 if ( (type
== REG_SZ
) || (type
== REG_EXPAND_SZ
) ) {
279 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_STRING
), hwnd
, modify_dlgproc
) == IDOK
) {
280 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (LPBYTE
)stringValueData
, (lstrlenW(stringValueData
) + 1) * sizeof(WCHAR
));
281 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
282 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
284 } else if ( type
== REG_DWORD
) {
285 static const WCHAR x
[] = {'%','x',0};
286 wsprintfW(stringValueData
, x
, *((DWORD
*)stringValueData
));
287 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_DWORD
), hwnd
, modify_dlgproc
) == IDOK
) {
289 CHAR
* valueA
= GetMultiByteString(stringValueData
);
290 if (sscanf(valueA
, isDecimal
? "%u" : "%x", &val
)) {
291 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (BYTE
*)&val
, sizeof(val
));
292 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
293 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
297 } else if ( type
== REG_MULTI_SZ
) {
298 WCHAR char1
= '\r', char2
= '\n';
299 WCHAR
*tmpValueData
= NULL
;
302 for ( i
= 0, count
= 0; i
< len
- 1; i
++)
303 if ( !stringValueData
[i
] && stringValueData
[i
+ 1] )
305 tmpValueData
= heap_xalloc((len
+ count
) * sizeof(WCHAR
));
307 for ( i
= 0, j
= 0; i
< len
- 1; i
++)
309 if ( !stringValueData
[i
] && stringValueData
[i
+ 1])
311 tmpValueData
[j
++] = char1
;
312 tmpValueData
[j
++] = char2
;
315 tmpValueData
[j
++] = stringValueData
[i
];
317 tmpValueData
[j
] = stringValueData
[i
];
318 heap_free(stringValueData
);
319 stringValueData
= tmpValueData
;
322 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_MULTI_STRING
), hwnd
, modify_dlgproc
) == IDOK
)
324 len
= lstrlenW( stringValueData
);
325 tmpValueData
= heap_xalloc((len
+ 2) * sizeof(WCHAR
));
327 for ( i
= 0, j
= 0; i
< len
- 1; i
++)
329 if ( stringValueData
[i
] == char1
&& stringValueData
[i
+ 1] == char2
)
331 if ( tmpValueData
[j
- 1] != 0)
332 tmpValueData
[j
++] = 0;
336 tmpValueData
[j
++] = stringValueData
[i
];
338 tmpValueData
[j
++] = stringValueData
[i
];
339 tmpValueData
[j
++] = 0;
340 tmpValueData
[j
++] = 0;
341 heap_free(stringValueData
);
342 stringValueData
= tmpValueData
;
344 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (LPBYTE
)stringValueData
, j
* sizeof(WCHAR
));
345 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
346 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
349 else /* hex data types */
351 struct edit_params params
;
354 params
.value_name
= valueName
;
356 params
.data
= stringValueData
;
358 result
= DialogBoxParamW(NULL
, MAKEINTRESOURCEW(IDD_EDIT_BINARY
), hwnd
,
359 bin_modify_dlgproc
, (LPARAM
)¶ms
);
362 /* Update the listview item with the new data string */
365 int index
= SendMessageW(g_pChildWnd
->hListWnd
, LVM_GETNEXTITEM
, -1,
366 MAKELPARAM(LVNI_FOCUSED
| LVNI_SELECTED
, 0));
367 heap_free(stringValueData
);
368 stringValueData
= read_value(hwnd
, hKey
, valueName
, &type
, &len
);
369 format_value_data(g_pChildWnd
->hListWnd
, index
, type
, stringValueData
, len
);
373 heap_free(stringValueData
);
374 stringValueData
= NULL
;
379 BOOL
DeleteKey(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
)
385 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
|KEY_SET_VALUE
, &hKey
);
387 error_code_messagebox(hwnd
, IDS_DELETE_KEY_FAILED
);
391 if (messagebox(hwnd
, MB_YESNO
| MB_ICONEXCLAMATION
, IDS_DELETE_KEY_TITLE
,
392 IDS_DELETE_KEY_TEXT
) != IDYES
)
395 lRet
= SHDeleteKeyW(hKeyRoot
, keyPath
);
397 error_code_messagebox(hwnd
, IDS_BAD_KEY
, keyPath
);
407 BOOL
DeleteValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR valueName
)
413 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
414 if (lRet
) return FALSE
;
416 lRet
= RegDeleteValueW(hKey
, valueName
);
417 if (lRet
&& valueName
) {
418 error_code_messagebox(hwnd
, IDS_BAD_VALUE
, valueName
);
428 BOOL
CreateValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, DWORD valueType
, LPWSTR valueName
)
430 LONG lRet
= ERROR_SUCCESS
;
432 DWORD valueDword
= 0;
438 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
440 error_code_messagebox(hwnd
, IDS_CREATE_VALUE_FAILED
);
444 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWVALUE
, newValue
, ARRAY_SIZE(newValue
))) goto done
;
446 /* try to find a name for the value being created (maximum = 100 attempts) */
447 for (valueNum
= 1; valueNum
< 100; valueNum
++) {
448 wsprintfW(valueName
, newValue
, valueNum
);
449 lRet
= RegQueryValueExW(hKey
, valueName
, 0, 0, 0, 0);
450 if (lRet
== ERROR_FILE_NOT_FOUND
) break;
452 if (lRet
!= ERROR_FILE_NOT_FOUND
) {
453 error_code_messagebox(hwnd
, IDS_CREATE_VALUE_FAILED
);
457 lRet
= RegSetValueExW(hKey
, valueName
, 0, valueType
, (BYTE
*)&valueDword
, sizeof(DWORD
));
459 error_code_messagebox(hwnd
, IDS_CREATE_VALUE_FAILED
);
463 /* Add the new item to the listview */
464 index
= AddEntryToList(g_pChildWnd
->hListWnd
, valueName
, valueType
,
465 (BYTE
*)&valueDword
, sizeof(DWORD
), -1);
466 item
.state
= LVIS_FOCUSED
| LVIS_SELECTED
;
467 item
.stateMask
= LVIS_FOCUSED
| LVIS_SELECTED
;
468 SendMessageW(g_pChildWnd
->hListWnd
, LVM_SETITEMSTATE
, index
, (LPARAM
)&item
);
477 BOOL
RenameValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR oldName
, LPCWSTR newName
)
485 if (!oldName
) return FALSE
;
486 if (!newName
) return FALSE
;
488 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
490 error_code_messagebox(hwnd
, IDS_RENAME_VALUE_FAILED
);
494 if (value_exists(hwnd
, hKey
, newName
)) {
495 error_code_messagebox(hwnd
, IDS_VALUE_EXISTS
, oldName
);
498 value
= read_value(hwnd
, hKey
, oldName
, &type
, &len
);
499 if(!value
) goto done
;
500 lRet
= RegSetValueExW(hKey
, newName
, 0, type
, (BYTE
*)value
, len
);
502 error_code_messagebox(hwnd
, IDS_RENAME_VALUE_FAILED
);
505 lRet
= RegDeleteValueW(hKey
, oldName
);
507 RegDeleteValueW(hKey
, newName
);
508 error_code_messagebox(hwnd
, IDS_RENAME_VALUE_FAILED
);
520 BOOL
RenameKey(HWND hwnd
, HKEY hRootKey
, LPCWSTR keyPath
, LPCWSTR newName
)
522 LPWSTR parentPath
= 0;
523 LPCWSTR srcSubKey
= 0;
530 if (!keyPath
|| !newName
) return FALSE
;
532 if (!strrchrW(keyPath
, '\\')) {
533 parentKey
= hRootKey
;
536 LPWSTR srcSubKey_copy
;
538 parentPath
= heap_xalloc((lstrlenW(keyPath
) + 1) * sizeof(WCHAR
));
539 lstrcpyW(parentPath
, keyPath
);
540 srcSubKey_copy
= strrchrW(parentPath
, '\\');
542 srcSubKey
= srcSubKey_copy
+ 1;
543 lRet
= RegOpenKeyExW(hRootKey
, parentPath
, 0, KEY_READ
| KEY_CREATE_SUB_KEY
, &parentKey
);
545 error_code_messagebox(hwnd
, IDS_RENAME_KEY_FAILED
);
550 /* The following fails if the old name is the same as the new name. */
551 if (!lstrcmpW(srcSubKey
, newName
)) goto done
;
553 lRet
= RegCreateKeyExW(parentKey
, newName
, 0, NULL
, REG_OPTION_NON_VOLATILE
,
554 KEY_WRITE
, NULL
/* FIXME */, &destKey
, &disposition
);
555 if (disposition
== REG_OPENED_EXISTING_KEY
)
556 lRet
= ERROR_FILE_EXISTS
;
558 error_code_messagebox(hwnd
, IDS_KEY_EXISTS
, srcSubKey
);
562 /* FIXME: SHCopyKey does not copy the security attributes */
563 lRet
= SHCopyKeyW(parentKey
, srcSubKey
, destKey
, 0);
565 RegCloseKey(destKey
);
566 RegDeleteKeyW(parentKey
, newName
);
567 error_code_messagebox(hwnd
, IDS_RENAME_KEY_FAILED
);
571 lRet
= SHDeleteKeyW(hRootKey
, keyPath
);
573 error_code_messagebox(hwnd
, IDS_RENAME_KEY_FAILED
);
580 RegCloseKey(destKey
);
582 RegCloseKey(parentKey
);
583 heap_free(parentPath
);