CRegBinding - An alternative Registry Class


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

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;      


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.


The class member variable to bind.


The name of the key in the registry.


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


  • about registry parameters...

    Posted by Legacy on 08/17/2003 07:00am

    Originally posted by: rezbipul

    after knowing the registry functions with the help of msdn,
    i encounter the primary problem:
    we know registry consists of a collection of 'name = value' pairs.
    eg. 'content/type = xml' (say)
    i want to learn all such names and the set of valid values for each name so that when a new software (eg Adobe photoshop) is installed it is able to instruct windows to open all files of type .psd automatically in adobe photoshop. also when some .psd files are copied to another hd, windows is also able to automatically show the appropriate icon in windows explorer.
    i want to ve all possible name = value pairs for registry not just content/type.
    thank u.

  • Great!, but with XP i have a problem.

    Posted by Legacy on 07/04/2002 07:00am

    Originally posted by: Willem Hofmans

    Thanks for this great class!

    I have one problem with windows XP. After linking i get the following error:
    Debug Assertion Failed!!
    file: CRegBinding.cpp
    line: 231

    The following code is located at line 231: ASSERT(0);
    if i comment this code out, the problem is over. But what does this code, is it bad to comment this code out? is there another way to solve the problem.



  • Problem binding CWnd properties

    Posted by Legacy on 11/22/2000 08:00am

    Originally posted by: Lars Hegde


    The class is working great, but I have some problems binding my Main window to keep the position.
    I the program is closed in minimized state, it won't show after the next startup...

    Any ideas??


  • Good Idea but...

    Posted by Legacy on 03/07/2000 08:00am

    Originally posted by: anon

    This is a great idea for persistence, but has one
    potentially fatal flaw...

    There is no guarantee as to the order of destruction of
    class members. It is entirely possible some objects will be
    destroyed before the CRegBind class object is.

    The solution to this is to explicitly write the data out
    during the destruction of the CRegBind object. eg.

    CMyObject::~CMyObject ()
    regbind.WriteToRegistry () // or something appropriate

    This will stop potential crashes occuring when MS upgrades
    their compiler, or you decide to use a different compiler
    for development.


    Dont let to much get written to the registry, its a good
    idea to keep the amount of data in a registry item under
    1024Kb (I think). It might be a nice enhancement to allow
    the redirection of the data out to a .ini file or some other
    type of file for this case.

  • PCH problems.

    Posted by Legacy on 03/01/2000 08:00am

    Originally posted by: Brent Mantooth

    I really like this class, but I have a problem that I can't figure out. If I compile my program in debug mode everything works fine, but if I compile in release mode I get the following error

    D:\Code\...\CRegBinding.cpp(346) : fatal error C1010: unexpected end of file while looking for precompiled header directive

    the PCH settings for debug and release are the same - any thing I should be looking for?


  • Very useful and easy to use.

    Posted by Legacy on 12/01/1999 08:00am

    Originally posted by: Robert Laumeyer

    My compliments on your very nice work with this class. I have used it in several areas either as a debugger or for persistent variables. You can just drop it in and not have to worry about it.

    I would like to be able to use this to store derived class members though.
    I haven't ever played with Hook/UnHook so maybe I am missing something obvious.

    Thanks for the Class.

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date