CRegConfig -- a Class to Easily Keep Your Data in the Registry

Friday Mar 21st 2003 by Alexander Hritonenkov
Share:

Read and write data in the Registry.

Environment: ATL/MFC, Windows

Well, here is a class to easily read/write your data from/to the Registry. It's quite simple to use and it's very powerful. Common features are:

  • CRecordset-like structure.
  • Autoload and autosave configuration.
  • Support for STRING, DWORD, and BINARY data.
  • Adding new data is very easy.
  • Warning level 4 complaint.

Here are the main parts of the CRegConfig class:

  • Constructor (initialize base class object here to allow autoload).
  • SetRegKey() function—to set a Registry key, where you will keep your settings.
  • Load() and Save() functions—for loading and saving.
  • Transfer() functions—they are taking care of each data member.
  • TransferAllData() pure virtual function—its purpose is to handle loading and saving of all your data members.
  • Data members.

How does it all work? The CRegConfig class is abstract (it has one pure virtual function). So, it cannot be instantiated directly. You should derive your class from it and override this function. Common steps are:

  • Derive your own class from CRegConfig (if you want to use autoload, supply a special constructor).
  • Add all data members you need to keep in the Registry.
  • Override the TransferAllData() function so that it will call the Transfer() function for each of your data members.
  • Instantiate your class and enjoy.

Let's look at the technique in more details:

class CMyRegConfig : public CRegConfig
{
public:
  CMyRegConfig();
  virtual ~CMyRegConfig();

  // data members
  DWORD m_dwValue;
  CString m_strValue;
  CByteArray m_bData;

protected:
  virtual void TransferAllData(BOOL bSave);
  // overridden pure virtual function to save and load our data
};

Next, the implementation:

CMyRegConfig::CMyRegConfig() : CRegConfig(HKEY_LOCAL_MACHINE,
                                          "Software\\test")

  // initializing our class to operate with the
  // "HKLM\software\test" registry key
{
  // set default values for our settings
  // they are used on the first start (if data is not present
  // in then Registry)
  m_dwValue=100;
  m_strValue="Sample string";

  Load();    // we can use autoload because we have initialized
             // the CRegConfig class
}

CMyRegConfig::~CMyRegConfig()
{
  Save();    // you can add automatic saving of config on
             // destruction
}

void CMyRegConfig::TransferAllData(BOOL bSave)
{
  // Add a Transfer() function for each data member in your class
  Transfer(bSave, "Dword", m_dwValue);
  // The first parameter is always bSave.
  // The second parameter is a value name in the Registry.
  // The third one is a reference to a data member.
  Transfer(bSave, "String", m_strValue);
  Transfer(bSave, "Binary", m_bData);
}

The next step is declaring the CMyRegConfig object. It will read settings from Registry during construction. Then, you can access them as usual member variables: you can read and modify them. Then, during destruction, this class will save your data. Also, you can always load/save data manually.

If you will need to add one more data member to the configuration, just add a data member to your class and add a corresponding Transfer() call to the TransferAllData() function.

That's all. I hope it will help you.

Downloads

Download demo project - 17 Kb
Download source - 2 Kb
Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved