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
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,
CString str = mystring; mystring = "TestValue"; mystring += "!"; myrect = CRect(100,200,300,400); LPRECT lpR = (LPRECT)myrect;
myword = GetTickCounts(); 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
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:
CRegDWORD(CString key, DWORD def = 0, BOOL force = FALSE,
HKEY base = HKEY_CURRENT_USER);
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
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 :
- added classes for use without MFC: CRegStdString, CRegStdDWORD
- now compiles for UNICODE too
- 9. July 2002 :
- Corrected a bug found by Hans Dietrich
- Extended the classes with two methods: removeKey() and removeValue()
Daniel Andersson wrote a templatized version of this class. See his article here.