.NET 里面操作注册表使用RegistryKey类,我用C++也实现一个RegistryKey类,发现确实很好用,不敢独享,让大家也看看也体验一下。
/********************************************************************
(c) 2003-2005 C2217 Studio
Module: Registry.h
Author: Yangjun D.
Created: 9/5/2005 13:24
Purpose: Wrapper registry operation like RegistryKey class in .NET
History:
*********************************************************************/
#pragma once
#include <string>
#include <vector>
using namespace std;
namespace C2217
{
namespace Win32
{
class RegistryKey
{
public:
RegistryKey(HKEY key=NULL, string name= "");
virtual ~RegistryKey(void);
void Close();
RegistryKey OpenSubKey(const string &keyName, bool writable= false) const ;
RegistryKey CreateSubKey(const string &keyName) const ;
void DelSubKey(const string &keyName) const ;
void SetValue(const string &valueName, const string &newStrValue) const;
void SetValue(const string &valueName, int newIntValue) const ;
void DelValue(const string &valueName) const ;
string GetStrValue(const string &keyName) const ;
int GetIntValue(const string &keyName) const ;
const string &get_name() const { return m_name; }
void Backup(const string &file) const ;
void Restore(const string &file) const;
void GetSubKeyNames(vector<string> & keyNames) const ;
void GetValueNames(vector<string> &valueNames) const ;
void Flush() const;
private:
HKEY m_key;
string m_name;
};
class Registry
{
public:
const static RegistryKey LocalMachine;
const static RegistryKey ClassesRoot;
const static RegistryKey CurrentConfig;
const static RegistryKey CurrentUser;
const static RegistryKey DynData;
const static RegistryKey PerformanceData;
const static RegistryKey Users;
};
}
}
/********************************************************************
(c) 2003-2005 C2217 Studio
Module: registry.cpp
Author: Yangjun D.
Created: 9/5/2005 13:24
Purpose: Implement the RegistryKey class
History:
*********************************************************************/
#include "StdAfx.h"
#include ".\registry.h"
#include "winexception.h"
#include "winex.h"
using namespace C2217::StdLib;
namespace C2217
{
namespace Win32
{
//Class RegistryKey
RegistryKey::RegistryKey(HKEY key/*=NULL*/, string name/*= ""*/)
{
m_key = key;
m_name = name;
}
RegistryKey::~RegistryKey(void)
{
//Close();
}
RegistryKey RegistryKey::OpenSubKey(const string &keyName,bool writable/*= false*/) const
{
DWORD dwAccess = KEY_READ;
if(writable)
{
dwAccess |=KEY_WRITE;
}
RegistryKey openKey;
int result =::RegOpenKeyEx(m_key,keyName.c_str(),0,dwAccess, &openKey.m_key);
if(result != ERROR_SUCCESS)
{
THROW_EX_CODE(result);
}
if (!m_name.empty())
{
openKey.m_name = keyName;
}
else
{
openKey.m_name = m_name +"\\" + keyName;
}
return openKey;
}
RegistryKey RegistryKey::CreateSubKey(const string &keyName) const
{
RegistryKey newKey;
int result = ::RegCreateKeyEx(
m_key,
keyName.c_str(),
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&newKey.m_key,
NULL
);
if(result != ERROR_SUCCESS ) {
THROW_EX_CODE(result);
}
if (!m_name.empty()) {
newKey.m_name = keyName;
}
else {
newKey.m_name = m_name +"\\" + keyName;
}
return newKey;
}
void RegistryKey::Close()
{
if(NULL!=m_key)
{
::RegCloseKey(m_key);
}
}
LPBYTE STRING_To_LPBYTE(string str)
{
LPBYTE lpb=new BYTE[str.length()+1];
for(size_t i=0;i<str.length();i++)
lpb[i]=str[i];
lpb[str.length()]=0;
return lpb;
}
void RegistryKey::SetValue(const string &valueName,const string &newStrValue) const
{
LPBYTE lpData = STRING_To_LPBYTE(newStrValue);
int result = ::RegSetValueEx(m_key, valueName.c_str(),0,REG_SZ,
lpData,
(int)newStrValue.length()+1);
delete[] lpData;
if(result != ERROR_SUCCESS)
{
THROW_EX_CODE(result);
}
}
void RegistryKey::SetValue( const string &valueName,int newIntValue) const
{
int result = ::RegSetValueEx(m_key, valueName.c_str() ,
0L,REG_DWORD,(const BYTE *) &newIntValue,
sizeof(int));
if(result != ERROR_SUCCESS)
{
THROW_EX_CODE(result);
}
}
string RegistryKey::GetStrValue(const string &valueName) const
{
DWORD dwType;
static TCHAR szValue[2048] = {0};
DWORD dwSize=sizeof(szValue);
int result = RegQueryValueEx(m_key, valueName.c_str(),
NULL,&dwType,(BYTE *)&szValue,&dwSize);
if(result != ERROR_SUCCESS)
{
THROW_EX_CODE(result);
}
return szValue;
}
int RegistryKey::GetIntValue(const string &valueName) const
{
DWORD dwType;
DWORD dwSize=sizeof(DWORD);
DWORD dwDest;
int result = RegQueryValueEx(m_key, valueName.c_str(),
NULL,&dwType,(BYTE *)&dwDest,&dwSize);
if(result != ERROR_SUCCESS)
{
THROW_EX_CODE(result);
}
return (int)dwDest;
}
void RegistryKey::DelSubKey(const string &keyName) const
{
int result = ::RegDeleteKey(m_key,keyName.c_str());
if(result != ERROR_SUCCESS)
{
THROW_EX_CODE(result);
}
}
void RegistryKey::DelValue(const string &valueName) const
{
int result = ::RegDeleteValue(m_key,valueName.c_str());
if(result != ERROR_SUCCESS)
{
THROW_EX_CODE(result);
}
}
void RegistryKey::Backup(const string &file) const
{
int result = RegSaveKey(m_key, file.c_str(),NULL);
if(result != ERROR_SUCCESS)
{
THROW_EX_CODE(result);
}
}
void RegistryKey::Restore(const string &file) const
{
int result = ::RegRestoreKey(m_key, file.c_str() ,REG_WHOLE_HIVE_VOLATILE);
if(result != ERROR_SUCCESS)
{
THROW_EX_CODE(result);
}
}
const static int ITEM_MAX_COUNT = 1024;
void RegistryKey::GetSubKeyNames(vector<string> & keyNames) const
{
int result=0;
TCHAR keyName [256] ={0};
DWORD nameSize;
for(int i=0; i< ITEM_MAX_COUNT; i++)
{
nameSize= sizeof(keyName);
memset(keyName, 0 , nameSize);
result = ::RegEnumKeyEx( m_key, i, keyName, &nameSize, NULL,NULL,NULL,NULL);
if(ERROR_SUCCESS == result)
{
keyNames.push_back(keyName);
}
else if(ERROR_NO_MORE_ITEMS == result)
{
break;
}
else
{
THROW_EX_CODE(result);
}
}
}
void RegistryKey::Flush() const
{
int result = ::RegFlushKey(m_key);
if(result != ERROR_SUCCESS)
{
THROW_EX_CODE(result);
}
}
const RegistryKey Registry::LocalMachine(HKEY_LOCAL_MACHINE);
const RegistryKey Registry::ClassesRoot(HKEY_CLASSES_ROOT);
const RegistryKey Registry::CurrentConfig(HKEY_CURRENT_CONFIG);
const RegistryKey Registry::CurrentUser(HKEY_CURRENT_USER);
const RegistryKey Registry::DynData(HKEY_DYN_DATA);
const RegistryKey Registry::PerformanceData(HKEY_PERFORMANCE_DATA);
const RegistryKey Registry::Users(HKEY_USERS);
}
}
操作方法跟.NET里面的RegistryKey 几乎相同,就不多说了。
posted on 2005-09-21 09:20
天下无双 阅读(1245)
评论(0) 编辑 收藏 引用