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 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



Comments

  • about registry parameters...

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

    Originally posted by: rezbipul

    hello,
    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.

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

    Posted by Legacy on 07/04/2002 12: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.

    Thanks,

    Willem

    Reply
  • Problem binding CWnd properties

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

    Originally posted by: Lars Hegde

    Hey!
    
    

    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??

    Lars

    Reply
  • Good Idea but...

    Posted by Legacy on 03/07/2000 12: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.


    BTW.

    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.

    Reply
  • PCH problems.

    Posted by Legacy on 03/01/2000 12: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

    CRegBinding.cpp
    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?

    thanks

    Reply
  • Very useful and easy to use.

    Posted by Legacy on 12/01/1999 12: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.

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

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Email is the most common communication vehicle used by organizations of all shapes and sizes. Among the billions of email messages sent every day are sensitive information, critical requests, and other essential business data. IT staff bear the burden of ensuring the confidentiality, integrity, and availability of the information contained within the communication. This white paper explores the email security landscape, an assessment of the threats organizations face,  and the building blocks of an effective …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds