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
, va_list ap
)
51 static const WCHAR errorW
[] = {'E','r','r','o','r',0};
52 static const WCHAR unknownW
[] = {'U','n','k','n','o','w','n',' ','e','r','r','o','r',' ','s','t','r','i','n','g','!',0};
58 if (!LoadStringW(hInst
, titleId
, title
, COUNT_OF(title
))) lstrcpyW(title
, errorW
);
59 if (!LoadStringW(hInst
, resId
, errfmt
, COUNT_OF(errfmt
))) lstrcpyW(errfmt
, unknownW
);
61 vsnprintfW(errstr
, COUNT_OF(errstr
), errfmt
, ap
);
63 return MessageBoxW(hwnd
, errstr
, title
, buttons
);
66 static INT
messagebox(HWND hwnd
, INT buttons
, INT titleId
, INT resId
, ...)
72 result
= vmessagebox(hwnd
, buttons
, titleId
, resId
, ap
);
78 void error(HWND hwnd
, INT resId
, ...)
83 vmessagebox(hwnd
, MB_OK
| MB_ICONERROR
, IDS_ERROR
, resId
, ap
);
87 static void error_code_messagebox(HWND hwnd
, DWORD error_code
)
92 static WCHAR fallback
[] = {'E','r','r','o','r',' ','d','i','s','p','l','a','y','i','n','g',' ','e','r','r','o','r',' ','m','e','s','s','a','g','e','.','\n',0};
93 static const WCHAR title_error
[] = {'E','r','r','o','r',0};
95 if (!LoadStringW(hInst
, IDS_ERROR
, title
, COUNT_OF(title
)))
96 lstrcpyW(title
, title_error
);
97 status
= FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
98 NULL
, error_code
, 0, (LPWSTR
)&lpMsgBuf
, 0, NULL
);
101 MessageBoxW(hwnd
, lpMsgBuf
, title
, MB_OK
| MB_ICONERROR
);
102 if (lpMsgBuf
!= fallback
)
106 static BOOL
change_dword_base(HWND hwndDlg
, BOOL toHex
)
108 static const WCHAR percent_u
[] = {'%','u',0};
109 static const WCHAR percent_x
[] = {'%','x',0};
114 if (!GetDlgItemTextW(hwndDlg
, IDC_VALUE_DATA
, buf
, COUNT_OF(buf
))) return FALSE
;
115 if (!swscanf(buf
, toHex
? percent_u
: percent_x
, &val
)) return FALSE
;
116 wsprintfW(buf
, toHex
? percent_x
: percent_u
, val
);
117 return SetDlgItemTextW(hwndDlg
, IDC_VALUE_DATA
, buf
);
120 static INT_PTR CALLBACK
modify_dlgproc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
128 SetDlgItemTextW(hwndDlg
, IDC_VALUE_NAME
, editValueName
);
129 SetDlgItemTextW(hwndDlg
, IDC_VALUE_DATA
, stringValueData
);
130 CheckRadioButton(hwndDlg
, IDC_DWORD_HEX
, IDC_DWORD_DEC
, isDecimal
? IDC_DWORD_DEC
: IDC_DWORD_HEX
);
133 switch (LOWORD(wParam
)) {
135 if (isDecimal
&& change_dword_base(hwndDlg
, TRUE
)) isDecimal
= FALSE
;
138 if (!isDecimal
&& change_dword_base(hwndDlg
, FALSE
)) isDecimal
= TRUE
;
141 if ((hwndValue
= GetDlgItem(hwndDlg
, IDC_VALUE_DATA
))) {
142 len
= GetWindowTextLengthW(hwndValue
);
143 if ((valueData
= HeapReAlloc(GetProcessHeap(), 0, stringValueData
, (len
+ 1) * sizeof(WCHAR
)))) {
144 stringValueData
= valueData
;
145 if (!GetWindowTextW(hwndValue
, stringValueData
, len
+ 1))
146 *stringValueData
= 0;
151 EndDialog(hwndDlg
, wParam
);
158 static INT_PTR CALLBACK
bin_modify_dlgproc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
160 struct edit_params
*params
;
167 params
= (struct edit_params
*)lParam
;
168 SetWindowLongPtrW(hwndDlg
, DWLP_USER
, (ULONG_PTR
)params
);
169 if (params
->lpszValueName
)
170 SetDlgItemTextW(hwndDlg
, IDC_VALUE_NAME
, params
->lpszValueName
);
172 SetDlgItemTextW(hwndDlg
, IDC_VALUE_NAME
, g_pszDefaultValueName
);
173 SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, HEM_SETDATA
, (WPARAM
)params
->cbData
, (LPARAM
)params
->pData
);
176 switch (LOWORD(wParam
)) {
178 params
= (struct edit_params
*)GetWindowLongPtrW(hwndDlg
, DWLP_USER
);
179 cbData
= SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, HEM_GETDATA
, 0, 0);
180 pData
= HeapAlloc(GetProcessHeap(), 0, cbData
);
184 SendDlgItemMessageW(hwndDlg
, IDC_VALUE_DATA
, HEM_GETDATA
, (WPARAM
)cbData
, (LPARAM
)pData
);
185 lRet
= RegSetValueExW(params
->hKey
, params
->lpszValueName
, 0, REG_BINARY
, pData
, cbData
);
188 lRet
= ERROR_OUTOFMEMORY
;
190 if (lRet
== ERROR_SUCCESS
)
191 EndDialog(hwndDlg
, 1);
194 error_code_messagebox(hwndDlg
, lRet
);
195 EndDialog(hwndDlg
, 0);
199 EndDialog(hwndDlg
, 0);
206 static BOOL
check_value(HWND hwnd
, HKEY hKey
, LPCWSTR valueName
)
209 LONG lRet
= RegQueryValueExW(hKey
, valueName
? valueName
: &empty
, 0, NULL
, 0, NULL
);
210 if(lRet
!= ERROR_SUCCESS
) return FALSE
;
214 static LPWSTR
read_value(HWND hwnd
, HKEY hKey
, LPCWSTR valueName
, DWORD
*lpType
, LONG
*len
)
217 LPWSTR buffer
= NULL
;
221 lRet
= RegQueryValueExW(hKey
, valueName
? valueName
: &empty
, 0, lpType
, 0, &valueDataLen
);
222 if (lRet
!= ERROR_SUCCESS
) {
223 if (lRet
== ERROR_FILE_NOT_FOUND
&& !valueName
) { /* no default value here, make it up */
225 if (lpType
) *lpType
= REG_SZ
;
226 buffer
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
));
230 error(hwnd
, IDS_BAD_VALUE
, valueName
);
233 if ( *lpType
== REG_DWORD
) valueDataLen
= sizeof(DWORD
);
234 if (!(buffer
= HeapAlloc(GetProcessHeap(), 0, valueDataLen
+sizeof(WCHAR
)))) {
235 error(hwnd
, IDS_TOO_BIG_VALUE
, valueDataLen
);
238 lRet
= RegQueryValueExW(hKey
, valueName
, 0, 0, (LPBYTE
)buffer
, &valueDataLen
);
239 if (lRet
!= ERROR_SUCCESS
) {
240 error(hwnd
, IDS_BAD_VALUE
, valueName
);
243 if((valueDataLen
% sizeof(WCHAR
)) == 0)
244 buffer
[valueDataLen
/ sizeof(WCHAR
)] = 0;
245 if(len
) *len
= valueDataLen
;
249 HeapFree(GetProcessHeap(), 0, buffer
);
253 BOOL
CreateKey(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPWSTR keyName
)
256 LONG lRet
= ERROR_SUCCESS
;
258 WCHAR newKey
[MAX_NEW_KEY_LEN
- 4];
262 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_CREATE_SUB_KEY
, &hKey
);
263 if (lRet
!= ERROR_SUCCESS
) {
264 error_code_messagebox(hwnd
, lRet
);
268 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWKEY
, newKey
, COUNT_OF(newKey
))) goto done
;
270 /* try to find out a name for the newly create key (max 100 times) */
271 for (keyNum
= 1; keyNum
< 100; keyNum
++) {
272 wsprintfW(keyName
, newKey
, keyNum
);
273 lRet
= RegOpenKeyW(hKey
, keyName
, &retKey
);
274 if (lRet
!= ERROR_SUCCESS
) break;
277 if (lRet
== ERROR_SUCCESS
) goto done
;
279 lRet
= RegCreateKeyW(hKey
, keyName
, &retKey
);
280 if (lRet
!= ERROR_SUCCESS
) {
281 error_code_messagebox(hwnd
, lRet
);
292 BOOL
ModifyValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR valueName
)
300 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
301 if (lRet
!= ERROR_SUCCESS
) {
302 error_code_messagebox(hwnd
, lRet
);
306 editValueName
= valueName
? valueName
: g_pszDefaultValueName
;
307 if(!(stringValueData
= read_value(hwnd
, hKey
, valueName
, &type
, &len
))) goto done
;
309 if ( (type
== REG_SZ
) || (type
== REG_EXPAND_SZ
) ) {
310 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_STRING
), hwnd
, modify_dlgproc
) == IDOK
) {
311 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (LPBYTE
)stringValueData
, (lstrlenW(stringValueData
) + 1) * sizeof(WCHAR
));
312 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
313 else error_code_messagebox(hwnd
, lRet
);
315 } else if ( type
== REG_DWORD
) {
316 const WCHAR u
[] = {'%','u',0};
317 const WCHAR x
[] = {'%','x',0};
318 wsprintfW(stringValueData
, isDecimal
? u
: x
, *((DWORD
*)stringValueData
));
319 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_DWORD
), hwnd
, modify_dlgproc
) == IDOK
) {
321 CHAR
* valueA
= GetMultiByteString(stringValueData
);
322 if (sscanf(valueA
, isDecimal
? "%u" : "%x", &val
)) {
323 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (BYTE
*)&val
, sizeof(val
));
324 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
325 else error_code_messagebox(hwnd
, lRet
);
327 HeapFree(GetProcessHeap(), 0, valueA
);
329 } else if ( type
== REG_BINARY
) {
330 struct edit_params params
;
332 params
.lpszValueName
= valueName
;
333 params
.pData
= stringValueData
;
335 result
= DialogBoxParamW(NULL
, MAKEINTRESOURCEW(IDD_EDIT_BINARY
), hwnd
,
336 bin_modify_dlgproc
, (LPARAM
)¶ms
);
337 } else if ( type
== REG_MULTI_SZ
) {
338 WCHAR char1
= '\r', char2
= '\n';
339 WCHAR
*tmpValueData
= NULL
;
342 for ( i
= 0, count
= 0; i
< len
- 1; i
++)
343 if ( !stringValueData
[i
] && stringValueData
[i
+ 1] )
345 tmpValueData
= HeapAlloc( GetProcessHeap(), 0, ( len
+ count
) * sizeof(WCHAR
));
346 if ( !tmpValueData
) goto done
;
348 for ( i
= 0, j
= 0; i
< len
- 1; i
++)
350 if ( !stringValueData
[i
] && stringValueData
[i
+ 1])
352 tmpValueData
[j
++] = char1
;
353 tmpValueData
[j
++] = char2
;
356 tmpValueData
[j
++] = stringValueData
[i
];
358 tmpValueData
[j
] = stringValueData
[i
];
359 HeapFree( GetProcessHeap(), 0, stringValueData
);
360 stringValueData
= tmpValueData
;
363 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_MULTI_STRING
), hwnd
, modify_dlgproc
) == IDOK
)
365 len
= lstrlenW( stringValueData
);
366 tmpValueData
= HeapAlloc( GetProcessHeap(), 0, (len
+ 2) * sizeof(WCHAR
));
367 if ( !tmpValueData
) goto done
;
369 for ( i
= 0, j
= 0; i
< len
- 1; i
++)
371 if ( stringValueData
[i
] == char1
&& stringValueData
[i
+ 1] == char2
)
373 if ( tmpValueData
[j
- 1] != 0)
374 tmpValueData
[j
++] = 0;
378 tmpValueData
[j
++] = stringValueData
[i
];
380 tmpValueData
[j
++] = stringValueData
[i
];
381 tmpValueData
[j
++] = 0;
382 tmpValueData
[j
++] = 0;
383 HeapFree( GetProcessHeap(), 0, stringValueData
);
384 stringValueData
= tmpValueData
;
386 lRet
= RegSetValueExW(hKey
, valueName
, 0, type
, (LPBYTE
)stringValueData
, j
* sizeof(WCHAR
));
387 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
388 else error_code_messagebox(hwnd
, lRet
);
391 error(hwnd
, IDS_UNSUPPORTED_TYPE
, type
);
395 HeapFree(GetProcessHeap(), 0, stringValueData
);
396 stringValueData
= NULL
;
401 BOOL
DeleteKey(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
)
407 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
|KEY_SET_VALUE
, &hKey
);
408 if (lRet
!= ERROR_SUCCESS
) {
409 error_code_messagebox(hwnd
, lRet
);
413 if (messagebox(hwnd
, MB_YESNO
| MB_ICONEXCLAMATION
, IDS_DELETE_BOX_TITLE
, IDS_DELETE_BOX_TEXT
, keyPath
) != IDYES
)
416 lRet
= SHDeleteKeyW(hKeyRoot
, keyPath
);
417 if (lRet
!= ERROR_SUCCESS
) {
418 error(hwnd
, IDS_BAD_KEY
, keyPath
);
428 BOOL
DeleteValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR valueName
, BOOL showMessageBox
)
433 LPCWSTR visibleValueName
= valueName
? valueName
: g_pszDefaultValueName
;
436 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
437 if (lRet
!= ERROR_SUCCESS
) return FALSE
;
441 if (messagebox(hwnd
, MB_YESNO
| MB_ICONEXCLAMATION
, IDS_DELETE_BOX_TITLE
, IDS_DELETE_BOX_TEXT
, visibleValueName
) != IDYES
)
445 lRet
= RegDeleteValueW(hKey
, valueName
? valueName
: &empty
);
446 if (lRet
!= ERROR_SUCCESS
&& valueName
) {
447 error(hwnd
, IDS_BAD_VALUE
, valueName
);
449 if (lRet
!= ERROR_SUCCESS
) goto done
;
457 BOOL
CreateValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, DWORD valueType
, LPWSTR valueName
)
459 LONG lRet
= ERROR_SUCCESS
;
461 DWORD valueDword
= 0;
466 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
467 if (lRet
!= ERROR_SUCCESS
) {
468 error_code_messagebox(hwnd
, lRet
);
472 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWVALUE
, newValue
, COUNT_OF(newValue
))) goto done
;
474 /* try to find out a name for the newly create key (max 100 times) */
475 for (valueNum
= 1; valueNum
< 100; valueNum
++) {
476 wsprintfW(valueName
, newValue
, valueNum
);
477 lRet
= RegQueryValueExW(hKey
, valueName
, 0, 0, 0, 0);
478 if (lRet
== ERROR_FILE_NOT_FOUND
) break;
480 if (lRet
!= ERROR_FILE_NOT_FOUND
) {
481 error_code_messagebox(hwnd
, lRet
);
485 lRet
= RegSetValueExW(hKey
, valueName
, 0, valueType
, (BYTE
*)&valueDword
, sizeof(DWORD
));
486 if (lRet
!= ERROR_SUCCESS
) {
487 error_code_messagebox(hwnd
, lRet
);
497 BOOL
RenameValue(HWND hwnd
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR oldName
, LPCWSTR newName
)
505 if (!oldName
) return FALSE
;
506 if (!newName
) return FALSE
;
508 lRet
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
509 if (lRet
!= ERROR_SUCCESS
) {
510 error_code_messagebox(hwnd
, lRet
);
513 /* check if value already exists */
514 if (check_value(hwnd
, hKey
, newName
)) goto done
;
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
, lRet
);
522 lRet
= RegDeleteValueW(hKey
, oldName
);
523 if (lRet
!= ERROR_SUCCESS
) {
524 RegDeleteValueW(hKey
, newName
);
525 error_code_messagebox(hwnd
, lRet
);
531 HeapFree(GetProcessHeap(), 0, value
);
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
= HeapAlloc(GetProcessHeap(), 0, (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
, lRet
);
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
; /* FIXME: we might want a better error message than this */
574 if (lRet
!= ERROR_SUCCESS
) {
575 error_code_messagebox(hwnd
, lRet
);
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
, lRet
);
588 lRet
= SHDeleteKeyW(hRootKey
, keyPath
);
589 if (lRet
!= ERROR_SUCCESS
) {
590 error_code_messagebox(hwnd
, lRet
);
597 RegCloseKey(destKey
);
599 RegCloseKey(parentKey
);
600 HeapFree(GetProcessHeap(), 0, parentPath
);