regedit: Convert value renaming to unicode.
[wine/multimedia.git] / programs / regedit / edit.c
blob3164eb1099b9fcb7ca664603a3656d4065337ece
1 /*
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 */
23 #include <windows.h>
24 #include <tchar.h>
25 #include <commctrl.h>
26 #include <commdlg.h>
27 #include <cderr.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <shellapi.h>
31 #include <shlwapi.h>
33 #include "main.h"
34 #include "regproc.h"
35 #include "resource.h"
37 static const WCHAR* editValueName;
38 static WCHAR* stringValueData;
39 static BOOL isDecimal;
41 struct edit_params
43 HKEY hKey;
44 LPCWSTR lpszValueName;
45 void *pData;
46 LONG cbData;
49 static INT vmessagebox(HWND hwnd, INT buttons, INT titleId, INT resId, va_list ap)
51 TCHAR title[256];
52 TCHAR errfmt[1024];
53 TCHAR errstr[1024];
55 if (!LoadString(hInst, titleId, title, COUNT_OF(title)))
56 lstrcpy(title, "Error");
58 if (!LoadString(hInst, resId, errfmt, COUNT_OF(errfmt)))
59 lstrcpy(errfmt, "Unknown error string!");
61 _vsntprintf(errstr, COUNT_OF(errstr), errfmt, ap);
63 return MessageBox(hwnd, errstr, title, buttons);
66 static INT messagebox(HWND hwnd, INT buttons, INT titleId, INT resId, ...)
68 va_list ap;
69 INT result;
71 va_start(ap, resId);
72 result = vmessagebox(hwnd, buttons, titleId, resId, ap);
73 va_end(ap);
75 return result;
78 void error(HWND hwnd, INT resId, ...)
80 va_list ap;
82 va_start(ap, resId);
83 vmessagebox(hwnd, MB_OK | MB_ICONERROR, IDS_ERROR, resId, ap);
84 va_end(ap);
87 static void error_code_messagebox(HWND hwnd, DWORD error_code)
89 LPTSTR lpMsgBuf;
90 DWORD status;
91 TCHAR title[256];
92 static TCHAR fallback[] = TEXT("Error displaying error message.\n");
93 if (!LoadString(hInst, IDS_ERROR, title, COUNT_OF(title)))
94 lstrcpy(title, TEXT("Error"));
95 status = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
96 NULL, error_code, 0, (LPTSTR)&lpMsgBuf, 0, NULL);
97 if (!status)
98 lpMsgBuf = fallback;
99 MessageBox(hwnd, lpMsgBuf, title, MB_OK | MB_ICONERROR);
100 if (lpMsgBuf != fallback)
101 LocalFree(lpMsgBuf);
104 static BOOL change_dword_base(HWND hwndDlg, BOOL toHex)
106 TCHAR buf[128];
107 DWORD val;
109 if (!GetDlgItemText(hwndDlg, IDC_VALUE_DATA, buf, COUNT_OF(buf))) return FALSE;
110 if (!_stscanf(buf, toHex ? "%u" : "%x", &val)) return FALSE;
111 wsprintf(buf, toHex ? "%x" : "%u", val);
112 return SetDlgItemText(hwndDlg, IDC_VALUE_DATA, buf);
115 static INT_PTR CALLBACK modify_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
117 WCHAR* valueData;
118 HWND hwndValue;
119 int len;
121 switch(uMsg) {
122 case WM_INITDIALOG:
123 SetDlgItemTextW(hwndDlg, IDC_VALUE_NAME, editValueName);
124 SetDlgItemTextW(hwndDlg, IDC_VALUE_DATA, stringValueData);
125 CheckRadioButton(hwndDlg, IDC_DWORD_HEX, IDC_DWORD_DEC, isDecimal ? IDC_DWORD_DEC : IDC_DWORD_HEX);
126 return TRUE;
127 case WM_COMMAND:
128 switch (LOWORD(wParam)) {
129 case IDC_DWORD_HEX:
130 if (isDecimal && change_dword_base(hwndDlg, TRUE)) isDecimal = FALSE;
131 break;
132 case IDC_DWORD_DEC:
133 if (!isDecimal && change_dword_base(hwndDlg, FALSE)) isDecimal = TRUE;
134 break;
135 case IDOK:
136 if ((hwndValue = GetDlgItem(hwndDlg, IDC_VALUE_DATA))) {
137 len = GetWindowTextLengthW(hwndValue);
138 if ((valueData = HeapReAlloc(GetProcessHeap(), 0, stringValueData, (len + 1) * sizeof(WCHAR)))) {
139 stringValueData = valueData;
140 if (!GetWindowTextW(hwndValue, stringValueData, len + 1))
141 *stringValueData = 0;
144 /* Fall through */
145 case IDCANCEL:
146 EndDialog(hwndDlg, wParam);
147 return TRUE;
150 return FALSE;
153 static INT_PTR CALLBACK bin_modify_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
155 struct edit_params *params;
156 LPBYTE pData;
157 LONG cbData;
158 LONG lRet;
160 switch(uMsg) {
161 case WM_INITDIALOG:
162 params = (struct edit_params *)lParam;
163 SetWindowLongPtr(hwndDlg, DWLP_USER, (ULONG_PTR)params);
164 if (params->lpszValueName)
165 SetDlgItemTextW(hwndDlg, IDC_VALUE_NAME, params->lpszValueName);
166 else
167 SetDlgItemTextW(hwndDlg, IDC_VALUE_NAME, g_pszDefaultValueNameW);
168 SendDlgItemMessage(hwndDlg, IDC_VALUE_DATA, HEM_SETDATA, (WPARAM)params->cbData, (LPARAM)params->pData);
169 return TRUE;
170 case WM_COMMAND:
171 switch (LOWORD(wParam)) {
172 case IDOK:
173 params = (struct edit_params *)GetWindowLongPtr(hwndDlg, DWLP_USER);
174 cbData = SendDlgItemMessage(hwndDlg, IDC_VALUE_DATA, HEM_GETDATA, 0, 0);
175 pData = HeapAlloc(GetProcessHeap(), 0, cbData);
177 if (pData)
179 SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, HEM_GETDATA, (WPARAM)cbData, (LPARAM)pData);
180 lRet = RegSetValueExW(params->hKey, params->lpszValueName, 0, REG_BINARY, pData, cbData);
182 else
183 lRet = ERROR_OUTOFMEMORY;
185 if (lRet == ERROR_SUCCESS)
186 EndDialog(hwndDlg, 1);
187 else
189 error_code_messagebox(hwndDlg, lRet);
190 EndDialog(hwndDlg, 0);
192 return TRUE;
193 case IDCANCEL:
194 EndDialog(hwndDlg, 0);
195 return TRUE;
198 return FALSE;
201 static BOOL check_value(HWND hwnd, HKEY hKey, LPCWSTR valueName)
203 WCHAR empty = 0;
204 LONG lRet = RegQueryValueExW(hKey, valueName ? valueName : &empty, 0, NULL, 0, NULL);
205 if(lRet != ERROR_SUCCESS) return FALSE;
206 return TRUE;
209 static LPWSTR read_value(HWND hwnd, HKEY hKey, LPCWSTR valueName, DWORD *lpType, LONG *len)
211 DWORD valueDataLen;
212 LPWSTR buffer = NULL;
213 LONG lRet;
214 WCHAR empty = 0;
216 lRet = RegQueryValueExW(hKey, valueName ? valueName : &empty, 0, lpType, 0, &valueDataLen);
217 if (lRet != ERROR_SUCCESS) {
218 if (lRet == ERROR_FILE_NOT_FOUND && !valueName) { /* no default value here, make it up */
219 if (len) *len = 1;
220 if (lpType) *lpType = REG_SZ;
221 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR));
222 *buffer = '\0';
223 return buffer;
225 error(hwnd, IDS_BAD_VALUE, valueName);
226 goto done;
228 if ( *lpType == REG_DWORD ) valueDataLen = sizeof(DWORD);
229 if (!(buffer = HeapAlloc(GetProcessHeap(), 0, valueDataLen+sizeof(WCHAR)))) {
230 error(hwnd, IDS_TOO_BIG_VALUE, valueDataLen);
231 goto done;
233 lRet = RegQueryValueExW(hKey, valueName, 0, 0, (LPBYTE)buffer, &valueDataLen);
234 if (lRet != ERROR_SUCCESS) {
235 error(hwnd, IDS_BAD_VALUE, valueName);
236 goto done;
238 if((valueDataLen % sizeof(WCHAR)) == 0)
239 buffer[valueDataLen / sizeof(WCHAR)] = 0;
240 if(len) *len = valueDataLen;
241 return buffer;
243 done:
244 HeapFree(GetProcessHeap(), 0, buffer);
245 return NULL;
248 BOOL CreateKey(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPWSTR keyName)
250 BOOL result = FALSE;
251 LONG lRet = ERROR_SUCCESS;
252 HKEY retKey = NULL;
253 WCHAR newKey[MAX_NEW_KEY_LEN - 4];
254 int keyNum;
255 HKEY hKey;
257 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_CREATE_SUB_KEY, &hKey);
258 if (lRet != ERROR_SUCCESS) {
259 error_code_messagebox(hwnd, lRet);
260 goto done;
263 if (!LoadStringW(GetModuleHandle(0), IDS_NEWKEY, newKey, COUNT_OF(newKey))) goto done;
265 /* try to find out a name for the newly create key (max 100 times) */
266 for (keyNum = 1; keyNum < 100; keyNum++) {
267 wsprintfW(keyName, newKey, keyNum);
268 lRet = RegOpenKeyW(hKey, keyName, &retKey);
269 if (lRet != ERROR_SUCCESS) break;
270 RegCloseKey(retKey);
272 if (lRet == ERROR_SUCCESS) goto done;
274 lRet = RegCreateKeyW(hKey, keyName, &retKey);
275 if (lRet != ERROR_SUCCESS) {
276 error_code_messagebox(hwnd, lRet);
277 goto done;
280 result = TRUE;
282 done:
283 RegCloseKey(retKey);
284 return result;
287 BOOL ModifyValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR valueName)
289 BOOL result = FALSE;
290 DWORD type;
291 LONG lRet;
292 HKEY hKey;
293 LONG len;
295 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
296 if (lRet != ERROR_SUCCESS) {
297 error_code_messagebox(hwnd, lRet);
298 return FALSE;
301 editValueName = valueName ? valueName : g_pszDefaultValueNameW;
302 if(!(stringValueData = read_value(hwnd, hKey, valueName, &type, &len))) goto done;
304 if ( (type == REG_SZ) || (type == REG_EXPAND_SZ) ) {
305 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_STRING), hwnd, modify_dlgproc) == IDOK) {
306 lRet = RegSetValueExW(hKey, valueName, 0, type, (LPBYTE)stringValueData, (lstrlenW(stringValueData) + 1) * sizeof(WCHAR));
307 if (lRet == ERROR_SUCCESS) result = TRUE;
308 else error_code_messagebox(hwnd, lRet);
310 } else if ( type == REG_DWORD ) {
311 const WCHAR u[] = {'%','u',0};
312 const WCHAR x[] = {'%','x',0};
313 wsprintfW(stringValueData, isDecimal ? u : x, *((DWORD*)stringValueData));
314 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_DWORD), hwnd, modify_dlgproc) == IDOK) {
315 DWORD val;
316 CHAR* valueA = GetMultiByteString(stringValueData);
317 if (_stscanf(valueA, isDecimal ? "%u" : "%x", &val)) {
318 lRet = RegSetValueExW(hKey, valueName, 0, type, (BYTE*)&val, sizeof(val));
319 if (lRet == ERROR_SUCCESS) result = TRUE;
320 else error_code_messagebox(hwnd, lRet);
322 HeapFree(GetProcessHeap(), 0, valueA);
324 } else if ( type == REG_BINARY ) {
325 struct edit_params params;
326 params.hKey = hKey;
327 params.lpszValueName = valueName;
328 params.pData = stringValueData;
329 params.cbData = len;
330 result = DialogBoxParam(NULL, MAKEINTRESOURCE(IDD_EDIT_BINARY), hwnd,
331 bin_modify_dlgproc, (LPARAM)&params);
332 } else if ( type == REG_MULTI_SZ ) {
333 WCHAR char1 = '\r', char2 = '\n';
334 WCHAR *tmpValueData = NULL;
335 INT i, j, count;
337 for ( i = 0, count = 0; i < len - 1; i++)
338 if ( !stringValueData[i] && stringValueData[i + 1] )
339 count++;
340 tmpValueData = HeapAlloc( GetProcessHeap(), 0, ( len + count ) * sizeof(WCHAR));
341 if ( !tmpValueData ) goto done;
343 for ( i = 0, j = 0; i < len - 1; i++)
345 if ( !stringValueData[i] && stringValueData[i + 1])
347 tmpValueData[j++] = char1;
348 tmpValueData[j++] = char2;
350 else
351 tmpValueData[j++] = stringValueData[i];
353 tmpValueData[j] = stringValueData[i];
354 HeapFree( GetProcessHeap(), 0, stringValueData);
355 stringValueData = tmpValueData;
356 tmpValueData = NULL;
358 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_MULTI_STRING), hwnd, modify_dlgproc) == IDOK)
360 len = lstrlenW( stringValueData );
361 tmpValueData = HeapAlloc( GetProcessHeap(), 0, (len + 2) * sizeof(WCHAR));
362 if ( !tmpValueData ) goto done;
364 for ( i = 0, j = 0; i < len - 1; i++)
366 if ( stringValueData[i] == char1 && stringValueData[i + 1] == char2)
368 if ( tmpValueData[j - 1] != 0)
369 tmpValueData[j++] = 0;
370 i++;
372 else
373 tmpValueData[j++] = stringValueData[i];
375 tmpValueData[j++] = stringValueData[i];
376 tmpValueData[j++] = 0;
377 tmpValueData[j++] = 0;
378 HeapFree( GetProcessHeap(), 0, stringValueData);
379 stringValueData = tmpValueData;
381 lRet = RegSetValueExW(hKey, valueName, 0, type, (LPBYTE)stringValueData, j * sizeof(TCHAR));
382 if (lRet == ERROR_SUCCESS) result = TRUE;
383 else error_code_messagebox(hwnd, lRet);
385 } else {
386 error(hwnd, IDS_UNSUPPORTED_TYPE, type);
389 done:
390 HeapFree(GetProcessHeap(), 0, stringValueData);
391 stringValueData = NULL;
392 RegCloseKey(hKey);
393 return result;
396 BOOL DeleteKey(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath)
398 BOOL result = FALSE;
399 LONG lRet;
400 HKEY hKey;
401 CHAR* keyPathA = GetMultiByteString(keyPath);
403 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ|KEY_SET_VALUE, &hKey);
404 if (lRet != ERROR_SUCCESS) {
405 error_code_messagebox(hwnd, lRet);
406 return FALSE;
409 if (messagebox(hwnd, MB_YESNO | MB_ICONEXCLAMATION, IDS_DELETE_BOX_TITLE, IDS_DELETE_BOX_TEXT, keyPathA) != IDYES)
410 goto done;
412 lRet = SHDeleteKeyW(hKeyRoot, keyPath);
413 if (lRet != ERROR_SUCCESS) {
414 error(hwnd, IDS_BAD_KEY, keyPath);
415 goto done;
417 result = TRUE;
419 done:
420 RegCloseKey(hKey);
421 HeapFree(GetProcessHeap(), 0, keyPathA);
422 return result;
425 BOOL DeleteValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR valueName, BOOL showMessageBox)
427 BOOL result = FALSE;
428 LONG lRet;
429 HKEY hKey;
430 LPCWSTR visibleValueName = valueName ? valueName : g_pszDefaultValueNameW;
431 WCHAR empty = 0;
433 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
434 if (lRet != ERROR_SUCCESS) return FALSE;
436 if (showMessageBox)
438 LPSTR visibleValueNameA = GetMultiByteString(visibleValueName);
439 if (messagebox(hwnd, MB_YESNO | MB_ICONEXCLAMATION, IDS_DELETE_BOX_TITLE, IDS_DELETE_BOX_TEXT, visibleValueNameA) != IDYES)
441 HeapFree(GetProcessHeap(), 0, visibleValueNameA);
442 goto done;
444 HeapFree(GetProcessHeap(), 0, visibleValueNameA);
447 lRet = RegDeleteValueW(hKey, valueName ? valueName : &empty);
448 if (lRet != ERROR_SUCCESS && valueName) {
449 error(hwnd, IDS_BAD_VALUE, valueName);
451 if (lRet != ERROR_SUCCESS) goto done;
452 result = TRUE;
454 done:
455 RegCloseKey(hKey);
456 return result;
459 BOOL CreateValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, DWORD valueType, LPWSTR valueName)
461 LONG lRet = ERROR_SUCCESS;
462 WCHAR newValue[256];
463 DWORD valueDword = 0;
464 BOOL result = FALSE;
465 int valueNum;
466 HKEY hKey;
468 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
469 if (lRet != ERROR_SUCCESS) {
470 error_code_messagebox(hwnd, lRet);
471 return FALSE;
474 if (!LoadStringW(GetModuleHandle(0), IDS_NEWVALUE, newValue, COUNT_OF(newValue))) goto done;
476 /* try to find out a name for the newly create key (max 100 times) */
477 for (valueNum = 1; valueNum < 100; valueNum++) {
478 wsprintfW(valueName, newValue, valueNum);
479 lRet = RegQueryValueExW(hKey, valueName, 0, 0, 0, 0);
480 if (lRet == ERROR_FILE_NOT_FOUND) break;
482 if (lRet != ERROR_FILE_NOT_FOUND) {
483 error_code_messagebox(hwnd, lRet);
484 goto done;
487 lRet = RegSetValueExW(hKey, valueName, 0, valueType, (BYTE*)&valueDword, sizeof(DWORD));
488 if (lRet != ERROR_SUCCESS) {
489 error_code_messagebox(hwnd, lRet);
490 goto done;
492 result = TRUE;
494 done:
495 RegCloseKey(hKey);
496 return result;
499 BOOL RenameValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR oldName, LPCWSTR newName)
501 LPWSTR value = NULL;
502 DWORD type;
503 LONG len, lRet;
504 BOOL result = FALSE;
505 HKEY hKey;
507 if (!oldName) return FALSE;
508 if (!newName) return FALSE;
510 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
511 if (lRet != ERROR_SUCCESS) {
512 error_code_messagebox(hwnd, lRet);
513 return FALSE;
515 /* check if value already exists */
516 if (check_value(hwnd, hKey, newName)) goto done;
517 value = read_value(hwnd, hKey, oldName, &type, &len);
518 if(!value) goto done;
519 lRet = RegSetValueExW(hKey, newName, 0, type, (BYTE*)value, len);
520 if (lRet != ERROR_SUCCESS) {
521 error_code_messagebox(hwnd, lRet);
522 goto done;
524 lRet = RegDeleteValueW(hKey, oldName);
525 if (lRet != ERROR_SUCCESS) {
526 RegDeleteValueW(hKey, newName);
527 error_code_messagebox(hwnd, lRet);
528 goto done;
530 result = TRUE;
532 done:
533 HeapFree(GetProcessHeap(), 0, value);
534 RegCloseKey(hKey);
535 return result;
539 BOOL RenameKey(HWND hwnd, HKEY hRootKey, LPCTSTR keyPath, LPCTSTR newName)
541 LPTSTR parentPath = 0;
542 LPCTSTR srcSubKey = 0;
543 HKEY parentKey = 0;
544 HKEY destKey = 0;
545 BOOL result = FALSE;
546 LONG lRet;
547 DWORD disposition;
549 if (!keyPath || !newName) return FALSE;
551 if (!strrchr(keyPath, '\\')) {
552 parentKey = hRootKey;
553 srcSubKey = keyPath;
554 } else {
555 LPTSTR srcSubKey_copy;
557 parentPath = strdup(keyPath);
558 srcSubKey_copy = strrchr(parentPath, '\\');
559 *srcSubKey_copy = 0;
560 srcSubKey = srcSubKey_copy + 1;
561 lRet = RegOpenKeyEx(hRootKey, parentPath, 0, KEY_READ | KEY_CREATE_SUB_KEY, &parentKey);
562 if (lRet != ERROR_SUCCESS) {
563 error_code_messagebox(hwnd, lRet);
564 goto done;
568 /* The following fails if the old name is the same as the new name. */
569 if (!strcmp(srcSubKey, newName)) goto done;
571 lRet = RegCreateKeyEx(parentKey, newName, 0, NULL, REG_OPTION_NON_VOLATILE,
572 KEY_WRITE, NULL /* FIXME */, &destKey, &disposition);
573 if (disposition == REG_OPENED_EXISTING_KEY)
574 lRet = ERROR_FILE_EXISTS; /* FIXME: we might want a better error message than this */
575 if (lRet != ERROR_SUCCESS) {
576 error_code_messagebox(hwnd, lRet);
577 goto done;
580 /* FIXME: SHCopyKey does not copy the security attributes */
581 lRet = SHCopyKey(parentKey, srcSubKey, destKey, 0);
582 if (lRet != ERROR_SUCCESS) {
583 RegCloseKey(destKey);
584 RegDeleteKey(parentKey, newName);
585 error_code_messagebox(hwnd, lRet);
586 goto done;
589 lRet = SHDeleteKey(hRootKey, keyPath);
590 if (lRet != ERROR_SUCCESS) {
591 error_code_messagebox(hwnd, lRet);
592 goto done;
595 result = TRUE;
597 done:
598 RegCloseKey(destKey);
599 if (parentKey) {
600 RegCloseKey(parentKey);
601 free(parentPath);
603 return result;