一些实用的注册表封装类

头文件"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
};

复制代码



本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/07/06/1236965.html,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值