A bit on terminology first. While Microsoft refers to the registry as being essentially a tree of keys where each key contains a number of values, I find this a bit confusing and prefer to think of the registry as consisting of sections rather than keys. Every section has a number of key-value pairs. This sets the background for explaining what makes the CRegistry class I wrote different from the ones already on Code Guru. Three items immediately stand out, namely navigation, iteration, and access.
The class provides a handy way of accessing data in three most commonly used areas of the registry. These are the defaults for all users and the overriden settings for the currently logged on one as well as the properties for the local machine (the one on which the program is currently executing). By passing one of modeCurrentUserPrefs, modeDefUserPrefs, and modeLocalMachineProps to the constructor of CRegistry (together with you application's name) you will effectively be instructing a newly created instance of the class to operate in an appropriate part of the registry. For modeCurrentUserPrefs the section taken to be the current one is "HKEY_CURRENT_USER\Software\YourAppName". Navigating back and forth is achieved through a stack-like interface -- Descend(const char* Section) and Ascend(). This is useful for ensuring that each part of an application stores its settings in a private portion of the registry. You would typically create a single instance of CRegistry for the whole app and then instruct each high level app object to serialize into the current section. These objects would then Descend() into areas of their own, access settings, instruct their children to do the same, and then Ascend() back to their parents' sections.
It is possible to query for an existance of a key or a subsection within the current section by using KeyExists(const char* Key) and SectionExists(const char* Section) methods. Note that Descend(const char* Section) will create the desired section if one does not already exist.
Enumerating subsections and keys in the current section is possible through the use of GetFirst...() and GetNext...() methods. These are similar to GetHeadPosition() and GetNext() methods in the MFC's CObList class.
Accessing the keys and their values is done through Store(..) and Restore(..) methods. All of the basic types (signed/unsigned, char, short, int, long, float, double) plus CString are supported. These are almost identical to the types supported by MFC's CArchive class. If you can serialize to a CArchive you can store the same information in a similar manner in a CRegistry.
The following are the important parts of the CRegistry header:
class CRegistry
{
public:
enum
{
modeCurrentUserPrefs = 0,
modeDefUserPrefs,
modeLocalMachineProps,
accessRead = 0x1,
accessWrite = 0x2,
};
// Construction/Destruction
public:
CRegistry(const char* ApplicationName, int Mode = modeCurrentUserPrefs, int Access = accessRead | accessWrite);
CRegistry(HKEY Area, const char* RootSection, int Access = accessRead | accessWrite);
~CRegistry();
// Attributes
public:
void SetRootSection(const char* ApplicationName, int Mode = modeCurrentUserPrefs);
void SetRootSection(HKEY Area, const char* RootSection);
void SetAccess(int Access = accessRead | accessWrite);
// Iteration
public:
POSITION GetFirstKeyPos();
CString GetNextKey(POSITION& Pos);
POSITION GetFirstSectionPos();
CString GetNextSection(POSITION& Pos);
// Operations
public:
void Descend(const char* Section);
void Ascend();
bool KeyExists(const char* Key);
bool SectionExists(const char* Section);
void Store(const char* Key, signed char Value);
void Store(const char* Key, unsigned char Value);
void Store(const char* Key, signed short Value);
void Store(const char* Key, unsigned short Value);
void Store(const char* Key, signed int Value);
void Store(const char* Key, unsigned int Value);
void Store(const char* Key, signed long Value);
void Store(const char* Key, unsigned long Value);
void Store(const char* Key, float Value);
void Store(const char* Key, double Value);
void Store(const char* Key, const CString& Value);
void Restore(const char* Key, signed char& Value);
void Restore(const char* Key, unsigned char& Value);
void Restore(const char* Key, signed short& Value);
void Restore(const char* Key, unsigned short& Value);
void Restore(const char* Key, signed int& Value);
void Restore(const char* Key, unsigned int& Value);
void Restore(const char* Key, signed long& Value);
void Restore(const char* Key, unsigned long& Value);
void Restore(const char* Key, float& Value);
void Restore(const char* Key, double& Value);
void Restore(const char* Key, CString& Value);
private:
...
};
Download Source Code and Example
Displays a dialog with an edit box. Position of the dialog and
the text in the edit box are stored in the registry.
Last updated: 24 May 1998
| Goto HomePage |
|
Contact me: zafir@home.com
|