1、头文件(声明)
/********************************************************************
* 文件名:RegOperator.h
* 文件描述:对注册表的常见操作进行封装
* 创建人: RainLeaf, 2009年4月10日
* 版本号:1.0
* 修改记录:
*********************************************************************/
#include <afx.h>
#include <assert.h>
#ifndef REGOP_H
#define REGOP_H
class CRegOperator
{
private:
HKEY m_hKey; /* 保存当前操作根键的句柄 */
public:
CRegOperator( HKEY hKey = HKEY_LOCAL_MACHINE ); /* 构造函数,默认参数为:HKEY_LOCAL_MACHINE */
BOOL SetHKEY( LPCTSTR strKey ); /* 设置当前操作的根键 */
BOOL OpenKey( LPCTSTR lpSubKey ); /* 读写的形式打开注册表 */
void Close(); /* 关闭键句柄 */
BOOL CreateKey( LPCTSTR lpSubKey ); /* 创建的形式打开注册表 */
BOOL DeleteKey( LPCTSTR lphKey, LPCTSTR lpSubKey ); /* 删除相应的子键(子键为空) */
BOOL DeleteValue( LPCTSTR lpValueName ); /* 删除子键处的相应的键值 */
BOOL SaveKey( LPCTSTR lpFileName ); /* 把当前键值保存到指定文件 */
BOOL RestoreKey( LPCTSTR lpFileName ); /* 从指定注册表文件中恢复 */
BOOL Read( LPCTSTR lpValueName, CString* lpVal ); /* 读出REG_SZ类型的值 */
BOOL Read( LPCTSTR lpValueName, DWORD* pdwVal ); /* 读出DWORD类型的值 */
BOOL Read( LPCTSTR lpValueName, int* pnVal ); /* 读出INT类型的值 */
BOOL Write( LPCTSTR lpSubKey, LPCTSTR lpVal ); /* 写入REG_SZ类型值 */
BOOL Write( LPCTSTR lpSubKey, DWORD dwVal ); /* 写入DWORD类型值 */
BOOL Write( LPCTSTR lpSubKey, int nVal ); /* 写入INT类型值 */
virtual ~CRegOperator();
};
#endif
2、实现文件
/********************************************************************
* 文件名:RegOperator.cpp
* 文件描述:对头文件中定义类的成员函数进行了实现
* 创建人: RainLeaf, 2009年4月10日
* 版本号:1.0
* 修改记录:
*********************************************************************/
#include "RegOperator.h"
/*============================================================
* 函 数 名:CRegOperator
* 参 数:HKEY [IN] : 默认是HKEY_LOCAL_MACHINE
* 功能描述:构造函数,初始化根键
* 返 回 值:无
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
CRegOperator::CRegOperator( HKEY hKey )
{
m_hKey = HKEY_LOCAL_MACHINE;
}
/*============================================================
* 函 数 名:~CRegOperator
* 参 数:NULL [IN]
* 功能描述:析构函数,关闭打开的注册表句柄
* 返 回 值:无
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
CRegOperator::~CRegOperator()
{
Close();
}
/*============================================================
* 函 数 名:Close
* 参 数:NULL [IN]
* 功能描述:关闭打开键的句柄
* 返 回 值:void
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
void CRegOperator::Close()
{
if( m_hKey )
{
/* 句柄非空进行关闭 */
RegCloseKey( m_hKey );
m_hKey = NULL;
}
}
/*============================================================
* 函 数 名:SetHKEY
* 参 数:LPCTSTR [IN] : 根键值
* 功能描述:由传入的根键字符串设置当前操作的根键的值
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
BOOL CRegOperator::SetHKEY( LPCTSTR strKey )
{
assert( m_hKey );
assert( strKey );
/* 逐个进行比较 */
if( 0 == strcmp(strKey,"HKEY_CLASSES_ROOT") )
{
m_hKey = HKEY_CLASSES_ROOT;
return TRUE;
}
if( 0 == strcmp(strKey,"HKEY_CURRENT_USER") )
{
m_hKey = HKEY_CURRENT_USER;
return TRUE;
}
if( 0 == strcmp(strKey,"HKEY_LOCAL_MACHINE") )
{
m_hKey = HKEY_LOCAL_MACHINE;
return TRUE;
}
if( 0 == strcmp(strKey,"HKEY_USERS") )
{
m_hKey = HKEY_USERS;
return TRUE;
}
if( 0 == strcmp(strKey,"HKEY_PERFORMANCE_DATA") )
{
m_hKey = HKEY_PERFORMANCE_DATA;
return TRUE;
}
if( 0 == strcmp(strKey,"HKEY_PERFORMANCE_TEXT") )
{
m_hKey = HKEY_PERFORMANCE_TEXT;
return TRUE;
}
if( 0 == strcmp(strKey,"HKEY_PERFORMANCE_NLSTEXT") )
{
m_hKey = HKEY_PERFORMANCE_NLSTEXT;
return TRUE;
}
/* 对操作系统版本进行测试 */
#if(WINVER >= 0x0400)
if( 0 == strcmp(strKey,"HKEY_CURRENT_CONFIG") )
{
m_hKey = HKEY_CURRENT_CONFIG;
return TRUE;
}
if( 0 == strcmp(strKey,"HKEY_DYN_DATA") )
{
m_hKey = HKEY_DYN_DATA;
return TRUE;
}
#endif
return FALSE;
}
/*============================================================
* 函 数 名:OpenKey
* 参 数:LPCTSTR [IN] : 子键字符串
* 功能描述:通过传入子键的字符串打开注册表相应的位置
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
BOOL CRegOperator::OpenKey( LPCTSTR lpSubKey )
{
assert( m_hKey );
assert( lpSubKey );
HKEY hKey;
long lReturn = RegOpenKeyEx( m_hKey, lpSubKey, 0L, KEY_READ | KEY_WRITE | KEY_EXECUTE, &hKey );
if( ERROR_SUCCESS == lReturn )
{
/* 成功打开则将打开的句柄保存 */
m_hKey = hKey;
return TRUE;
}
/* 打开失败 */
return FALSE;
}
/*============================================================
* 函 数 名:CreateKey
* 参 数:LPCTSTR [IN] : 子键字符串
* 功能描述:通过传入子键的字符串打开(存在子键)或者创建(不存在子键)相应的子键
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
BOOL CRegOperator::CreateKey( LPCTSTR lpSubKey )
{
assert( m_hKey );
assert( lpSubKey );
HKEY hKey;
DWORD dw;
long lReturn = RegCreateKeyEx( m_hKey, lpSubKey, 0L, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dw);
if( ERROR_SUCCESS == lReturn )
{
/* 成功打开或者创建则将句柄保存 */
m_hKey = hKey;
return TRUE;
}
/* 打开或者创建失败 */
return FALSE;
}
/*============================================================
* 函 数 名:DeleteKey
* 参 数:LPCTSTR LPCTSTR [IN] : 根键值 子键值
* 功能描述:通过传入的根键和子键,将子键删除删除,不能包含子键
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
//BOOL CRegOperator::DeleteKey( HKEY hKey, LPCTSTR lpSubKey )
BOOL CRegOperator::DeleteKey( LPCTSTR lphKey, LPCTSTR lpSubKey )
{
assert( lphKey );
assert( lpSubKey );
assert( m_hKey );
SetHKEY( lphKey );
long lReturn = RegDeleteValue( m_hKey, lpSubKey );
if( ERROR_SUCCESS == lReturn )
{
/* 删除成功 */
return TRUE;
}
/* 删除失败 */
return FALSE;
}
/*============================================================
* 函 数 名:DeleteValue
* 参 数:LPCTSTR [IN] : 要删除键值的名称
* 功能描述:通过传入键值名称,删除对应的键值
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
BOOL CRegOperator::DeleteValue( LPCTSTR lpValueName )
{
assert( m_hKey );
assert( lpValueName );
long lReturn = RegDeleteValue( m_hKey, lpValueName );
if( ERROR_SUCCESS == lReturn )
{
/* 删除成功 */
return TRUE;
}
/* 删除失败 */
return FALSE;
}
/*============================================================
* 函 数 名:SaveKey
* 参 数:LPCTSTR [IN] : 待保存的文件名
* 功能描述:通过保存的文件名称,保存对应的子键
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
BOOL CRegOperator::SaveKey( LPCTSTR lpFileName )
{
assert( m_hKey );
assert( lpFileName );
long lReturn = RegSaveKey( m_hKey, lpFileName, NULL );
if( ERROR_SUCCESS == lReturn )
{
/* 保存成功 */
return TRUE;
}
/* 保存失败 */
return FALSE;
}
/*============================================================
* 函 数 名:RestoreKey
* 参 数:LPCTSTR [IN] : 待恢复的文件名
* 功能描述:通过文件名称,从其中导入注册表中
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
BOOL CRegOperator::RestoreKey( LPCTSTR lpFileName )
{
assert( m_hKey );
assert( lpFileName );
long lReturn = RegRestoreKey( m_hKey, lpFileName, 0);
if( ERROR_SUCCESS == lReturn )
{
/* 导入成功 */
return TRUE;
}
/* 导入失败 */
return FALSE;
}
/*============================================================
* 函 数 名:Read
* 参 数:LPCTSTR CString [IN] : 键值 读取值的类型
* 功能描述:将指定位置的CString类型的值读出
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
BOOL CRegOperator::Read( LPCTSTR lpValueName, CString* lpVal )
{
assert( m_hKey );
assert( lpValueName );
assert( lpVal );
DWORD dwType;
DWORD dwSize=200;
char szString[2048];
memset( szString, 0, 2048 * sizeof(char) );
long lReturn = RegQueryValueEx( m_hKey, lpValueName, NULL, &dwType, (BYTE *)szString, &dwSize );
if( ERROR_SUCCESS == lReturn )
{
/* 查询成功 */
*lpVal = szString;
return TRUE;
}
/* 查询失败 */
return FALSE;
}
/*============================================================
* 函 数 名:Read
* 参 数:LPCTSTR DWORD [IN] : 键值 读取值的类型
* 功能描述:将指定位置DWORD类型的值读出
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
BOOL CRegOperator::Read( LPCTSTR lpValueName, DWORD* pdwVal )
{
assert( m_hKey );
assert( lpValueName );
assert( pdwVal );
DWORD dwType;
DWORD dwSize=sizeof(DWORD);
DWORD dwDest;
long lReturn = RegQueryValueEx( m_hKey, lpValueName, NULL, &dwType, (BYTE *)&dwDest, &dwSize );
if( ERROR_SUCCESS == lReturn )
{
/* 查询成功 */
*pdwVal = dwDest;
return TRUE;
}
/* 查询失败 */
return FALSE;
}
/*============================================================
* 函 数 名:Read
* 参 数:LPCTSTR int [IN] : 键值 读取值的类型
* 功能描述:将指定位置int类型的值读出
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
BOOL CRegOperator::Read( LPCTSTR lpValueName, int* pnVal)
{
assert( m_hKey );
assert( lpValueName );
assert( pnVal );
DWORD dwType;
DWORD dwSize=sizeof(DWORD);
DWORD dwDest;
long lReturn = RegQueryValueEx( m_hKey, lpValueName, NULL, &dwType, (BYTE *)&dwDest, &dwSize );
if( ERROR_SUCCESS == lReturn )
{
/* 查询成功 */
*pnVal=(int)dwDest;
return TRUE;
}
return FALSE;
}
/*============================================================
* 函 数 名:Write
* 参 数:LPCTSTR LPCTSTR [IN] : 键值 写入值的类型
* 功能描述:将LPCTSTR类型的值写入指定位置
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
BOOL CRegOperator::Write( LPCTSTR lpValueName, LPCTSTR lpValue )
{
assert( m_hKey );
assert( lpValueName );
assert( lpValue );
long lReturn = RegSetValueEx( m_hKey, lpValueName, 0L, REG_SZ, (const BYTE *) lpValue, strlen(lpValue)+1 );
if( ERROR_SUCCESS == lReturn )
{
/* 成功写入 */
return TRUE;
}
/* 写入失败 */
return FALSE;
}
/*============================================================
* 函 数 名:Write
* 参 数:LPCTSTR DWORD [IN] : 键值 写入值的类型
* 功能描述:将DWORD类型的值写入指定位置
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
BOOL CRegOperator::Write( LPCTSTR lpSubKey, DWORD dwVal )
{
assert( m_hKey );
assert( lpSubKey );
long lReturn = RegSetValueEx( m_hKey, lpSubKey, 0L, REG_DWORD, (const BYTE *) &dwVal, sizeof(DWORD) );
if( ERROR_SUCCESS == lReturn )
{
/* 成功写入 */
return TRUE;
}
/* 写入失败 */
return FALSE;
}
/*============================================================
* 函 数 名:Write
* 参 数:LPCTSTR int [IN] : 键值 写入值的类型
* 功能描述:将整型数写入指定位置
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
BOOL CRegOperator::Write( LPCTSTR lpSubKey, int nVal )
{
assert( m_hKey );
assert( lpSubKey );
DWORD dwValue;
dwValue=(DWORD)nVal;
long lReturn = RegSetValueEx( m_hKey, lpSubKey, 0L, REG_DWORD, (const BYTE *) &dwValue, sizeof(DWORD) );
if( ERROR_SUCCESS == lReturn )
{
/* 成功写入 */
return TRUE;
}
/* 写入失败 */
return FALSE;
}
3、主文件(调用和测试)
/********************************************************************
* 文件名:main.cpp
* 文件描述:主文件,对封装的注册表操作类进行测试
* 创建人: RainLeaf, 2009年4月10日
* 版本号:1.0
* 修改记录:
*********************************************************************/
#include "RegOperator.h"
#include <iostream>
using namespace std;
void RegOperatorHelp();
int main( int argc, char *argv[])
{
BOOL bReturn;
/* 命令行没有参数则输出帮助提示信息 */
if( 1 == argc )
{
RegOperatorHelp();
return 0;
}
for( int i = 0; i < argc; i ++)
{
assert( argv[i] );
}
/*============================================================
* 分支功能:读取键值
* 参 数:-read RootKey SubKey ValueName
* 参数说明:命令 根键 子键 值的名字
* 抛出异常:操作失败,提示信息。
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
if( 0 == (strcmp(argv[1], "-read")) )
{
if( 5 == argc )
{
CRegOperator reg;
CString strPrint;
bReturn = reg.SetHKEY( argv[2] );/* 设置当前根键 */
if( FALSE == bReturn )
{
/* 根键设置失败 */
cout << "/nThe Key [" << argv[2] << "] set fail!" << endl;
return 0;
}
bReturn = reg.OpenKey( argv[3] );
if( FALSE == bReturn )
{
cout << "Open [" << argv[3] << "] fail!" << endl;
return 0;
}
bReturn = reg.Read( argv[4], &strPrint );
if( FALSE == bReturn )
{
cout << "Read [" << argv[4] << "] fail!" << endl;
return 0;
}
/* 读取成功 */
cout << "The [" << argv[4] << "] value is [" << strPrint << "]" << endl;
return 0;
}
else
{
cout << "Warning:Must have four param with /'-read/' option!" << endl;
return 0;
}
}
/*============================================================
* 分支功能:创建子键
* 参 数:-create RootKey SubKey
* 参数说明:命令 根键 子键
* 抛出异常:操作失败,提示信息。
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
if( 0 == (strcmp(argv[1], "-create")) )
{
if( 4 == argc )
{
CRegOperator reg;
bReturn = reg.SetHKEY(argv[2]);
if( FALSE == bReturn )
{
cout << "The Key [" << argv[2] << "] set fail!" << endl;
return 0;
}
bReturn = reg.CreateKey( argv[3] );
if( FALSE == bReturn )
{
cout << "Create the key [" << argv[3] << "] fail!" << endl;
return 0;
}
cout << "Create the key [" << argv[3] << "] successfully!" << endl;
return 0;
}
else
{
cout << "Warning:Must have three param with /'-create/' option!" << endl;
return 0;
}
}
/*============================================================
* 分支功能:创建键值
* 参 数:-write RootKey SubKey ValueName Value
* 参数说明:命令 根键 子键 值的名字 值
* 抛出异常:操作失败,提示信息。
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
if( 0 == (strcmp(argv[1], "-write")) )
{
if( 6 == argc )
{
CRegOperator reg;
bReturn = reg.SetHKEY(argv[2]);
if( FALSE == bReturn )
{
cout << "The Key [" << argv[2] << "] set fail!" << endl;
return 0;
}
bReturn = reg.OpenKey(argv[3]);
if( FALSE == bReturn )
{
cout << "Open [" << argv[3] << "] fail!" << endl;
return 0;
}
bReturn = reg.Write(argv[4], argv[5]);
if( FALSE == bReturn )
{
cout << "Write [" << argv[4] << "] fail!" << endl;
return 0;
}
cout << "Write [" << argv[5] << "] value into [" << argv[4] << "] successfully!" << endl;
return 0;
}
else
{
cout << "Warning:Must have five param with /'-write/' option!" << endl;
return 0;
}
}
/*============================================================
* 分支功能:删除键值
* 参 数:-delval RootKey SubKey ValueName
* 参数说明:命令 根键 子键 值的名字
* 抛出异常:操作失败,提示信息。
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
if( 0 == (strcmp(argv[1], "-delval")) )
{
if( 5 == argc )
{
CRegOperator reg;
bReturn = reg.SetHKEY(argv[2]);
if( FALSE == bReturn )
{
cout << "The Key [" << argv[2] << "] set fail!" << endl;
return 0;
}
bReturn = reg.OpenKey(argv[3]);
if( FALSE == bReturn )
{
cout << "Open [" << argv[3] << "] fail!" << endl;
return 0;
}
bReturn = reg.DeleteValue( argv[4] );
if( FALSE == bReturn )
{
cout << "Delete the [" << argv[4] << "] fail!" << endl;
return 0;
}
cout << "Delete [" << argv[4] << "] value from [" << argv[3] << "] successfully!" << endl;
return 0;
}
else
{
cout << "Warning:Must have four param with /'-delval/' option!" << endl;
return 0;
}
}
/*============================================================
* 分支功能:删除非空子键
* 参 数:-delkey RootKey SubKey
* 参数说明:命令 根键 子键
* 抛出异常:操作失败,提示信息。
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
if( 0 == (strcmp(argv[1], "-delkey")) )
{
if( 4 == argc )
{
CRegOperator reg;
bReturn = reg.DeleteKey( argv[2] ,argv[3] );
if( FALSE == bReturn )
{
cout << "Delete the [" << argv[3] << "] fail!" << endl;
return 0;
}
cout << "Delete [" << argv[3] << "] value from [" << argv[2] << "] successfully!" << endl;
return 0;
}
else
{
cout << "Warning:Must have three param with /'-delkey/' option!" << endl;
return 0;
}
}
/*============================================================
* 分支功能:备份注册表
* 参 数:-save RootKey SubKey Filename
* 参数说明:命令 根键 子键 文件名
* 抛出异常:操作失败,提示信息。
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
if( 0 == (strcmp(argv[1], "-save")) )
{
if( 5 == argc )
{
CRegOperator reg;
bReturn = reg.SetHKEY(argv[2]);
if( FALSE == bReturn )
{
cout << "The Key [" << argv[2] << "] set fail!" << endl;
return 0;
}
bReturn = reg.OpenKey(argv[3]);
if( FALSE == bReturn )
{
cout << "Open [" << argv[3] << "] fail!" << endl;
return 0;
}
bReturn = reg.SaveKey( argv[4] );
if( FALSE == bReturn )
{
cout << "Save the [" << argv[4] << "] fail!" << endl;
return 0;
}
cout << "Save [" << argv[4] << "] value from [" << argv[3] << "] successfully!" << endl;
return 0;
}
else
{
cout << "Warning:Must have four param with /'-save/' option!" << endl;
return 0;
}
}
/*============================================================
* 分支功能:恢复注册表
* 参 数:-restore RootKey Filename
* 参数说明:命令 根键 文件名
* 抛出异常:操作失败,提示信息。
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
if( 0 == (strcmp(argv[1], "-restore")) )
{
if(4 == argc )
{
CRegOperator reg;
bReturn = reg.SetHKEY(argv[2]);
if( FALSE == bReturn )
{
cout << "The Key [" << argv[2] << "] set fail!" << endl;
return 0;
}
bReturn = reg.RestoreKey( argv[3] );
if( FALSE == bReturn )
{
cout << "Restore from [" << argv[3] << "] fail!" << endl;
return 0;
}
cout << "Restore [" << argv[2] << "] value from file [" << argv[3] << "] successfully!" << endl;
return 0;
}
else
{
cout << "Warning:Must have three param with /'-restore/' option!" << endl;
return 0;
}
}
/*============================================================
* 分支功能:版主信息
* 参 数:-h [-help]
* 参数说明:命令
* 抛出异常:操作失败,提示信息。
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
if( ( 0 == strcmp( argv[1], "-h" ) ) || ( 0 == strcmp( argv[1], "-help" ) ) )
{
RegOperatorHelp();
return 0;
}
cout << "Error param! Please execute [" << argv[0] << "] or [" << argv[0] << " -h] to get help!" << endl;
return 0;
}
void RegOperatorHelp()
{
cout << " * 作 者:RainLeaf, 2009年4月10日" << endl;
cout << "/*============================================================" << endl;
cout << "* 分支功能:读取键值" << endl;
cout << "* 参 数:-read RootKey SubKey ValueName" << endl;
cout << "* 参数说明:命令 根键 子键 值的名字" << endl;
cout << "* 抛出异常:操作失败,提示信息。" << endl;
cout << "*============================================================*/" << endl;
cout << endl;
cout << "/*============================================================" << endl;
cout << "* 分支功能:创建子键" << endl;
cout << "* 参 数:-create RootKey SubKey" << endl;
cout << "* 参数说明:命令 根键 子键" << endl;
cout << "* 抛出异常:操作失败,提示信息。" << endl;
cout << "*============================================================*/" << endl;
cout << endl;
cout << "/*============================================================" << endl;
cout << "* 分支功能:创建键值" << endl;
cout << "* 参 数:-write RootKey SubKey ValueName Value" << endl;
cout << "* 参数说明:命令 根键 子键 值的名字 值" << endl;
cout << "* 抛出异常:操作失败,提示信息。" << endl;
cout << "*============================================================*/" << endl;
cout << endl;
cout << "/*============================================================" << endl;
cout << "* 分支功能:删除键值" << endl;
cout << "* 参 数:-delval RootKey SubKey ValueName" << endl;
cout << "* 参数说明:命令 根键 子键 值的名字" << endl;
cout << "* 抛出异常:操作失败,提示信息。" << endl;
cout << "*============================================================*/" << endl;
cout << endl;
cout << "/*============================================================" << endl;
cout << "* 分支功能:删除非空子键" << endl;
cout << "* 参 数:-delkey RootKey SubKey" << endl;
cout << "* 参数说明:命令 根键 子键" << endl;
cout << "* 抛出异常:操作失败,提示信息。" << endl;
cout << "*============================================================*/" << endl;
cout << endl;
cout << "/*============================================================" << endl;
cout << "* 分支功能:备份注册表" << endl;
cout << "* 参 数:-save RootKey SubKey Filename" << endl;
cout << "* 参数说明:命令 根键 子键 文件名" << endl;
cout << "* 抛出异常:操作失败,提示信息。" << endl;
cout << "*============================================================*/" << endl;
cout << endl;
cout << "/*============================================================" << endl;
cout << "* 分支功能:恢复注册表" << endl;
cout << "* 参 数:-restore RootKey Filename" << endl;
cout << "* 参数说明:命令 根键 文件名" << endl;
cout << "* 抛出异常:操作失败,提示信息。" << endl;
cout << "*============================================================*/" << endl;
cout << endl;
cout << "/*============================================================" << endl;
cout << "* 分支功能:版主信息" << endl;
cout << "* 参 数:-h [-help]" << endl;
cout << "* 参数说明:命令" << endl;
cout << "* 抛出异常:操作失败,提示信息。" << endl;
cout << "*============================================================*/" << endl;
cout << endl;
}
4、编译时注意事项
A、原文件编译方法:右击解决方案 -> Properties -> Configuration Properties -> General -> Project Defaults ->Use of MFC
select "Use MFC in a Shared DLL"
B、接口参数详细说明见代码注释。
说明:主函数只是对键的类型为REG_SZ的类型进行了操作,其它操作方法一样,均封装到了CRegOperator类中。
5、调用测试批处理文件
RegOperator.exe -read "HKEY_LOCAL_MACHINE" "SOFTWARE//Microsoft//Windows NT//CurrentVersion" "RegisteredOwner"
RegOperator.exe -write "HKEY_LOCAL_MACHINE" "SOFTWARE//Microsoft//Windows NT//CurrentVersion" "TestValueName" "TestValue"
RegOperator.exe -create "HKEY_LOCAL_MACHINE" "SOFTWARE//Microsoft/Windows NT/CurrentVersion//TestSubKey"
RegOperator.exe -delval "HKEY_LOCAL_MACHINE" "SOFTWARE//Microsoft//Windows NT//CurrentVersion" "Test"
RegOperator.exe -delkey "HKEY_LOCAL_MACHINE" "SOFTWARE//Microsoft//Windows NT//CurrentVersion//TestSubKey"
RegOperator.exe -save "HKEY_LOCAL_MACHINE" "SOFTWARE//Microsoft//Windows NT//CurrentVersion" "D://TestBak.reg"
RegOperator.exe -restore "HKEY_LOCAL_MACHINE" "D://TestBak.reg"
pause
/********************************************************************
* 文件名:RegOperator.h
* 文件描述:对注册表的常见操作进行封装
* 创建人: RainLeaf, 2009年4月10日
* 版本号:1.0
* 修改记录:
*********************************************************************/
#include <afx.h>
#include <assert.h>
#ifndef REGOP_H
#define REGOP_H
class CRegOperator
{
private:
HKEY m_hKey; /* 保存当前操作根键的句柄 */
public:
CRegOperator( HKEY hKey = HKEY_LOCAL_MACHINE ); /* 构造函数,默认参数为:HKEY_LOCAL_MACHINE */
BOOL SetHKEY( LPCTSTR strKey ); /* 设置当前操作的根键 */
BOOL OpenKey( LPCTSTR lpSubKey ); /* 读写的形式打开注册表 */
void Close(); /* 关闭键句柄 */
BOOL CreateKey( LPCTSTR lpSubKey ); /* 创建的形式打开注册表 */
BOOL DeleteKey( LPCTSTR lphKey, LPCTSTR lpSubKey ); /* 删除相应的子键(子键为空) */
BOOL DeleteValue( LPCTSTR lpValueName ); /* 删除子键处的相应的键值 */
BOOL SaveKey( LPCTSTR lpFileName ); /* 把当前键值保存到指定文件 */
BOOL RestoreKey( LPCTSTR lpFileName ); /* 从指定注册表文件中恢复 */
BOOL Read( LPCTSTR lpValueName, CString* lpVal ); /* 读出REG_SZ类型的值 */
BOOL Read( LPCTSTR lpValueName, DWORD* pdwVal ); /* 读出DWORD类型的值 */
BOOL Read( LPCTSTR lpValueName, int* pnVal ); /* 读出INT类型的值 */
BOOL Write( LPCTSTR lpSubKey, LPCTSTR lpVal ); /* 写入REG_SZ类型值 */
BOOL Write( LPCTSTR lpSubKey, DWORD dwVal ); /* 写入DWORD类型值 */
BOOL Write( LPCTSTR lpSubKey, int nVal ); /* 写入INT类型值 */
virtual ~CRegOperator();
};
#endif
2、实现文件
/********************************************************************
* 文件名:RegOperator.cpp
* 文件描述:对头文件中定义类的成员函数进行了实现
* 创建人: RainLeaf, 2009年4月10日
* 版本号:1.0
* 修改记录:
*********************************************************************/
#include "RegOperator.h"
/*============================================================
* 函 数 名:CRegOperator
* 参 数:HKEY [IN] : 默认是HKEY_LOCAL_MACHINE
* 功能描述:构造函数,初始化根键
* 返 回 值:无
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
CRegOperator::CRegOperator( HKEY hKey )
{
m_hKey = HKEY_LOCAL_MACHINE;
}
/*============================================================
* 函 数 名:~CRegOperator
* 参 数:NULL [IN]
* 功能描述:析构函数,关闭打开的注册表句柄
* 返 回 值:无
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
CRegOperator::~CRegOperator()
{
Close();
}
/*============================================================
* 函 数 名:Close
* 参 数:NULL [IN]
* 功能描述:关闭打开键的句柄
* 返 回 值:void
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
void CRegOperator::Close()
{
if( m_hKey )
{
/* 句柄非空进行关闭 */
RegCloseKey( m_hKey );
m_hKey = NULL;
}
}
/*============================================================
* 函 数 名:SetHKEY
* 参 数:LPCTSTR [IN] : 根键值
* 功能描述:由传入的根键字符串设置当前操作的根键的值
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
BOOL CRegOperator::SetHKEY( LPCTSTR strKey )
{
assert( m_hKey );
assert( strKey );
/* 逐个进行比较 */
if( 0 == strcmp(strKey,"HKEY_CLASSES_ROOT") )
{
m_hKey = HKEY_CLASSES_ROOT;
return TRUE;
}
if( 0 == strcmp(strKey,"HKEY_CURRENT_USER") )
{
m_hKey = HKEY_CURRENT_USER;
return TRUE;
}
if( 0 == strcmp(strKey,"HKEY_LOCAL_MACHINE") )
{
m_hKey = HKEY_LOCAL_MACHINE;
return TRUE;
}
if( 0 == strcmp(strKey,"HKEY_USERS") )
{
m_hKey = HKEY_USERS;
return TRUE;
}
if( 0 == strcmp(strKey,"HKEY_PERFORMANCE_DATA") )
{
m_hKey = HKEY_PERFORMANCE_DATA;
return TRUE;
}
if( 0 == strcmp(strKey,"HKEY_PERFORMANCE_TEXT") )
{
m_hKey = HKEY_PERFORMANCE_TEXT;
return TRUE;
}
if( 0 == strcmp(strKey,"HKEY_PERFORMANCE_NLSTEXT") )
{
m_hKey = HKEY_PERFORMANCE_NLSTEXT;
return TRUE;
}
/* 对操作系统版本进行测试 */
#if(WINVER >= 0x0400)
if( 0 == strcmp(strKey,"HKEY_CURRENT_CONFIG") )
{
m_hKey = HKEY_CURRENT_CONFIG;
return TRUE;
}
if( 0 == strcmp(strKey,"HKEY_DYN_DATA") )
{
m_hKey = HKEY_DYN_DATA;
return TRUE;
}
#endif
return FALSE;
}
/*============================================================
* 函 数 名:OpenKey
* 参 数:LPCTSTR [IN] : 子键字符串
* 功能描述:通过传入子键的字符串打开注册表相应的位置
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
BOOL CRegOperator::OpenKey( LPCTSTR lpSubKey )
{
assert( m_hKey );
assert( lpSubKey );
HKEY hKey;
long lReturn = RegOpenKeyEx( m_hKey, lpSubKey, 0L, KEY_READ | KEY_WRITE | KEY_EXECUTE, &hKey );
if( ERROR_SUCCESS == lReturn )
{
/* 成功打开则将打开的句柄保存 */
m_hKey = hKey;
return TRUE;
}
/* 打开失败 */
return FALSE;
}
/*============================================================
* 函 数 名:CreateKey
* 参 数:LPCTSTR [IN] : 子键字符串
* 功能描述:通过传入子键的字符串打开(存在子键)或者创建(不存在子键)相应的子键
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
BOOL CRegOperator::CreateKey( LPCTSTR lpSubKey )
{
assert( m_hKey );
assert( lpSubKey );
HKEY hKey;
DWORD dw;
long lReturn = RegCreateKeyEx( m_hKey, lpSubKey, 0L, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dw);
if( ERROR_SUCCESS == lReturn )
{
/* 成功打开或者创建则将句柄保存 */
m_hKey = hKey;
return TRUE;
}
/* 打开或者创建失败 */
return FALSE;
}
/*============================================================
* 函 数 名:DeleteKey
* 参 数:LPCTSTR LPCTSTR [IN] : 根键值 子键值
* 功能描述:通过传入的根键和子键,将子键删除删除,不能包含子键
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
//BOOL CRegOperator::DeleteKey( HKEY hKey, LPCTSTR lpSubKey )
BOOL CRegOperator::DeleteKey( LPCTSTR lphKey, LPCTSTR lpSubKey )
{
assert( lphKey );
assert( lpSubKey );
assert( m_hKey );
SetHKEY( lphKey );
long lReturn = RegDeleteValue( m_hKey, lpSubKey );
if( ERROR_SUCCESS == lReturn )
{
/* 删除成功 */
return TRUE;
}
/* 删除失败 */
return FALSE;
}
/*============================================================
* 函 数 名:DeleteValue
* 参 数:LPCTSTR [IN] : 要删除键值的名称
* 功能描述:通过传入键值名称,删除对应的键值
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
BOOL CRegOperator::DeleteValue( LPCTSTR lpValueName )
{
assert( m_hKey );
assert( lpValueName );
long lReturn = RegDeleteValue( m_hKey, lpValueName );
if( ERROR_SUCCESS == lReturn )
{
/* 删除成功 */
return TRUE;
}
/* 删除失败 */
return FALSE;
}
/*============================================================
* 函 数 名:SaveKey
* 参 数:LPCTSTR [IN] : 待保存的文件名
* 功能描述:通过保存的文件名称,保存对应的子键
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
BOOL CRegOperator::SaveKey( LPCTSTR lpFileName )
{
assert( m_hKey );
assert( lpFileName );
long lReturn = RegSaveKey( m_hKey, lpFileName, NULL );
if( ERROR_SUCCESS == lReturn )
{
/* 保存成功 */
return TRUE;
}
/* 保存失败 */
return FALSE;
}
/*============================================================
* 函 数 名:RestoreKey
* 参 数:LPCTSTR [IN] : 待恢复的文件名
* 功能描述:通过文件名称,从其中导入注册表中
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
BOOL CRegOperator::RestoreKey( LPCTSTR lpFileName )
{
assert( m_hKey );
assert( lpFileName );
long lReturn = RegRestoreKey( m_hKey, lpFileName, 0);
if( ERROR_SUCCESS == lReturn )
{
/* 导入成功 */
return TRUE;
}
/* 导入失败 */
return FALSE;
}
/*============================================================
* 函 数 名:Read
* 参 数:LPCTSTR CString [IN] : 键值 读取值的类型
* 功能描述:将指定位置的CString类型的值读出
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
BOOL CRegOperator::Read( LPCTSTR lpValueName, CString* lpVal )
{
assert( m_hKey );
assert( lpValueName );
assert( lpVal );
DWORD dwType;
DWORD dwSize=200;
char szString[2048];
memset( szString, 0, 2048 * sizeof(char) );
long lReturn = RegQueryValueEx( m_hKey, lpValueName, NULL, &dwType, (BYTE *)szString, &dwSize );
if( ERROR_SUCCESS == lReturn )
{
/* 查询成功 */
*lpVal = szString;
return TRUE;
}
/* 查询失败 */
return FALSE;
}
/*============================================================
* 函 数 名:Read
* 参 数:LPCTSTR DWORD [IN] : 键值 读取值的类型
* 功能描述:将指定位置DWORD类型的值读出
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
BOOL CRegOperator::Read( LPCTSTR lpValueName, DWORD* pdwVal )
{
assert( m_hKey );
assert( lpValueName );
assert( pdwVal );
DWORD dwType;
DWORD dwSize=sizeof(DWORD);
DWORD dwDest;
long lReturn = RegQueryValueEx( m_hKey, lpValueName, NULL, &dwType, (BYTE *)&dwDest, &dwSize );
if( ERROR_SUCCESS == lReturn )
{
/* 查询成功 */
*pdwVal = dwDest;
return TRUE;
}
/* 查询失败 */
return FALSE;
}
/*============================================================
* 函 数 名:Read
* 参 数:LPCTSTR int [IN] : 键值 读取值的类型
* 功能描述:将指定位置int类型的值读出
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
BOOL CRegOperator::Read( LPCTSTR lpValueName, int* pnVal)
{
assert( m_hKey );
assert( lpValueName );
assert( pnVal );
DWORD dwType;
DWORD dwSize=sizeof(DWORD);
DWORD dwDest;
long lReturn = RegQueryValueEx( m_hKey, lpValueName, NULL, &dwType, (BYTE *)&dwDest, &dwSize );
if( ERROR_SUCCESS == lReturn )
{
/* 查询成功 */
*pnVal=(int)dwDest;
return TRUE;
}
return FALSE;
}
/*============================================================
* 函 数 名:Write
* 参 数:LPCTSTR LPCTSTR [IN] : 键值 写入值的类型
* 功能描述:将LPCTSTR类型的值写入指定位置
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
BOOL CRegOperator::Write( LPCTSTR lpValueName, LPCTSTR lpValue )
{
assert( m_hKey );
assert( lpValueName );
assert( lpValue );
long lReturn = RegSetValueEx( m_hKey, lpValueName, 0L, REG_SZ, (const BYTE *) lpValue, strlen(lpValue)+1 );
if( ERROR_SUCCESS == lReturn )
{
/* 成功写入 */
return TRUE;
}
/* 写入失败 */
return FALSE;
}
/*============================================================
* 函 数 名:Write
* 参 数:LPCTSTR DWORD [IN] : 键值 写入值的类型
* 功能描述:将DWORD类型的值写入指定位置
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
BOOL CRegOperator::Write( LPCTSTR lpSubKey, DWORD dwVal )
{
assert( m_hKey );
assert( lpSubKey );
long lReturn = RegSetValueEx( m_hKey, lpSubKey, 0L, REG_DWORD, (const BYTE *) &dwVal, sizeof(DWORD) );
if( ERROR_SUCCESS == lReturn )
{
/* 成功写入 */
return TRUE;
}
/* 写入失败 */
return FALSE;
}
/*============================================================
* 函 数 名:Write
* 参 数:LPCTSTR int [IN] : 键值 写入值的类型
* 功能描述:将整型数写入指定位置
* 返 回 值:BOOL
* 抛出异常:
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
BOOL CRegOperator::Write( LPCTSTR lpSubKey, int nVal )
{
assert( m_hKey );
assert( lpSubKey );
DWORD dwValue;
dwValue=(DWORD)nVal;
long lReturn = RegSetValueEx( m_hKey, lpSubKey, 0L, REG_DWORD, (const BYTE *) &dwValue, sizeof(DWORD) );
if( ERROR_SUCCESS == lReturn )
{
/* 成功写入 */
return TRUE;
}
/* 写入失败 */
return FALSE;
}
3、主文件(调用和测试)
/********************************************************************
* 文件名:main.cpp
* 文件描述:主文件,对封装的注册表操作类进行测试
* 创建人: RainLeaf, 2009年4月10日
* 版本号:1.0
* 修改记录:
*********************************************************************/
#include "RegOperator.h"
#include <iostream>
using namespace std;
void RegOperatorHelp();
int main( int argc, char *argv[])
{
BOOL bReturn;
/* 命令行没有参数则输出帮助提示信息 */
if( 1 == argc )
{
RegOperatorHelp();
return 0;
}
for( int i = 0; i < argc; i ++)
{
assert( argv[i] );
}
/*============================================================
* 分支功能:读取键值
* 参 数:-read RootKey SubKey ValueName
* 参数说明:命令 根键 子键 值的名字
* 抛出异常:操作失败,提示信息。
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
if( 0 == (strcmp(argv[1], "-read")) )
{
if( 5 == argc )
{
CRegOperator reg;
CString strPrint;
bReturn = reg.SetHKEY( argv[2] );/* 设置当前根键 */
if( FALSE == bReturn )
{
/* 根键设置失败 */
cout << "/nThe Key [" << argv[2] << "] set fail!" << endl;
return 0;
}
bReturn = reg.OpenKey( argv[3] );
if( FALSE == bReturn )
{
cout << "Open [" << argv[3] << "] fail!" << endl;
return 0;
}
bReturn = reg.Read( argv[4], &strPrint );
if( FALSE == bReturn )
{
cout << "Read [" << argv[4] << "] fail!" << endl;
return 0;
}
/* 读取成功 */
cout << "The [" << argv[4] << "] value is [" << strPrint << "]" << endl;
return 0;
}
else
{
cout << "Warning:Must have four param with /'-read/' option!" << endl;
return 0;
}
}
/*============================================================
* 分支功能:创建子键
* 参 数:-create RootKey SubKey
* 参数说明:命令 根键 子键
* 抛出异常:操作失败,提示信息。
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
if( 0 == (strcmp(argv[1], "-create")) )
{
if( 4 == argc )
{
CRegOperator reg;
bReturn = reg.SetHKEY(argv[2]);
if( FALSE == bReturn )
{
cout << "The Key [" << argv[2] << "] set fail!" << endl;
return 0;
}
bReturn = reg.CreateKey( argv[3] );
if( FALSE == bReturn )
{
cout << "Create the key [" << argv[3] << "] fail!" << endl;
return 0;
}
cout << "Create the key [" << argv[3] << "] successfully!" << endl;
return 0;
}
else
{
cout << "Warning:Must have three param with /'-create/' option!" << endl;
return 0;
}
}
/*============================================================
* 分支功能:创建键值
* 参 数:-write RootKey SubKey ValueName Value
* 参数说明:命令 根键 子键 值的名字 值
* 抛出异常:操作失败,提示信息。
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
if( 0 == (strcmp(argv[1], "-write")) )
{
if( 6 == argc )
{
CRegOperator reg;
bReturn = reg.SetHKEY(argv[2]);
if( FALSE == bReturn )
{
cout << "The Key [" << argv[2] << "] set fail!" << endl;
return 0;
}
bReturn = reg.OpenKey(argv[3]);
if( FALSE == bReturn )
{
cout << "Open [" << argv[3] << "] fail!" << endl;
return 0;
}
bReturn = reg.Write(argv[4], argv[5]);
if( FALSE == bReturn )
{
cout << "Write [" << argv[4] << "] fail!" << endl;
return 0;
}
cout << "Write [" << argv[5] << "] value into [" << argv[4] << "] successfully!" << endl;
return 0;
}
else
{
cout << "Warning:Must have five param with /'-write/' option!" << endl;
return 0;
}
}
/*============================================================
* 分支功能:删除键值
* 参 数:-delval RootKey SubKey ValueName
* 参数说明:命令 根键 子键 值的名字
* 抛出异常:操作失败,提示信息。
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
if( 0 == (strcmp(argv[1], "-delval")) )
{
if( 5 == argc )
{
CRegOperator reg;
bReturn = reg.SetHKEY(argv[2]);
if( FALSE == bReturn )
{
cout << "The Key [" << argv[2] << "] set fail!" << endl;
return 0;
}
bReturn = reg.OpenKey(argv[3]);
if( FALSE == bReturn )
{
cout << "Open [" << argv[3] << "] fail!" << endl;
return 0;
}
bReturn = reg.DeleteValue( argv[4] );
if( FALSE == bReturn )
{
cout << "Delete the [" << argv[4] << "] fail!" << endl;
return 0;
}
cout << "Delete [" << argv[4] << "] value from [" << argv[3] << "] successfully!" << endl;
return 0;
}
else
{
cout << "Warning:Must have four param with /'-delval/' option!" << endl;
return 0;
}
}
/*============================================================
* 分支功能:删除非空子键
* 参 数:-delkey RootKey SubKey
* 参数说明:命令 根键 子键
* 抛出异常:操作失败,提示信息。
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
if( 0 == (strcmp(argv[1], "-delkey")) )
{
if( 4 == argc )
{
CRegOperator reg;
bReturn = reg.DeleteKey( argv[2] ,argv[3] );
if( FALSE == bReturn )
{
cout << "Delete the [" << argv[3] << "] fail!" << endl;
return 0;
}
cout << "Delete [" << argv[3] << "] value from [" << argv[2] << "] successfully!" << endl;
return 0;
}
else
{
cout << "Warning:Must have three param with /'-delkey/' option!" << endl;
return 0;
}
}
/*============================================================
* 分支功能:备份注册表
* 参 数:-save RootKey SubKey Filename
* 参数说明:命令 根键 子键 文件名
* 抛出异常:操作失败,提示信息。
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
if( 0 == (strcmp(argv[1], "-save")) )
{
if( 5 == argc )
{
CRegOperator reg;
bReturn = reg.SetHKEY(argv[2]);
if( FALSE == bReturn )
{
cout << "The Key [" << argv[2] << "] set fail!" << endl;
return 0;
}
bReturn = reg.OpenKey(argv[3]);
if( FALSE == bReturn )
{
cout << "Open [" << argv[3] << "] fail!" << endl;
return 0;
}
bReturn = reg.SaveKey( argv[4] );
if( FALSE == bReturn )
{
cout << "Save the [" << argv[4] << "] fail!" << endl;
return 0;
}
cout << "Save [" << argv[4] << "] value from [" << argv[3] << "] successfully!" << endl;
return 0;
}
else
{
cout << "Warning:Must have four param with /'-save/' option!" << endl;
return 0;
}
}
/*============================================================
* 分支功能:恢复注册表
* 参 数:-restore RootKey Filename
* 参数说明:命令 根键 文件名
* 抛出异常:操作失败,提示信息。
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
if( 0 == (strcmp(argv[1], "-restore")) )
{
if(4 == argc )
{
CRegOperator reg;
bReturn = reg.SetHKEY(argv[2]);
if( FALSE == bReturn )
{
cout << "The Key [" << argv[2] << "] set fail!" << endl;
return 0;
}
bReturn = reg.RestoreKey( argv[3] );
if( FALSE == bReturn )
{
cout << "Restore from [" << argv[3] << "] fail!" << endl;
return 0;
}
cout << "Restore [" << argv[2] << "] value from file [" << argv[3] << "] successfully!" << endl;
return 0;
}
else
{
cout << "Warning:Must have three param with /'-restore/' option!" << endl;
return 0;
}
}
/*============================================================
* 分支功能:版主信息
* 参 数:-h [-help]
* 参数说明:命令
* 抛出异常:操作失败,提示信息。
* 作 者:RainLeaf, 2009年4月10日
*============================================================*/
if( ( 0 == strcmp( argv[1], "-h" ) ) || ( 0 == strcmp( argv[1], "-help" ) ) )
{
RegOperatorHelp();
return 0;
}
cout << "Error param! Please execute [" << argv[0] << "] or [" << argv[0] << " -h] to get help!" << endl;
return 0;
}
void RegOperatorHelp()
{
cout << " * 作 者:RainLeaf, 2009年4月10日" << endl;
cout << "/*============================================================" << endl;
cout << "* 分支功能:读取键值" << endl;
cout << "* 参 数:-read RootKey SubKey ValueName" << endl;
cout << "* 参数说明:命令 根键 子键 值的名字" << endl;
cout << "* 抛出异常:操作失败,提示信息。" << endl;
cout << "*============================================================*/" << endl;
cout << endl;
cout << "/*============================================================" << endl;
cout << "* 分支功能:创建子键" << endl;
cout << "* 参 数:-create RootKey SubKey" << endl;
cout << "* 参数说明:命令 根键 子键" << endl;
cout << "* 抛出异常:操作失败,提示信息。" << endl;
cout << "*============================================================*/" << endl;
cout << endl;
cout << "/*============================================================" << endl;
cout << "* 分支功能:创建键值" << endl;
cout << "* 参 数:-write RootKey SubKey ValueName Value" << endl;
cout << "* 参数说明:命令 根键 子键 值的名字 值" << endl;
cout << "* 抛出异常:操作失败,提示信息。" << endl;
cout << "*============================================================*/" << endl;
cout << endl;
cout << "/*============================================================" << endl;
cout << "* 分支功能:删除键值" << endl;
cout << "* 参 数:-delval RootKey SubKey ValueName" << endl;
cout << "* 参数说明:命令 根键 子键 值的名字" << endl;
cout << "* 抛出异常:操作失败,提示信息。" << endl;
cout << "*============================================================*/" << endl;
cout << endl;
cout << "/*============================================================" << endl;
cout << "* 分支功能:删除非空子键" << endl;
cout << "* 参 数:-delkey RootKey SubKey" << endl;
cout << "* 参数说明:命令 根键 子键" << endl;
cout << "* 抛出异常:操作失败,提示信息。" << endl;
cout << "*============================================================*/" << endl;
cout << endl;
cout << "/*============================================================" << endl;
cout << "* 分支功能:备份注册表" << endl;
cout << "* 参 数:-save RootKey SubKey Filename" << endl;
cout << "* 参数说明:命令 根键 子键 文件名" << endl;
cout << "* 抛出异常:操作失败,提示信息。" << endl;
cout << "*============================================================*/" << endl;
cout << endl;
cout << "/*============================================================" << endl;
cout << "* 分支功能:恢复注册表" << endl;
cout << "* 参 数:-restore RootKey Filename" << endl;
cout << "* 参数说明:命令 根键 文件名" << endl;
cout << "* 抛出异常:操作失败,提示信息。" << endl;
cout << "*============================================================*/" << endl;
cout << endl;
cout << "/*============================================================" << endl;
cout << "* 分支功能:版主信息" << endl;
cout << "* 参 数:-h [-help]" << endl;
cout << "* 参数说明:命令" << endl;
cout << "* 抛出异常:操作失败,提示信息。" << endl;
cout << "*============================================================*/" << endl;
cout << endl;
}
4、编译时注意事项
A、原文件编译方法:右击解决方案 -> Properties -> Configuration Properties -> General -> Project Defaults ->Use of MFC
select "Use MFC in a Shared DLL"
B、接口参数详细说明见代码注释。
说明:主函数只是对键的类型为REG_SZ的类型进行了操作,其它操作方法一样,均封装到了CRegOperator类中。
5、调用测试批处理文件
RegOperator.exe -read "HKEY_LOCAL_MACHINE" "SOFTWARE//Microsoft//Windows NT//CurrentVersion" "RegisteredOwner"
RegOperator.exe -write "HKEY_LOCAL_MACHINE" "SOFTWARE//Microsoft//Windows NT//CurrentVersion" "TestValueName" "TestValue"
RegOperator.exe -create "HKEY_LOCAL_MACHINE" "SOFTWARE//Microsoft/Windows NT/CurrentVersion//TestSubKey"
RegOperator.exe -delval "HKEY_LOCAL_MACHINE" "SOFTWARE//Microsoft//Windows NT//CurrentVersion" "Test"
RegOperator.exe -delkey "HKEY_LOCAL_MACHINE" "SOFTWARE//Microsoft//Windows NT//CurrentVersion//TestSubKey"
RegOperator.exe -save "HKEY_LOCAL_MACHINE" "SOFTWARE//Microsoft//Windows NT//CurrentVersion" "D://TestBak.reg"
RegOperator.exe -restore "HKEY_LOCAL_MACHINE" "D://TestBak.reg"
pause
本文介绍了一个用于封装Windows注册表操作的C++类CRegOperator,包括读写注册表、创建和删除键值等功能,并提供了主文件进行测试。
166

被折叠的 条评论
为什么被折叠?



