Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / WTL
Article

Access Registry Settings Declaratively

Rate me:
Please Sign up or sign in to vote.
4.59/5 (25 votes)
12 Apr 2004CPOL5 min read 91.9K   523   17   21
A set of macros and classes that allow declarative access to registry settings.

Introduction

This is my first posting on CodeProject, so take it easy on me ;-). This is a set of macros and classes that I designed that make it much easier to deal with the registry. I've always liked the way that ATL makes a lot of tedious coding tasks appear to take on a declarative form and that is the spirit in which these classes were designed.

Where's the beef?

So, what problem am I trying to solve? Well, consider the following code that tries to access the registry to get a simple string value:

HKEY hSetting = NULL;
if (::RegCreateKey(HKEY_CURRENT_USER, 
      _T("Software\\CodeProject\\RegistrySettings"), 
      &hSetting) == ERROR_SUCCESS)
{
  DWORD cbData = 0;
  if (::RegQueryValueEx(hSetting, _T("Message"), NULL, 
            NULL, NULL, &cbData) == ERROR_SUCCESS)
  {
    LPTSTR szValue = _alloca(cbData);
    ::RegQueryValueEx(hSetting, _T("Message"), 
          NULL, NULL, (LPBYTE) szValue, &cbData);
  }
}

Painful to say the least, and this doesn't even contain full error checking, and if the value never existed, you have to write all kinds of code to provide defaults. Well, that is what my code provides. It doesn't have a spiffy name, so I have to keep referring to it as my code or my classes / macros, so let's just call it CoolReg from here on out. CoolReg provides a way to declaratively access the registry so that the code above becomes:

CString strMessage = UserSettings.Message;

How can I accomplish this amazing feat? With some macros and a little __declspec(property) kung-fu action! Let's take a look at how to use CoolReg from within an application.

BEGIN_SETTINGS_ROOT(User, HKEY_CURRENT_USER, 
  _T("Software\\CodeProject\\RegistrySettings"))
  DECLARE_SETTING(CString, Message, 
  _T("This application has never been run"))
  DECLARE_SETTING(int, RunCount, 0)
  BEGIN_SETTINGS_GROUP(MoreSettings, User)
    DECLARE_SETTING(int, AnotherSetting, 80)
  END_SETTINGS_GROUP(MoreSettings)
END_SETTINGS_ROOT(User)
 
BEGIN_SETTINGS_ROOT(Machine, HKEY_LOCAL_MACHINE, 
       _T("SOFTWARE\\CodeProject\\RegistrySettings"))
  // no settings defined under HKEY_LOCAL_MACHINE yet
END_SETTINGS_ROOT(Machine)
 
DEFINE_SETTINGS_ROOT(User)
DEFINE_SETTINGS_ROOT(Machine)

All that kung-fu is wrapped up in the macros used above (which is all given in the Settings.h file included in the demo project). The BEGIN_SETTINGS_ROOT macro defines the base of a registry tree. The first argument is the name of the settings tree as seen in our source code (but in the actual source, this name will be appended with Settings, so that to access this tree, we must type UserSettings). The second argument is the registry key that the one we are defining will live under, and the third argument is the actual path to the registry key we are defining.

After that, we have several DECLARE_SETTING macros being used all of which define some settings. A setting is defined by its type, name and default. In the first DECLARE_SETTING, you can see that we are using CString as the type, Message as the name and a string literal as the default. The name that you specify becomes the name of both the C++ property and the registry value.

After a couple of settings are defined, we use the BEGIN_SETTINGS_GROUP macro to define a new sub key under the current key. The new sub key will be called, quite logically, MoreSettings and its parent must be given as the second argument of the macro which is of course User. After we are done defining the settings that make up this group, we must use the END_SETTINGS_GROUP macro which will finish off the class we defined (which is really what all these macros are doing internally). Lastly, we must use the DEFINE_SETTINGS_ROOT to give some storage to the roots that we defined above (though in actuality, these classes use very little storage space).

Enough, I Wanna Use It! How?

Well, it's actually really easy. To access the Message setting, simply type:

UserSettings.Message

And to access AnotherSetting, simply type:

UserSettings.MoreSettings.AnotherSetting

That's about all there is to it really.

Other cool stuff...

The DECLARE_SETTING_EX macro provides some extra functionality that most people will find useful. Using this macro allows you to define your settings intended type in the registry. This allows you to declare a CString setting but set its registry type to REG_EXPAND_SZ. When you then try to access this property from your code, CoolReg will automagically expand the environment variables without you ever having to call ExpandEnvironmentStrings (which many people forget to do). DECLARE_SETTING_EX and its companion DECLARE_SETTINGS_GROUP_EX also allow you the flexibility of specifying what name the setting / group should be mapped to in the registry.

Limitations

Well, as with anything free, there are certain limitations (though not in your usage, feel free to use this wherever you like and modify to your heart's content). First, the names of your properties must be valid C++ identifiers, but registry property names have no such restriction, so if you are trying to graft this onto your existing applications, you may have to use the more verbose _EX variants of these macros. I also had some code that would handle arbitrary data types through the use of templates, but when I wrote the sample app, I realized that this template code overrode my handling of CString. What is really required is a template specialization method instead of the method I am currently using (which just relies on method overloading). This would be easy to provide, but I got a little bit lazy and decided to just comment out the template code that I had in place, but the slightly brave among you should find it easy to change this.

Update: doh!!! No one asked for it, but I decided to not be lazy and rewrote some of the internal classes and macros to use the template specialization method I mentioned above to achieve the benefits of arbitrary (well, not quite arbitrary) object storage. This is no longer a limitation, but you should be aware that if you try to use any types that contain pointers (_bstr_t, CComString, etc...), they will not work. This support is strictly for storing objects containing only simple types (such as WINDOWPLACEMENTs or RECTs, etc...). The demo contains a sample of how to do this.

Lastly, if you try to do something like the following:

UserSettings.RunCount++;

it will simply not work. This is a limitation of __declspec(property), but the slightly less friendly:

UserSettings.RunCount += 1;

will work just fine. This may only be an issue with VC6 as I didn't test this under VC7, but the compiler will die with an internal compiler error (C1001) if you attempt to use the first code fragment above. This error basically means that the compiler cannot generate correct code for the first construct (which is understandable I guess).

One thing to realize is that every time you access one of these properties, an access is made to the registry. There is no caching of values whatsoever, so invoke these properties sparingly (in fact, in the above code, the RunCount property is accessed twice, once to read the initial value and another to store the incremented value).

That's about it. Enjoy!!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAntivirus + Repairer Pin
praveen.vinny20-Sep-08 3:17
praveen.vinny20-Sep-08 3:17 
GeneralProblem on VC71 Pin
Alexander Shevchenko10-Nov-04 2:05
Alexander Shevchenko10-Nov-04 2:05 
Generalatlapp.h Pin
Vectorio4-Nov-04 5:10
Vectorio4-Nov-04 5:10 
GeneralRe: atlapp.h Pin
Lonnie McCullough4-Nov-04 8:01
Lonnie McCullough4-Nov-04 8:01 
Generaljust briliant Pin
MyBlindy22-Apr-04 18:41
MyBlindy22-Apr-04 18:41 
GeneralRe: just briliant Pin
Bob Stanneveld8-Jun-05 21:55
Bob Stanneveld8-Jun-05 21:55 
GeneralGot my five too Pin
Rodrigo Strauss14-Apr-04 15:40
Rodrigo Strauss14-Apr-04 15:40 
GeneralSimilar. Pin
WREY14-Apr-04 6:56
WREY14-Apr-04 6:56 
GeneralRe: Similar. Pin
Lonnie McCullough14-Apr-04 7:09
Lonnie McCullough14-Apr-04 7:09 
GeneralRe: Scrolling annoyance. Pin
WREY14-Apr-04 7:53
WREY14-Apr-04 7:53 
GeneralRe: Scrolling annoyance. Pin
Lonnie McCullough14-Apr-04 8:10
Lonnie McCullough14-Apr-04 8:10 
GeneralEND_SETTINGS_ROOT(User) Pin
Darren Schroeder13-Apr-04 15:49
Darren Schroeder13-Apr-04 15:49 
In your demo code as well as your article you demonstrate your code. One question about it. Shouldn't your second END_SETTINGS_ROOT(User) actually be END_SETTINGS_ROOT(Machine) ? Otherwise you have no END_SETTINGS_ROOT to correspond to your BEGIN_SETTINGS_ROOT.

Nice job.

Darren
GeneralRe: END_SETTINGS_ROOT(User) Pin
Darren Schroeder13-Apr-04 15:50
Darren Schroeder13-Apr-04 15:50 
GeneralRe: END_SETTINGS_ROOT(User) Pin
Lonnie McCullough13-Apr-04 16:15
Lonnie McCullough13-Apr-04 16:15 
GeneralGreat work Pin
Rob Manderson13-Apr-04 13:17
protectorRob Manderson13-Apr-04 13:17 
GeneralOoopsss Pin
Lonnie McCullough13-Apr-04 13:08
Lonnie McCullough13-Apr-04 13:08 
GeneralRe: Ooopsss Pin
Lonnie McCullough13-Apr-04 16:15
Lonnie McCullough13-Apr-04 16:15 
GeneralRe: Ooopsss Pin
Rodrigo Strauss14-Apr-04 15:45
Rodrigo Strauss14-Apr-04 15:45 
GeneralRe: Ooopsss Pin
Lonnie McCullough14-Apr-04 17:30
Lonnie McCullough14-Apr-04 17:30 
GeneralGreat! Pin
Daniel Turini13-Apr-04 12:30
Daniel Turini13-Apr-04 12:30 
GeneralRe: Great! Pin
John M. Drescher13-Apr-04 17:54
John M. Drescher13-Apr-04 17:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.