Phinecos(洞庭散人)
光荣在于平淡,艰巨因为漫长
一些实用的注册表封装类
头文件
"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);
r