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(HKEY hKey
)
160 LONG lRet
= ERROR_SUCCESS
;
163 TCHAR newKey
[COUNT_OF(keyName
) - 4];
166 /* If we have illegal parameter return with operation failure */
167 if (!hKey
) return FALSE
;
169 if (!LoadString(GetModuleHandle(0), IDS_NEWKEY
, newKey
, COUNT_OF(newKey
))) return FALSE
;
171 /* try to find out a name for the newly create key (max 100 times) */
172 for (keyNum
= 1; keyNum
< 100; keyNum
++) {
173 wsprintf(keyName
, newKey
, keyNum
);
174 lRet
= RegOpenKey(hKey
, keyName
, &retKey
);
175 if (lRet
!= ERROR_SUCCESS
) break;
178 if (lRet
== ERROR_SUCCESS
) return FALSE
;
180 lRet
= RegCreateKey(hKey
, keyName
, &retKey
);
181 if (lRet
!= ERROR_SUCCESS
) return FALSE
;
187 BOOL
ModifyValue(HWND hwnd
, HKEY hKey
, LPCTSTR valueName
)
193 if (!hKey
|| !valueName
) return FALSE
;
195 editValueName
= valueName
;
196 if(!(stringValueData
= read_value(hwnd
, hKey
, valueName
, &type
, 0))) goto done
;
198 if ( (type
== REG_SZ
) || (type
== REG_EXPAND_SZ
) ) {
199 if (DialogBox(0, MAKEINTRESOURCE(IDD_EDIT_STRING
), hwnd
, modify_dlgproc
) == IDOK
) {
200 lRet
= RegSetValueEx(hKey
, valueName
, 0, type
, stringValueData
, lstrlen(stringValueData
) + 1);
201 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
203 } else if ( type
== REG_DWORD
) {
204 wsprintf(stringValueData
, isDecimal
? "%ld" : "%lx", *((DWORD
*)stringValueData
));
205 if (DialogBox(0, MAKEINTRESOURCE(IDD_EDIT_DWORD
), hwnd
, modify_dlgproc
) == IDOK
) {
207 if (_stscanf(stringValueData
, isDecimal
? "%ld" : "%lx", &val
)) {
208 lRet
= RegSetValueEx(hKey
, valueName
, 0, type
, (BYTE
*)&val
, sizeof(val
));
209 if (lRet
== ERROR_SUCCESS
) result
= TRUE
;
213 error(hwnd
, IDS_UNSUPPORTED_TYPE
, type
);
217 HeapFree(GetProcessHeap(), 0, stringValueData
);
218 stringValueData
= NULL
;
223 BOOL
DeleteValue(HWND hwnd
, HKEY hKey
, LPCTSTR valueName
)
227 if (!hKey
|| !valueName
) return FALSE
;
229 if (messagebox(hwnd
, MB_YESNO
| MB_ICONEXCLAMATION
, IDS_DELETE_BOX_TITLE
, IDS_DELETE_BOX_TEXT
, valueName
) != IDYES
)
232 lRet
= RegDeleteValue(hKey
, valueName
);
233 if (lRet
!= ERROR_SUCCESS
) {
234 error(hwnd
, IDS_BAD_VALUE
, valueName
);
236 return lRet
== ERROR_SUCCESS
;
239 BOOL
CreateValue(HWND hwnd
, HKEY hKey
, DWORD valueType
)
241 LONG lRet
= ERROR_SUCCESS
;
243 TCHAR newValue
[COUNT_OF(valueName
) - 4];
244 DWORD valueDword
= 0;
247 /* If we have illegal parameter return with operation failure */
248 if (!hKey
) return FALSE
;
250 if (!LoadString(GetModuleHandle(0), IDS_NEWVALUE
, newValue
, COUNT_OF(newValue
))) return FALSE
;
252 /* try to find out a name for the newly create key (max 100 times) */
253 for (valueNum
= 1; valueNum
< 100; valueNum
++) {
254 wsprintf(valueName
, newValue
, valueNum
);
255 lRet
= RegQueryValueEx(hKey
, valueName
, 0, 0, 0, 0);
256 if (lRet
!= ERROR_SUCCESS
) break;
258 if (lRet
== ERROR_SUCCESS
) return FALSE
;
260 lRet
= RegSetValueEx(hKey
, valueName
, 0, valueType
, (BYTE
*)&valueDword
, sizeof(DWORD
));
261 if (lRet
!= ERROR_SUCCESS
) return FALSE
;
266 BOOL
RenameValue(HWND hwnd
, HKEY hRootKey
, LPCTSTR keyPath
, LPCTSTR oldName
, LPCTSTR newName
)
274 lRet
= RegOpenKeyEx(hRootKey
, keyPath
, 0, KEY_ALL_ACCESS
, &hKey
);
275 if (lRet
!= ERROR_SUCCESS
) goto done
;
276 value
= read_value(hwnd
, hKey
, oldName
, &type
, &len
);
277 if(!value
) goto done
;
278 lRet
= RegSetValueEx(hKey
, newName
, 0, type
, (BYTE
*)value
, len
);
279 if (lRet
!= ERROR_SUCCESS
) goto done
;
280 lRet
= RegDeleteValue(hKey
, oldName
);
281 if (lRet
!= ERROR_SUCCESS
) {
282 RegDeleteValue(hKey
, newName
);
288 HeapFree(GetProcessHeap(), 0, value
);