CRegBinding – An alternative Registry Class

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

This is another Registry Class.
CRegBinding is geared to make class member variables persistent in the registry.
CRegBinding is easy to use.

The following features are implemented:

1. Automatic variable binding

Adding persistence to variables

Every value of registered variables would automatically read on startup.

On class destruction the values of the variables are written back to the registry.

2. Window state binding

Adding persistence to your CWnd object

Binding a CWnd to the CRegBinding object lets you save the window state, size and position in the registry.

How to use CRegBinding

In the Debug build the code contains many check routines that would detect improper use of the class object.

Step 1 – Place a CRegBinding member in your class declaration

In a class derived from CObject you can decide which member variables you want
to bind to a CRegBinding object.Set the CRegBinding class member behind the
last structure member.

For instance:


class A : public CDialog // (CWnd)
{
int a;
CByteArray b;
CString c;
DWORD dw;
CString d;
// .
// .
// .
// .
// place the CRegBinding object here
CRegBinding rs;

A();
};

Step 2 – Make the bindings

After you have declared the class ‘A’ you should make the bindings in the
class constructor or any other initializing member function.
The third optional parameter of the Bind() function lets you specify
a default value for initialization.


A::A() : rs(“My section”)
{
rs.Bind(a, “a”, 0);
rs.Bind(dw, “DW”, 0xfffffffa);
rs.Bind(b, “b”);
rs.Bind(c, “C-Key”, “CCC”);
rs.Bind(d, “D-Key”);

CreateEx( … );

// After you have created the window, you can also bind the CWnd object
rs.Bind(this, “Persistence Window”);
}

If the window or class is about to be destroyed the current values of the
variables are written back to the registry.

The Source

The library itself comes as two source files, CRegBinding.cpp and CRegBinding.h.
The header defines the CRegBinding class with the following members:


CRegBinding::CRegBinding(const char *section)

Construct a new binding object taking the name of the section for the
specified keys in the registration database


void CRegBinding::Bind(int &value, const char *name, const int def)
void CRegBinding::Bind(DWORD &value, const char *name, const DWORD def)
void CRegBinding::Bind(CString &value, const char *name, const char *def)
void CRegBinding::Bind(CByteArray &value, const char *name, const CByteArray *def)
void CRegBinding::Bind(CWnd *value, const char *name)

Bind member variables of various types.

value:

The class member variable to bind.

name:

The name of the key in the registry.

def:

Optionally you can specify a default value if the key not exist.


void CRegBinding::Write(const BOOL clear)

Write data explicit to the registry. The parameter specifies whether
to delete all bindings.

Download demo project – 22 KB

Date Last Updated: February 5, 1999

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read