下载本文所附源代码
#if !defined _REG_H
#define _REG_H
/************************************************************************
* 文件名: reg.h
* 文件描述: 注册表读写
*************************************************************************/
class CReg : public CObject
{
public:
CReg(HKEY hRootKey = HKEY_LOCAL_MACHINE); //构造函数带有默认参数
virtual ~CReg();
public:
BOOL VerifyKey (LPCTSTR pszPath);
BOOL VerifyValue (LPCTSTR pszValue);
BOOL CreateKey (LPCTSTR pszPath);
void Close();
BOOL DeleteValue (LPCTSTR pszValue);
BOOL DeleteKey (LPCTSTR pszPath);
BOOL Write (LPCTSTR pszKey, int iVal);
BOOL Write (LPCTSTR pszKey, DWORD dwVal);
BOOL Write (LPCTSTR pszKey, LPCTSTR pszVal);
BOOL Read (LPCTSTR pszKey, int& iVal);
BOOL Read (LPCTSTR pszKey, DWORD& dwVal);
BOOL Read (LPCTSTR pszKey, CString& sVal);
BOOL IsEqual(LPCTSTR pszValue,int nn);
BOOL IsEqual(LPCTSTR pszValue,DWORD dw);
BOOL IsEqual(LPCTSTR pszValue,LPCTSTR lpsz);
protected:
HKEY m_hSubKey; //保存打开的子键句柄
HKEY m_hRootKey; //保存根键句柄
};
#endif
#include "stdafx.h"
#include "reg.h"
/*================================================================
* 函数名: CReg
* 参数: (HKEY hRootKey)
* 说明: 如果构造函数不带参数,则使用默认的参数,m_hRootKey被初始化
为HKEY_LOCAL_MACHINE, 如果带有参数则 m_hRootKey为指定的值
================================================================*/
CReg::CReg(HKEY hRootKey)
{
m_hRootKey = hRootKey;
}
CReg::~CReg() //在析构函数中关闭打开注册表句柄
{
Close();
}
/*================================================================
* 函数名: VerifyKey
* 参数: (HKEY hRootKey, LPCTSTR pszPath)
* 功能描述: 判断给定的路径是否存在 (兼有打开的功能)
如果第一个参数为NULL,则使用默认的根键。
* 返回值: BOOL
================================================================*/
BOOL CReg::VerifyKey (LPCTSTR pszPath)
{
LONG ReturnValue = ::RegOpenKeyEx (m_hRootKey, pszPath, 0L,
KEY_ALL_ACCESS, &m_hSubKey);
if(ReturnValue == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
/*================================================================
* 函数名: VerifyValue
* 参数: (LPCTSTR pszValue)
* 功能描述: 判断给定的值是否存在 (请先调用VerifyKey,然后在使用该函数)
* 返回值: BOOL
================================================================*/
BOOL CReg::VerifyValue (LPCTSTR pszValue)
{
LONG lReturn = ::RegQueryValueEx(m_hSubKey, pszValue, NULL,
NULL, NULL, NULL);
