regedit: Directly use RegQueryValueExW() instead of a helper function.
[wine.git] / programs / regedit / edit.c
blob330b535d732488b221deed59bf787e53baf68d65
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 <commctrl.h>
25 #include <commdlg.h>
26 #include <cderr.h>
27 #include <stdlib.h>
28 #include <shellapi.h>
29 #include <shlwapi.h>
31 #include "wine/heap.h"
32 #include "main.h"
34 static const WCHAR* editValueName;
35 static WCHAR* stringValueData;
36 static BOOL isDecimal;
38 struct edit_params
40 HKEY hkey;
41 const WCHAR *value_name;
42 DWORD type;
43 void *data;
44 DWORD size;
47 static int vmessagebox(HWND hwnd, int buttons, int titleId, int resId, va_list va_args)
49 WCHAR title[256];
50 WCHAR fmt[1024];
51 WCHAR *str;
52 int ret;
54 LoadStringW(hInst, titleId, title, ARRAY_SIZE(title));
55 LoadStringW(hInst, resId, fmt, ARRAY_SIZE(fmt));
57 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
58 fmt, 0, 0, (WCHAR *)&str, 0, &va_args);
59 ret = MessageBoxW(hwnd, str, title, buttons);
60 LocalFree(str);
62 return ret;
65 int WINAPIV messagebox(HWND hwnd, int buttons, int titleId, int resId, ...)
67 va_list ap;
68 INT result;
70 va_start(ap, resId);
71 result = vmessagebox(hwnd, buttons, titleId, resId, ap);
72 va_end(ap);
74 return result;
77 static void WINAPIV error_code_messagebox(HWND hwnd, unsigned int msg_id, ...)
79 va_list ap;
81 va_start(ap, msg_id);
82 vmessagebox(hwnd, MB_OK|MB_ICONERROR, IDS_ERROR, msg_id, ap);
83 va_end(ap);
86 static BOOL change_dword_base(HWND hwndDlg, BOOL toHex)
88 WCHAR buf[128];
89 UINT64 val;
91 if (!GetDlgItemTextW(hwndDlg, IDC_VALUE_DATA, buf, ARRAY_SIZE(buf))) return FALSE;
92 if (!swscanf(buf, toHex ? L"%I64u" : L"%I64x", &val)) return FALSE;
93 wsprintfW(buf, toHex ? L"%I64x" : L"%I64u", val);
94 return SetDlgItemTextW(hwndDlg, IDC_VALUE_DATA, buf);
97 static INT_PTR CALLBACK modify_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
99 HWND hwndValue;
100 WCHAR buff[64];
101 int len;
103 switch(uMsg) {
104 case WM_INITDIALOG:
105 SetDlgItemTextW(hwndDlg, IDC_VALUE_NAME, editValueName);
106 SetDlgItemTextW(hwndDlg, IDC_VALUE_DATA, stringValueData);
107 CheckRadioButton(hwndDlg, IDC_DWORD_HEX, IDC_DWORD_DEC, IDC_DWORD_HEX);
108 isDecimal = FALSE;
109 if (lParam == REG_QWORD && LoadStringW(GetModuleHandleW(0), IDS_EDIT_QWORD, buff, ARRAY_SIZE(buff)))
110 SetWindowTextW(hwndDlg, buff);
111 return TRUE;
112 case WM_COMMAND:
113 switch (LOWORD(wParam)) {
114 case IDC_DWORD_HEX:
115 if (isDecimal && change_dword_base(hwndDlg, TRUE)) isDecimal = FALSE;
116 break;
117 case IDC_DWORD_DEC:
118 if (!isDecimal && change_dword_base(hwndDlg, FALSE)) isDecimal = TRUE;
119 break;
120 case IDOK:
121 if ((hwndValue = GetDlgItem(hwndDlg, IDC_VALUE_DATA))) {
122 len = GetWindowTextLengthW(hwndValue);
123 stringValueData = heap_xrealloc(stringValueData, (len + 1) * sizeof(WCHAR));
124 if (!GetWindowTextW(hwndValue, stringValueData, len + 1))
125 *stringValueData = 0;
127 /* Fall through */
128 case IDCANCEL:
129 EndDialog(hwndDlg, wParam);
130 return TRUE;
133 return FALSE;
136 static INT_PTR CALLBACK bin_modify_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
138 struct edit_params *params;
139 BYTE *data;
140 LONG size;
141 LONG lRet;
143 switch(uMsg) {
144 case WM_INITDIALOG:
145 params = (struct edit_params *)lParam;
146 SetWindowLongPtrW(hwndDlg, DWLP_USER, (ULONG_PTR)params);
147 if (params->value_name)
148 SetDlgItemTextW(hwndDlg, IDC_VALUE_NAME, params->value_name);
149 else
150 SetDlgItemTextW(hwndDlg, IDC_VALUE_NAME, g_pszDefaultValueName);
151 SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, HEM_SETDATA, (WPARAM)params->size, (LPARAM)params->data);
152 SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, WM_SETFONT, (WPARAM) GetStockObject(ANSI_FIXED_FONT), TRUE);
153 return TRUE;
154 case WM_COMMAND:
155 switch (LOWORD(wParam)) {
156 case IDOK:
157 params = (struct edit_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
158 size = SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, HEM_GETDATA, 0, 0);
159 data = heap_xalloc(size);
161 SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, HEM_GETDATA, (WPARAM)size, (LPARAM)data);
162 lRet = RegSetValueExW(params->hkey, params->value_name, 0, params->type, data, size);
163 heap_free(data);
165 if (lRet == ERROR_SUCCESS)
166 EndDialog(hwndDlg, 1);
167 else
169 error_code_messagebox(hwndDlg, IDS_SET_VALUE_FAILED);
170 EndDialog(hwndDlg, 0);
172 return TRUE;
173 case IDCANCEL:
174 EndDialog(hwndDlg, 0);
175 return TRUE;
178 return FALSE;
181 static LPWSTR read_value(HWND hwnd, HKEY hKey, LPCWSTR valueName, DWORD *lpType, LONG *len)
183 DWORD valueDataLen;
184 LPWSTR buffer = NULL;
185 LONG lRet;
187 lRet = RegQueryValueExW(hKey, valueName, NULL, lpType, NULL, &valueDataLen);
188 if (lRet) {
189 if (lRet == ERROR_FILE_NOT_FOUND && !valueName) { /* no default value here, make it up */
190 if (len) *len = 1;
191 if (lpType) *lpType = REG_SZ;
192 buffer = heap_xalloc(sizeof(WCHAR));
193 *buffer = '\0';
194 return buffer;
196 error_code_messagebox(hwnd, IDS_BAD_VALUE, valueName);
197 goto done;
200 buffer = heap_xalloc(valueDataLen + sizeof(WCHAR));
201 lRet = RegQueryValueExW(hKey, valueName, 0, 0, (LPBYTE)buffer, &valueDataLen);
202 if (lRet) {
203 error_code_messagebox(hwnd, IDS_BAD_VALUE, valueName);
204 goto done;
206 if((valueDataLen % sizeof(WCHAR)) == 0)
207 buffer[valueDataLen / sizeof(WCHAR)] = 0;
208 if(len) *len = valueDataLen;
209 return buffer;
211 done:
212 heap_free(buffer);
213 return NULL;
216 BOOL CreateKey(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPWSTR keyName)
218 BOOL result = FALSE;
219 LONG lRet = ERROR_SUCCESS;
220 HKEY retKey = NULL;
221 WCHAR newKey[MAX_NEW_KEY_LEN - 4];
222 int keyNum;
223 HKEY hKey;
225 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_CREATE_SUB_KEY, &hKey);
226 if (lRet) {
227 error_code_messagebox(hwnd, IDS_CREATE_KEY_FAILED);
228 goto done;
231 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWKEY, newKey, ARRAY_SIZE(newKey))) goto done;
233 /* try to find a name for the key being created (maximum = 100 attempts) */
234 for (keyNum = 1; keyNum < 100; keyNum++) {
235 wsprintfW(keyName, newKey, keyNum);
236 lRet = RegOpenKeyW(hKey, keyName, &retKey);
237 if (lRet) break;
238 RegCloseKey(retKey);
240 if (lRet == ERROR_SUCCESS) goto done;
242 lRet = RegCreateKeyW(hKey, keyName, &retKey);
243 if (lRet) {
244 error_code_messagebox(hwnd, IDS_CREATE_KEY_FAILED);
245 goto done;
248 result = TRUE;
250 done:
251 RegCloseKey(retKey);
252 return result;
255 BOOL ModifyValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR valueName)
257 BOOL result = FALSE;
258 DWORD type;
259 LONG lRet;
260 HKEY hKey;
261 LONG len;
263 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
264 if (lRet) {
265 error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
266 return FALSE;
269 editValueName = valueName ? valueName : g_pszDefaultValueName;
270 if(!(stringValueData = read_value(hwnd, hKey, valueName, &type, &len))) goto done;
272 if ( (type == REG_SZ) || (type == REG_EXPAND_SZ) ) {
273 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_STRING), hwnd, modify_dlgproc) == IDOK) {
274 lRet = RegSetValueExW(hKey, valueName, 0, type, (LPBYTE)stringValueData, (lstrlenW(stringValueData) + 1) * sizeof(WCHAR));
275 if (lRet == ERROR_SUCCESS) result = TRUE;
276 else error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
278 } else if ( type == REG_DWORD ) {
279 DWORD value = *((DWORD*)stringValueData);
280 stringValueData = heap_xrealloc(stringValueData, 64);
281 wsprintfW(stringValueData, L"%x", value);
282 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_DWORD), hwnd, modify_dlgproc) == IDOK)
284 DWORD val;
285 if (swscanf(stringValueData, isDecimal ? L"%lu" : L"%lx", &val))
287 lRet = RegSetValueExW(hKey, valueName, 0, type, (BYTE*)&val, sizeof(val));
288 if (lRet == ERROR_SUCCESS) result = TRUE;
289 else error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
292 } else if ( type == REG_QWORD ) {
293 UINT64 value = *((UINT64 *)stringValueData);
294 stringValueData = heap_xrealloc(stringValueData, 64);
295 swprintf(stringValueData, 64, L"%I64x", value);
296 if (DialogBoxParamW(0, MAKEINTRESOURCEW(IDD_EDIT_DWORD), hwnd, modify_dlgproc, type) == IDOK)
298 if (swscanf(stringValueData, isDecimal ? L"%I64u" : L"%I64x", &value))
300 lRet = RegSetValueExW(hKey, valueName, 0, type, (BYTE *)&value, sizeof(value));
301 if (lRet == ERROR_SUCCESS) result = TRUE;
302 else error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
305 } else if ( type == REG_MULTI_SZ ) {
306 WCHAR char1 = '\r', char2 = '\n';
307 WCHAR *tmpValueData = NULL;
308 INT i, j, count;
310 for (i = 0, count = 0; i < len / sizeof(WCHAR); i++)
311 if ( !stringValueData[i] && stringValueData[i + 1] )
312 count++;
313 tmpValueData = heap_xalloc(len + (count * sizeof(WCHAR)));
315 for ( i = 0, j = 0; i < len / sizeof(WCHAR); i++)
317 if ( !stringValueData[i] && stringValueData[i + 1])
319 tmpValueData[j++] = char1;
320 tmpValueData[j++] = char2;
322 else
323 tmpValueData[j++] = stringValueData[i];
326 heap_free(stringValueData);
327 stringValueData = tmpValueData;
328 tmpValueData = NULL;
330 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_MULTI_STRING), hwnd, modify_dlgproc) == IDOK)
332 len = lstrlenW( stringValueData );
333 tmpValueData = heap_xalloc((len + 2) * sizeof(WCHAR));
335 for (i = 0, j = 0; i < len; i++)
337 if ( stringValueData[i] == char1 && stringValueData[i + 1] == char2)
339 if ( tmpValueData[j - 1] != 0)
340 tmpValueData[j++] = 0;
341 i++;
343 else
344 tmpValueData[j++] = stringValueData[i];
347 tmpValueData[j++] = 0;
348 tmpValueData[j++] = 0;
349 heap_free(stringValueData);
350 stringValueData = tmpValueData;
352 lRet = RegSetValueExW(hKey, valueName, 0, type, (LPBYTE)stringValueData, j * sizeof(WCHAR));
353 if (lRet == ERROR_SUCCESS) result = TRUE;
354 else error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
357 else /* hex data types */
359 struct edit_params params;
361 params.hkey = hKey;
362 params.value_name = valueName;
363 params.type = type;
364 params.data = stringValueData;
365 params.size = len;
366 result = DialogBoxParamW(NULL, MAKEINTRESOURCEW(IDD_EDIT_BINARY), hwnd,
367 bin_modify_dlgproc, (LPARAM)&params);
370 /* Update the listview item with the new data string */
371 if (result)
373 int index = SendMessageW(g_pChildWnd->hListWnd, LVM_GETNEXTITEM, -1,
374 MAKELPARAM(LVNI_FOCUSED | LVNI_SELECTED, 0));
375 heap_free(stringValueData);
376 stringValueData = read_value(hwnd, hKey, valueName, &type, &len);
377 format_value_data(g_pChildWnd->hListWnd, index, type, stringValueData, len);
380 done:
381 heap_free(stringValueData);
382 stringValueData = NULL;
383 RegCloseKey(hKey);
384 return result;
387 BOOL DeleteKey(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath)
389 BOOL result = FALSE;
390 LONG lRet;
391 HKEY hKey;
393 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ|KEY_SET_VALUE, &hKey);
394 if (lRet) {
395 error_code_messagebox(hwnd, IDS_DELETE_KEY_FAILED);
396 return FALSE;
399 if (messagebox(hwnd, MB_YESNO | MB_ICONEXCLAMATION, IDS_DELETE_KEY_TITLE,
400 IDS_DELETE_KEY_TEXT) != IDYES)
401 goto done;
403 lRet = SHDeleteKeyW(hKeyRoot, keyPath);
404 if (lRet) {
405 error_code_messagebox(hwnd, IDS_BAD_KEY, keyPath);
406 goto done;
408 result = TRUE;
410 done:
411 RegCloseKey(hKey);
412 return result;
415 BOOL DeleteValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR valueName)
417 BOOL result = FALSE;
418 LONG lRet;
419 HKEY hKey;
421 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
422 if (lRet) return FALSE;
424 lRet = RegDeleteValueW(hKey, valueName);
425 if (lRet && valueName) {
426 error_code_messagebox(hwnd, IDS_BAD_VALUE, valueName);
428 if (lRet) goto done;
429 result = TRUE;
431 done:
432 RegCloseKey(hKey);
433 return result;
436 BOOL CreateValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, DWORD valueType, LPWSTR valueName)
438 LONG lRet = ERROR_SUCCESS;
439 WCHAR newValue[256];
440 UINT64 value = 0;
441 DWORD size_bytes;
442 BOOL result = FALSE;
443 int valueNum, index;
444 HKEY hKey;
445 LVITEMW item;
447 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
448 if (lRet) {
449 error_code_messagebox(hwnd, IDS_CREATE_VALUE_FAILED);
450 return FALSE;
453 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWVALUE, newValue, ARRAY_SIZE(newValue))) goto done;
455 /* try to find a name for the value being created (maximum = 100 attempts) */
456 for (valueNum = 1; valueNum < 100; valueNum++) {
457 wsprintfW(valueName, newValue, valueNum);
458 lRet = RegQueryValueExW(hKey, valueName, 0, 0, 0, 0);
459 if (lRet == ERROR_FILE_NOT_FOUND) break;
461 if (lRet != ERROR_FILE_NOT_FOUND) {
462 error_code_messagebox(hwnd, IDS_CREATE_VALUE_FAILED);
463 goto done;
466 switch (valueType)
468 case REG_DWORD:
469 case REG_DWORD_BIG_ENDIAN:
470 size_bytes = sizeof(DWORD);
471 break;
472 case REG_QWORD:
473 size_bytes = sizeof(UINT64);
474 break;
475 case REG_BINARY:
476 size_bytes = 0;
477 break;
478 case REG_MULTI_SZ:
479 size_bytes = 2 * sizeof(WCHAR);
480 break;
481 default: /* REG_NONE, REG_SZ, REG_EXPAND_SZ */
482 size_bytes = sizeof(WCHAR);
485 lRet = RegSetValueExW(hKey, valueName, 0, valueType, (BYTE *)&value, size_bytes);
486 if (lRet) {
487 error_code_messagebox(hwnd, IDS_CREATE_VALUE_FAILED);
488 goto done;
491 /* Add the new item to the listview */
492 index = AddEntryToList(g_pChildWnd->hListWnd, valueName, valueType, (BYTE *)&value, size_bytes, -1);
493 item.state = LVIS_FOCUSED | LVIS_SELECTED;
494 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
495 SendMessageW(g_pChildWnd->hListWnd, LVM_SETITEMSTATE, index, (LPARAM)&item);
497 result = TRUE;
499 done:
500 RegCloseKey(hKey);
501 return result;
504 BOOL RenameValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR oldName, LPCWSTR newName)
506 LPWSTR value = NULL;
507 DWORD type;
508 LONG len, lRet;
509 BOOL result = FALSE;
510 HKEY hKey;
512 if (!oldName) return FALSE;
513 if (!newName) return FALSE;
515 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
516 if (lRet) {
517 error_code_messagebox(hwnd, IDS_RENAME_VALUE_FAILED);
518 return FALSE;
521 if (!RegQueryValueExW(hKey, newName, NULL, NULL, NULL, NULL)) {
522 error_code_messagebox(hwnd, IDS_VALUE_EXISTS, oldName);
523 goto done;
525 value = read_value(hwnd, hKey, oldName, &type, &len);
526 if(!value) goto done;
527 lRet = RegSetValueExW(hKey, newName, 0, type, (BYTE*)value, len);
528 if (lRet) {
529 error_code_messagebox(hwnd, IDS_RENAME_VALUE_FAILED);
530 goto done;
532 lRet = RegDeleteValueW(hKey, oldName);
533 if (lRet) {
534 RegDeleteValueW(hKey, newName);
535 error_code_messagebox(hwnd, IDS_RENAME_VALUE_FAILED);
536 goto done;
538 result = TRUE;
540 done:
541 heap_free(value);
542 RegCloseKey(hKey);
543 return result;
547 BOOL RenameKey(HWND hwnd, HKEY hRootKey, LPCWSTR keyPath, LPCWSTR newName)
549 LPWSTR parentPath = 0;
550 LPCWSTR srcSubKey = 0;
551 HKEY parentKey = 0;
552 HKEY destKey = 0;
553 BOOL result = FALSE;
554 LONG lRet;
555 DWORD disposition;
557 if (!keyPath || !newName) return FALSE;
559 if (!wcsrchr(keyPath, '\\')) {
560 parentKey = hRootKey;
561 srcSubKey = keyPath;
562 } else {
563 LPWSTR srcSubKey_copy;
565 parentPath = heap_xalloc((lstrlenW(keyPath) + 1) * sizeof(WCHAR));
566 lstrcpyW(parentPath, keyPath);
567 srcSubKey_copy = wcsrchr(parentPath, '\\');
568 *srcSubKey_copy = 0;
569 srcSubKey = srcSubKey_copy + 1;
570 lRet = RegOpenKeyExW(hRootKey, parentPath, 0, KEY_READ | KEY_CREATE_SUB_KEY, &parentKey);
571 if (lRet) {
572 error_code_messagebox(hwnd, IDS_RENAME_KEY_FAILED);
573 goto done;
577 /* The following fails if the old name is the same as the new name. */
578 if (!lstrcmpW(srcSubKey, newName)) goto done;
580 lRet = RegCreateKeyExW(parentKey, newName, 0, NULL, REG_OPTION_NON_VOLATILE,
581 KEY_WRITE, NULL /* FIXME */, &destKey, &disposition);
582 if (disposition == REG_OPENED_EXISTING_KEY)
583 lRet = ERROR_FILE_EXISTS;
584 if (lRet) {
585 error_code_messagebox(hwnd, IDS_KEY_EXISTS, srcSubKey);
586 goto done;
589 /* FIXME: SHCopyKey does not copy the security attributes */
590 lRet = SHCopyKeyW(parentKey, srcSubKey, destKey, 0);
591 if (lRet) {
592 RegCloseKey(destKey);
593 RegDeleteKeyW(parentKey, newName);
594 error_code_messagebox(hwnd, IDS_RENAME_KEY_FAILED);
595 goto done;
598 lRet = SHDeleteKeyW(hRootKey, keyPath);
599 if (lRet) {
600 error_code_messagebox(hwnd, IDS_RENAME_KEY_FAILED);
601 goto done;
604 result = TRUE;
606 done:
607 RegCloseKey(destKey);
608 if (parentKey) {
609 RegCloseKey(parentKey);
610 heap_free(parentPath);
612 return result;