一些实用的注册表封装类

  头文件"registry.h"

复制代码
#include <string>
#include 
<Shlwapi.h>
#include 
<tchar.h>

/**
 * \ingroup CommonClasses
 * Base class for the registry classes.
*/

class CRegBase
{
public:    //methods
    /**
     * Removes the whole registry key including all values. So if you set the registry
     * entry to be HKCU\Software\Company\Product\key\value there will only be
     * HKCU\Software\Company\Product key in the registry.
     * \return ERROR_SUCCESS or an nonzero errorcode. Use FormatMessage() to get an error description.
     
*/

    DWORD removeKey() 
{ RegOpenKeyEx(m_base, m_path, 0, KEY_WRITE, &m_hKey); return SHDeleteKey(m_base, (LPCTSTR)m_path); }
    
/**
     * Removes the value of the registry object. If you set the registry entry to
     * be HKCU\Software\Company\Product\key\value there will only be
     * HKCU\Software\Company\Product\key\ in the registry.
     * \return ERROR_SUCCESS or an nonzero errorcode. Use FormatMessage() to get an error description.
     
*/

    LONG removeValue() 
{ RegOpenKeyEx(m_base, m_path, 0, KEY_WRITE, &m_hKey); return RegDeleteValue(m_hKey, (LPCTSTR)m_key); }

public:    //members
    HKEY m_base;        ///< handle to the registry base
    HKEY m_hKey;        ///< handle to the open registry key
    CString m_key;        ///< the name of the value
    CString m_path;        ///< the path to the key
};


class CRegDWORD : public CRegBase
{
public:
    CRegDWORD(
void);
    
/**
     * Constructor.
     * \param key the path to the key, including the key. example: "Software\\Company\\SubKey\\MyValue"
     * \param def the default value used when the key does not exist or a read error occured
     * \param force set to TRUE if no cache should be used, i.e. always read and write directly from/to registry
     * \param base a predefined base key like HKEY_LOCAL_MACHINE. see the SDK documentation for more information.
     
*/

    CRegDWORD(CString key, DWORD def 
= 0, BOOL force = FALSE, HKEY base = HKEY_CURRENT_USER);
    
~CRegDWORD(void);
    
/**
     * reads the assigned value from the registry. Use this method only if you think the registry
     * value could have been altered without using the CRegDWORD object.
     * \return the read value
     
*/

    DWORD    read();                        
///< reads the value from the registry
    void    write();                    ///< writes the value to the registry

    operator DWORD();
    CRegDWORD
& operator=(DWORD d);
    CRegDWORD
& operator+=(DWORD d) return *this = *this + d;}
    CRegDWORD
& operator-=(DWORD d) return *this = *this - d;}
    CRegDWORD
& operator*=(DWORD d) return *this = *this * d;}
    CRegDWORD
& operator/=(DWORD d) return *this = *this / d;}
    CRegDWORD
& operator%=(DWORD d) return *this = *this % d;}
    CRegDWORD
& operator<<=(DWORD d) return *this = *this << d;}
    CRegDWORD
& operator>>=(DWORD d) return *this = *this >> d;}
    CRegDWORD
& operator&=(DWORD d) return *this = *this & d;}
    CRegDWORD
& operator|=(DWORD d) return *this = *this | d;}
    CRegDWORD
& operator^=(DWORD d) return *this = *this ^ d;}
    
protected:

    DWORD    m_value;                    
///< the cached value of the registry
    DWORD    m_defaultvalue;                ///< the default value to use
    BOOL    m_read;                        ///< indicates if the value has already been read from the registry
    BOOL    m_force;                    ///< indicates if no cache should be used, i.e. always read and write directly from registry
};


class CRegString : public CRegBase
{
public:
    CRegString();
    
/**
     * Constructor.
     * \param key the path to the key, including the key. example: "Software\\Company\\SubKey\\MyValue"
     * \param def the default value used when the key does not exist or a read error occured
     * \param force set to TRUE if no cache should be used, i.e. always read and write directly from/to registry
     * \param base a predefined base key like HKEY_LOCAL_MACHINE. see the SDK documentation for more information.
     
*/

    CRegString(CString key, CString def 
= _T(""), BOOL force = FALSE, HKEY base = HKEY_CURRENT_USER);
    
~CRegString(void);
    
    CString read();                        
///< reads the value from the registry
    void    write();                    ///< writes the value to the registry
        

    operator CString();
    CRegString
& operator=(CString s);
    CRegString
& operator+=(CString s) return *this = (CString)*this + s; }
    
    
    
protected:

    CString    m_value;                    
///< the cached value of the registry
    CString    m_defaultvalue;                ///< the default value to use
    BOOL    m_read;                        ///< indicates if the value has already been read from the registry
    BOOL    m_force;                    ///< indicates if no cache should be used, i.e. always read and write directly from registry
};


class CRegRect : public CRegBase
{
public:
    CRegRect();
    
/**
     * Constructor.
     * \param key the path to the key, including the key. example: "Software\\Company\\SubKey\\MyValue"
     * \param def the default value used when the key does not exist or a read error occured
     * \param force set to TRUE if no cache should be used, i.e. always read and write directly from/to registry
     * \param base a predefined base key like HKEY_LOCAL_MACHINE. see the SDK documentation for more information.
     
*/

    CRegRect(CString key, CRect def 
= CRect(), BOOL force = FALSE, HKEY base = HKEY_CURRENT_USER);
    
~CRegRect(void);
    
    CRect read();                        
///< reads the value from the registry
    void    write();                    ///< writes the value to the registry
    

    operator CRect();
    
operator LPCRECT() return (LPCRECT)(CRect)*this; }
    
operator LPRECT() return (LPRECT)(CRect)*this; }
    CRegRect
& operator=(CRect r);
    CRegRect
& operator+=(POINT r) return *this = (CRect)*this + r;}
    CRegRect
& operator+=(SIZE r) return *this = (CRect)*this + r;}
    CRegRect
& operator+=(LPCRECT  r) return *this = (CRect)*this + r;}
    CRegRect
& operator-=(POINT r) return *this = (CRect)*this - r;}
    CRegRect
& operator-=(SIZE r) return *this = (CRect)*this - r;}
    CRegRect
& operator-=(LPCRECT  r) return *this = (CRect)*this - r;}
    
    CRegRect
& operator&=(CRect r) return *this = r & *this;}
    CRegRect
& operator|=(CRect r) return *this = r | *this;}
    
    
protected:

    CRect    m_value;                    
///< the cached value of the registry
    CRect    m_defaultvalue;                ///< the default value to use
    BOOL    m_read;                        ///< indicates if the value has already been read from the registry
    BOOL    m_force;                    ///< indicates if no cache should be used, i.e. always read and write directly from registry
};


class CRegPoint : public CRegBase
{
public:
    CRegPoint();
    
/**
     * Constructor.
     * \param key the path to the key, including the key. example: "Software\\Company\\SubKey\\MyValue"
     * \param def the default value used when the key does not exist or a read error occured
     * \param force set to TRUE if no cache should be used, i.e. always read and write directly from/to registry
     * \param base a predefined base key like HKEY_LOCAL_MACHINE. see the SDK documentation for more information.
     
*/

    CRegPoint(CString key, CPoint def 
= CPoint(), BOOL force = FALSE, HKEY base = HKEY_CURRENT_USER);
    
~CRegPoint(void);
    
    CPoint read();
    
void    write();                    ///< writes the value to the registry
    

    operator CPoint();
    CRegPoint
& operator=(CPoint p);
    
    CRegPoint
& operator+=(CPoint p) return *this = p + *this; }
    CRegPoint
& operator-=(CPoint p) return *this = p - *this; }
    
    
protected:

    CPoint    m_value;                    
///< the cached value of the registry
    CPoint    m_defaultvalue;                ///< the default value to use
    BOOL    m_read;                        ///< indicates if the value has already been read from the registry
    BOOL    m_force;                    ///< indicates if no cache should be used, i.e. always read and write directly from registry
};


#endif

typedef std::basic_string
<TCHAR> stdstring;

class CRegStdBase
{
public:    //methods
    /**
     * Removes the whole registry key including all values. So if you set the registry
     * entry to be HKCU\Software\Company\Product\key\value there will only be
     * HKCU\Software\Company\Product key in the registry.
     * \return ERROR_SUCCESS or an nonzero errorcode. Use FormatMessage() to get an error description.
     
*/

    DWORD removeKey() 
{ RegOpenKeyEx(m_base, m_path.c_str(), 0, KEY_WRITE, &m_hKey); return SHDeleteKey(m_base, m_path.c_str()); }
    
/**
     * Removes the value of the registry object. If you set the registry entry to
     * be HKCU\Software\Company\Product\key\value there will only be
     * HKCU\Software\Company\Product\key\ in the registry.
     * \return ERROR_SUCCESS or an nonzero errorcode. Use FormatMessage() to get an error description.
     
*/

    LONG removeValue() 
{ RegOpenKeyEx(m_base, m_path.c_str(), 0, KEY_WRITE, &m_hKey); return RegDeleteValue(m_hKey, m_key.c_str()); }

public:    //members
    HKEY m_base;        ///< handle to the registry base
    HKEY m_hKey;        ///< handle to the open registry key
    stdstring m_key;        ///< the name of the value
    stdstring m_path;        ///< the path to the key
};


class CRegStdString : public CRegStdBase
{
public:
    CRegStdString();
    
/**
     * Constructor.
     * \param key the path to the key, including the key. example: "Software\\Company\\SubKey\\MyValue"
     * \param def the default value used when the key does not exist or a read error occured
     * \param force set to TRUE if no cache should be used, i.e. always read and write directly from/to registry
     * \param base a predefined base key like HKEY_LOCAL_MACHINE. see the SDK documentation for more information.
     
*/

    CRegStdString(stdstring key, stdstring def 
= _T(""), BOOL force = FALSE, HKEY base = HKEY_CURRENT_USER);
    
~CRegStdString(void);
    
    stdstring read();                        
///< reads the value from the registry
    void    write();                    ///< writes the value to the registry
        

    operator stdstring();
    CRegStdString
& operator=(stdstring s);
    CRegStdString
& operator+=(stdstring s) return *this = (stdstring)*this + s; }
    
operator LPCTSTR();
    
    
protected:

    stdstring    m_value;                
///< the cached value of the registry
    stdstring    m_defaultvalue;            ///< the default value to use
    BOOL    m_read;                        ///< indicates if the value has already been read from the registry
    BOOL    m_force;                    ///< indicates if no cache should be used, i.e. always read and write directly from registry
};


class CRegStdWORD : public CRegStdBase
{
public:
    CRegStdWORD();
    
/**
     * Constructor.
     * \param key the path to the key, including the key. example: "Software\\Company\\SubKey\\MyValue"
     * \param def the default value used when the key does not exist or a read error occured
     * \param force set to TRUE if no cache should be used, i.e. always read and write directly from/to registry
     * \param base a predefined base key like HKEY_LOCAL_MACHINE. see the SDK documentation for more information.
     
*/

    CRegStdWORD(stdstring key, DWORD def 
= 0, BOOL force = FALSE, HKEY base = HKEY_CURRENT_USER);
    
~CRegStdWORD(void);
    
    DWORD read();                        
///< reads the value from the registry
    void    write();                    ///< writes the value to the registry
        

    operator DWORD();
    CRegStdWORD
& operator=(DWORD d);
    CRegStdWORD
& operator+=(DWORD d) return *this = *this + d;}
    CRegStdWORD
& operator-=(DWORD d) return *this = *this - d;}
    CRegStdWORD
& operator*=(DWORD d) return *this = *this * d;}
    CRegStdWORD
& operator/=(DWORD d) return *this = *this / d;}
    CRegStdWORD
& operator%=(DWORD d) return *this = *this % d;}
    CRegStdWORD
& operator<<=(DWORD d) return *this = *this << d;}
    CRegStdWORD
& operator>>=(DWORD d) return *this = *this >> d;}
    CRegStdWORD
& operator&=(DWORD d) return *this = *this & d;}
    CRegStdWORD
& operator|=(DWORD d) return *this = *this | d;}
    CRegStdWORD
& operator^=(DWORD d) return *this = *this ^ d;}
    
    
protected:

    DWORD    m_value;                
///< the cached value of the registry
    DWORD    m_defaultvalue;            ///< the default value to use
    BOOL    m_read;                    ///< indicates if the value has already been read from the registry
    BOOL    m_force;                ///< indicates if no cache should be used, i.e. always read and write directly from registry
};

复制代码

registry.cpp

posted on   Phinecos(洞庭散人)  阅读(817)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2007-07-06 SWT Designer 6.0安装小记

导航

统计

点击右上角即可分享
微信分享提示