fixed changing of string values
[reactos.git] / reactos / subsys / system / regedit / edit.c
1 /*
2 * Registry editing UI functions.
3 *
4 * Copyright (C) 2003 Dimitrie O. Paun
5 *
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.
10 *
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.
15 *
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
22
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
32 #include "main.h"
33 #include "regproc.h"
34 #include "resource.h"
35
36 static const TCHAR* editValueName;
37 static TCHAR* stringValueData;
38
39 void error(HWND hwnd, INT resId, ...)
40 {
41 va_list ap;
42 TCHAR title[256];
43 TCHAR errfmt[1024];
44 TCHAR errstr[1024];
45 HINSTANCE hInstance;
46
47 hInstance = GetModuleHandle(0);
48
49 if (!LoadString(hInstance, IDS_ERROR, title, COUNT_OF(title)))
50 lstrcpy(title, "Error");
51
52 if (!LoadString(hInstance, resId, errfmt, COUNT_OF(errfmt)))
53 lstrcpy(errfmt, "Unknown error string!");
54
55 va_start(ap, resId);
56 _vsntprintf(errstr, COUNT_OF(errstr), errfmt, ap);
57 va_end(ap);
58
59 MessageBox(hwnd, errstr, title, MB_OK | MB_ICONERROR);
60 }
61
62 INT_PTR CALLBACK modify_string_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
63 {
64 TCHAR* valueData;
65 HWND hwndValue;
66 int len;
67
68 switch(uMsg) {
69 case WM_INITDIALOG:
70 if(editValueName && strcmp(editValueName, _T("")))
71 {
72 SetDlgItemText(hwndDlg, IDC_VALUE_NAME, editValueName);
73 }
74 else
75 {
76 SetDlgItemText(hwndDlg, IDC_VALUE_NAME, _T("(Default)"));
77 }
78 SetDlgItemText(hwndDlg, IDC_VALUE_DATA, stringValueData);
79 return TRUE;
80 case WM_COMMAND:
81 switch (LOWORD(wParam)) {
82 case IDOK:
83 if ((hwndValue = GetDlgItem(hwndDlg, IDC_VALUE_DATA))) {
84 if ((len = GetWindowTextLength(hwndValue))) {
85 if(stringValueData)
86 {
87 if ((valueData = HeapReAlloc(GetProcessHeap(), 0, stringValueData, (len + 1) * sizeof(TCHAR)))) {
88 stringValueData = valueData;
89 if (!GetWindowText(hwndValue, stringValueData, len + 1))
90 *stringValueData = 0;
91 }
92 }
93 else
94 {
95 if ((valueData = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(TCHAR)))) {
96 stringValueData = valueData;
97 if (!GetWindowText(hwndValue, stringValueData, len + 1))
98 *stringValueData = 0;
99 }
100 }
101 }
102 else
103 {
104 if(stringValueData)
105 *stringValueData = 0;
106 }
107 }
108 EndDialog(hwndDlg, IDOK);
109 break;
110 case IDCANCEL:
111 EndDialog(hwndDlg, IDCANCEL);
112 return TRUE;
113 }
114 }
115 return FALSE;
116 }
117
118 BOOL ModifyValue(HWND hwnd, HKEY hKey, LPCTSTR valueName)
119 {
120 DWORD valueDataLen;
121 DWORD type;
122 LONG lRet;
123 BOOL result = FALSE;
124
125 if (!hKey || !valueName) return FALSE;
126
127 editValueName = valueName;
128
129 lRet = RegQueryValueEx(hKey, valueName, 0, &type, 0, &valueDataLen);
130 if(lRet != ERROR_SUCCESS && (!strcmp(valueName, _T("")) || valueName == NULL))
131 {
132 lRet = ERROR_SUCCESS; /* Allow editing of (Default) values which don't exist */
133 type = REG_SZ;
134 valueDataLen = 0;
135 stringValueData = NULL;
136 }
137
138 if (lRet != ERROR_SUCCESS) {
139 error(hwnd, IDS_BAD_VALUE, valueName);
140 goto done;
141 }
142
143 if ( (type == REG_SZ) || (type == REG_EXPAND_SZ) ) {
144 if(valueDataLen > 0)
145 {
146 if (!(stringValueData = HeapAlloc(GetProcessHeap(), 0, valueDataLen))) {
147 error(hwnd, IDS_TOO_BIG_VALUE, valueDataLen);
148 goto done;
149 }
150 lRet = RegQueryValueEx(hKey, valueName, 0, 0, stringValueData, &valueDataLen);
151 if (lRet != ERROR_SUCCESS) {
152 error(hwnd, IDS_BAD_VALUE, valueName);
153 goto done;
154 }
155 }
156 else
157 {
158 stringValueData = NULL;
159 }
160 if (DialogBox(0, MAKEINTRESOURCE(IDD_EDIT_STRING), hwnd, modify_string_dlgproc) == IDOK) {
161 if(stringValueData)
162 lRet = RegSetValueEx(hKey, valueName, 0, type, stringValueData, lstrlen(stringValueData) + 1);
163 else
164 lRet = RegSetValueEx(hKey, valueName, 0, type, NULL, 0);
165 if (lRet == ERROR_SUCCESS) result = TRUE;
166 }
167 } else if ( type == REG_DWORD ) {
168 MessageBox(hwnd, "Can't edit dwords for now", "Error", MB_OK | MB_ICONERROR);
169 } else {
170 error(hwnd, IDS_UNSUPPORTED_TYPE, type);
171 }
172
173 done:
174 if(stringValueData)
175 HeapFree(GetProcessHeap(), 0, stringValueData);
176 stringValueData = NULL;
177
178 return result;
179 }