Click here to Skip to main content
15,441,527 members
Articles / Programming Languages / C++
Posted 17 Nov 1999


78 bookmarked

Registry Class

Rate me:
Please Sign up or sign in to vote.
4.95/5 (28 votes)
17 Nov 1999
A simple registry class

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

  1. Declare a CRegistry object local to your function.
  2. Call CRegistry::SetRootKey(HKEY hRootKey) to set the root key.
  3. 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();
	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");
		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

int m_nLastError;

// CRegistry properties	
	HKEY m_hRootKey;
	BOOL m_bLazyWrite;
	CString m_strCurrentPath;

	inline BOOL PathIsValid() {
		return (m_strCurrentPath.GetLength() > 0); }
	inline CString GetCurrentPath() {
		return m_strCurrentPath; }
	inline HKEY GetRootKey() {
		return m_hRootKey; }

//CRegistry	methods
	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 strName, CSize* pSize);
	BOOL WriteRect(CString strName, CRect* pRect);
	BOOL WriteDword(CString strName, DWORD dwValue);

};// end of CRegistry class definition



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
President Starpoint Software Inc.
United States United States
Bob Pittenger is founder and President of Starpoint Software Inc. He holds a B.A. degree from Miami University, M.S. and Ph.D. degrees from Purdue University, and an MBA from Xavier University. He has been programming since 1993, starting with Windows application development in C++/MFC and moving to C# and .NET around 2005 and is a .NET Microsoft Certified Professional Developer.

Bob is the author of two books:
Billionaire: How the Ultra-Rich Built Their Fortunes Through Good and Evil and What You Can Learn from Them
Wealthonomics: The Most Important Economic and Financial Concepts that Can Make You Rich Fast.
Visit for more information.

Comments and Discussions

GeneralCString Input Parameters Pin
Pete Burgess28-Mar-01 1:41
MemberPete Burgess28-Mar-01 1:41 
GeneralRe: CString Input Parameters Pin
28-Mar-01 3:09
suss28-Mar-01 3:09 
GeneralRe: CString Input Parameters Pin
Anonymous14-Nov-02 8:27
MemberAnonymous14-Nov-02 8:27 
QuestionHow to write windows 2000 registry Pin
13-Mar-01 19:14
suss13-Mar-01 19:14 
AnswerRe: How to write windows 2000 registry Pin
4-Apr-01 16:27
suss4-Apr-01 16:27 
Generallinkage error Pin
sridhar11-Oct-00 13:52
Membersridhar11-Oct-00 13:52 
GeneralRe: linkage error Pin
Buck12-Oct-00 16:19
MemberBuck12-Oct-00 16:19 
GeneralProblem writing to a Windows 95 registry. Pin
Bob Eastman28-May-00 9:45
MemberBob Eastman28-May-00 9:45 
I find the class to be quite nice to us. It writes as expected to a Windows 98 registry.

I find however that it is not writing successfully to a Windows 95 registry. Has
anyone else experienced this, and if so, has anyone creatd a version of the class
that will write successfully to both a Windows 95 and Windows 98 registry?
GeneralRe: Problem writing to a Windows 95 registry. Pin
Buck28-May-00 19:36
MemberBuck28-May-00 19:36 
GeneralRe: Problem writing to a Windows 95 registry. Pin
Ken Murphy7-Mar-01 14:36
MemberKen Murphy7-Mar-01 14:36 
GeneralRe: Problem writing to a Windows 95 registry. Pin
Micah16-Apr-02 11:05
MemberMicah16-Apr-02 11:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.