VC函数组件类注册表操作地类

#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);

 if(lReturn == ERROR_SUCCESS)
  return TRUE;

 return FALSE;
}

/*================================================================
* 函数名:    VerifyValue
* 参数:      (LPCTSTR pszValue)
* 功能描述:   判断指定的键名是否等于某个值
* 返回值:    BOOL
================================================================*/
BOOL CReg::IsEqual(LPCTSTR pszValue,int nn)
{
 int nTemp;
 this->Read(pszValue,nTemp);
 if(nTemp==nn)
  return TRUE;
 return FALSE;
}
BOOL CReg::IsEqual(LPCTSTR pszValue,DWORD dw)
{
 DWORD dwTemp;
 this->Read(pszValue,dwTemp);
 if(dwTemp==dw)
  return TRUE;
 return FALSE;
}
BOOL CReg::IsEqual(LPCTSTR pszValue,LPCTSTR lpsz)
{
 CString str;
 this->Read(pszValue,str);
 if(str.CompareNoCase(lpsz)==0)
  return TRUE;
 return FALSE;
}


/*================================================================
* 函数名:    CreateKey
* 参数:      (HKEY hRootKey, LPCTSTR pszPath)
* 功能描述:   创建路径
* 返回值:    BOOL
================================================================*/
BOOL CReg::CreateKey (LPCTSTR pszPath)
{
 DWORD dw;

 LONG ReturnValue = ::RegCreateKeyEx (m_hRootKey, pszPath, 0L, NULL,
       REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL,
       &m_hSubKey, &dw);

 if(ReturnValue == ERROR_SUCCESS)
  return TRUE;

 return FALSE;
}

 

/*================================================================
* 函数名:    Write
* 参数:      (LPCTSTR lpszKeyName, int iVal)
* 功能描述:   写入整型值
* 返回值:    BOOL
================================================================*/
BOOL CReg::Write (LPCTSTR lpszKeyName, int iVal)
{
 DWORD dwValue;

 dwValue = (DWORD)iVal;
 LONG ReturnValue = ::RegSetValueEx (m_hSubKey, lpszKeyName, 0L, REG_DWORD,
  (CONST BYTE*) &dwValue, sizeof(DWORD));


 if(ReturnValue == ERROR_SUCCESS)
  return TRUE;
 
 return FALSE;
}

/*================================================================
* 函数名:    Write
* 参数:      (LPCTSTR lpszKeyName, DWORD dwVal)
* 功能描述:   写入DWORD值
* 返回值:    BOOL
================================================================*/
BOOL CReg::Write (LPCTSTR lpszKeyName, DWORD dwVal)
{
 return ::RegSetValueEx (m_hSubKey, lpszKeyName, 0L, REG_DWORD,
  (CONST BYTE*) &dwVal, sizeof(DWORD));
}


/*================================================================
* 函数名:    Write
* 参数:      (LPCTSTR lpszKeyName, LPCTSTR pszData)
* 功能描述:   写入字符串值
* 返回值:    BOOL
================================================================*/
BOOL CReg::Write (LPCTSTR lpszKeyName, LPCTSTR pszData)
{

 LONG ReturnValue = ::RegSetValueEx (m_hSubKey, lpszKeyName, 0L, REG_SZ,
  (CONST BYTE*) pszData, strlen(pszData) + 1);


 if(ReturnValue == ERROR_SUCCESS)
  return TRUE;
 
 return FALSE;
}


/*================================================================
* 函数名:    Read
* 参数:      (LPCTSTR lpszKeyName, int& iVal) 第2个参数通过引用传递,可以在函数中修改实参
* 功能描述:   读取整数
* 返回值:    BOOL
================================================================*/
BOOL CReg::Read(LPCTSTR lpszKeyName, int& iVal)
{

 DWORD dwType;
 DWORD dwSize = sizeof (DWORD);
 DWORD dwDest;

 LONG lReturn = ::RegQueryValueEx (m_hSubKey, (LPSTR) lpszKeyName, NULL,
  &dwType, (BYTE *) &dwDest, &dwSize);

 if(lReturn == ERROR_SUCCESS)
 {
  iVal = (int)dwDest;
  return TRUE;
 }

 return FALSE;
}


/*================================================================
* 函数名:    Read
* 参数:      (LPCTSTR lpszKeyName, DWORD& dwVal) 第2个参数通过引用传递,可以在函数中修改实参
* 功能描述:   读取DWORD值
* 返回值:    BOOL
================================================================*/
BOOL CReg::Read (LPCTSTR lpszKeyName, DWORD& dwVal)
{

 DWORD dwType;
 DWORD dwSize = sizeof (DWORD);
 DWORD dwDest;

 LONG lReturn = ::RegQueryValueEx (m_hSubKey, (LPSTR) lpszKeyName, NULL,
  &dwType, (BYTE *) &dwDest, &dwSize);


 if(lReturn == ERROR_SUCCESS)
 {
  dwVal = dwDest;
  return TRUE;
 }

 return FALSE;
}


/*================================================================
* 函数名:    Read
* 参数:      (LPCTSTR lpszKeyName, CString& sVal) 第2个参数通过引用传递,可以在函数中修改实参
* 功能描述:   读取字符串值
* 返回值:    BOOL
================================================================*/
BOOL CReg::Read (LPCTSTR lpszKeyName, CString& sVal)
{

 DWORD dwType;
 DWORD dwSize = 200;
 char  szString[255];

 LONG lReturn = ::RegQueryValueEx (m_hSubKey, (LPSTR) lpszKeyName, NULL,
  &dwType, (BYTE *) szString, &dwSize);


 if(lReturn == ERROR_SUCCESS)
 {
  sVal = szString;
  return TRUE;
 }

 return FALSE;
}

 

/*================================================================
* 函数名:    DeleteValue
* 参数:      (LPCTSTR pszValue)
* 功能描述:   删除值
* 返回值:    BOOL
================================================================*/
BOOL CReg::DeleteValue (LPCTSTR pszValue)
{
 if(::RegDeleteValue(m_hSubKey, pszValue)== ERROR_SUCCESS)  
  return TRUE;
 else
  return FALSE;
}

/*================================================================
* 函数名:    DeleteKey
* 参数:      (HKEY hRootKey, LPCTSTR pszPath)
* 功能描述:   删除路径
* 返回值:    BOOL
================================================================*/
BOOL CReg::DeleteKey (LPCTSTR pszPath)
{

 if(::RegDeleteKey(m_hRootKey, pszPath) == ERROR_SUCCESS)
  return TRUE;
 else
  return FALSE;
}


/*================================================================
* 函数名:    Close
* 参数:     
* 功能描述:   关闭注册表
* 返回值:    void
================================================================*/
 void CReg::Close()
{
 if (m_hSubKey)
 {
  ::RegCloseKey (m_hSubKey);
  m_hSubKey = NULL;
 }
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值