写了一个注册表操作的类,可以实现创建/删除键,创建、获取,删除键值(包括字符、DWORD、BINARY类型),备份、还原键值。具体如下:
#pragma once
#include
class CRegeditOperate
{
public:
CRegeditOperate(void);
~CRegeditOperate(void);
//选择根键
void selectRootKey(int nKey);
//创建子键
bool createSubKey(char* chPath, char* chName);
//删除子键
bool deleteSubKey(char* chPath, char* chName);
//创建字符键值对
bool createKeyValue(char* chPath, char* chKey, char* chValue);
//创建DWORD键值对
bool createKeyValue(char* chPath, char* chKey, DWORD dwValue);
//创建BINARY键值对
bool createKeyValue(char* chPath, char* chKey, BYTE btValue[]);
//删除键值对
bool deleteKeyValue(char* chPath, char* chKey);
//获取DWORD键值
bool getKeyValue(char* chPath, char* chKey, DWORD& dwValue);
//获取字符键值
bool getKeyValue(char* chPath, char* chKey, std::string& value);
//获取BINARY键值,返回二进制数据
bool getKeyValue(char* chPath, char* chKey, BYTE value[]);
//获取BINARY键值,返回string类型
bool getKeyValueEx(char* chPath, char* chKey, std::string& value);
//备份键值对
bool backupKey(char* chPath, char* fileName);
//还原键值对
bool restoreKey(char* chPath,char* fileName, bool isSure);
private:
HKEY m_hKey;
};
#include "StdAfx.h"
#include "RegeditOperate.h"
CRegeditOperate::CRegeditOperate(void)
{
m_hKey = HKEY_CURRENT_USER;
}
CRegeditOperate::~CRegeditOperate(void)
{
}
void CRegeditOperate::selectRootKey(int nKey)
{
switch (nKey)
{
case 0:
m_hKey = HKEY_CLASSES_ROOT;
break;
case 1:
m_hKey = HKEY_CURRENT_USER;
break;
case 2:
m_hKey = HKEY_LOCAL_MACHINE;
break;
case 3:
m_hKey = HKEY_USERS;
break;
case 4:
m_hKey = HKEY_CURRENT_CONFIG;
break;
default:
m_hKey = HKEY_CURRENT_USER;
break;
}
return;
}
bool CRegeditOperate::createSubKey(char* chPath, char* chName)
{
HKEY hKey;
HKEY hNextKey;
TCHAR path[128] = {0};
TCHAR name[128] = {0};
//多字节转宽字节
int iLength1 = MultiByteToWideChar(CP_ACP, 0, chPath, strlen(chPath) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, chPath, strlen(chPath) + 1, path, iLength1);
int iLength2 = MultiByteToWideChar(CP_ACP, 0, chName, strlen(chName) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, chName, strlen(chName) + 1, name, iLength2);
//打开注册表
if (ERROR_SUCCESS != ::RegOpenKeyEx(m_hKey, path, 0, KEY_SET_VALUE | KEY_WOW64_64KEY, &hKey))
{
return false;
}
else
{
if (ERROR_SUCCESS == ::RegCreateKey(hKey, name, &hNextKey))
{
::RegCloseKey(hKey);
return true;
}
else
{
::RegCloseKey(hKey);
return false;
}
}
}
bool CRegeditOperate::createKeyValue(char* chPath, char* chKey, char* chValue)
{
HKEY hKey;
TCHAR path[128] = {0};
TCHAR key[128] = {0};
TCHAR value[128] = {0};
//多字节转宽字节
int iLength1 = MultiByteToWideChar(CP_ACP, 0, chPath, strlen(chPath) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, chPath, strlen(chPath) + 1, path, iLength1);
int iLength2 = MultiByteToWideChar(CP_ACP, 0, chKey, strlen(chKey) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, chKey, strlen(chKey) + 1, key, iLength2);
int iLength3 = MultiByteToWideChar(CP_ACP, 0, chValue, strlen(chValue) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, chValue, strlen(chValue) + 1, value, iLength3);
//打开注册表
if (ERROR_SUCCESS != ::RegOpenKeyEx(m_hKey, path, 0, KEY_SET_VALUE | KEY_WOW64_64KEY, &hKey))
{
return false;
}
else
{
if (ERROR_SUCCESS == ::RegSetValueEx(hKey, key, 0, REG_SZ, (const BYTE*)value, MAX_PATH))
{
::RegCloseKey(hKey);
return true;
}
else
{
::RegCloseKey(hKey);
return false;
}
}
}
bool CRegeditOperate::createKeyValue(char* chPath, char* chKey, DWORD dwValue)
{
HKEY hKey;
TCHAR path[128] = {0};
TCHAR key[128] = {0};
//多字节转宽字节
int iLength1 = MultiByteToWideChar(CP_ACP, 0, chPath, strlen(chPath) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, chPath, strlen(chPath) + 1, path, iLength1);
int iLength2 = MultiByteToWideChar(CP_ACP, 0, chKey, strlen(chKey) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, chKey, strlen(chKey) + 1, key, iLength2);
//打开注册表
if (ERROR_SUCCESS != ::RegOpenKeyEx(m_hKey, path, 0, KEY_SET_VALUE | KEY_WOW64_64KEY, &hKey))
{
return false;
}
else
{
if (ERROR_SUCCESS == ::RegSetValueEx(hKey, key, 0, REG_DWORD, (const BYTE*)&dwValue, sizeof(DWORD)))
{
::RegCloseKey(hKey);
return true;
}
else
{
::RegCloseKey(hKey);
return false;
}
}
}
bool CRegeditOperate::createKeyValue(char* chPath, char* chKey, BYTE btValue[])
{
HKEY hKey;
TCHAR path[128] = {0};
TCHAR key[128] = {0};
//多字节转宽字节
int iLength1 = MultiByteToWideChar(CP_ACP, 0, chPath, strlen(chPath) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, chPath, strlen(chPath) + 1, path, iLength1);
int iLength2 = MultiByteToWideChar(CP_ACP, 0, chKey, strlen(chKey) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, chKey, strlen(chKey) + 1, key, iLength2);
//打开注册表
if (ERROR_SUCCESS != ::RegOpenKeyEx(m_hKey, path, 0, KEY_SET_VALUE | KEY_WOW64_64KEY, &hKey))
{
return false;
}
else
{
if (ERROR_SUCCESS == ::RegSetValueEx(hKey, key, 0, REG_DWORD, (const BYTE*)btValue, sizeof(btValue)))
{
::RegCloseKey(hKey);
return true;
}
else
{
::RegCloseKey(hKey);
return false;
}
}
}
bool CRegeditOperate::deleteKeyValue(char* chPath, char* chKey)
{
HKEY hKey;
TCHAR path[128] = {0};
TCHAR key[128] = {0};
//多字节转宽字节
int iLength1 = MultiByteToWideChar(CP_ACP, 0, chPath, strlen(chPath) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, chPath, strlen(chPath) + 1, path, iLength1);
int iLength2 = MultiByteToWideChar(CP_ACP, 0, chKey, strlen(chKey) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, chKey, strlen(chKey) + 1, key, iLength2);
//打开注册表
if (ERROR_SUCCESS != ::RegOpenKeyEx(m_hKey, path, 0, KEY_SET_VALUE | KEY_WOW64_64KEY, &hKey))
{
return false;
}
else
{
if (ERROR_SUCCESS == ::RegDeleteValue(hKey, key))
{
::RegCloseKey(hKey);
return true;
}
else
{
::RegCloseKey(hKey);
return false;
}
}
}
bool CRegeditOperate::getKeyValue(char* chPath, char* chKey, DWORD& dwValue)
{
HKEY hKey;
TCHAR path[128] = {0};
TCHAR key[128] = {0};
//多字节转宽字节
int iLength1 = MultiByteToWideChar(CP_ACP, 0, chPath, strlen(chPath) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, chPath, strlen(chPath) + 1, path, iLength1);
int iLength2 = MultiByteToWideChar(CP_ACP, 0, chKey, strlen(chKey) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, chKey, strlen(chKey) + 1, key, iLength2);
//打开注册表
if (ERROR_SUCCESS != ::RegOpenKeyEx(m_hKey, path, 0, KEY_READ | KEY_WOW64_64KEY, &hKey))
{
return false;
}
else
{
DWORD dwGetValue = 0;
DWORD dwSize = sizeof(DWORD);
DWORD dwType = REG_DWORD;
if (ERROR_SUCCESS == ::RegQueryValueEx(hKey, key, 0, &dwType, (LPBYTE)&dwGetValue, &dwSize))
{
dwValue = dwGetValue;
::RegCloseKey(hKey);
return true;
}
else
{
::RegCloseKey(hKey);
return false;
}
}
}
bool CRegeditOperate::getKeyValue(char* chPath, char* chKey, std::string& value)
{
HKEY hKey;
TCHAR path[128] = {0};
TCHAR key[128] = {0};
TCHAR data[128] = {0};
char result[1024] = {0};
//多字节转宽字节
int iLength1 = MultiByteToWideChar(CP_ACP, 0, chPath, strlen(chPath) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, chPath, strlen(chPath) + 1, path, iLength1);
int iLength2 = MultiByteToWideChar(CP_ACP, 0, chKey, strlen(chKey) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, chKey, strlen(chKey) + 1, key, iLength2);
//打开注册表
if (ERROR_SUCCESS != ::RegOpenKeyEx(m_hKey, path, 0, KEY_READ | KEY_WOW64_64KEY, &hKey))
{
return false;
}
else
{
DWORD dwSize = 1024;
DWORD dwType = REG_SZ;
if (ERROR_SUCCESS == ::RegQueryValueEx(hKey, key, 0, &dwType, (LPBYTE)&data, &dwSize))
{
DWORD bufferSize = WideCharToMultiByte(CP_OEMCP, 0, data, -1, NULL, 0, NULL, NULL);
WideCharToMultiByte(CP_OEMCP, 0, data, -1, result, bufferSize, NULL,NULL);
value = std::string(result);
::RegCloseKey(hKey);
return true;
}
else
{
::RegCloseKey(hKey);
return false;
}
}
}
bool CRegeditOperate::getKeyValue(char* chPath, char* chKey, BYTE value[])
{
HKEY hKey;
TCHAR path[128] = {0};
TCHAR key[128] = {0};
//多字节转宽字节
int iLength1 = MultiByteToWideChar(CP_ACP, 0, chPath, strlen(chPath) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, chPath, strlen(chPath) + 1, path, iLength1);
int iLength2 = MultiByteToWideChar(CP_ACP, 0, chKey, strlen(chKey) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, chKey, strlen(chKey) + 1, key, iLength2);
//打开注册表
if (ERROR_SUCCESS != ::RegOpenKeyEx(m_hKey, path, 0, KEY_READ | KEY_WOW64_64KEY, &hKey))
{
return false;
}
else
{
DWORD dwKeyValueSize = 1024;
LPBYTE lpbKeyValueData = new BYTE[1024];
DWORD dwType = REG_BINARY;
if (ERROR_SUCCESS == ::RegQueryValueEx(hKey, key, 0, &dwType, (LPBYTE)lpbKeyValueData, &dwKeyValueSize))
{
for (DWORD i = 0; i < dwKeyValueSize; i++)
{
value[i] = lpbKeyValueData[i];
}
::RegCloseKey(hKey);
return true;
}
else
{
::RegCloseKey(hKey);
return false;
}
}
return true;
}
bool CRegeditOperate::getKeyValueEx(char* chPath, char* chKey, std::string& value)
{
HKEY hKey;
TCHAR path[128] = {0};
TCHAR key[128] = {0};
//多字节转宽字节
int iLength1 = MultiByteToWideChar(CP_ACP, 0, chPath, strlen(chPath) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, chPath, strlen(chPath) + 1, path, iLength1);
int iLength2 = MultiByteToWideChar(CP_ACP, 0, chKey, strlen(chKey) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, chKey, strlen(chKey) + 1, key, iLength2);
//打开注册表
if (ERROR_SUCCESS != ::RegOpenKeyEx(m_hKey, path, 0, KEY_READ | KEY_WOW64_64KEY, &hKey))
{
::RegCloseKey(hKey);
return false;
}
else
{
DWORD dwKeyValueSize = 1024;
LPBYTE lpbKeyValueData = new BYTE[1024];
DWORD dwType = REG_BINARY;
if (ERROR_SUCCESS == ::RegQueryValueEx(hKey, key, 0, &dwType, (LPBYTE)lpbKeyValueData, &dwKeyValueSize))
{
for (DWORD i = 0; i < dwKeyValueSize; i++)
{
char buffer[4] = {0};
sprintf_s(buffer,sizeof(buffer), "%02x", lpbKeyValueData[i]);
//sprintf(buffer, "%02x", lpbKeyValueData[i]);
value.append(buffer);
}
::RegCloseKey(hKey);
return true;
}
else
{
::RegCloseKey(hKey);
return false;
}
}
return true;
}
bool CRegeditOperate::backupKey(char* chPath, char* chfileName)
{
HKEY hKey;
TCHAR path[128] = {0};
TCHAR filename[128] = {0};
//多字节转宽字节
int iLength1 = MultiByteToWideChar(CP_ACP, 0, chPath, strlen(chPath) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, chPath, strlen(chPath) + 1, path, iLength1);
int iLength2 = MultiByteToWideChar(CP_ACP, 0, chfileName, strlen(chfileName) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, chfileName, strlen(chfileName) + 1, filename, iLength2);
//申请权限
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
return false;
}
LookupPrivilegeValue(NULL, SE_BACKUP_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
int b = GetLastError();
if (ERROR_SUCCESS != ::RegOpenKeyEx(m_hKey, path, 0, KEY_SET_VALUE | KEY_WOW64_64KEY, &hKey))
{
return false;
}
else
{
//保存
if (ERROR_SUCCESS != ::RegSaveKey(hKey, filename, NULL))
{
::RegCloseKey(hKey);
return false;
}
else
{
::RegCloseKey(hKey);
return true;
}
}
}
bool CRegeditOperate::restoreKey(char* chPath,char* fileName, bool isSure)
{
HKEY hKey;
TCHAR path[128] = {0};
TCHAR filename[128] = {0};
//多字节转宽字节
int iLength1 = MultiByteToWideChar(CP_ACP, 0, chPath, strlen(chPath) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, chPath, strlen(chPath) + 1, path, iLength1);
int iLength2 = MultiByteToWideChar(CP_ACP, 0, fileName, strlen(fileName) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, fileName, strlen(fileName) + 1, filename, iLength2);
//申请权限
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
return false;
}
LookupPrivilegeValue(NULL, SE_RESTORE_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
if (ERROR_SUCCESS != ::RegOpenKeyEx(m_hKey, path, 0, KEY_SET_VALUE | KEY_WOW64_64KEY, &hKey))
{
return false;
}
else
{
DWORD dwFlag;
if (isSure)
{
dwFlag = REG_FORCE_RESTORE;
}
else
{
dwFlag = REG_WHOLE_HIVE_VOLATILE;
}
//还原
if (ERROR_SUCCESS != ::RegRestoreKey(hKey, filename, dwFlag))
{
::RegCloseKey(hKey);
return false;
}
else
{
::RegCloseKey(hKey);
return true;
}
}
}
bool CRegeditOperate::deleteSubKey(char* chPath, char* chName)
{
HKEY hKey;
TCHAR path[128] = {0};
TCHAR name[128] = {0};
//多字节转宽字节
int iLength1 = MultiByteToWideChar(CP_ACP, 0, chPath, strlen(chPath) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, chPath, strlen(chPath) + 1, path, iLength1);
int iLength2 = MultiByteToWideChar(CP_ACP, 0, chName, strlen(chName) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, chName, strlen(chName) + 1, name, iLength2);
//打开注册表
if (ERROR_SUCCESS != ::RegOpenKeyEx(m_hKey, path, 0, KEY_SET_VALUE | KEY_WOW64_64KEY, &hKey))
{
return false;
}
else
{
if (ERROR_SUCCESS == ::RegDeleteKey(hKey, name))
{
::RegCloseKey(hKey);
return true;
}
else
{
::RegCloseKey(hKey);
return false;
}
}
}
用MFC写了一个测试界面,测试代码如下:
// RegTestDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "RegTest.h"
#include "RegTestDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CRegTestDlg 对话框
CRegTestDlg::CRegTestDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CRegTestDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CRegTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CRegTestDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_CREATE_STRING_KEY, &CRegTestDlg::OnBnClickedButtonCreateStringKey)
ON_BN_CLICKED(IDC_BUTTON_DELSUBKEY, &CRegTestDlg::OnBnClickedButtonDelsubkey)
ON_BN_CLICKED(IDC_BUTTON_CREATESTRINGKEY, &CRegTestDlg::OnBnClickedButtonCreatestringkey)
ON_BN_CLICKED(IDC_BUTTON_CREATEDWORDKEY, &CRegTestDlg::OnBnClickedButtonCreatedwordkey)
ON_BN_CLICKED(IDC_BUTTON_CREATEBINARYKEY, &CRegTestDlg::OnBnClickedButtonCreatebinarykey)
ON_BN_CLICKED(IDC_BUTTON_DELKEY, &CRegTestDlg::OnBnClickedButtonDelkey)
ON_BN_CLICKED(IDC_BUTTON_GETSTRINGVALUE, &CRegTestDlg::OnBnClickedButtonGetstringvalue)
ON_BN_CLICKED(IDC_BUTTON_GETDWORDVALUE, &CRegTestDlg::OnBnClickedButtonGetdwordvalue)
ON_BN_CLICKED(IDC_BUTTON_GETBINARYVALUE, &CRegTestDlg::OnBnClickedButtonGetbinaryvalue)
ON_BN_CLICKED(IDC_BUTTON_GETBINARYSTRINGVALUE, &CRegTestDlg::OnBnClickedButtonGetbinarystringvalue)
ON_BN_CLICKED(IDC_BUTTON_BACKUP, &CRegTestDlg::OnBnClickedButtonBackup)
ON_BN_CLICKED(IDC_BUTTON_RESTOREKEY, &CRegTestDlg::OnBnClickedButtonRestorekey)
END_MESSAGE_MAP()
// CRegTestDlg 消息处理程序
BOOL CRegTestDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
m_keyOpeate.selectRootKey(1);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CRegTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CRegTestDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CRegTestDlg::OnQueryDragIcon()
{
return static_cast(m_hIcon);
}
void CRegTestDlg::OnBnClickedButtonCreateStringKey()
{
// TODO: 在此添加控件通知处理程序代码
CString value;
GetDlgItemText(IDC_CREATE_STRING_KEY, value);
if (value.IsEmpty())
{
AfxMessageBox(L"输入不能为空!");
return;
}
char path[] = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
if (TRUE == m_keyOpeate.createSubKey(path, "test"))
{
AfxMessageBox(L"创建成功!");
}
else
{
AfxMessageBox(L"创建失败!");
}
CDialog::OnOK();
return;
}
void CRegTestDlg::OnBnClickedButtonDelsubkey()
{
// TODO: 在此添加控件通知处理程序代码
CString value;
GetDlgItemText(IDC_DELSUBKEY, value);
if (value.IsEmpty())
{
AfxMessageBox(L"输入不能为空!");
return;
}
char path[] = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
if (TRUE == m_keyOpeate.deleteSubKey(path, "test"))
{
AfxMessageBox(L"删除成功!");
}
else
{
AfxMessageBox(L"删除失败!");
}
//CDialog::OnOK();
return;
}
void CRegTestDlg::OnBnClickedButtonCreatestringkey()
{
// TODO: 在此添加控件通知处理程序代码
CString key, value;
GetDlgItemText(IDC_CREATE_STRINGKEY, key);
GetDlgItemText(IDC_CREATE_STRINGVALUE, value);
if (value.IsEmpty() || key.IsEmpty())
{
AfxMessageBox(L"输入不能为空!");
return;
}
char path[] = "Software\\Microsoft\\Windows\\CurrentVersion\\Run\\test";
USES_CONVERSION;//CString转char*
if (TRUE == m_keyOpeate.createKeyValue(path, T2A(key), T2A(value)))
{
AfxMessageBox(L"创建成功!");
}
else
{
AfxMessageBox(L"创建失败!");
}
CDialog::OnOK();
return;
}
void CRegTestDlg::OnBnClickedButtonCreatedwordkey()
{
// TODO: 在此添加控件通知处理程序代码
CString key;
int value;
GetDlgItemText(IDC_CREATE_DWORDKEY, key);
value = GetDlgItemInt(IDC_CREATE_DWORDVALUE);//输入非整型或者空值为0
if (key.IsEmpty())
{
AfxMessageBox(L"输入不能为空!");
return;
}
char path[] = "Software\\Microsoft\\Windows\\CurrentVersion\\Run\\test";
USES_CONVERSION;//CString转char*
if (TRUE == m_keyOpeate.createKeyValue(path, T2A(key), (DWORD)value))
{
AfxMessageBox(L"创建成功!");
}
else
{
AfxMessageBox(L"创建失败!");
}
CDialog::OnOK();
return;
}
void CRegTestDlg::OnBnClickedButtonCreatebinarykey()
{
// TODO: 在此添加控件通知处理程序代码
CString key;
GetDlgItemText(IDC_CREATE_BINARYKEY, key);
BYTE value[] = {02, 23, 45, 9};//由低位到高位
if (key.IsEmpty())
{
AfxMessageBox(L"输入不能为空!");
return;
}
char path[] = "Software\\Microsoft\\Windows\\CurrentVersion\\Run\\test";
USES_CONVERSION;//CString转char*
if (TRUE == m_keyOpeate.createKeyValue(path, T2A(key), value))
{
AfxMessageBox(L"创建成功!");
}
else
{
AfxMessageBox(L"创建失败!");
}
CDialog::OnOK();
return;
}
void CRegTestDlg::OnBnClickedButtonDelkey()
{
// TODO: 在此添加控件通知处理程序代码
CString value;
GetDlgItemText(IDC_DELKEY, value);
if (value.IsEmpty())
{
AfxMessageBox(L"输入不能为空!");
return;
}
char path[] = "Software\\Microsoft\\Windows\\CurrentVersion\\Run\\test";
if (TRUE == m_keyOpeate.deleteKeyValue(path, "deleteKey"))
{
AfxMessageBox(L"删除成功!");
}
else
{
AfxMessageBox(L"删除失败!");
}
CDialog::OnOK();
return;
}
void CRegTestDlg::OnBnClickedButtonGetstringvalue()
{
// TODO: 在此添加控件通知处理程序代码
CString key;
std::string stringValue;
GetDlgItemText(IDC_GETSTRINGVALUE, key);
if (key.IsEmpty())
{
AfxMessageBox(L"输入不能为空!");
return;
}
char path[] = "Software\\Microsoft\\Windows\\CurrentVersion\\Run\\test";
USES_CONVERSION;//CString转char*
if (TRUE == m_keyOpeate.getKeyValue(path, T2A(key), stringValue))
{
AfxMessageBox(CString(stringValue.c_str()));
}
else
{
AfxMessageBox(L"获取失败!");
}
CDialog::OnOK();
return;
}
void CRegTestDlg::OnBnClickedButtonGetdwordvalue()
{
// TODO: 在此添加控件通知处理程序代码
CString key;
DWORD dwValue;
GetDlgItemText(IDC_GETDWORDKEY, key);
if (key.IsEmpty())
{
AfxMessageBox(L"输入不能为空!");
return;
}
char path[] = "Software\\Microsoft\\Windows\\CurrentVersion\\Run\\test";
USES_CONVERSION;//CString转char*
if (TRUE == m_keyOpeate.getKeyValue(path, T2A(key), dwValue))
{
CString value;
value.Format(_T("%d"), dwValue);
AfxMessageBox(value);
}
else
{
AfxMessageBox(L"获取失败!");
}
CDialog::OnOK();
return;
}
void CRegTestDlg::OnBnClickedButtonGetbinaryvalue()
{
// TODO: 在此添加控件通知处理程序代码
CString key;
BYTE value[64] = {0};
GetDlgItemText(IDC_GETDBINARYKEY, key);
if (key.IsEmpty())
{
AfxMessageBox(L"输入不能为空!");
return;
}
char path[] = "Software\\Microsoft\\Windows\\CurrentVersion\\Run\\test";
USES_CONVERSION;//CString转char*
if (TRUE == m_keyOpeate.getKeyValue(path, T2A(key), value))
{
value;
AfxMessageBox(L"获取成功!");
}
else
{
AfxMessageBox(L"获取失败!");
}
CDialog::OnOK();
return;
}
void CRegTestDlg::OnBnClickedButtonGetbinarystringvalue()
{
// TODO: 在此添加控件通知处理程序代码
CString key;
std::string value;
GetDlgItemText(IDC_GETDBINARSTRINGYKEY, key);
if (key.IsEmpty())
{
AfxMessageBox(L"输入不能为空!");
return;
}
char path[] = "Software\\Microsoft\\Windows\\CurrentVersion\\Run\\test";
USES_CONVERSION;//CString转char*
if (TRUE == m_keyOpeate.getKeyValueEx(path, T2A(key), value))
{
AfxMessageBox(CString(value.c_str()));
}
else
{
AfxMessageBox(L"获取失败!");
}
CDialog::OnOK();
}
void CRegTestDlg::OnBnClickedButtonBackup()
{
// TODO: 在此添加控件通知处理程序代码
CString name;
GetDlgItemText(IDC_EDIT_BACKUPNAME, name);
if (name.IsEmpty())
{
AfxMessageBox(L"输入不能为空!");
return;
}
char path[] = "Software\\Microsoft\\Windows\\CurrentVersion\\Run\\test";
USES_CONVERSION;//CString转char*
if (TRUE == m_keyOpeate.backupKey(path, T2A(name)))
{
AfxMessageBox(L"备份成功!");
}
else
{
AfxMessageBox(L"备份失败!");
}
CDialog::OnOK();
return;
}
void CRegTestDlg::OnBnClickedButtonRestorekey()
{
// TODO: 在此添加控件通知处理程序代码
CString name;
GetDlgItemText(IDC_EDIT_RESTORENAME, name);
if (name.IsEmpty())
{
AfxMessageBox(L"输入不能为空!");
return;
}
char path[] = "Software\\Microsoft\\Windows\\CurrentVersion\\Run\\test";
USES_CONVERSION;//CString转char*
if (TRUE == m_keyOpeate.restoreKey(path, T2A(name), 1))
{
AfxMessageBox(L"还原成功!");
}
else
{
AfxMessageBox(L"还原失败!");
}
CDialog::OnOK();
return;
}
根键 - HKEY_CURRENT_USER
键 - Software\Microsoft\Windows\CurrentVersion\Run
1 创建子健test
2 删除子键test
3 创建字符键值对 testStringKey “testStringKeyValue”
4 创建DWORD键值对 testDWORDKey 10
5 创建BINARY键值对 testBINARYkey 92d1702
6 删除键值对 deleteKey
7 获取DWORD键值 testDWORDKey
8 获取BINARY键值,返回二进制数据 testBINARYkey
9 获取BINARY键值,返回string数据(主要用于UI 显示)
10 备份键值
11 还原键值
注意宽字节转多字节,另外就是如果备份还原失败,需要以管理员运行程序。