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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
36 static const TCHAR
* editValueName
;
37 static TCHAR
* stringValueData
;
38 static BOOL isDecimal
;
40 INT
vmessagebox(HWND hwnd
, INT buttons
, INT titleId
, INT resId
, va_list ap
)
46 if (!LoadString(hInst
, titleId
, title
, COUNT_OF(title
)))
47 lstrcpy(title
, "Error");
49 if (!LoadString(hInst
, resId
, errfmt
, COUNT_OF(errfmt
)))
50 lstrcpy(errfmt
, "Unknown error string!");
52 _vsntprintf(errstr
, COUNT_OF(errstr
), errfmt
, ap
);
54 return MessageBox(hwnd
, errstr
, title
, buttons
);
57 INT
messagebox(HWND hwnd
, INT buttons
, INT titleId
, INT resId
, ...)
63 result
= vmessagebox(hwnd
, buttons
, titleId
, resId
, ap
);
69 void error(HWND hwnd
, INT resId
, ...)
74 vmessagebox(hwnd
, MB_OK
| MB_ICONERROR
, IDS_ERROR
, resId
, ap
);
78 BOOL
change_dword_base(HWND hwndDlg
, BOOL toHex
)
83 if (!GetDlgItemText(hwndDlg
, IDC_VALUE_DATA
, buf
, COUNT_OF(buf
))) return FALSE
;
84 if (!_stscanf(buf
, toHex
? "%ld" : "%lx", &val
)) return FALSE
;
85 wsprintf(buf
, toHex
? "%lx" : "%ld", val
);
86 return SetDlgItemText(hwndDlg
, IDC_VALUE_DATA
, buf
);
89 INT_PTR CALLBACK
modify_dlgproc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
97 SetDlgItemText(hwndDlg
, IDC_VALUE_NAME
, editValueName
);
98 SetDlgItemText(hwndDlg
, IDC_VALUE_DATA
, stringValueData
);
99 CheckRadioButton(hwndDlg
, IDC_DWORD_HEX
, IDC_DWORD_DEC
, isDecimal
? IDC_DWORD_DEC
: IDC_DWORD_HEX
);
102 switch (LOWORD(wParam
)) {
104 if (isDecimal
&& change_dword_base(hwndDlg
, TRUE
)) isDecimal
= FALSE
;
107 if (!isDecimal
&& change_dword_base(hwndDlg
, FALSE
)) isDecimal
= TRUE
;
110 if ((hwndValue
= GetDlgItem(hwndDlg
, IDC_VALUE_DATA
))) {
111 if ((len
= GetWindowTextLength(hwndValue
))) {
112 if ((valueData
= HeapReAlloc(GetProcessHeap(), 0, stringValueData
, (len
+ 1) * sizeof(TCHAR
)))) {
113 stringValueData
= valueData
;
114 if (!GetWindowText(hwndValue
, stringValueData
, len
+ 1))
115 *stringValueData
= 0;
121 EndDialog(hwndDlg
, wParam
);
128 static LPTSTR
read_value(HWND hwnd
, HKEY hKey
, LPCTSTR valueName
, DWORD
*lpType
, LONG
*len
)
131 LPTSTR buffer
= NULL
;
134 lRet
= RegQueryValueEx(hKey
, valueName
, 0, lpType
, 0, &valueDataLen
);
135 if (lRet
!= ERROR_SUCCESS
) {
136 error(hwnd
, IDS_BAD_VALUE
, valueName
);
139 if ( *lpType
== REG_DWORD
) valueDataLen
= sizeof(DWORD
);
140 if (!(buffer
= HeapAlloc(GetProcessHeap(), 0, valueDataLen
))) {
141 error(hwnd
, IDS_TOO_BIG_VALUE
, valueDataLen
);
144 lRet
= RegQueryValueEx(hKey
, valueName
, 0, 0, buffer
, &valueDataLen
);
145 if (lRet
!= ERROR_SUCCESS
) {
146 error(hwnd
, IDS_BAD_VALUE
, valueName
);
150 if(len
) *len
= valueDataLen
;
154 HeapFree(GetProcessHeap(), 0, buffer
);
158 BOOL
CreateKey(HWND hwnd
, HKEY hKeyRoot
, LPCTSTR keyPath
)
161 LONG lRet
= ERROR_SUCCESS
;
164 TCHAR newKey
[COUNT_OF(keyName
) - 4];
168 lRet
= RegOpenKeyEx(hKeyRoot
, keyPath
, 0, KEY_CREATE_SUB_KEY
, &hKey
);
169 if (lRet
!= ERROR_SUCCESS
) return FALSE
;
171 if (!LoadString(GetModuleHandle(0), IDS_NEWKEY
, newKey
, COUNT_OF(newKey
))) goto done
;
173 /* try to find out a name for the newly create key (max 100 times) */
174 for (keyNum
= 1; keyNum
< 100; keyNum
++) {
175 wsprintf(keyName
, newKey
, keyNum
);
176 lRet
= RegOpenKey(hKey
, keyName
, &retKey
);
177 if (lRet
!= ERROR_SUCCESS
) break;
180 if (lRet
== ERROR_SUCCESS
) goto done
;
182 lRet
= RegCreateKey(hKey
, keyName
, &retKey
);
183 if (lRet
!= ERROR_SUCCESS
) goto done
;
191 BOOL
ModifyValue(HWND hwnd
, HKEY hKeyRoot
, LPCTSTR keyPath
, LPCTSTR valueName
)
198 lRet
= RegOpenKeyEx(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
199 if (lRet
!= ERROR_SUCCESS
) return FALSE
;
201 editValueName
= valueName
;
202 if(!(stringValueData
= read_value(hwnd
, hKey
, valueName
, &type
, 0))) goto done
;
204 if ( (type
== REG_SZ
) || (type
== REG_EXPAND_SZ
) ) {
205 if (DialogBox(0, MAKEINTRESOURCE(IDD_EDIT_STRING
), hwnd
, modify_dlgproc
) == IDOK
) {
206 lRet
= RegSetValueEx(hKey
, valueName
, 0, type
, stringValueData
, lstrlen(stringValueData
) + 1);
207 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
209 } else if ( type
== REG_DWORD
) {
210 wsprintf(stringValueData
, isDecimal
? "%ld" : "%lx", *((DWORD
*)stringValueData
));
211 if (DialogBox(0, MAKEINTRESOURCE(IDD_EDIT_DWORD
), hwnd
, modify_dlgproc
) == IDOK
) {
213 if (_stscanf(stringValueData
, isDecimal
? "%ld" : "%lx", &val
)) {
214 lRet
= RegSetValueEx(hKey
, valueName
, 0, type
, (BYTE
*)&val
, sizeof(val
));
215 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
219 error(hwnd
, IDS_UNSUPPORTED_TYPE
, type
);
223 HeapFree(GetProcessHeap(), 0, stringValueData
);
224 stringValueData
= NULL
;
229 BOOL
DeleteValue(HWND hwnd
, HKEY hKeyRoot
, LPCTSTR keyPath
, LPCTSTR valueName
)
235 lRet
= RegOpenKeyEx(hKeyRoot
, keyPath
, 0, KEY_SET_VALUE
, &hKey
);
236 if (lRet
!= ERROR_SUCCESS
) return FALSE
;
238 if (messagebox(hwnd
, MB_YESNO
| MB_ICONEXCLAMATION
, IDS_DELETE_BOX_TITLE
, IDS_DELETE_BOX_TEXT
, valueName
) != IDYES
)
241 lRet
= RegDeleteValue(hKey
, valueName
);
242 if (lRet
!= ERROR_SUCCESS
) {
243 error(hwnd
, IDS_BAD_VALUE
, valueName
);
245 if (lRet
!= ERROR_SUCCESS
) goto done
;
253 BOOL
CreateValue(HWND hwnd
, HKEY hKeyRoot
, LPCTSTR keyPath
, DWORD valueType
)
255 LONG lRet
= ERROR_SUCCESS
;
257 TCHAR newValue
[COUNT_OF(valueName
) - 4];
258 DWORD valueDword
= 0;
263 lRet
= RegOpenKeyEx(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
264 if (lRet
!= ERROR_SUCCESS
) return FALSE
;
266 if (!LoadString(GetModuleHandle(0), IDS_NEWVALUE
, newValue
, COUNT_OF(newValue
))) goto done
;
268 /* try to find out a name for the newly create key (max 100 times) */
269 for (valueNum
= 1; valueNum
< 100; valueNum
++) {
270 wsprintf(valueName
, newValue
, valueNum
);
271 lRet
= RegQueryValueEx(hKey
, valueName
, 0, 0, 0, 0);
272 if (lRet
!= ERROR_SUCCESS
) break;
274 if (lRet
== ERROR_SUCCESS
) goto done
;
276 lRet
= RegSetValueEx(hKey
, valueName
, 0, valueType
, (BYTE
*)&valueDword
, sizeof(DWORD
));
277 if (lRet
!= ERROR_SUCCESS
) goto done
;
285 BOOL
RenameValue(HWND hwnd
, HKEY hKeyRoot
, LPCTSTR keyPath
, LPCTSTR oldName
, LPCTSTR newName
)
293 lRet
= RegOpenKeyEx(hKeyRoot
, keyPath
, 0, KEY_READ
| KEY_SET_VALUE
, &hKey
);
294 if (lRet
!= ERROR_SUCCESS
) return FALSE
;
295 value
= read_value(hwnd
, hKey
, oldName
, &type
, &len
);
296 if(!value
) goto done
;
297 lRet
= RegSetValueEx(hKey
, newName
, 0, type
, (BYTE
*)value
, len
);
298 if (lRet
!= ERROR_SUCCESS
) goto done
;
299 lRet
= RegDeleteValue(hKey
, oldName
);
300 if (lRet
!= ERROR_SUCCESS
) {
301 RegDeleteValue(hKey
, newName
);
307 HeapFree(GetProcessHeap(), 0, value
);