Click here to Skip to main content
11,704,509 members (57,880 online)
Click here to Skip to main content

Registry Class

, 17 Nov 1999 201.5K 3.1K 70
Rate this:
Please Sign up or sign in to vote.
A simple registry class
<!-- Article Starts -->

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();
	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;

// CRegistry properties	
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; }


//CRegistry	methods
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);

	// 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


#endif

License

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

Share

About the Author

Robert Pittenger, MCPD-EAD
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
and
Wealthonomics: The Most Important Economic and Financial Concepts that Can Make You Rich Fast.
Visit http://www.billionairebook.net for more information.

You may also be interested in...

Comments and Discussions

 
QuestionWriteString Unicode Pin
dimag18-Jun-14 7:11
memberdimag18-Jun-14 7:11 
QuestionIs there any license limitation ? Pin
Member 449889214-Jun-09 22:46
memberMember 449889214-Jun-09 22:46 
GeneralQuerySubKeys Pin
Vider1-Aug-07 2:31
memberVider1-Aug-07 2:31 
GeneralRe: QuerySubKeys Pin
kid_sputnik7-Oct-07 5:27
memberkid_sputnik7-Oct-07 5:27 
GeneralRegistry problem when UNICODE is defined Pin
rajan40083-Aug-06 5:09
memberrajan40083-Aug-06 5:09 
AnswerRe: Registry problem when UNICODE is defined Pin
chsml7-Oct-06 9:50
memberchsml7-Oct-06 9:50 
GeneralRegnum buffer Pin
JRaiden31-Jul-06 0:40
memberJRaiden31-Jul-06 0:40 
NewsNT Native Registry API version of this here! Pin
Dan Madden19-Jun-06 18:29
memberDan Madden19-Jun-06 18:29 
GeneralRead type Binary Pin
DRLaverdure16-May-06 7:37
memberDRLaverdure16-May-06 7:37 
NewsRe: Read type Binary Pin
Dan Madden19-Jun-06 18:32
memberDan Madden19-Jun-06 18:32 
GeneralWriteDword Pin
Jason Sundram16-Oct-05 19:47
memberJason Sundram16-Oct-05 19:47 
NewsRe: WriteDword Pin
Dan Madden19-Jun-06 18:39
memberDan Madden19-Jun-06 18:39 
GeneralUNICODE bug Pin
Warren Stevens11-Aug-05 6:17
memberWarren Stevens11-Aug-05 6:17 
GeneralRe: UNICODE bug Pin
sGrabert11-Sep-07 21:26
membersGrabert11-Sep-07 21:26 
QuestionKEY_ALL_ACCESS for reading ? Pin
cross bones style23-Mar-05 3:20
membercross bones style23-Mar-05 3:20 
AnswerRe: KEY_ALL_ACCESS for reading ? Pin
kid_sputnik7-Oct-07 5:59
memberkid_sputnik7-Oct-07 5:59 
GeneralRe: KEY_ALL_ACCESS for reading ? Pin
kid_sputnik7-Oct-07 6:15
memberkid_sputnik7-Oct-07 6:15 
if anyone is interested, here is my hack:

in protected section, i added

int m_Access;


i set this to KEY_ALL_ACCESS is the constructor.

then, i added these methods to the header file:


// changes current access level to KEY_READ.
void SetToReadAccess() { m_Access = KEY_READ; }

// changes current access level to KEY_WRITE.
void SetToWriteAccess() { m_Access = KEY_WRITE; }

// changes current access level to KEY_EXECUTE.
void SetToExecuteAccess() { m_Access = KEY_EXECUTE; }

// changes current access level to KEY_ALL_ACCESS. This is the default.
void SetToAllAccess() { m_Access = KEY_ALL_ACCESS; }

i thought this method, of the access being set by the last SetToAccess... call fit in with the way the rest of the class works (like, main and sub keys being remembered from the last set calls), and this avoids the user having to dig out the access constants in the winnt.h file. i excluded the specific access constants, and just went with these 4 that are combos of the original ones.

with this i am finally able to use this class, i am reading values set by the VCP FTDI USB-to-ComPort driver, and it wouldn't read with KEY_ALL_ACCESS privelages.

- daniel b
GeneralAnother Thanks from me! Pin
hazem-mk22-May-04 3:43
memberhazem-mk22-May-04 3:43 
GeneralExcellent Pin
Balkrishna Talele11-May-04 21:08
memberBalkrishna Talele11-May-04 21:08 
GeneralAn Excellent Piece of Code Pin
Karen030227-Nov-03 19:31
memberKaren030227-Nov-03 19:31 
GeneralBugfix: Reading/writing long strings Pin
PEK21-Jan-03 2:46
memberPEK21-Jan-03 2:46 
GeneralRe: Bugfix: Reading/writing long strings Pin
Hamed Mosavi12-May-08 20:51
memberHamed Mosavi12-May-08 20:51 
GeneralKey/Value Enumeration Pin
GermanTux22-Dec-02 2:24
memberGermanTux22-Dec-02 2:24 
GeneralRe: Key/Value Enumeration Pin
lzandman24-Aug-04 2:26
memberlzandman24-Aug-04 2:26 
GeneralLinker warning in Release Builds Pin
karl_w28-Nov-02 22:52
memberkarl_w28-Nov-02 22:52 
GeneralRe: Linker warning in Release Builds Pin
karl_w1-Dec-02 23:10
memberkarl_w1-Dec-02 23:10 
GeneralRe: Linker warning in Release Builds Pin
drake2825-Nov-04 17:32
memberdrake2825-Nov-04 17:32 
GeneralBugfix: Writing Unicode strings Pin
PEK28-Sep-02 11:06
sussPEK28-Sep-02 11:06 
GeneralRe: Bugfix: Writing Unicode strings Pin
Alex Rosenbaum9-Apr-06 19:51
memberAlex Rosenbaum9-Apr-06 19:51 
GeneralGreat Class but ... Pin
Anonymous2-Jul-02 2:43
memberAnonymous2-Jul-02 2:43 
GeneralRe: Great Class but ... Pin
Anonymous2-Jul-02 2:47
memberAnonymous2-Jul-02 2:47 
GeneralWriting to Registry as Unprivileged User Pin
perlmunger29-May-02 17:30
memberperlmunger29-May-02 17:30 
GeneralRe: Writing to Registry as Unprivileged User Pin
clintsinger31-May-02 13:34
memberclintsinger31-May-02 13:34 
GeneralRe: Writing to Registry as Unprivileged User Pin
perlmunger31-May-02 14:25
memberperlmunger31-May-02 14:25 
GeneralRe: Writing to Registry as Unprivileged User Pin
clintsinger3-Jun-02 5:40
memberclintsinger3-Jun-02 5:40 
GeneralRe: Writing to Registry as Unprivileged User Pin
perlmunger3-Jun-02 15:16
memberperlmunger3-Jun-02 15:16 
GeneralRe: Writing to Registry as Unprivileged User Pin
daerid7-Jul-04 9:51
memberdaerid7-Jul-04 9:51 
QuestionWin98? Pin
perlmunger23-May-02 7:59
memberperlmunger23-May-02 7:59 
AnswerRe: Win98? Pin
Anonymous23-May-02 8:13
memberAnonymous23-May-02 8:13 
AnswerRe: Win98? Pin
Anonymous23-May-02 10:13
memberAnonymous23-May-02 10:13 
GeneralAwesome! Pin
perlmunger20-May-02 18:29
memberperlmunger20-May-02 18:29 
GeneralGeneric- vs Specific-Type Read/Write Pin
alpha+9-Apr-02 20:42
memberalpha+9-Apr-02 20:42 
GeneralQuery Remote Registry Pin
i386.com7-Apr-02 5:33
memberi386.com7-Apr-02 5:33 
Generalthanx, but... Pin
Anonymous3-Mar-02 5:07
memberAnonymous3-Mar-02 5:07 
GeneralRe: thanx, but... Pin
Anonymous10-Mar-02 17:23
memberAnonymous10-Mar-02 17:23 
GeneralWriteDword Pin
asX25-Feb-02 4:43
memberasX25-Feb-02 4:43 
GeneralGreat f***ing class ! Thanks man, you rock Pin
Anonymous10-Feb-02 21:13
memberAnonymous10-Feb-02 21:13 
GeneralAbout Registry Pin
silviuoliviu7-Feb-02 1:05
membersilviuoliviu7-Feb-02 1:05 
GeneralWonderful! Pin
Sean Cundiff20-Dec-01 2:12
memberSean Cundiff20-Dec-01 2:12 
GeneralThanks Rob Pin
Bob Moore2-Nov-01 2:17
memberBob Moore2-Nov-01 2:17 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 18 Nov 1999
Article Copyright 1999 by Robert Pittenger, MCPD-EAD
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid