c++ 注册表类

C/C++ code复制代码

#if !defined(AFX_REGISTRY_H__E0610A5D_7166_4D02_9D7E_11AF7CF8E229__INCLUDED_)
#define AFX_REGISTRY_H__E0610A5D_7166_4D02_9D7E_11AF7CF8E229__INCLUDED_

//
#include <winreg.h>
/
// CRegistry window

class CRegistry : public CObject
{
// Construction
public:
    CRegistry(HKEY hKey=HKEY_LOCAL_MACHINE);

public:
    BOOL SaveKey(LPCTSTR lpFileName);
    BOOL RestoreKey(LPCTSTR lpFileName);
    BOOL Read(LPCTSTR lpValueName, CString* lpVal);
    BOOL Read(LPCTSTR lpValueName, DWORD* pdwVal);
    BOOL Read(LPCTSTR lpValueName, int* pnVal);
    BOOL Write(LPCTSTR lpSubKey, LPCTSTR lpVal);
    BOOL Write(LPCTSTR lpSubKey, DWORD dwVal);
    BOOL Write(LPCTSTR lpSubKey, int nVal);
    BOOL DeleteKey(HKEY hKey, LPCTSTR lpSubKey);
    BOOL DeleteValue(LPCTSTR lpValueName);
    void Close();
    BOOL Open(LPCTSTR lpSubKey);
    BOOL CreateKey(LPCTSTR lpSubKey);
    virtual ~CRegistry();

protected:
    HKEY m_hKey;
   
};

/

#endif // !defined(AFX_REGISTRY_H__E0610A5D_7166_4D02_9D7E_11AF7CF8E229__INCLUDED_)
 

Registry.cpp

C/C++ code 复制代码
// Registry.cpp : implementation file
//

#include "stdafx.h"
#include "Registry.h"

/
// CRegistry

CRegistry::CRegistry(HKEY hKey)
{
    m_hKey=hKey;
}

CRegistry::~CRegistry()
{
    Close();
}

/
// CRegistry Functions

BOOL CRegistry::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(lReturn==ERROR_SUCCESS)
    {
        m_hKey=hKey;
        return TRUE;
    }
   
    return FALSE;
   
}

BOOL CRegistry::Open(LPCTSTR lpSubKey)
{
    ASSERT(m_hKey);
    ASSERT(lpSubKey);
   
    HKEY hKey;
    long lReturn=RegOpenKeyEx(m_hKey,lpSubKey,0L,KEY_ALL_ACCESS,&hKey);
   
    if(lReturn==ERROR_SUCCESS)
    {
        m_hKey=hKey;
        return TRUE;
    }
    return FALSE;
   
}

void CRegistry::Close()
{
    if(m_hKey)
    {
        RegCloseKey(m_hKey);
        m_hKey=NULL;
    }
   
}

BOOL CRegistry::DeleteValue(LPCTSTR lpValueName)
{
    ASSERT(m_hKey);
    ASSERT(lpValueName);
   
    long lReturn=RegDeleteValue(m_hKey,lpValueName);
   
    if(lReturn==ERROR_SUCCESS)
        return TRUE;
    return FALSE;
   
}

BOOL CRegistry::DeleteKey(HKEY hKey, LPCTSTR lpSubKey)
{
    ASSERT(hKey);
    ASSERT(lpSubKey);
   
    long lReturn=RegDeleteValue(hKey,lpSubKey);
   
    if(lReturn==ERROR_SUCCESS)
        return TRUE;
    return FALSE;
   
}

BOOL CRegistry::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(lReturn==ERROR_SUCCESS)
        return TRUE;
   
    return FALSE;
   
}

BOOL CRegistry::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(lReturn==ERROR_SUCCESS)
        return TRUE;
   
    return FALSE;
   
}

BOOL CRegistry::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(lReturn==ERROR_SUCCESS)
        return TRUE;
   
    return FALSE;
   
}

BOOL CRegistry::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(lReturn==ERROR_SUCCESS)
    {
        *pnVal=(int)dwDest;
        return TRUE;
    }
    return FALSE;
   
}

BOOL CRegistry::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(lReturn==ERROR_SUCCESS)
    {
        *pdwVal=dwDest;
        return TRUE;
    }
    return FALSE;
   
}

BOOL CRegistry::RestoreKey(LPCTSTR lpFileName)
{
    ASSERT(m_hKey);
    ASSERT(lpFileName);
   
    long lReturn=RegRestoreKey(m_hKey,lpFileName,REG_WHOLE_HIVE_VOLATILE);
   
    if(lReturn==ERROR_SUCCESS)
        return TRUE;
   
    return FALSE;
}

BOOL CRegistry::SaveKey(LPCTSTR lpFileName)
{
    ASSERT(m_hKey);
    ASSERT(lpFileName);
   
    long lReturn=RegSaveKey(m_hKey,lpFileName,NULL);
   
    if(lReturn==ERROR_SUCCESS)
        return TRUE;
   
    return FALSE;
}

BOOL CRegistry::Read(LPCTSTR lpValueName, CString* lpVal)
{
    ASSERT(m_hKey);
    ASSERT(lpValueName);
    ASSERT(lpVal);
   
    DWORD dwType;
    DWORD dwSize=200;
    char szString[2550];
   
    long lReturn=RegQueryValueEx(m_hKey,lpValueName,NULL,&dwType,(BYTE *)szString,&dwSize);
   
    if(lReturn==ERROR_SUCCESS)
    {
        *lpVal=szString;
        return TRUE;
    }
    return FALSE;
   
}
#ifndef __REGISTRY_H__ #define __REGISTRY_H__ class CRegistry { public: CRegistry(); ~CRegistry(); int m_nLastError; // CRegistry properties protected: HKEY m_hRootKey; BOOL m_bLazyWrite; CString m_strCurrentPath; public: inline BOOL PathIsValid() { return (m_strCurrentPath.GetLength() > 0); } inline CString GetCurrentPath() { return m_strCurrentPath; } inline HKEY GetRootKey() { return m_hRootKey; } //CRegistry methods public: BOOL ClearKey(); BOOL SetRootKey(HKEY hRootKey); BOOL CreateKey(CString strKey); BOOL DeleteKey(CString strKey); BOOL DeleteValue(CString strName); int GetDataSize(CString strValueName); DWORD GetDataType(CString strValueName); int GetSubKeyCount(); int GetValueCount(); BOOL KeyExists(CString strKey, HKEY hRootKey = NULL); BOOL SetKey(CString strKey, BOOL bCanCreate); BOOL ValueExists(CString strName); void RenameValue(CString strOldName, CString strNewName); // data reading functions COleDateTime ReadDateTime(CString strName, COleDateTime dtDefault); double ReadFloat(CString strName, double fDefault); CString ReadString(CString strName, CString strDefault); int ReadInt(CString strName, int nDefault); BOOL ReadBool(CString strName, BOOL bDefault); COLORREF ReadColor(CString strName, COLORREF rgbDefault); BOOL ReadFont(CString strName, CFont* pFont); BOOL ReadPoint(CString strName, CPoint* pPoint); BOOL ReadSize(CString strName, CSize* pSize); BOOL ReadRect(CString strName, CRect* pRect); DWORD ReadDword(CString strName, DWORD dwDefault); // data writing functions BOOL WriteBool(CString strName, BOOL bValue); BOOL WriteDateTime(CString strName, COleDateTime dtValue); BOOL WriteString(CString strName, CString strValue); BOOL WriteFloat(CString strName, double fValue); BOOL WriteInt(CString strName, int nValue); BOOL WriteColor(CString strName, COLORREF rgbValue); BOOL WriteFont(CString strName, CFont* pFont); BOOL WritePoint(CString strName, CPoint* pPoint); BOOL WriteSize(CString strNam
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值