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 static const WCHAR
* editValueName
;
33 static WCHAR
* stringValueData
;
34 static BOOL isDecimal
;
39 const WCHAR
*value_name
;
45 static int vmessagebox(HWND hwnd
, int buttons
, int titleId
, int resId
, va_list va_args
)
52 LoadStringW(hInst
, titleId
, title
, ARRAY_SIZE(title
));
53 LoadStringW(hInst
, resId
, fmt
, ARRAY_SIZE(fmt
));
55 FormatMessageW(FORMAT_MESSAGE_FROM_STRING
|FORMAT_MESSAGE_ALLOCATE_BUFFER
,
56 fmt
, 0, 0, (WCHAR
*)&str
, 0, &va_args
);
57 ret
= MessageBoxW(hwnd
, str
, title
, buttons
);
63 int WINAPIV
messagebox(HWND hwnd
, int buttons
, int titleId
, int resId
, ...)
69 result
= vmessagebox(hwnd
, buttons
, titleId
, resId
, ap
);
75 static void WINAPIV
error_code_messagebox(HWND hwnd
, unsigned int msg_id
, ...)
80 vmessagebox(hwnd
, MB_OK
|MB_ICONERROR
, IDS_ERROR
, msg_id
, ap
);
84 static INT_PTR CALLBACK
modify_string_dlgproc(HWND hwndDlg
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
92 SetDlgItemTextW(hwndDlg
, IDC_VALUE_NAME
, editValueName
);
93 SetDlgItemTextW(hwndDlg
, IDC_VALUE_DATA
, stringValueData
);
96 switch (LOWORD(wparam
))
99 hwndValue
= GetDlgItem(hwndDlg
, IDC_VALUE_DATA
);
100 len
= GetWindowTextLengthW(hwndValue
);
101 stringValueData
= realloc(stringValueData
, (len
+ 1) * sizeof(WCHAR
));
102 if (!GetWindowTextW(hwndValue
, stringValueData
, len
+ 1))
103 *stringValueData
= 0;
106 EndDialog(hwndDlg
, wparam
);
113 static BOOL
change_dword_base(HWND hwndDlg
, BOOL toHex
)
118 if (!GetDlgItemTextW(hwndDlg
, IDC_VALUE_DATA
, buf
, ARRAY_SIZE(buf
))) return FALSE
;
119 if (!swscanf(buf
, toHex
? L
"%I64u" : L
"%I64x", &val
)) return FALSE
;
120 wsprintfW(buf
, toHex
? L
"%I64x" : L
"%I64u", val
);
121 return SetDlgItemTextW(hwndDlg
, IDC_VALUE_DATA
, buf
);
124 static INT_PTR CALLBACK
modify_dlgproc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
132 SetDlgItemTextW(hwndDlg
, IDC_VALUE_NAME
, editValueName
);
133 SetDlgItemTextW(hwndDlg
, IDC_VALUE_DATA
, stringValueData
);
134 CheckRadioButton(hwndDlg
, IDC_DWORD_HEX
, IDC_DWORD_DEC
, IDC_DWORD_HEX
);
136 if (lParam
== REG_QWORD
&& LoadStringW(GetModuleHandleW(0), IDS_EDIT_QWORD
, buff
, ARRAY_SIZE(buff
)))
137 SetWindowTextW(hwndDlg
, buff
);
140 switch (LOWORD(wParam
)) {
142 if (isDecimal
&& change_dword_base(hwndDlg
, TRUE
)) isDecimal
= FALSE
;
145 if (!isDecimal
&& change_dword_base(hwndDlg
, FALSE
)) isDecimal
= TRUE
;
148 if ((hwndValue
= GetDlgItem(hwndDlg
, IDC_VALUE_DATA
))) {
149 len
= GetWindowTextLengthW(hwndValue
);
150 stringValueData
= realloc(stringValueData
, (len
+ 1) * sizeof(WCHAR
));
151 if (!GetWindowTextW(hwndValue
, stringValueData
, len
+ 1))
152 *stringValueData
= 0;
156 EndDialog(hwndDlg
, wParam
);
163 static INT_PTR CALLBACK
bin_modify_dlgproc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
165 struct edit_params
*params
;
172 params
= (struct edit_params
*)lParam
;
173 SetWindowLongPtrW(hwndDlg
, DWLP_USER
, (ULONG_PTR
)params
);
174 if (params
->value_name
)
175 SetDlgItemTextW(hwndDlg
, IDC_VALUE_NAME
, params
->value_name
);
177 SetDlgItemTextW(hwndDlg
, IDC_VALUE_NAME
, g_pszDefaultValueName
);
178 SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, HEM_SETDATA
, (WPARAM
)params
->size
, (LPARAM
)params
->data
);
179 SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, WM_SETFONT
, (WPARAM
) GetStockObject(ANSI_FIXED_FONT
), TRUE
);
182 switch (LOWORD(wParam
)) {
184 params
= (struct edit_params
*)GetWindowLongPtrW(hwndDlg
, DWLP_USER
);
185 size
= SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, HEM_GETDATA
, 0, 0);
188 SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, HEM_GETDATA
, (WPARAM
)size
, (LPARAM
)data
);
189 lRet
= RegSetValueExW(params
->hkey
, params
->value_name
, 0, params
->type
, data
, size
);
192 if (lRet
== ERROR_SUCCESS
)
193 EndDialog(hwndDlg
, 1);
196 error_code_messagebox(hwndDlg
, IDS_SET_VALUE_FAILED
);
197 EndDialog(hwndDlg
, 0);
201 EndDialog(hwndDlg
, 0);
208 static LPWSTR
read_value(HWND hwnd
, HKEY hKey
, LPCWSTR valueName
, DWORD
*lpType
, LONG
*len
)
211 LPWSTR buffer
= NULL
;
214 lRet
= RegQueryValueExW(hKey
, valueName
, NULL
, lpType
, NULL
, &valueDataLen
);
216 if (lRet
== ERROR_FILE_NOT_FOUND
&& !valueName
) { /* no default value here, make it up */
218 if (lpType
) *lpType
= REG_SZ
;
219 buffer
= malloc(sizeof(WCHAR
));
223 error_code_messagebox(hwnd
, IDS_BAD_VALUE
, valueName
);
227 buffer
= malloc(valueDataLen
+ sizeof(WCHAR
));
228 lRet
= RegQueryValueExW(hKey
, valueName
, 0, 0, (LPBYTE
)buffer
, &valueDataLen
);
230 error_code_messagebox(hwnd
, IDS_BAD_VALUE
, valueName
);
233 if((valueDataLen
% sizeof(WCHAR
)) == 0)
234 buffer
[valueDataLen
/ sizeof(WCHAR
)] = 0;
235 if(len
) *len
= valueDataLen
;
243 BOOL
CreateKey(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPWSTR keyName
)
246 LONG lRet
= ERROR_SUCCESS
;
248 WCHAR newKey
[MAX_NEW_KEY_LEN
- 4];
252 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_CREATE_SUB_KEY
, &hKey
);
254 error_code_messagebox(hwnd
, IDS_CREATE_KEY_FAILED
);
258 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWKEY
, newKey
, ARRAY_SIZE(newKey
))) goto done
;
260 /* try to find a name for the key being created (maximum = 100 attempts) */
261 for (keyNum
= 1; keyNum
< 100; keyNum
++) {
262 wsprintfW(keyName
, newKey
, keyNum
);
263 lRet
= RegOpenKeyW(hKey
, keyName
, &retKey
);
267 if (lRet
== ERROR_SUCCESS
) goto done
;
269 lRet
= RegCreateKeyW(hKey
, keyName
, &retKey
);
271 error_code_messagebox(hwnd
, IDS_CREATE_KEY_FAILED
);
282 BOOL
ModifyValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR valueName
)
290 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
292 error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
296 editValueName
= valueName
? valueName
: g_pszDefaultValueName
;
297 if(!(stringValueData
= read_value(hwnd
, hKey
, valueName
, &type
, &len
))) goto done
;
299 if ( (type
== REG_SZ
) || (type
== REG_EXPAND_SZ
) ) {
300 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_STRING
), hwnd
, modify_string_dlgproc
) == IDOK
) {
301 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (LPBYTE
)stringValueData
, (lstrlenW(stringValueData
) + 1) * sizeof(WCHAR
));
302 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
303 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
305 } else if ( type
== REG_DWORD
) {
306 DWORD value
= *((DWORD
*)stringValueData
);
307 stringValueData
= realloc(stringValueData
, 64);
308 wsprintfW(stringValueData
, L
"%x", value
);
309 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_DWORD
), hwnd
, modify_dlgproc
) == IDOK
)
312 if (swscanf(stringValueData
, isDecimal
? L
"%lu" : L
"%lx", &val
))
314 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (BYTE
*)&val
, sizeof(val
));
315 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
316 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
319 } else if ( type
== REG_QWORD
) {
320 UINT64 value
= *((UINT64
*)stringValueData
);
321 stringValueData
= realloc(stringValueData
, 64);
322 swprintf(stringValueData
, 64, L
"%I64x", value
);
323 if (DialogBoxParamW(0, MAKEINTRESOURCEW(IDD_EDIT_DWORD
), hwnd
, modify_dlgproc
, type
) == IDOK
)
325 if (swscanf(stringValueData
, isDecimal
? L
"%I64u" : L
"%I64x", &value
))
327 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (BYTE
*)&value
, sizeof(value
));
328 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
329 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
332 } else if ( type
== REG_MULTI_SZ
) {
333 WCHAR char1
= '\r', char2
= '\n';
334 WCHAR
*tmpValueData
= NULL
;
337 for (i
= 0, count
= 0; i
< len
/ sizeof(WCHAR
); i
++)
338 if ( !stringValueData
[i
] && stringValueData
[i
+ 1] )
340 tmpValueData
= malloc(len
+ (count
* sizeof(WCHAR
)));
342 for ( i
= 0, j
= 0; i
< len
/ sizeof(WCHAR
); i
++)
344 if ( !stringValueData
[i
] && stringValueData
[i
+ 1])
346 tmpValueData
[j
++] = char1
;
347 tmpValueData
[j
++] = char2
;
350 tmpValueData
[j
++] = stringValueData
[i
];
353 free(stringValueData
);
354 stringValueData
= tmpValueData
;
357 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_MULTI_STRING
), hwnd
, modify_dlgproc
) == IDOK
)
359 len
= lstrlenW( stringValueData
);
360 tmpValueData
= malloc((len
+ 2) * sizeof(WCHAR
));
362 for (i
= 0, j
= 0; i
< len
; i
++)
364 if ( stringValueData
[i
] == char1
&& stringValueData
[i
+ 1] == char2
)
366 if ( tmpValueData
[j
- 1] != 0)
367 tmpValueData
[j
++] = 0;
371 tmpValueData
[j
++] = stringValueData
[i
];
374 tmpValueData
[j
++] = 0;
375 tmpValueData
[j
++] = 0;
376 free(stringValueData
);
377 stringValueData
= tmpValueData
;
379 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (LPBYTE
)stringValueData
, j
* sizeof(WCHAR
));
380 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
381 else error_code_messagebox(hwnd
, IDS_SET_VALUE_FAILED
);
384 else /* hex data types */
386 struct edit_params params
;
389 params
.value_name
= valueName
;
391 params
.data
= stringValueData
;
393 result
= DialogBoxParamW(NULL
, MAKEINTRESOURCEW(IDD_EDIT_BINARY
), hwnd
,
394 bin_modify_dlgproc
, (LPARAM
)¶ms
);
397 /* Update the listview item with the new data string */
400 int index
= SendMessageW(g_pChildWnd
->hListWnd
, LVM_GETNEXTITEM
, -1,
401 MAKELPARAM(LVNI_FOCUSED
| LVNI_SELECTED
, 0));
402 free(stringValueData
);
403 stringValueData
= read_value(hwnd
, hKey
, valueName
, &type
, &len
);
404 format_value_data(g_pChildWnd
->hListWnd
, index
, type
, stringValueData
, len
);
408 free(stringValueData
);
409 stringValueData
= NULL
;
414 BOOL
DeleteKey(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
)
420 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
|KEY_SET_VALUE
, &hKey
);
422 error_code_messagebox(hwnd
, IDS_DELETE_KEY_FAILED
);
426 if (messagebox(hwnd
, MB_YESNO
| MB_ICONEXCLAMATION
, IDS_DELETE_KEY_TITLE
,
427 IDS_DELETE_KEY_TEXT
) != IDYES
)
430 lRet
= SHDeleteKeyW(hKeyRoot
, keyPath
);
432 error_code_messagebox(hwnd
, IDS_BAD_KEY
, keyPath
);
442 BOOL
DeleteValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR valueName
)
448 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
449 if (lRet
) return FALSE
;
451 lRet
= RegDeleteValueW(hKey
, valueName
);
452 if (lRet
&& valueName
) {
453 error_code_messagebox(hwnd
, IDS_BAD_VALUE
, valueName
);
463 BOOL
CreateValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, DWORD valueType
, LPWSTR valueName
)
465 LONG lRet
= ERROR_SUCCESS
;
474 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
476 error_code_messagebox(hwnd
, IDS_CREATE_VALUE_FAILED
);
480 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWVALUE
, newValue
, ARRAY_SIZE(newValue
))) goto done
;
482 /* try to find a name for the value being created (maximum = 100 attempts) */
483 for (valueNum
= 1; valueNum
< 100; valueNum
++) {
484 wsprintfW(valueName
, newValue
, valueNum
);
485 lRet
= RegQueryValueExW(hKey
, valueName
, 0, 0, 0, 0);
486 if (lRet
== ERROR_FILE_NOT_FOUND
) break;
488 if (lRet
!= ERROR_FILE_NOT_FOUND
) {
489 error_code_messagebox(hwnd
, IDS_CREATE_VALUE_FAILED
);
496 case REG_DWORD_BIG_ENDIAN
:
497 size_bytes
= sizeof(DWORD
);
500 size_bytes
= sizeof(UINT64
);
506 size_bytes
= 2 * sizeof(WCHAR
);
508 default: /* REG_NONE, REG_SZ, REG_EXPAND_SZ */
509 size_bytes
= sizeof(WCHAR
);
512 lRet
= RegSetValueExW(hKey
, valueName
, 0, valueType
, (BYTE
*)&value
, size_bytes
);
514 error_code_messagebox(hwnd
, IDS_CREATE_VALUE_FAILED
);
518 /* Add the new item to the listview */
519 index
= AddEntryToList(g_pChildWnd
->hListWnd
, valueName
, valueType
, (BYTE
*)&value
, size_bytes
, -1);
520 item
.state
= LVIS_FOCUSED
| LVIS_SELECTED
;
521 item
.stateMask
= LVIS_FOCUSED
| LVIS_SELECTED
;
522 SendMessageW(g_pChildWnd
->hListWnd
, LVM_SETITEMSTATE
, index
, (LPARAM
)&item
);
531 BOOL
RenameValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR oldName
, LPCWSTR newName
)
539 if (!oldName
) return FALSE
;
540 if (!newName
) return FALSE
;
542 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
544 error_code_messagebox(hwnd
, IDS_RENAME_VALUE_FAILED
);
548 if (!RegQueryValueExW(hKey
, newName
, NULL
, NULL
, NULL
, NULL
)) {
549 error_code_messagebox(hwnd
, IDS_VALUE_EXISTS
, oldName
);
552 value
= read_value(hwnd
, hKey
, oldName
, &type
, &len
);
553 if(!value
) goto done
;
554 lRet
= RegSetValueExW(hKey
, newName
, 0, type
, (BYTE
*)value
, len
);
556 error_code_messagebox(hwnd
, IDS_RENAME_VALUE_FAILED
);
559 lRet
= RegDeleteValueW(hKey
, oldName
);
561 RegDeleteValueW(hKey
, newName
);
562 error_code_messagebox(hwnd
, IDS_RENAME_VALUE_FAILED
);
574 BOOL
RenameKey(HWND hwnd
, HKEY hRootKey
, LPCWSTR keyPath
, LPCWSTR newName
)
576 LPWSTR parentPath
= 0;
577 LPCWSTR srcSubKey
= 0;
584 if (!keyPath
|| !newName
) return FALSE
;
586 if (!wcsrchr(keyPath
, '\\')) {
587 parentKey
= hRootKey
;
590 LPWSTR srcSubKey_copy
;
592 parentPath
= malloc((lstrlenW(keyPath
) + 1) * sizeof(WCHAR
));
593 lstrcpyW(parentPath
, keyPath
);
594 srcSubKey_copy
= wcsrchr(parentPath
, '\\');
596 srcSubKey
= srcSubKey_copy
+ 1;
597 lRet
= RegOpenKeyExW(hRootKey
, parentPath
, 0, KEY_READ
| KEY_CREATE_SUB_KEY
, &parentKey
);
599 error_code_messagebox(hwnd
, IDS_RENAME_KEY_FAILED
);
604 /* The following fails if the old name is the same as the new name. */
605 if (!lstrcmpW(srcSubKey
, newName
)) goto done
;
607 lRet
= RegCreateKeyExW(parentKey
, newName
, 0, NULL
, REG_OPTION_NON_VOLATILE
,
608 KEY_WRITE
, NULL
/* FIXME */, &destKey
, &disposition
);
609 if (disposition
== REG_OPENED_EXISTING_KEY
)
610 lRet
= ERROR_FILE_EXISTS
;
612 error_code_messagebox(hwnd
, IDS_KEY_EXISTS
, srcSubKey
);
616 /* FIXME: SHCopyKey does not copy the security attributes */
617 lRet
= SHCopyKeyW(parentKey
, srcSubKey
, destKey
, 0);
619 RegCloseKey(destKey
);
620 RegDeleteKeyW(parentKey
, newName
);
621 error_code_messagebox(hwnd
, IDS_RENAME_KEY_FAILED
);
625 lRet
= SHDeleteKeyW(hRootKey
, keyPath
);
627 error_code_messagebox(hwnd
, IDS_RENAME_KEY_FAILED
);
634 RegCloseKey(destKey
);
636 RegCloseKey(parentKey
);