dpnet/tests: Add tests to show IDirectPlay8ThreadPool is a singleton object.
[wine.git] / programs / regedit / edit.c
blobc6a6629c2bd6933a6e0aa71a13feda9eac8eaba4
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 <stdio.h>
29 #include <shellapi.h>
30 #include <shlwapi.h>
32 #include "wine/unicode.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, __ms_va_list va_args)
51 WCHAR title[256];
52 WCHAR fmt[1024];
53 WCHAR *str;
54 int ret;
56 LoadStringW(hInst, titleId, title, COUNT_OF(title));
57 LoadStringW(hInst, resId, fmt, COUNT_OF(fmt));
59 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
60 fmt, 0, 0, (WCHAR *)&str, 0, &va_args);
61 ret = MessageBoxW(hwnd, str, title, buttons);
62 LocalFree(str);
64 return ret;
67 int __cdecl messagebox(HWND hwnd, int buttons, int titleId, int resId, ...)
69 __ms_va_list ap;
70 INT result;
72 __ms_va_start(ap, resId);
73 result = vmessagebox(hwnd, buttons, titleId, resId, ap);
74 __ms_va_end(ap);
76 return result;
79 static void __cdecl error_code_messagebox(HWND hwnd, unsigned int msg_id, ...)
81 __ms_va_list ap;
83 __ms_va_start(ap, msg_id);
84 vmessagebox(hwnd, MB_OK|MB_ICONERROR, IDS_ERROR, msg_id, ap);
85 __ms_va_end(ap);
88 static BOOL change_dword_base(HWND hwndDlg, BOOL toHex)
90 static const WCHAR percent_u[] = {'%','u',0};
91 static const WCHAR percent_x[] = {'%','x',0};
93 WCHAR buf[128];
94 DWORD val;
96 if (!GetDlgItemTextW(hwndDlg, IDC_VALUE_DATA, buf, COUNT_OF(buf))) return FALSE;
97 if (!swscanf(buf, toHex ? percent_u : percent_x, &val)) return FALSE;
98 wsprintfW(buf, toHex ? percent_x : percent_u, val);
99 return SetDlgItemTextW(hwndDlg, IDC_VALUE_DATA, buf);
102 static INT_PTR CALLBACK modify_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
104 WCHAR* valueData;
105 HWND hwndValue;
106 int len;
108 switch(uMsg) {
109 case WM_INITDIALOG:
110 SetDlgItemTextW(hwndDlg, IDC_VALUE_NAME, editValueName);
111 SetDlgItemTextW(hwndDlg, IDC_VALUE_DATA, stringValueData);
112 CheckRadioButton(hwndDlg, IDC_DWORD_HEX, IDC_DWORD_DEC, isDecimal ? IDC_DWORD_DEC : IDC_DWORD_HEX);
113 return TRUE;
114 case WM_COMMAND:
115 switch (LOWORD(wParam)) {
116 case IDC_DWORD_HEX:
117 if (isDecimal && change_dword_base(hwndDlg, TRUE)) isDecimal = FALSE;
118 break;
119 case IDC_DWORD_DEC:
120 if (!isDecimal && change_dword_base(hwndDlg, FALSE)) isDecimal = TRUE;
121 break;
122 case IDOK:
123 if ((hwndValue = GetDlgItem(hwndDlg, IDC_VALUE_DATA))) {
124 len = GetWindowTextLengthW(hwndValue);
125 if ((valueData = HeapReAlloc(GetProcessHeap(), 0, stringValueData, (len + 1) * sizeof(WCHAR)))) {
126 stringValueData = valueData;
127 if (!GetWindowTextW(hwndValue, stringValueData, len + 1))
128 *stringValueData = 0;
131 /* Fall through */
132 case IDCANCEL:
133 EndDialog(hwndDlg, wParam);
134 return TRUE;
137 return FALSE;
140 static INT_PTR CALLBACK bin_modify_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
142 struct edit_params *params;
143 LPBYTE pData;
144 LONG cbData;
145 LONG lRet;
147 switch(uMsg) {
148 case WM_INITDIALOG:
149 params = (struct edit_params *)lParam;
150 SetWindowLongPtrW(hwndDlg, DWLP_USER, (ULONG_PTR)params);
151 if (params->lpszValueName)
152 SetDlgItemTextW(hwndDlg, IDC_VALUE_NAME, params->lpszValueName);
153 else
154 SetDlgItemTextW(hwndDlg, IDC_VALUE_NAME, g_pszDefaultValueName);
155 SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, HEM_SETDATA, (WPARAM)params->cbData, (LPARAM)params->pData);
156 return TRUE;
157 case WM_COMMAND:
158 switch (LOWORD(wParam)) {
159 case IDOK:
160 params = (struct edit_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
161 cbData = SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, HEM_GETDATA, 0, 0);
162 pData = HeapAlloc(GetProcessHeap(), 0, cbData);
164 if (pData)
166 SendDlgItemMessageW(hwndDlg, IDC_VALUE_DATA, HEM_GETDATA, (WPARAM)cbData, (LPARAM)pData);
167 lRet = RegSetValueExW(params->hKey, params->lpszValueName, 0, REG_BINARY, pData, cbData);
169 else
170 lRet = ERROR_OUTOFMEMORY;
172 if (lRet == ERROR_SUCCESS)
173 EndDialog(hwndDlg, 1);
174 else
176 error_code_messagebox(hwndDlg, IDS_SET_VALUE_FAILED);
177 EndDialog(hwndDlg, 0);
179 return TRUE;
180 case IDCANCEL:
181 EndDialog(hwndDlg, 0);
182 return TRUE;
185 return FALSE;
188 static BOOL check_value(HWND hwnd, HKEY hKey, LPCWSTR valueName)
190 WCHAR empty = 0;
191 LONG lRet = RegQueryValueExW(hKey, valueName ? valueName : &empty, 0, NULL, 0, NULL);
192 if(lRet != ERROR_SUCCESS) return FALSE;
193 return TRUE;
196 static LPWSTR read_value(HWND hwnd, HKEY hKey, LPCWSTR valueName, DWORD *lpType, LONG *len)
198 DWORD valueDataLen;
199 LPWSTR buffer = NULL;
200 LONG lRet;
201 WCHAR empty = 0;
203 lRet = RegQueryValueExW(hKey, valueName ? valueName : &empty, 0, lpType, 0, &valueDataLen);
204 if (lRet != ERROR_SUCCESS) {
205 if (lRet == ERROR_FILE_NOT_FOUND && !valueName) { /* no default value here, make it up */
206 if (len) *len = 1;
207 if (lpType) *lpType = REG_SZ;
208 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR));
209 *buffer = '\0';
210 return buffer;
212 error_code_messagebox(hwnd, IDS_BAD_VALUE, valueName);
213 goto done;
215 if ( *lpType == REG_DWORD ) valueDataLen = sizeof(DWORD);
216 if (!(buffer = HeapAlloc(GetProcessHeap(), 0, valueDataLen+sizeof(WCHAR)))) {
217 error_code_messagebox(hwnd, IDS_TOO_BIG_VALUE, valueDataLen);
218 goto done;
220 lRet = RegQueryValueExW(hKey, valueName, 0, 0, (LPBYTE)buffer, &valueDataLen);
221 if (lRet != ERROR_SUCCESS) {
222 error_code_messagebox(hwnd, IDS_BAD_VALUE, valueName);
223 goto done;
225 if((valueDataLen % sizeof(WCHAR)) == 0)
226 buffer[valueDataLen / sizeof(WCHAR)] = 0;
227 if(len) *len = valueDataLen;
228 return buffer;
230 done:
231 HeapFree(GetProcessHeap(), 0, buffer);
232 return NULL;
235 BOOL CreateKey(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPWSTR keyName)
237 BOOL result = FALSE;
238 LONG lRet = ERROR_SUCCESS;
239 HKEY retKey = NULL;
240 WCHAR newKey[MAX_NEW_KEY_LEN - 4];
241 int keyNum;
242 HKEY hKey;
244 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_CREATE_SUB_KEY, &hKey);
245 if (lRet != ERROR_SUCCESS) {
246 error_code_messagebox(hwnd, IDS_CREATE_KEY_FAILED);
247 goto done;
250 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWKEY, newKey, COUNT_OF(newKey))) goto done;
252 /* try to find a name for the key being created (maximum = 100 attempts) */
253 for (keyNum = 1; keyNum < 100; keyNum++) {
254 wsprintfW(keyName, newKey, keyNum);
255 lRet = RegOpenKeyW(hKey, keyName, &retKey);
256 if (lRet != ERROR_SUCCESS) break;
257 RegCloseKey(retKey);
259 if (lRet == ERROR_SUCCESS) goto done;
261 lRet = RegCreateKeyW(hKey, keyName, &retKey);
262 if (lRet != ERROR_SUCCESS) {
263 error_code_messagebox(hwnd, IDS_CREATE_KEY_FAILED);
264 goto done;
267 result = TRUE;
269 done:
270 RegCloseKey(retKey);
271 return result;
274 BOOL ModifyValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR valueName)
276 BOOL result = FALSE;
277 DWORD type;
278 LONG lRet;
279 HKEY hKey;
280 LONG len;
282 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
283 if (lRet != ERROR_SUCCESS) {
284 error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
285 return FALSE;
288 editValueName = valueName ? valueName : g_pszDefaultValueName;
289 if(!(stringValueData = read_value(hwnd, hKey, valueName, &type, &len))) goto done;
291 if ( (type == REG_SZ) || (type == REG_EXPAND_SZ) ) {
292 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_STRING), hwnd, modify_dlgproc) == IDOK) {
293 lRet = RegSetValueExW(hKey, valueName, 0, type, (LPBYTE)stringValueData, (lstrlenW(stringValueData) + 1) * sizeof(WCHAR));
294 if (lRet == ERROR_SUCCESS) result = TRUE;
295 else error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
297 } else if ( type == REG_DWORD ) {
298 const WCHAR u[] = {'%','u',0};
299 const WCHAR x[] = {'%','x',0};
300 wsprintfW(stringValueData, isDecimal ? u : x, *((DWORD*)stringValueData));
301 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_DWORD), hwnd, modify_dlgproc) == IDOK) {
302 DWORD val;
303 CHAR* valueA = GetMultiByteString(stringValueData);
304 if (sscanf(valueA, isDecimal ? "%u" : "%x", &val)) {
305 lRet = RegSetValueExW(hKey, valueName, 0, type, (BYTE*)&val, sizeof(val));
306 if (lRet == ERROR_SUCCESS) result = TRUE;
307 else error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
309 HeapFree(GetProcessHeap(), 0, valueA);
311 } else if ( type == REG_BINARY ) {
312 struct edit_params params;
313 params.hKey = hKey;
314 params.lpszValueName = valueName;
315 params.pData = stringValueData;
316 params.cbData = len;
317 result = DialogBoxParamW(NULL, MAKEINTRESOURCEW(IDD_EDIT_BINARY), hwnd,
318 bin_modify_dlgproc, (LPARAM)&params);
319 } else if ( type == REG_MULTI_SZ ) {
320 WCHAR char1 = '\r', char2 = '\n';
321 WCHAR *tmpValueData = NULL;
322 INT i, j, count;
324 for ( i = 0, count = 0; i < len - 1; i++)
325 if ( !stringValueData[i] && stringValueData[i + 1] )
326 count++;
327 tmpValueData = HeapAlloc( GetProcessHeap(), 0, ( len + count ) * sizeof(WCHAR));
328 if ( !tmpValueData ) goto done;
330 for ( i = 0, j = 0; i < len - 1; i++)
332 if ( !stringValueData[i] && stringValueData[i + 1])
334 tmpValueData[j++] = char1;
335 tmpValueData[j++] = char2;
337 else
338 tmpValueData[j++] = stringValueData[i];
340 tmpValueData[j] = stringValueData[i];
341 HeapFree( GetProcessHeap(), 0, stringValueData);
342 stringValueData = tmpValueData;
343 tmpValueData = NULL;
345 if (DialogBoxW(0, MAKEINTRESOURCEW(IDD_EDIT_MULTI_STRING), hwnd, modify_dlgproc) == IDOK)
347 len = lstrlenW( stringValueData );
348 tmpValueData = HeapAlloc( GetProcessHeap(), 0, (len + 2) * sizeof(WCHAR));
349 if ( !tmpValueData ) goto done;
351 for ( i = 0, j = 0; i < len - 1; i++)
353 if ( stringValueData[i] == char1 && stringValueData[i + 1] == char2)
355 if ( tmpValueData[j - 1] != 0)
356 tmpValueData[j++] = 0;
357 i++;
359 else
360 tmpValueData[j++] = stringValueData[i];
362 tmpValueData[j++] = stringValueData[i];
363 tmpValueData[j++] = 0;
364 tmpValueData[j++] = 0;
365 HeapFree( GetProcessHeap(), 0, stringValueData);
366 stringValueData = tmpValueData;
368 lRet = RegSetValueExW(hKey, valueName, 0, type, (LPBYTE)stringValueData, j * sizeof(WCHAR));
369 if (lRet == ERROR_SUCCESS) result = TRUE;
370 else error_code_messagebox(hwnd, IDS_SET_VALUE_FAILED);
372 } else {
373 error_code_messagebox(hwnd, IDS_UNSUPPORTED_TYPE, type);
376 done:
377 HeapFree(GetProcessHeap(), 0, stringValueData);
378 stringValueData = NULL;
379 RegCloseKey(hKey);
380 return result;
383 BOOL DeleteKey(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath)
385 BOOL result = FALSE;
386 LONG lRet;
387 HKEY hKey;
389 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ|KEY_SET_VALUE, &hKey);
390 if (lRet != ERROR_SUCCESS) {
391 error_code_messagebox(hwnd, IDS_DELETE_KEY_FAILED);
392 return FALSE;
395 if (messagebox(hwnd, MB_YESNO | MB_ICONEXCLAMATION, IDS_DELETE_BOX_TITLE, IDS_DELETE_BOX_TEXT, keyPath) != IDYES)
396 goto done;
398 lRet = SHDeleteKeyW(hKeyRoot, keyPath);
399 if (lRet != ERROR_SUCCESS) {
400 error_code_messagebox(hwnd, IDS_BAD_KEY, keyPath);
401 goto done;
403 result = TRUE;
405 done:
406 RegCloseKey(hKey);
407 return result;
410 BOOL DeleteValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR valueName, BOOL showMessageBox)
412 BOOL result = FALSE;
413 LONG lRet;
414 HKEY hKey;
415 LPCWSTR visibleValueName = valueName ? valueName : g_pszDefaultValueName;
416 WCHAR empty = 0;
418 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
419 if (lRet != ERROR_SUCCESS) return FALSE;
421 if (showMessageBox)
423 if (messagebox(hwnd, MB_YESNO | MB_ICONEXCLAMATION, IDS_DELETE_BOX_TITLE, IDS_DELETE_BOX_TEXT, visibleValueName) != IDYES)
424 goto done;
427 lRet = RegDeleteValueW(hKey, valueName ? valueName : &empty);
428 if (lRet != ERROR_SUCCESS && valueName) {
429 error_code_messagebox(hwnd, IDS_BAD_VALUE, valueName);
431 if (lRet != ERROR_SUCCESS) goto done;
432 result = TRUE;
434 done:
435 RegCloseKey(hKey);
436 return result;
439 BOOL CreateValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, DWORD valueType, LPWSTR valueName)
441 LONG lRet = ERROR_SUCCESS;
442 WCHAR newValue[256];
443 DWORD valueDword = 0;
444 BOOL result = FALSE;
445 int valueNum;
446 HKEY hKey;
448 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
449 if (lRet != ERROR_SUCCESS) {
450 error_code_messagebox(hwnd, IDS_CREATE_VALUE_FAILED);
451 return FALSE;
454 if (!LoadStringW(GetModuleHandleW(0), IDS_NEWVALUE, newValue, COUNT_OF(newValue))) goto done;
456 /* try to find a name for the value being created (maximum = 100 attempts) */
457 for (valueNum = 1; valueNum < 100; valueNum++) {
458 wsprintfW(valueName, newValue, valueNum);
459 lRet = RegQueryValueExW(hKey, valueName, 0, 0, 0, 0);
460 if (lRet == ERROR_FILE_NOT_FOUND) break;
462 if (lRet != ERROR_FILE_NOT_FOUND) {
463 error_code_messagebox(hwnd, IDS_CREATE_VALUE_FAILED);
464 goto done;
467 lRet = RegSetValueExW(hKey, valueName, 0, valueType, (BYTE*)&valueDword, sizeof(DWORD));
468 if (lRet != ERROR_SUCCESS) {
469 error_code_messagebox(hwnd, IDS_CREATE_VALUE_FAILED);
470 goto done;
472 result = TRUE;
474 done:
475 RegCloseKey(hKey);
476 return result;
479 BOOL RenameValue(HWND hwnd, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR oldName, LPCWSTR newName)
481 LPWSTR value = NULL;
482 DWORD type;
483 LONG len, lRet;
484 BOOL result = FALSE;
485 HKEY hKey;
487 if (!oldName) return FALSE;
488 if (!newName) return FALSE;
490 lRet = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
491 if (lRet != ERROR_SUCCESS) {
492 error_code_messagebox(hwnd, IDS_RENAME_VALUE_FAILED);
493 return FALSE;
495 /* check if the value already exists */
496 if (check_value(hwnd, hKey, newName)) {
497 error_code_messagebox(hwnd, IDS_VALUE_EXISTS, oldName);
498 goto done;
500 value = read_value(hwnd, hKey, oldName, &type, &len);
501 if(!value) goto done;
502 lRet = RegSetValueExW(hKey, newName, 0, type, (BYTE*)value, len);
503 if (lRet != ERROR_SUCCESS) {
504 error_code_messagebox(hwnd, IDS_RENAME_VALUE_FAILED);
505 goto done;
507 lRet = RegDeleteValueW(hKey, oldName);
508 if (lRet != ERROR_SUCCESS) {
509 RegDeleteValueW(hKey, newName);
510 error_code_messagebox(hwnd, IDS_RENAME_VALUE_FAILED);
511 goto done;
513 result = TRUE;
515 done:
516 HeapFree(GetProcessHeap(), 0, value);
517 RegCloseKey(hKey);
518 return result;
522 BOOL RenameKey(HWND hwnd, HKEY hRootKey, LPCWSTR keyPath, LPCWSTR newName)
524 LPWSTR parentPath = 0;
525 LPCWSTR srcSubKey = 0;
526 HKEY parentKey = 0;
527 HKEY destKey = 0;
528 BOOL result = FALSE;
529 LONG lRet;
530 DWORD disposition;
532 if (!keyPath || !newName) return FALSE;
534 if (!strrchrW(keyPath, '\\')) {
535 parentKey = hRootKey;
536 srcSubKey = keyPath;
537 } else {
538 LPWSTR srcSubKey_copy;
540 parentPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(keyPath)+1)*sizeof(WCHAR));
541 lstrcpyW(parentPath, keyPath);
542 srcSubKey_copy = strrchrW(parentPath, '\\');
543 *srcSubKey_copy = 0;
544 srcSubKey = srcSubKey_copy + 1;
545 lRet = RegOpenKeyExW(hRootKey, parentPath, 0, KEY_READ | KEY_CREATE_SUB_KEY, &parentKey);
546 if (lRet != ERROR_SUCCESS) {
547 error_code_messagebox(hwnd, IDS_RENAME_KEY_FAILED);
548 goto done;
552 /* The following fails if the old name is the same as the new name. */
553 if (!lstrcmpW(srcSubKey, newName)) goto done;
555 lRet = RegCreateKeyExW(parentKey, newName, 0, NULL, REG_OPTION_NON_VOLATILE,
556 KEY_WRITE, NULL /* FIXME */, &destKey, &disposition);
557 if (disposition == REG_OPENED_EXISTING_KEY)
558 lRet = ERROR_FILE_EXISTS;
559 if (lRet != ERROR_SUCCESS) {
560 error_code_messagebox(hwnd, IDS_KEY_EXISTS, srcSubKey);
561 goto done;
564 /* FIXME: SHCopyKey does not copy the security attributes */
565 lRet = SHCopyKeyW(parentKey, srcSubKey, destKey, 0);
566 if (lRet != ERROR_SUCCESS) {
567 RegCloseKey(destKey);
568 RegDeleteKeyW(parentKey, newName);
569 error_code_messagebox(hwnd, IDS_RENAME_KEY_FAILED);
570 goto done;
573 lRet = SHDeleteKeyW(hRootKey, keyPath);
574 if (lRet != ERROR_SUCCESS) {
575 error_code_messagebox(hwnd, IDS_RENAME_KEY_FAILED);
576 goto done;
579 result = TRUE;
581 done:
582 RegCloseKey(destKey);
583 if (parentKey) {
584 RegCloseKey(parentKey);
585 HeapFree(GetProcessHeap(), 0, parentPath);
587 return result;