CRegBinding - An alternative Registry Class | CodeGuru

CRegBinding – An alternative Registry Class

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 5, 1999
2 minute read
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.

Advertisement

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.