2003-12-03 21:25:24 +01:00
|
|
|
/*
|
|
|
|
* Registry editing UI functions.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003 Dimitrie O. Paun
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2003-12-03 21:25:24 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <tchar.h>
|
|
|
|
#include <commctrl.h>
|
|
|
|
#include <commdlg.h>
|
|
|
|
#include <cderr.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <shellapi.h>
|
2004-03-12 20:44:47 +01:00
|
|
|
#include <shlwapi.h>
|
2003-12-03 21:25:24 +01:00
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
#include "regproc.h"
|
|
|
|
#include "resource.h"
|
|
|
|
|
2003-12-12 05:08:59 +01:00
|
|
|
static const TCHAR* editValueName;
|
2003-12-03 21:25:24 +01:00
|
|
|
static TCHAR* stringValueData;
|
2004-01-05 22:14:19 +01:00
|
|
|
static BOOL isDecimal;
|
2003-12-03 21:25:24 +01:00
|
|
|
|
2005-04-14 13:30:31 +02:00
|
|
|
struct edit_params
|
|
|
|
{
|
|
|
|
HKEY hKey;
|
|
|
|
LPCTSTR lpszValueName;
|
|
|
|
void *pData;
|
|
|
|
LONG cbData;
|
|
|
|
};
|
|
|
|
|
2005-06-04 12:01:25 +02:00
|
|
|
static INT vmessagebox(HWND hwnd, INT buttons, INT titleId, INT resId, va_list ap)
|
2003-12-03 21:25:24 +01:00
|
|
|
{
|
|
|
|
TCHAR title[256];
|
|
|
|
TCHAR errfmt[1024];
|
|
|
|
TCHAR errstr[1024];
|
|
|
|
|
2004-01-05 22:14:19 +01:00
|
|
|
if (!LoadString(hInst, titleId, title, COUNT_OF(title)))
|
2003-12-08 23:48:07 +01:00
|
|
|
lstrcpy(title, "Error");
|
2003-12-03 21:25:24 +01:00
|
|
|
|
2004-01-05 22:14:19 +01:00
|
|
|
if (!LoadString(hInst, resId, errfmt, COUNT_OF(errfmt)))
|
2003-12-08 23:48:07 +01:00
|
|
|
lstrcpy(errfmt, "Unknown error string!");
|
2003-12-03 21:25:24 +01:00
|
|
|
|
|
|
|
_vsntprintf(errstr, COUNT_OF(errstr), errfmt, ap);
|
2004-01-05 22:14:19 +01:00
|
|
|
|
|
|
|
return MessageBox(hwnd, errstr, title, buttons);
|
|
|
|
}
|
|
|
|
|
2005-06-04 12:01:25 +02:00
|
|
|
static INT messagebox(HWND hwnd, INT buttons, INT titleId, INT resId, ...)
|
2004-01-05 22:14:19 +01:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
INT result;
|
|
|
|
|
|
|
|
va_start(ap, resId);
|
|
|
|
result = vmessagebox(hwnd, buttons, titleId, resId, ap);
|
2003-12-03 21:25:24 +01:00
|
|
|
va_end(ap);
|
|
|
|
|
2004-01-05 22:14:19 +01:00
|
|
|
return result;
|
2003-12-03 21:25:24 +01:00
|
|
|
}
|
|
|
|
|
2005-10-26 14:07:55 +02:00
|
|
|
void error(HWND hwnd, INT resId, ...)
|
2004-01-05 22:14:19 +01:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, resId);
|
|
|
|
vmessagebox(hwnd, MB_OK | MB_ICONERROR, IDS_ERROR, resId, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2005-03-21 11:29:09 +01:00
|
|
|
static void error_code_messagebox(HWND hwnd, DWORD error_code)
|
|
|
|
{
|
|
|
|
LPTSTR lpMsgBuf;
|
|
|
|
DWORD status;
|
|
|
|
TCHAR title[256];
|
2006-10-05 23:01:55 +02:00
|
|
|
static TCHAR fallback[] = TEXT("Error displaying error message.\n");
|
2005-03-21 11:29:09 +01:00
|
|
|
if (!LoadString(hInst, IDS_ERROR, title, COUNT_OF(title)))
|
|
|
|
lstrcpy(title, TEXT("Error"));
|
|
|
|
status = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
|
|
|
|
NULL, error_code, 0, (LPTSTR)&lpMsgBuf, 0, NULL);
|
|
|
|
if (!status)
|
2006-10-05 23:01:55 +02:00
|
|
|
lpMsgBuf = fallback;
|
2005-03-21 11:29:09 +01:00
|
|
|
MessageBox(hwnd, lpMsgBuf, title, MB_OK | MB_ICONERROR);
|
|
|
|
if (lpMsgBuf != fallback)
|
|
|
|
LocalFree(lpMsgBuf);
|
|
|
|
}
|
|
|
|
|
2005-06-04 12:01:25 +02:00
|
|
|
static BOOL change_dword_base(HWND hwndDlg, BOOL toHex)
|
2004-01-05 22:14:19 +01:00
|
|
|
{
|
|
|
|
TCHAR buf[128];
|
|
|
|
DWORD val;
|
|
|
|
|
|
|
|
if (!GetDlgItemText(hwndDlg, IDC_VALUE_DATA, buf, COUNT_OF(buf))) return FALSE;
|
2006-10-09 18:38:30 +02:00
|
|
|
if (!_stscanf(buf, toHex ? "%d" : "%x", &val)) return FALSE;
|
2004-01-05 22:14:19 +01:00
|
|
|
wsprintf(buf, toHex ? "%lx" : "%ld", val);
|
|
|
|
return SetDlgItemText(hwndDlg, IDC_VALUE_DATA, buf);
|
|
|
|
}
|
|
|
|
|
2005-06-04 12:01:25 +02:00
|
|
|
static INT_PTR CALLBACK modify_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
2003-12-03 21:25:24 +01:00
|
|
|
{
|
|
|
|
TCHAR* valueData;
|
|
|
|
HWND hwndValue;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
switch(uMsg) {
|
2003-12-08 23:48:07 +01:00
|
|
|
case WM_INITDIALOG:
|
|
|
|
SetDlgItemText(hwndDlg, IDC_VALUE_NAME, editValueName);
|
|
|
|
SetDlgItemText(hwndDlg, IDC_VALUE_DATA, stringValueData);
|
2004-01-05 22:14:19 +01:00
|
|
|
CheckRadioButton(hwndDlg, IDC_DWORD_HEX, IDC_DWORD_DEC, isDecimal ? IDC_DWORD_DEC : IDC_DWORD_HEX);
|
2003-12-08 23:48:07 +01:00
|
|
|
return TRUE;
|
|
|
|
case WM_COMMAND:
|
|
|
|
switch (LOWORD(wParam)) {
|
2004-01-05 22:14:19 +01:00
|
|
|
case IDC_DWORD_HEX:
|
|
|
|
if (isDecimal && change_dword_base(hwndDlg, TRUE)) isDecimal = FALSE;
|
|
|
|
break;
|
|
|
|
case IDC_DWORD_DEC:
|
|
|
|
if (!isDecimal && change_dword_base(hwndDlg, FALSE)) isDecimal = TRUE;
|
|
|
|
break;
|
2003-12-08 23:48:07 +01:00
|
|
|
case IDOK:
|
|
|
|
if ((hwndValue = GetDlgItem(hwndDlg, IDC_VALUE_DATA))) {
|
2004-05-04 04:55:28 +02:00
|
|
|
len = GetWindowTextLength(hwndValue);
|
|
|
|
if ((valueData = HeapReAlloc(GetProcessHeap(), 0, stringValueData, (len + 1) * sizeof(TCHAR)))) {
|
|
|
|
stringValueData = valueData;
|
|
|
|
if (!GetWindowText(hwndValue, stringValueData, len + 1))
|
|
|
|
*stringValueData = 0;
|
2003-12-08 23:48:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Fall through */
|
|
|
|
case IDCANCEL:
|
|
|
|
EndDialog(hwndDlg, wParam);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2003-12-03 21:25:24 +01:00
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2005-04-14 13:30:31 +02:00
|
|
|
static INT_PTR CALLBACK bin_modify_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
|
|
|
struct edit_params *params;
|
|
|
|
LPBYTE pData;
|
|
|
|
LONG cbData;
|
|
|
|
LONG lRet;
|
|
|
|
|
|
|
|
switch(uMsg) {
|
|
|
|
case WM_INITDIALOG:
|
|
|
|
params = (struct edit_params *)lParam;
|
|
|
|
SetWindowLongPtr(hwndDlg, DWLP_USER, (ULONG_PTR)params);
|
|
|
|
if (params->lpszValueName)
|
|
|
|
SetDlgItemText(hwndDlg, IDC_VALUE_NAME, params->lpszValueName);
|
|
|
|
else
|
|
|
|
SetDlgItemText(hwndDlg, IDC_VALUE_NAME, g_pszDefaultValueName);
|
|
|
|
SendDlgItemMessage(hwndDlg, IDC_VALUE_DATA, HEM_SETDATA, (WPARAM)params->cbData, (LPARAM)params->pData);
|
|
|
|
return TRUE;
|
|
|
|
case WM_COMMAND:
|
|
|
|
switch (LOWORD(wParam)) {
|
|
|
|
case IDOK:
|
|
|
|
params = (struct edit_params *)GetWindowLongPtr(hwndDlg, DWLP_USER);
|
|
|
|
cbData = SendDlgItemMessage(hwndDlg, IDC_VALUE_DATA, HEM_GETDATA, 0, 0);
|
|
|
|
pData = HeapAlloc(GetProcessHeap(), 0, cbData);
|
|
|
|
|
|
|
|
if (pData)
|
|
|
|
{
|
|
|
|
SendDlgItemMessage(hwndDlg, IDC_VALUE_DATA, HEM_GETDATA, (WPARAM)cbData, (LPARAM)pData);
|
|
|
|
lRet = RegSetValueEx(params->hKey, params->lpszValueName, 0, REG_BINARY, pData, cbData);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
lRet = ERROR_OUTOFMEMORY;
|
|
|
|
|
|
|
|
if (lRet == ERROR_SUCCESS)
|
|
|
|
EndDialog(hwndDlg, 1);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
error_code_messagebox(hwndDlg, lRet);
|
|
|
|
EndDialog(hwndDlg, 0);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
case IDCANCEL:
|
|
|
|
EndDialog(hwndDlg, 0);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2004-05-04 04:55:28 +02:00
|
|
|
static BOOL check_value(HWND hwnd, HKEY hKey, LPCTSTR valueName)
|
|
|
|
{
|
|
|
|
LONG lRet = RegQueryValueEx(hKey, valueName ? valueName : _T(""), 0, NULL, 0, NULL);
|
|
|
|
if(lRet != ERROR_SUCCESS) return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2004-01-14 00:18:12 +01:00
|
|
|
static LPTSTR read_value(HWND hwnd, HKEY hKey, LPCTSTR valueName, DWORD *lpType, LONG *len)
|
|
|
|
{
|
|
|
|
DWORD valueDataLen;
|
|
|
|
LPTSTR buffer = NULL;
|
|
|
|
LONG lRet;
|
|
|
|
|
2004-05-04 04:55:28 +02:00
|
|
|
lRet = RegQueryValueEx(hKey, valueName ? valueName : _T(""), 0, lpType, 0, &valueDataLen);
|
2004-01-14 00:18:12 +01:00
|
|
|
if (lRet != ERROR_SUCCESS) {
|
2004-05-04 04:55:28 +02:00
|
|
|
if (lRet == ERROR_FILE_NOT_FOUND && !valueName) { /* no default value here, make it up */
|
|
|
|
if (len) *len = 1;
|
|
|
|
if (lpType) *lpType = REG_SZ;
|
|
|
|
buffer = HeapAlloc(GetProcessHeap(), 0, 1);
|
|
|
|
*buffer = '\0';
|
|
|
|
return buffer;
|
|
|
|
}
|
2004-01-14 00:18:12 +01:00
|
|
|
error(hwnd, IDS_BAD_VALUE, valueName);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if ( *lpType == REG_DWORD ) valueDataLen = sizeof(DWORD);
|
|
|
|
if (!(buffer = HeapAlloc(GetProcessHeap(), 0, valueDataLen))) {
|
|
|
|
error(hwnd, IDS_TOO_BIG_VALUE, valueDataLen);
|
|
|
|
goto done;
|
|
|
|
}
|
2005-07-07 14:08:42 +02:00
|
|
|
lRet = RegQueryValueEx(hKey, valueName, 0, 0, (LPBYTE)buffer, &valueDataLen);
|
2004-01-14 00:18:12 +01:00
|
|
|
if (lRet != ERROR_SUCCESS) {
|
|
|
|
error(hwnd, IDS_BAD_VALUE, valueName);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(len) *len = valueDataLen;
|
|
|
|
return buffer;
|
|
|
|
|
|
|
|
done:
|
|
|
|
HeapFree(GetProcessHeap(), 0, buffer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-03-15 21:19:38 +01:00
|
|
|
BOOL CreateKey(HWND hwnd, HKEY hKeyRoot, LPCTSTR keyPath, LPTSTR keyName)
|
2004-01-03 01:33:56 +01:00
|
|
|
{
|
2004-01-16 03:21:23 +01:00
|
|
|
BOOL result = FALSE;
|
2004-01-06 21:38:56 +01:00
|
|
|
LONG lRet = ERROR_SUCCESS;
|
2006-10-23 21:30:58 +02:00
|
|
|
HKEY retKey = NULL;
|
2004-03-15 21:19:38 +01:00
|
|
|
TCHAR newKey[MAX_NEW_KEY_LEN - 4];
|
2004-01-06 21:38:56 +01:00
|
|
|
int keyNum;
|
2004-01-16 03:21:23 +01:00
|
|
|
HKEY hKey;
|
2004-01-03 01:33:56 +01:00
|
|
|
|
2004-01-16 03:21:23 +01:00
|
|
|
lRet = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_CREATE_SUB_KEY, &hKey);
|
2005-03-21 11:29:09 +01:00
|
|
|
if (lRet != ERROR_SUCCESS) {
|
|
|
|
error_code_messagebox(hwnd, lRet);
|
|
|
|
goto done;
|
|
|
|
}
|
2004-01-03 01:33:56 +01:00
|
|
|
|
2004-01-16 03:21:23 +01:00
|
|
|
if (!LoadString(GetModuleHandle(0), IDS_NEWKEY, newKey, COUNT_OF(newKey))) goto done;
|
2004-01-03 01:33:56 +01:00
|
|
|
|
2004-01-06 21:38:56 +01:00
|
|
|
/* try to find out a name for the newly create key (max 100 times) */
|
|
|
|
for (keyNum = 1; keyNum < 100; keyNum++) {
|
|
|
|
wsprintf(keyName, newKey, keyNum);
|
2004-01-05 22:14:19 +01:00
|
|
|
lRet = RegOpenKey(hKey, keyName, &retKey);
|
2004-01-06 21:38:56 +01:00
|
|
|
if (lRet != ERROR_SUCCESS) break;
|
|
|
|
RegCloseKey(retKey);
|
2004-01-03 01:33:56 +01:00
|
|
|
}
|
2004-01-16 03:21:23 +01:00
|
|
|
if (lRet == ERROR_SUCCESS) goto done;
|
2004-01-03 01:33:56 +01:00
|
|
|
|
|
|
|
lRet = RegCreateKey(hKey, keyName, &retKey);
|
2005-03-21 11:29:09 +01:00
|
|
|
if (lRet != ERROR_SUCCESS) {
|
|
|
|
error_code_messagebox(hwnd, lRet);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2004-01-16 03:21:23 +01:00
|
|
|
result = TRUE;
|
2004-01-06 21:38:56 +01:00
|
|
|
|
2004-01-16 03:21:23 +01:00
|
|
|
done:
|
2004-01-06 21:38:56 +01:00
|
|
|
RegCloseKey(retKey);
|
2004-01-16 03:21:23 +01:00
|
|
|
return result;
|
2004-01-03 01:33:56 +01:00
|
|
|
}
|
|
|
|
|
2004-01-16 03:21:23 +01:00
|
|
|
BOOL ModifyValue(HWND hwnd, HKEY hKeyRoot, LPCTSTR keyPath, LPCTSTR valueName)
|
2003-12-03 21:25:24 +01:00
|
|
|
{
|
2004-01-16 03:21:23 +01:00
|
|
|
BOOL result = FALSE;
|
2003-12-03 21:25:24 +01:00
|
|
|
DWORD type;
|
|
|
|
LONG lRet;
|
2004-01-16 03:21:23 +01:00
|
|
|
HKEY hKey;
|
2005-04-14 13:30:31 +02:00
|
|
|
LONG len;
|
2003-12-03 21:25:24 +01:00
|
|
|
|
2004-01-16 03:21:23 +01:00
|
|
|
lRet = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
|
2005-03-21 11:29:09 +01:00
|
|
|
if (lRet != ERROR_SUCCESS) {
|
|
|
|
error_code_messagebox(hwnd, lRet);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2003-12-03 21:25:24 +01:00
|
|
|
|
2004-05-04 04:55:28 +02:00
|
|
|
editValueName = valueName ? valueName : g_pszDefaultValueName;
|
2005-04-14 13:30:31 +02:00
|
|
|
if(!(stringValueData = read_value(hwnd, hKey, valueName, &type, &len))) goto done;
|
2003-12-03 21:25:24 +01:00
|
|
|
|
|
|
|
if ( (type == REG_SZ) || (type == REG_EXPAND_SZ) ) {
|
2004-01-05 22:14:19 +01:00
|
|
|
if (DialogBox(0, MAKEINTRESOURCE(IDD_EDIT_STRING), hwnd, modify_dlgproc) == IDOK) {
|
2005-07-07 14:08:42 +02:00
|
|
|
lRet = RegSetValueEx(hKey, valueName, 0, type, (LPBYTE)stringValueData, lstrlen(stringValueData) + 1);
|
2003-12-08 23:48:07 +01:00
|
|
|
if (lRet == ERROR_SUCCESS) result = TRUE;
|
2005-03-21 11:29:09 +01:00
|
|
|
else error_code_messagebox(hwnd, lRet);
|
2003-12-08 23:48:07 +01:00
|
|
|
}
|
2003-12-03 21:25:24 +01:00
|
|
|
} else if ( type == REG_DWORD ) {
|
2004-01-05 22:14:19 +01:00
|
|
|
wsprintf(stringValueData, isDecimal ? "%ld" : "%lx", *((DWORD*)stringValueData));
|
|
|
|
if (DialogBox(0, MAKEINTRESOURCE(IDD_EDIT_DWORD), hwnd, modify_dlgproc) == IDOK) {
|
2005-03-21 11:29:09 +01:00
|
|
|
DWORD val;
|
2006-10-09 18:38:30 +02:00
|
|
|
if (_stscanf(stringValueData, isDecimal ? "%d" : "%x", &val)) {
|
2004-01-05 22:14:19 +01:00
|
|
|
lRet = RegSetValueEx(hKey, valueName, 0, type, (BYTE*)&val, sizeof(val));
|
2005-03-21 11:29:09 +01:00
|
|
|
if (lRet == ERROR_SUCCESS) result = TRUE;
|
|
|
|
else error_code_messagebox(hwnd, lRet);
|
|
|
|
}
|
2004-01-05 22:14:19 +01:00
|
|
|
}
|
2005-04-14 13:30:31 +02:00
|
|
|
} else if ( type == REG_BINARY ) {
|
|
|
|
struct edit_params params;
|
|
|
|
params.hKey = hKey;
|
|
|
|
params.lpszValueName = valueName;
|
|
|
|
params.pData = stringValueData;
|
|
|
|
params.cbData = len;
|
|
|
|
result = DialogBoxParam(NULL, MAKEINTRESOURCE(IDD_EDIT_BINARY), hwnd,
|
|
|
|
bin_modify_dlgproc, (LPARAM)¶ms);
|
2003-12-03 21:25:24 +01:00
|
|
|
} else {
|
2003-12-08 23:48:07 +01:00
|
|
|
error(hwnd, IDS_UNSUPPORTED_TYPE, type);
|
2003-12-03 21:25:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
HeapFree(GetProcessHeap(), 0, stringValueData);
|
|
|
|
stringValueData = NULL;
|
2004-01-16 03:21:23 +01:00
|
|
|
RegCloseKey(hKey);
|
2003-12-03 21:25:24 +01:00
|
|
|
return result;
|
|
|
|
}
|
2004-01-05 22:14:19 +01:00
|
|
|
|
2004-03-12 20:44:47 +01:00
|
|
|
BOOL DeleteKey(HWND hwnd, HKEY hKeyRoot, LPCTSTR keyPath)
|
|
|
|
{
|
|
|
|
BOOL result = FALSE;
|
|
|
|
LONG lRet;
|
|
|
|
HKEY hKey;
|
|
|
|
|
2004-05-04 04:55:28 +02:00
|
|
|
lRet = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_READ|KEY_SET_VALUE, &hKey);
|
2005-03-21 11:29:09 +01:00
|
|
|
if (lRet != ERROR_SUCCESS) {
|
|
|
|
error_code_messagebox(hwnd, lRet);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2004-03-12 20:44:47 +01:00
|
|
|
|
|
|
|
if (messagebox(hwnd, MB_YESNO | MB_ICONEXCLAMATION, IDS_DELETE_BOX_TITLE, IDS_DELETE_BOX_TEXT, keyPath) != IDYES)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
lRet = SHDeleteKey(hKeyRoot, keyPath);
|
|
|
|
if (lRet != ERROR_SUCCESS) {
|
|
|
|
error(hwnd, IDS_BAD_KEY, keyPath);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
result = TRUE;
|
|
|
|
|
|
|
|
done:
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-01-16 03:21:23 +01:00
|
|
|
BOOL DeleteValue(HWND hwnd, HKEY hKeyRoot, LPCTSTR keyPath, LPCTSTR valueName)
|
2004-01-05 22:14:19 +01:00
|
|
|
{
|
2004-01-16 03:21:23 +01:00
|
|
|
BOOL result = FALSE;
|
2004-01-05 22:14:19 +01:00
|
|
|
LONG lRet;
|
2004-01-16 03:21:23 +01:00
|
|
|
HKEY hKey;
|
2004-05-04 04:55:28 +02:00
|
|
|
LPCSTR visibleValueName = valueName ? valueName : g_pszDefaultValueName;
|
2004-01-05 22:14:19 +01:00
|
|
|
|
2004-05-04 04:55:28 +02:00
|
|
|
lRet = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
|
2004-01-16 03:21:23 +01:00
|
|
|
if (lRet != ERROR_SUCCESS) return FALSE;
|
2004-01-05 22:14:19 +01:00
|
|
|
|
2004-05-04 04:55:28 +02:00
|
|
|
if (messagebox(hwnd, MB_YESNO | MB_ICONEXCLAMATION, IDS_DELETE_BOX_TITLE, IDS_DELETE_BOX_TEXT, visibleValueName) != IDYES)
|
2004-01-16 03:21:23 +01:00
|
|
|
goto done;
|
2004-01-05 22:14:19 +01:00
|
|
|
|
2004-05-04 04:55:28 +02:00
|
|
|
lRet = RegDeleteValue(hKey, valueName ? valueName : "");
|
|
|
|
if (lRet != ERROR_SUCCESS && valueName) {
|
2004-01-05 22:14:19 +01:00
|
|
|
error(hwnd, IDS_BAD_VALUE, valueName);
|
|
|
|
}
|
2004-01-16 03:21:23 +01:00
|
|
|
if (lRet != ERROR_SUCCESS) goto done;
|
|
|
|
result = TRUE;
|
|
|
|
|
|
|
|
done:
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
return result;
|
2004-01-05 22:14:19 +01:00
|
|
|
}
|
2004-01-06 21:38:56 +01:00
|
|
|
|
2004-05-04 04:55:28 +02:00
|
|
|
BOOL CreateValue(HWND hwnd, HKEY hKeyRoot, LPCTSTR keyPath, DWORD valueType, LPTSTR valueName)
|
2004-01-06 21:38:56 +01:00
|
|
|
{
|
|
|
|
LONG lRet = ERROR_SUCCESS;
|
2004-05-04 04:55:28 +02:00
|
|
|
TCHAR newValue[256];
|
2004-01-06 21:38:56 +01:00
|
|
|
DWORD valueDword = 0;
|
2004-01-16 03:21:23 +01:00
|
|
|
BOOL result = FALSE;
|
2004-01-06 21:38:56 +01:00
|
|
|
int valueNum;
|
2004-01-16 03:21:23 +01:00
|
|
|
HKEY hKey;
|
2004-01-06 21:38:56 +01:00
|
|
|
|
2004-01-16 03:21:23 +01:00
|
|
|
lRet = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
|
2005-03-21 11:29:09 +01:00
|
|
|
if (lRet != ERROR_SUCCESS) {
|
|
|
|
error_code_messagebox(hwnd, lRet);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2004-01-06 21:38:56 +01:00
|
|
|
|
2004-01-16 03:21:23 +01:00
|
|
|
if (!LoadString(GetModuleHandle(0), IDS_NEWVALUE, newValue, COUNT_OF(newValue))) goto done;
|
2004-01-06 21:38:56 +01:00
|
|
|
|
|
|
|
/* try to find out a name for the newly create key (max 100 times) */
|
|
|
|
for (valueNum = 1; valueNum < 100; valueNum++) {
|
|
|
|
wsprintf(valueName, newValue, valueNum);
|
|
|
|
lRet = RegQueryValueEx(hKey, valueName, 0, 0, 0, 0);
|
2005-03-21 11:29:09 +01:00
|
|
|
if (lRet == ERROR_FILE_NOT_FOUND) break;
|
|
|
|
}
|
|
|
|
if (lRet != ERROR_FILE_NOT_FOUND) {
|
|
|
|
error_code_messagebox(hwnd, lRet);
|
|
|
|
goto done;
|
2004-01-06 21:38:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lRet = RegSetValueEx(hKey, valueName, 0, valueType, (BYTE*)&valueDword, sizeof(DWORD));
|
2005-03-21 11:29:09 +01:00
|
|
|
if (lRet != ERROR_SUCCESS) {
|
|
|
|
error_code_messagebox(hwnd, lRet);
|
|
|
|
goto done;
|
|
|
|
}
|
2004-01-16 03:21:23 +01:00
|
|
|
result = TRUE;
|
2004-01-06 21:38:56 +01:00
|
|
|
|
2004-01-16 03:21:23 +01:00
|
|
|
done:
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
return result;
|
2004-01-06 21:38:56 +01:00
|
|
|
}
|
2004-01-14 00:18:12 +01:00
|
|
|
|
2004-01-16 03:21:23 +01:00
|
|
|
BOOL RenameValue(HWND hwnd, HKEY hKeyRoot, LPCTSTR keyPath, LPCTSTR oldName, LPCTSTR newName)
|
2004-01-14 00:18:12 +01:00
|
|
|
{
|
|
|
|
LPTSTR value = NULL;
|
|
|
|
DWORD type;
|
|
|
|
LONG len, lRet;
|
|
|
|
BOOL result = FALSE;
|
|
|
|
HKEY hKey;
|
|
|
|
|
2004-05-04 04:55:28 +02:00
|
|
|
if (!oldName) return FALSE;
|
2004-01-17 00:02:44 +01:00
|
|
|
if (!newName) return FALSE;
|
|
|
|
|
2004-01-16 03:21:23 +01:00
|
|
|
lRet = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_READ | KEY_SET_VALUE, &hKey);
|
2005-03-21 11:29:09 +01:00
|
|
|
if (lRet != ERROR_SUCCESS) {
|
|
|
|
error_code_messagebox(hwnd, lRet);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2004-02-06 06:16:56 +01:00
|
|
|
/* check if value already exists */
|
2004-05-04 04:55:28 +02:00
|
|
|
if (check_value(hwnd, hKey, newName)) goto done;
|
2004-01-14 00:18:12 +01:00
|
|
|
value = read_value(hwnd, hKey, oldName, &type, &len);
|
|
|
|
if(!value) goto done;
|
|
|
|
lRet = RegSetValueEx(hKey, newName, 0, type, (BYTE*)value, len);
|
2005-03-21 11:29:09 +01:00
|
|
|
if (lRet != ERROR_SUCCESS) {
|
|
|
|
error_code_messagebox(hwnd, lRet);
|
|
|
|
goto done;
|
|
|
|
}
|
2004-01-14 00:18:12 +01:00
|
|
|
lRet = RegDeleteValue(hKey, oldName);
|
|
|
|
if (lRet != ERROR_SUCCESS) {
|
|
|
|
RegDeleteValue(hKey, newName);
|
2005-03-21 11:29:09 +01:00
|
|
|
error_code_messagebox(hwnd, lRet);
|
2004-01-14 00:18:12 +01:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
result = TRUE;
|
|
|
|
|
|
|
|
done:
|
|
|
|
HeapFree(GetProcessHeap(), 0, value);
|
2004-01-16 03:21:23 +01:00
|
|
|
RegCloseKey(hKey);
|
2004-01-14 00:18:12 +01:00
|
|
|
return result;
|
|
|
|
}
|
2004-03-15 21:19:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
BOOL RenameKey(HWND hwnd, HKEY hRootKey, LPCTSTR keyPath, LPCTSTR newName)
|
|
|
|
{
|
|
|
|
LPTSTR parentPath = 0;
|
|
|
|
LPCTSTR srcSubKey = 0;
|
|
|
|
HKEY parentKey = 0;
|
|
|
|
HKEY destKey = 0;
|
|
|
|
BOOL result = FALSE;
|
|
|
|
LONG lRet;
|
2005-03-21 11:29:09 +01:00
|
|
|
DWORD disposition;
|
2004-03-15 21:19:38 +01:00
|
|
|
|
|
|
|
if (!keyPath || !newName) return FALSE;
|
|
|
|
|
|
|
|
if (!strrchr(keyPath, '\\')) {
|
|
|
|
parentKey = hRootKey;
|
|
|
|
srcSubKey = keyPath;
|
|
|
|
} else {
|
2006-10-05 23:01:55 +02:00
|
|
|
LPTSTR srcSubKey_copy;
|
|
|
|
|
2004-03-15 21:19:38 +01:00
|
|
|
parentPath = strdup(keyPath);
|
2006-10-05 23:01:55 +02:00
|
|
|
srcSubKey_copy = strrchr(parentPath, '\\');
|
|
|
|
*srcSubKey_copy = 0;
|
|
|
|
srcSubKey = srcSubKey_copy + 1;
|
2004-03-15 21:19:38 +01:00
|
|
|
lRet = RegOpenKeyEx(hRootKey, parentPath, 0, KEY_READ | KEY_CREATE_SUB_KEY, &parentKey);
|
2005-03-21 11:29:09 +01:00
|
|
|
if (lRet != ERROR_SUCCESS) {
|
|
|
|
error_code_messagebox(hwnd, lRet);
|
|
|
|
goto done;
|
|
|
|
}
|
2004-03-15 21:19:38 +01:00
|
|
|
}
|
|
|
|
|
2005-01-10 17:03:22 +01:00
|
|
|
/* The following fails if the old name is the same as the new name. */
|
|
|
|
if (!strcmp(srcSubKey, newName)) goto done;
|
|
|
|
|
2005-03-21 11:29:09 +01:00
|
|
|
lRet = RegCreateKeyEx(parentKey, newName, 0, NULL, REG_OPTION_NON_VOLATILE,
|
|
|
|
KEY_WRITE, NULL /* FIXME */, &destKey, &disposition);
|
|
|
|
if (disposition == REG_OPENED_EXISTING_KEY)
|
|
|
|
lRet = ERROR_FILE_EXISTS; /* FIXME: we might want a better error message than this */
|
|
|
|
if (lRet != ERROR_SUCCESS) {
|
|
|
|
error_code_messagebox(hwnd, lRet);
|
|
|
|
goto done;
|
|
|
|
}
|
2004-03-15 21:19:38 +01:00
|
|
|
|
|
|
|
/* FIXME: SHCopyKey does not copy the security attributes */
|
|
|
|
lRet = SHCopyKey(parentKey, srcSubKey, destKey, 0);
|
2005-03-21 11:29:09 +01:00
|
|
|
if (lRet != ERROR_SUCCESS) {
|
|
|
|
RegCloseKey(destKey);
|
|
|
|
RegDeleteKey(parentKey, newName);
|
|
|
|
error_code_messagebox(hwnd, lRet);
|
|
|
|
goto done;
|
|
|
|
}
|
2004-03-15 21:19:38 +01:00
|
|
|
|
|
|
|
lRet = SHDeleteKey(hRootKey, keyPath);
|
2005-03-21 11:29:09 +01:00
|
|
|
if (lRet != ERROR_SUCCESS) {
|
|
|
|
error_code_messagebox(hwnd, lRet);
|
|
|
|
goto done;
|
|
|
|
}
|
2004-03-15 21:19:38 +01:00
|
|
|
|
|
|
|
result = TRUE;
|
|
|
|
|
|
|
|
done:
|
|
|
|
RegCloseKey(destKey);
|
|
|
|
if (parentKey) {
|
|
|
|
RegCloseKey(parentKey);
|
|
|
|
free(parentPath);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|