一个实用的注册表类
作者:
在日常编程中,注册表的使用非常广泛,为此,笔者仔细研究了关于注册表的API函数(全部在WINREG.H中定义),编写了这个注册表类class
CRegistry,供大家参考。 主要函数介绍如下:
1、 CreateKey(LPCTSTR lpSubKey)
创建指定键。
CreateKey("Staff\0");
2、 Open(LPCTSTR lpSubKey)
打开指定键。
Open( "Software\\Staff\0");
3、 Read(LPCTSTR lpValueName, Cstring* lpVal);
Read(LPCTSTR lpValueName, DWORD* pdwVal);
Read(LPCTSTR lpValueName, int* pnVal);
读指定键值内容。
4、Write(LPCTSTR lpSubKey, LPCTSTR lpVal);
Write(LPCTSTR lpSubKey, DWORD dwVal);
Write(LPCTSTR lpSubKey, int nVal);
将内容写入指定键。
5、 DeleteKey(HKEY hKey, LPCTSTR lpSubKey);
删除指定键。
DeleteKey(HKEY_LOCAL_MACHINE,"staff\0");
DeleteKey(m_hKey,"name\0");
6、 DeleteValue(LPCTSTR lpValueName);
从指定键删除指定的值。
DeleteKey("name");
7、SaveKey(LPCTSTR lpFileName);
将指定键、子键及值存入文件。
SaveKey("save.reg");
8、 RestoreKey(LPCTSTR lpFileName);
从指定文件读取注册表信息。
RestoreKey("save.reg");
9、 Close(); 释放指定键的句柄。
--------------------next---------------------
![](../image/doc2.gif)
其实这个类完全可以不用继承自CObject,把用到的CString 全部替换为LPCTSTR,能用的地方就多了。 ( ldehai 发表于 2007-6-29 14:53:00)
![](../image/doc2.gif)
关于“guanghezhou:注册信息添加到注册表中后,重新启动,注册信息都没有了,但是注消不会丢失是怎么回事?"的解释:
long lReturn=RegCreateKeyEx(m_hKey,lpSubKey,0L,NULL,REG_OPTION_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,&dw);
这里REG_OPTION_VOLATILE 表示重启后无效
正式用的话要改成REG_OPTION_NON_VOLATILE,表示重启后依然有效。 ( ldehai 发表于 2007-6-29 14:49:00)
![](../image/doc2.gif)
void CRegistry::Close()
{
if(m_hKey)
{
RegCloseKey(m_hKey);
m_hKey=NULL;
m_strCurrentKey.Empty();
m_strRegRoot.Empty();
}
}
BOOL CRegistry::EnumKey(CStringList &strList)
{
ASSERT(m_hKey);
char buf[MAX_PATH] = { 0 };
DWORD dwIndex = 0;
CString strListNote;
long lReturn = RegEnumKey (m_hKey,dwIndex,buf,MAX_PATH);
while (lReturn == ERROR_SUCCESS)
{
strListNote = buf;
strList.AddTail(strListNote);
lReturn = RegEnumKey(m_hKey,++dwIndex,buf,MAX_PATH);
}
return TRUE;
}
// that is all. ( 24431188 发表于 2006-3-28 16:01:00)
![](../image/doc2.gif)
BOOL CRegistry::Open(LPCTSTR lpSubKey)
{
............
if(lReturn==ERROR_SUCCESS)
{
m_hKey=hKey;
m_strCurrentKey += lpSubKey;
m_strCurrentKey.TrimRight("\\");
m_strCurrentKey += "\\";
return TRUE;
}
}
BOOL CRegistry::UpLevelKey()
{
ASSERT(m_hKey);
CString strRoot = m_strRegRoot;
CString strCurrentKey = m_strCurrentKey;
Close();
if( strRoot == "HKEY_LOCAL_MACHINE")
m_hKey = HKEY_LOCAL_MACHINE;
//if strRoot == "HKEY_CLASSES_ROOT"
//m_hKey = HKEY_CLASSES_ROOT; and so on.
strCurrentKey.TrimRight("\\");
CString strUpLevel = strCurrentKey.Left(strCurrentKey.ReverseFind('\\'));
return Open(strUpLevel);
...................
} ( 24431188 发表于 2006-3-28 16:00:00)
![](../image/doc2.gif)
I had modify the class.
Registry.h:
/*--------------------------------\
public://add the follow functions.
BOOL EnumKey(CStringList &strList);
BOOL UpLevelKey();
CString GetCurrentKey();
CString GetRegRoot();
protected://add the follow propertys
CString m_strCurrentKey;
CString m_strRegRoot;
\--------------------------------*/
Registry.cpp
/*----------------------------------\
CRegistry::CRegistry(HKEY hKey)
{
m_hKey=hKey;
if( hKey == HKEY_LOCAL_MACHINE)
m_strRegRoot = "HKEY_LOCAL_MACHINE";
//recode current RegRoot in CString.
//if hKey == HKEY_CLASSES_ROOT ....and so on
.........
}
BOOL CRegistry::CreateKey(LPCTSTR lpSubKey)
{
.......
if(lReturn==ERROR_SUCCESS)
{
m_hKey=hKey;
m_strCurrentKey += lpSubKey;
m_strCurrentKey.TrimRight("\\");
m_strCurrentKey += "\\";
return TRUE;
}
......
}
\-----------------------------------*/ ( 24431188 发表于 2006-3-28 15:55:00)
![](../image/doc2.gif)
RegCreateKeyEx()
注意第五个参数dwOptions用的是哪个值~~
如果是 REG_OPTION_VOLATILE的话,创建的值只要重起机器就没了~~
要保留的话,要用 REG_OPTION_NON_VOLATILE~~
( nonocast 发表于 2005-7-17 15:25:00)
![](../image/doc2.gif)
注册信息添加到注册表中后,重新启动,注册信息都没有了,但是注消不会丢失是怎么回事!!!
发现同样问题!!!
怎么解决 ( nonocast 发表于 2005-7-17 15:19:00)
![](../image/doc2.gif)
注册信息添加到注册表中后,重新启动,注册信息都没有了,但是注消不会丢失是怎么回事 ( guanghezhou 发表于 2005-3-21 16:04:00)
![](../image/doc2.gif)
谢谢了,这个类比较好。
我只用到write成员函数时发现不能设置二进制值,不过只要将设置二进制值的write成员函数中的RegSetValueEx的参数REG_DWORD改为REG_BINARY即可写入二进制值。
另外,我还不知道怎么样设置长度为零的二进制值。我如果将nVal赋值为0的话,在注册表中的二进制值是00 00, 并不是我想要的“长度为零的二进制值”。
请教高手。 ( wyqml 发表于 2003-12-19 17:38:00)
![](../image/doc2.gif)
注册信息添加到注册表中后,重新启动,注册信息都没有了!!!! ( chambers 发表于 2003-9-23 15:18:00)
.......................................................
--------------------next---------------------