OK, there are many registry classes out there, but this is the one I wrote. I have used it for years in many commercial applications, so it is quite well tested in terms of reading and writing values. The only weakness of the class is that it does not have extensive support for deleting or copying keys.
There are a few useful features in the class worth noting:
- When reading a value, you can provide a default value in case the operation fails.
- You can read and write objects such as CFonts, CPoints, etc. from/to the registry.
How to Use the Class
- Declare a CRegistry object local to your function.
- Call CRegistry::SetRootKey(HKEY hRootKey) to set the root key.
- Call CRegistry::SetKey(CString strKey, BOOL bCanCreate). Pass bCanCreate=TRUE to write to the registry. Pass bCanCreate=FALSE to read from the registry.
Here is an example...
void CMyApp::ReadRegistry()
{
CRegistry Reg();
Reg.SetRootKey(HKEY_LOCAL_MACHINE);
if (Reg.SetKey("Software\\MyApp\\Settings", FALSE))
{
m_nData = Reg.ReadInt("Data1", 0);
pi = Reg.ReadFloat("Pi", 3.14159);
m_strUserName = Reg.ReadString("UserName", "Default User Name");
Reg.ReadFont(&m_font);
}
else
{
TRACE("Failed to open key\n");
}
}
It's just that simple!
I have included the class interface below to illustrate the variety of member functions.
#ifndef __REGISTRY_H__
#define __REGISTRY_H__
class CRegistry
{
public:
CRegistry();
~CRegistry();
int m_nLastError;
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; }
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);
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);
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 strName, CSize* pSize);
BOOL WriteRect(CString strName, CRect* pRect);
BOOL WriteDword(CString strName, DWORD dwValue);
};
#endif