Click here to Skip to main content
15,860,972 members
Articles / Desktop Programming / MFC
Article

Another registry class

Rate me:
Please Sign up or sign in to vote.
4.92/5 (40 votes)
25 Apr 2003CPOL2 min read 185.6K   2.6K   63   59
A registry class with overloaded operators to use registry values like normal variables

Introduction

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.

Usage

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();
((CString)mystring).Trim();

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!

Update

  • 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.

License

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


Written By
Software Developer
Switzerland Switzerland
---

Comments and Discussions

 
QuestionPossible Issue with Visual Studio 2019 V142 builds Pin
rossy6521-Dec-20 6:34
rossy6521-Dec-20 6:34 
GeneralMy vote of 5 Pin
despana5-Feb-13 11:29
despana5-Feb-13 11:29 
QuestionBug for Unicode projects? Pin
rh_20-Apr-07 2:26
rh_20-Apr-07 2:26 
GeneralModified to read REG_MULTI_SZ correctly Pin
Douglas R. Keesler23-Dec-06 4:54
Douglas R. Keesler23-Dec-06 4:54 
Generaltab ctrl Pin
locoone2-Sep-06 13:29
locoone2-Sep-06 13:29 
GeneralNice... Pin
HumanJHawkins14-Jun-06 11:39
HumanJHawkins14-Jun-06 11:39 
Generalunicode for registry Pin
raycadarena13-Feb-06 18:45
raycadarena13-Feb-06 18:45 
GeneralNeed error handling Pin
charlieg28-Jul-05 6:08
charlieg28-Jul-05 6:08 
GeneralVery nice, thank you a lot! Pin
Alex Chirokov4-Jul-05 16:47
Alex Chirokov4-Jul-05 16:47 
GeneralConstructor / read problem Pin
Bob Stanneveld13-Apr-05 3:08
Bob Stanneveld13-Apr-05 3:08 
Generalerror C2039: 'Trim' : is not a member of 'CString' Pin
qiek4-Nov-04 5:07
qiek4-Nov-04 5:07 
GeneralRe: error C2039: 'Trim' : is not a member of 'CString' Pin
EvanTao30-Nov-04 0:03
EvanTao30-Nov-04 0:03 
GeneralRe: error C2039: 'Trim' : is not a member of 'CString' Pin
Alex Chirokov4-Jul-05 16:52
Alex Chirokov4-Jul-05 16:52 
AnswerRe: error C2039: 'Trim' : is not a member of 'CString' Pin
Douglas R. Keesler23-Dec-06 5:30
Douglas R. Keesler23-Dec-06 5:30 
Generalexport registry as text file programmatically Pin
yamei14-Oct-04 12:28
yamei14-Oct-04 12:28 
GeneralLinker error wtih removeKey() Pin
JR Cooper7-Oct-04 13:34
JR Cooper7-Oct-04 13:34 
GeneralRe: Linker error wtih removeKey() Pin
mnowok12-Oct-04 0:07
mnowok12-Oct-04 0:07 
Generali can write to the registry, yaay! Pin
yageroy19-Jun-04 23:33
yageroy19-Jun-04 23:33 
GeneralUse in DLL that does not use MFC Pin
ltang724-Apr-04 19:01
ltang724-Apr-04 19:01 
General"Simple" is great! Pin
XeonSniper13-Apr-04 17:40
XeonSniper13-Apr-04 17:40 
Generalloading .reg file programmatically Pin
bruno leclerc18-Mar-04 1:14
bruno leclerc18-Mar-04 1:14 
GeneralCRegStdDWORD instead of CRegStdWORD Pin
ujohnc0016-Mar-04 10:11
ujohnc0016-Mar-04 10:11 
GeneralGreat! thank you Pin
PMartos18-Dec-03 9:14
PMartos18-Dec-03 9:14 
GeneralFelicidades :) Pin
Torres O.17-Dec-03 8:41
Torres O.17-Dec-03 8:41 
QuestionBug? Pin
Igor Vigdorchik16-Dec-03 9:29
Igor Vigdorchik16-Dec-03 9:29 

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.