push a9ea98eed33411fd355fd4e8b78f7880f1dd09a3
[wine/hacks.git] / programs / regedit / edit.c
blob1c26baa5ad484b86d4a21e6f49a76041470365f1
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 TCHAR* editValueName;
38 static TCHAR* stringValueData;
39 static BOOL isDecimal;
41 struct edit_params
43 HKEY hKey;
44 LPCTSTR 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 TCHAR* valueData;
118 HWND hwndValue;
119 int len;
121 switch(uMsg) {
122 case WM_INITDIALOG:
123 SetDlgItemText(hwndDlg, IDC_VALUE_NAME, editValueName);
124 SetDlgItemText(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 = GetWindowTextLength(hwndValue);
138 if ((valueData = HeapReAlloc(GetProcessHeap(), 0, stringValueData, (len + 1) * sizeof(TCHAR)))) {
139 stringValueData = valueData;
140 if (!GetWindowText(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 SetDlgItemText(hwndDlg, IDC_VALUE_NAME, params->lpszValueName);
166 else
167 SetDlgItemText(hwndDlg, IDC_VALUE_NAME, g_pszDefaultValueName);
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 SendDlgItemMessage(hwndDlg, IDC_VALUE_DATA, HEM_GETDATA, (WPARAM)cbData, (LPARAM)pData);
180 lRet = RegSetValueEx(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, LPCTSTR valueName)
203 LONG lRet = RegQueryValueEx(hKey, valueName ? valueName : _T(""), 0, NULL, 0, NULL);
204 if(lRet != ERROR_SUCCESS) return FALSE;
205 return TRUE;
208 static LPTSTR read_value(HWND hwnd, HKEY hKey, LPCTSTR valueName, DWORD *lpType, LONG *len)
210 DWORD valueDataLen;
211 LPTSTR buffer = NULL;
212 LONG lRet;
214 lRet = RegQueryValueEx(hKey, valueName ? valueName : _T(""), 0, lpType, 0, &valueDataLen);
215 if (lRet != ERROR_SUCCESS) {
216 if (lRet == ERROR_FILE_NOT_FOUND && !valueName) { /* no default value here, make it up */
217 if (len) *len = 1;
218 if (lpType) *lpType = REG_SZ;
219 buffer = HeapAlloc(GetProcessHeap(), 0, 1);
220 *buffer = '\0';
221 return buffer;
223 error(hwnd, IDS_BAD_VALUE, valueName);
224 goto done;
226 if ( *lpType == REG_DWORD ) valueDataLen = sizeof(DWORD);
227 if (!(buffer = HeapAlloc(GetProcessHeap(), 0, valueDataLen+1))) {
228 error(hwnd, IDS_TOO_BIG_VALUE, valueDataLen);
229 goto done;
231 lRet = RegQueryValueEx(hKey, valueName, 0, 0, (LPBYTE)buffer, &valueDataLen);
232 if (lRet != ERROR_SUCCESS) {
233 error(hwnd, IDS_BAD_VALUE, valueName);
234 goto done;
236 buffer[valueDataLen] = 0;
237 if(len) *len = valueDataLen;
238 return buffer;
240 done:
241 HeapFree(GetProcessHeap(), 0, buffer);
242 return NULL;
245 BOOL CreateKey(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPWSTR keyName)
247 BOOL result = FALSE;
248 LONG lRet = ERROR_SUCCESS;
249 HKEY retKey = NULL;
250 WCHAR newKey[MAX_NEW_KEY_LEN - 4];
251 int keyNum;
252 HKEY hKey;
254 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_CREATE_SUB_KEY, &hKey);
255 if (lRet != ERROR_SUCCESS) {
256 error_code_messagebox(hwnd, lRet);
257 goto done;
260 if (!LoadStringW(GetModuleHandle(0), IDS_NEWKEY, newKey, COUNT_OF(newKey))) goto done;
262 /* try to find out a name for the newly create key (max 100 times) */
263 for (keyNum = 1; keyNum < 100; keyNum++) {
264 wsprintfW(keyName, newKey, keyNum);
265 lRet = RegOpenKeyW(hKey, keyName, &retKey);
266 if (lRet != ERROR_SUCCESS) break;
267 RegCloseKey(retKey);
269 if (lRet == ERROR_SUCCESS) goto done;
271 lRet = RegCreateKeyW(hKey, keyName, &retKey);
272 if (lRet != ERROR_SUCCESS) {
273 error_code_messagebox(hwnd, lRet);
274 goto done;
277 result = TRUE;
279 done:
280 RegCloseKey(retKey);
281 return result;
284 BOOL ModifyValue(HWND hwnd, HKEY hKeyRoot, LPCTSTR keyPath, LPCTSTR valueName)
286 BOOL result = FALSE;
287 DWORD type;
288 LONG lRet;
289 HKEY hKey;
290 LONG len;
292 lRet = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
293 if (lRet != ERROR_SUCCESS) {
294 error_code_messagebox(hwnd, lRet);
295 return FALSE;
298 editValueName = valueName ? valueName : g_pszDefaultValueName;
299 if(!(stringValueData = read_value(hwnd, hKey, valueName, &type, &len))) goto done;
301 if ( (type == REG_SZ) || (type == REG_EXPAND_SZ) ) {
302 if (DialogBox(0, MAKEINTRESOURCE(IDD_EDIT_STRING), hwnd, modify_dlgproc) == IDOK) {
303 lRet = RegSetValueEx(hKey, valueName, 0, type, (LPBYTE)stringValueData, lstrlen(stringValueData) + 1);
304 if (lRet == ERROR_SUCCESS) result = TRUE;
305 else error_code_messagebox(hwnd, lRet);
307 } else if ( type == REG_DWORD ) {
308 wsprintf(stringValueData, isDecimal ? "%u" : "%x", *((DWORD*)stringValueData));
309 if (DialogBox(0, MAKEINTRESOURCE(IDD_EDIT_DWORD), hwnd, modify_dlgproc) == IDOK) {
310 DWORD val;
311 if (_stscanf(stringValueData, isDecimal ? "%u" : "%x", &val)) {
312 lRet = RegSetValueEx(hKey, valueName, 0, type, (BYTE*)&val, sizeof(val));
313 if (lRet == ERROR_SUCCESS) result = TRUE;
314 else error_code_messagebox(hwnd, lRet);
317 } else if ( type == REG_BINARY ) {
318 struct edit_params params;
319 params.hKey = hKey;
320 params.lpszValueName = valueName;
321 params.pData = stringValueData;
322 params.cbData = len;
323 result = DialogBoxParam(NULL, MAKEINTRESOURCE(IDD_EDIT_BINARY), hwnd,
324 bin_modify_dlgproc, (LPARAM)&params);
325 } else if ( type == REG_MULTI_SZ ) {
326 TCHAR char1 = (TCHAR)'\r', char2 = (TCHAR)'\n';
327 TCHAR *tmpValueData = NULL;
328 INT i, j, count;
330 for ( i = 0, count = 0; i < len - 1; i++)
331 if ( !stringValueData[i] && stringValueData[i + 1] )
332 count++;
333 tmpValueData = HeapAlloc( GetProcessHeap(), 0, ( len + count ) * sizeof(TCHAR));
334 if ( !tmpValueData ) goto done;
336 for ( i = 0, j = 0; i < len - 1; i++)
338 if ( !stringValueData[i] && stringValueData[i + 1])
340 tmpValueData[j++] = char1;
341 tmpValueData[j++] = char2;
343 else
344 tmpValueData[j++] = stringValueData[i];
346 tmpValueData[j] = stringValueData[i];
347 HeapFree( GetProcessHeap(), 0, stringValueData);
348 stringValueData = tmpValueData;
349 tmpValueData = NULL;
351 if (DialogBox(0, MAKEINTRESOURCE(IDD_EDIT_MULTI_STRING), hwnd, modify_dlgproc) == IDOK)
353 len = lstrlen( stringValueData );
354 tmpValueData = HeapAlloc( GetProcessHeap(), 0, (len + 2) * sizeof(TCHAR));
355 if ( !tmpValueData ) goto done;
357 for ( i = 0, j = 0; i < len - 1; i++)
359 if ( stringValueData[i] == char1 && stringValueData[i + 1] == char2)
361 if ( tmpValueData[j - 1] != 0)
362 tmpValueData[j++] = 0;
363 i++;
365 else
366 tmpValueData[j++] = stringValueData[i];
368 tmpValueData[j++] = stringValueData[i];
369 tmpValueData[j++] = 0;
370 tmpValueData[j++] = 0;
371 HeapFree( GetProcessHeap(), 0, stringValueData);
372 stringValueData = tmpValueData;
374 lRet = RegSetValueEx(hKey, valueName, 0, type, (LPBYTE)stringValueData, j * sizeof(TCHAR));
375 if (lRet == ERROR_SUCCESS) result = TRUE;
376 else error_code_messagebox(hwnd, lRet);
378 } else {
379 error(hwnd, IDS_UNSUPPORTED_TYPE, type);
382 done:
383 HeapFree(GetProcessHeap(), 0, stringValueData);
384 stringValueData = NULL;
385 RegCloseKey(hKey);
386 return result;
389 BOOL DeleteKey(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath)
391 BOOL result = FALSE;
392 LONG lRet;
393 HKEY hKey;
394 CHAR* keyPathA = GetMultiByteString(keyPath);
396 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ|KEY_SET_VALUE, &hKey);
397 if (lRet != ERROR_SUCCESS) {
398 error_code_messagebox(hwnd, lRet);
399 return FALSE;
402 if (messagebox(hwnd, MB_YESNO | MB_ICONEXCLAMATION, IDS_DELETE_BOX_TITLE, IDS_DELETE_BOX_TEXT, keyPathA) != IDYES)
403 goto done;
405 lRet = SHDeleteKeyW(hKeyRoot, keyPath);
406 if (lRet != ERROR_SUCCESS) {
407 error(hwnd, IDS_BAD_KEY, keyPath);
408 goto done;
410 result = TRUE;
412 done:
413 RegCloseKey(hKey);
414 HeapFree(GetProcessHeap(), 0, keyPathA);
415 return result;
418 BOOL DeleteValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR valueName, BOOL showMessageBox)
420 BOOL result = FALSE;
421 LONG lRet;
422 HKEY hKey;
423 LPCWSTR visibleValueName = valueName ? valueName : g_pszDefaultValueNameW;
424 WCHAR empty = 0;
426 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
427 if (lRet != ERROR_SUCCESS) return FALSE;
429 if (showMessageBox)
431 LPSTR visibleValueNameA = GetMultiByteString(visibleValueName);
432 if (messagebox(hwnd, MB_YESNO | MB_ICONEXCLAMATION, IDS_DELETE_BOX_TITLE, IDS_DELETE_BOX_TEXT, visibleValueNameA) != IDYES)
434 HeapFree(GetProcessHeap(), 0, visibleValueNameA);
435 goto done;
437 HeapFree(GetProcessHeap(), 0, visibleValueNameA);
440 lRet = RegDeleteValueW(hKey, valueName ? valueName : &empty);
441 if (lRet != ERROR_SUCCESS && valueName) {
442 error(hwnd, IDS_BAD_VALUE, valueName);
444 if (lRet != ERROR_SUCCESS) goto done;
445 result = TRUE;
447 done:
448 RegCloseKey(hKey);
449 return result;
452 BOOL CreateValue(HWND hwnd, HKEY hKeyRoot, LPCTSTR keyPath, DWORD valueType, LPTSTR valueName)
454 LONG lRet = ERROR_SUCCESS;
455 TCHAR newValue[256];
456 DWORD valueDword = 0;
457 BOOL result = FALSE;
458 int valueNum;
459 HKEY hKey;
461 lRet = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
462 if (lRet != ERROR_SUCCESS) {
463 error_code_messagebox(hwnd, lRet);
464 return FALSE;
467 if (!LoadString(GetModuleHandle(0), IDS_NEWVALUE, newValue, COUNT_OF(newValue))) goto done;
469 /* try to find out a name for the newly create key (max 100 times) */
470 for (valueNum = 1; valueNum < 100; valueNum++) {
471 wsprintf(valueName, newValue, valueNum);
472 lRet = RegQueryValueEx(hKey, valueName, 0, 0, 0, 0);
473 if (lRet == ERROR_FILE_NOT_FOUND) break;
475 if (lRet != ERROR_FILE_NOT_FOUND) {
476 error_code_messagebox(hwnd, lRet);
477 goto done;
480 lRet = RegSetValueEx(hKey, valueName, 0, valueType, (BYTE*)&valueDword, sizeof(DWORD));
481 if (lRet != ERROR_SUCCESS) {
482 error_code_messagebox(hwnd, lRet);
483 goto done;
485 result = TRUE;
487 done:
488 RegCloseKey(hKey);
489 return result;
492 BOOL RenameValue(HWND hwnd, HKEY hKeyRoot, LPCTSTR keyPath, LPCTSTR oldName, LPCTSTR newName)
494 LPTSTR value = NULL;
495 DWORD type;
496 LONG len, lRet;
497 BOOL result = FALSE;
498 HKEY hKey;
500 if (!oldName) return FALSE;
501 if (!newName) return FALSE;
503 lRet = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
504 if (lRet != ERROR_SUCCESS) {
505 error_code_messagebox(hwnd, lRet);
506 return FALSE;
508 /* check if value already exists */
509 if (check_value(hwnd, hKey, newName)) goto done;
510 value = read_value(hwnd, hKey, oldName, &type, &len);
511 if(!value) goto done;
512 lRet = RegSetValueEx(hKey, newName, 0, type, (BYTE*)value, len);
513 if (lRet != ERROR_SUCCESS) {
514 error_code_messagebox(hwnd, lRet);
515 goto done;
517 lRet = RegDeleteValue(hKey, oldName);
518 if (lRet != ERROR_SUCCESS) {
519 RegDeleteValue(hKey, newName);
520 error_code_messagebox(hwnd, lRet);
521 goto done;
523 result = TRUE;
525 done:
526 HeapFree(GetProcessHeap(), 0, value);
527 RegCloseKey(hKey);
528 return result;
532 BOOL RenameKey(HWND hwnd, HKEY hRootKey, LPCTSTR keyPath, LPCTSTR newName)
534 LPTSTR parentPath = 0;
535 LPCTSTR srcSubKey = 0;
536 HKEY parentKey = 0;
537 HKEY destKey = 0;
538 BOOL result = FALSE;
539 LONG lRet;
540 DWORD disposition;
542 if (!keyPath || !newName) return FALSE;
544 if (!strrchr(keyPath, '\\')) {
545 parentKey = hRootKey;
546 srcSubKey = keyPath;
547 } else {
548 LPTSTR srcSubKey_copy;
550 parentPath = strdup(keyPath);
551 srcSubKey_copy = strrchr(parentPath, '\\');
552 *srcSubKey_copy = 0;
553 srcSubKey = srcSubKey_copy + 1;
554 lRet = RegOpenKeyEx(hRootKey, parentPath, 0, KEY_READ | KEY_CREATE_SUB_KEY, &parentKey);
555 if (lRet != ERROR_SUCCESS) {
556 error_code_messagebox(hwnd, lRet);
557 goto done;
561 /* The following fails if the old name is the same as the new name. */
562 if (!strcmp(srcSubKey, newName)) goto done;
564 lRet = RegCreateKeyEx(parentKey, newName, 0, NULL, REG_OPTION_NON_VOLATILE,
565 KEY_WRITE, NULL /* FIXME */, &destKey, &disposition);
566 if (disposition == REG_OPENED_EXISTING_KEY)
567 lRet = ERROR_FILE_EXISTS; /* FIXME: we might want a better error message than this */
568 if (lRet != ERROR_SUCCESS) {
569 error_code_messagebox(hwnd, lRet);
570 goto done;
573 /* FIXME: SHCopyKey does not copy the security attributes */
574 lRet = SHCopyKey(parentKey, srcSubKey, destKey, 0);
575 if (lRet != ERROR_SUCCESS) {
576 RegCloseKey(destKey);
577 RegDeleteKey(parentKey, newName);
578 error_code_messagebox(hwnd, lRet);
579 goto done;
582 lRet = SHDeleteKey(hRootKey, keyPath);
583 if (lRet != ERROR_SUCCESS) {
584 error_code_messagebox(hwnd, lRet);
585 goto done;
588 result = TRUE;
590 done:
591 RegCloseKey(destKey);
592 if (parentKey) {
593 RegCloseKey(parentKey);
594 free(parentPath);
596 return result;