Click here to Skip to main content
11,932,734 members (52,739 online)
Click here to Skip to main content
Add your own
alternative version


62 bookmarked

Another registry class

, 25 Apr 2003 CPOL
Rate this:
Please Sign up or sign in to vote.
A registry class with overloaded operators to use registry values like normal variables


In many projects I need to store simple strings, DWORDS or for window position CRect's in the registry. I know there are many registry classes out there, so why one more?

The answer: simplicity. I want to write to the registry as easy as assigning a value to a variable. Like an eeprom variable in neuron controllers (those of you who use those controllers know what I mean). So I wrote exactly such a class.


So, how to use this class - or better classes. I wrote a separate class for CString, DWORD, CRect and CPoint.

// initialize the variables
CRegString mystring("Software\\Company\\Subkey\\MyString", "defaultvalue");
CRegRect   myrect("Software\\Company\\Subkey\\MyRect", CRect(1,1,1,1));
CRegDWORD  myword("Software\\Company\\Subkey\\mydword", 100, 
                     TRUE, HKEY_LOCAL_MACHINE);
CRegPoint  mypoint("Software\\Company\\Subkey\\mypoint");

// now use those variables
CString str = mystring;     //if registry does not exist, the default value is used
mystring = "TestValue";     //"TestValue" is stored in the registry
mystring += "!";            //now the registry value MyString contains "TestValue!"
myrect = CRect(100,200,300,400);    //store in the registry
LPRECT lpR = (LPRECT)myrect;
myword = GetTickCounts();   //just an example
mypoint = CPoint(0,0);

As you can see, once the CRegXXX object knows to which registry key/value it is assigned to you can use it like any other variable!

To avoid too much access to the registry the value is cached. Writes occur only if the new value is different than the current value. Reads occur only once in the constructor. Since such a cached behaviour could lead to some problems, you can force reads and writes to the registry with the methods read() and write(). To disable the cache and force the class to always access the registry just set that in the constructor. the constructor for the CRegDWORD looks like this:

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

The classes CRegString, CRegRect and CRegPoint have similar constructors. See the header files for details.

Not all methods of the "base" classes are overloaded. So if you want to, for example, use some methods of the CString class you have to typecast the CRegString to a CString first:

int l = ((CString)mystring).GetLength();

Please note that in the second line where the Trim() is used that the changed string is NOT written to the registry!

And please: keep your complaints about "not again a registry class" and the like for yourselves. If you don't like it, don't use it!


  • 2. April 2003 :
    1. added classes for use without MFC: CRegStdString, CRegStdDWORD
    2. now compiles for UNICODE too
  • 9. July 2002 :
    1. Corrected a bug found by Hans Dietrich
    2. Extended the classes with two methods: removeKey() and removeValue()

Daniel Andersson wrote a templatized version of this class. See his article here.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Switzerland Switzerland

You may also be interested in...

Comments and Discussions

GeneralRe: Bug? Pin
Igor Vigdorchik6-Feb-04 19:03
memberIgor Vigdorchik6-Feb-04 19:03 
GeneralDid not compile with VC6 Pin
Jacques Cooper12-Dec-03 14:16
memberJacques Cooper12-Dec-03 14:16 
GeneralRe: Did not compile with VC6 Pin
Jonas Nordlund6-Feb-04 0:55
memberJonas Nordlund6-Feb-04 0:55 
Generalcool code... a lot of thanks and a suggestion... Pin
pHreak081528-Sep-03 5:11
memberpHreak081528-Sep-03 5:11 
GeneralGreat Pin
BEamer18-Sep-03 20:32
memberBEamer18-Sep-03 20:32 
GeneralHere's how to read/write to REG_MULTI_SZ Pin
univega_r30410-Aug-03 19:13
memberunivega_r30410-Aug-03 19:13 
GeneralRe: Here's how to read/write to REG_MULTI_SZ Pin
jfernb3-Jun-04 12:07
sussjfernb3-Jun-04 12:07 
GeneralRe: Here's how to read/write to REG_MULTI_SZ Pin
univega_r30422-Dec-04 8:15
memberunivega_r30422-Dec-04 8:15 

When I first modified the code to read multi-string I was just grabbing other pieces from this class and modifying it. I obviously didn't need all those calls to RegQueryValueEx. It does work however as I get the results I wanted. I wasn't really paying attention to the details when I posted the note and I'm a bit more experienced at coding now than I was back then. The following bit of code should get you the same results without all the extra coding. I honestly don't remember where I got the bit to read multistring reg values as I didn't come up with the code in it's entirety. I just applied it to this class because I found it useful and thought someone else may need to read/write multistring values like I did.

Here's the same code that's used by CRegString and also works for reading multistrings.

CString CRegMultiString::read()
ASSERT(m_key != _T(""));
if (RegOpenKeyEx(m_base, m_path, 0, KEY_EXECUTE, &m_hKey)==ERROR_SUCCESS)
int size = 0;
DWORD type;
RegQueryValueEx(m_hKey, m_key, NULL, &type, NULL, (LPDWORD) &size);
TCHAR* pStr = new TCHAR[size];
if (RegQueryValueEx(m_hKey, m_key, NULL, &type, (BYTE*) pStr,(LPDWORD) &size)==ERROR_SUCCESS)
m_value = CString(pStr);
delete [] pStr;
m_read = TRUE;
return m_value;
delete [] pStr;
return m_defaultvalue;
return m_defaultvalue;
GeneralRe: Here's how to read/write to REG_MULTI_SZ Pin
MoSs.gr3-Nov-04 8:56
memberMoSs.gr3-Nov-04 8:56 
GeneralRe: Here's how to read/write to REG_MULTI_SZ Pin
farzadb8-Apr-05 13:31
memberfarzadb8-Apr-05 13:31 
QuestionHow to use as a member variable? Pin
Sorak26-May-03 20:40
memberSorak26-May-03 20:40 
AnswerRe: How to use as a member variable? Pin
SteveKing27-May-03 0:04
memberSteveKing27-May-03 0:04 
AnswerRe: How to use as a member variable? Pin
Chris Richardson27-May-03 17:47
memberChris Richardson27-May-03 17:47 
GeneralBeautiful Pin
=[ Abin ]=1-Apr-03 3:51
member=[ Abin ]=1-Apr-03 3:51 
Generala little problem working with UNICODE Pin
nothingremained10-Nov-02 21:16
membernothingremained10-Nov-02 21:16 
GeneralRe: a little problem working with UNICODE Pin
TDWiseley8-Jan-03 15:11
memberTDWiseley8-Jan-03 15:11 
GeneralTrim Pin
Dins_C10-Sep-02 11:20
memberDins_C10-Sep-02 11:20 
GeneralRe: Trim Pin
SteveKing11-Sep-02 4:00
memberSteveKing11-Sep-02 4:00 
GeneralCould you help me please! Pin
Anonymous14-Jul-02 19:14
sussAnonymous14-Jul-02 19:14 
GeneralRe: Could you help me please! Pin
SteveKing15-Jul-02 7:05
memberSteveKing15-Jul-02 7:05 
GeneralRe: Could you help me please! Pin
univega_r3049-Aug-03 6:25
memberunivega_r3049-Aug-03 6:25 
GeneralQuestion about new removeKey function Pin
Hans Dietrich10-Jul-02 10:36
memberHans Dietrich10-Jul-02 10:36 
GeneralRe: Question about new removeKey function Pin
SteveKing10-Jul-02 22:35
memberSteveKing10-Jul-02 22:35 
GeneralVery nice! but one bug... Pin
Hans Dietrich8-Jul-02 9:40
memberHans Dietrich8-Jul-02 9:40 
GeneralRe: Very nice! but one bug... Pin
SteveKing9-Jul-02 0:01
memberSteveKing9-Jul-02 0:01 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 26 Apr 2003
Article Copyright 2002 by SteveKing
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid