Click here to Skip to main content
Click here to Skip to main content

A handy class to make use of Windows Registry

By , 15 Aug 2004
Rate this:
Please Sign up or sign in to vote.

Sources update

The sources have been updated. (Hopefully) This will make them compilable on VC 7+. I couldn't test it with VC 7, but I expect it will be OK now. For more info take a look what was the problem.

New Additions

This class has been modified:

  • Improved internal logic.
  • Improved support for writing structured data to registry.
  • Added possibility to see internal execution flow (e.g., when data is read from and written to the registry).
  • Added support for HKEY_LOCAL_MACHINE, HKEY_USERS, and all the others...

In short, in order to store a structure in registry, you only need to do like this:

SomeStruct X;
//set struct values...

In order to read back the stored structure:

SomeStruct X;
//set struct values...
    //Ok, X contains the read data...
}else {
    //error reading struct.

In order to use HKEY_LOCAL_MACHINE or other, you can pass a second parameter of type registry::Key to the registry constructor:

registry reg("Sowtware\\Whatever",registry::hkey_local_machine);

The second parameter is optional, it defaults to registry::hkey_current_user.

In order to see internal execution flow, you may #define WANT_TRACE. And in console mode, you'll see when and what objects are created, and when values read to/ written from the registry. There was added a new project with_trace to see that.

//with trace

#define WANT_TRACE //define this to be able to see internal details...

#include "registry.h"
#include "string.h"
#include <STRING>

struct Something{
    int area;
    float height;

int main(int, char*)
    registry reg("Software\\Company");
        Something X;

    Something Z;
        printf("area is: %i\nheight is: %f\n", Z.area,Z.height);

    return 0;

It produces the following output:

with_trace output


First of all, you need only one file registry.h, that's included in the source distribution. Read on...

//Dirty and fast howto

#include "registry.h"
#include "string.h"

int main(int, char*){

    registry reg("Software\\Company");
    reg["Language"]="English";        //set value
    printf("The address is: %s\n",(char*)reg["Address"]); 
    //prints nothing unless you put something in
    return 0;

A more descriptive howto:

//Long story:

#include "registry.h"
#include "string.h"

struct ServerAddress{
    char name[64];
    char ip[15];
    short port;

int main(int,char*){

    //first step is to create our home registry 
    //record(or open it if exists)
    //the constructor accepts a string and if you do:
    registry settings("Software\\MyCoolApp");
    //you open HKEY_CURRENT_USER\Sowtware\MyCoolApp

    //Due to simplicity this class is desined to deal with only 
    //HKEY_CURRENT_USER registry group. 
    //You may, however, modify this behavior if you need

    //To make sure that the home key has been created or 
    //opened you can test it like this:

        //error code goes here...

    //The next step is to create (or open if exists) 
    //a value key:
    //the official way is like this: (for another, 
    //simpler way, read below.)

    registry::iterator lang=settings["Language"];    
    //Personally I don't use it this way - too much to type...

    //and then you can retrive value of "Language" key 
    //as follows:

    printf("The selected language is: \"%s\"\n",(char*)lang);

    //if you want to modify a value you can do simply like this:


    //Internally this class stores values as 
    //binary data and therefore 
    //you are not limited to strings only.

    // Example:

    ServerAddress MyCoolServer;

    //now let's put this record into registry key "server"
    //it can be simply done like this:

    registry::iterator server=settings["server"];
    server=string((const char *)(&MyCoolServer),sizeof(MyCoolServer));

    //now let's test by retriving the structure back:

    ServerAddress serAdr;
    const string &FromRegistry=server;
    //copy data only if it's of the size of the struct ServerAddress
        memcpy(&serAdr,FromRegistry.c_str(), sizeof(ServerAddress));
        //now let's see what we have (see a pic below):
        printf("Print out for my server:\n");
        printf("\tserver's host name is:  %s\n",;
        printf("\tserver's ip address is: %s\n",    serAdr.ip);
        printf("\tserver's port is:       %u\n\n",  serAdr.port);
        //error code goes here...
    return 0;

Sample Image - long_story.jpg

Pic.1 Output from a more descriptive howto.

I coded these few lines with simplicity in mind. I like the style, when I can assign value to a registry key as to a usual variable. I will describe shortly how I use this code in my projects. I put a member variable reg (instance of registry) to my app's class (there is a reason, read below) that needs access to the registry. Then, in the class' constructor, I instantiate reg variable to the registry home key. Since there is no default constructor in the registry class, you may do it like this:

class my_app{
    registry reg;
    string UserName;
    my_app() : reg("Software\\my_app"){ // <--
        UserName = reg["UserName"]; //read value
        //store value back to the registry
        reg["UserName"] = UserName;

In the preceding line, you actually don't need to create a separate string for UserName - you may have a member variable of registry::iterator type, that will be automatically stored to the registry when your class is destructed (see the final example at the bottom of the page).

And then, whenever you need, you can do like this:

string UserName=reg["User"];

that's really simple...


Now, a little background on how it works internally. This is actually a very lightweight class. It makes less API calls than you would probably do.

For example, the line:


will only once physically access registry on destruction of the returned key-value to assign value "nothing" to key "something".

Same happens with the next lines (only one API call when the group goes out of scope):

registry::iterator name=settings["UserName"];
name="Someone Else";

Basically, if you intend to use this class, you should be aware that statement reg["something"]="nothing"; doesn't modify anything in registry by itself. The value will be set only when the statement goes out of scope, like this:

    printf("The username is: %s\n", (char*)reg["UserName"]); 
    //it will print the value that was before the previous line
}//at this point the value Bill is in the registry
printf("The username is: %s\n", (char*)reg["UserName"]); //now it prints Bill;

In order to force it to commit to the registry at some point, you can use flush() method like this:

registry::iterator name=settings["UserName"];
name.flush(); //at this point it will write value "John" to registry.

The same applies to reading values - they are retrieved only when you request them, and only once! So, if you know (or think) that something else (perhaps Earth's magnetic field Smile | :) ) has modified a key's value, you may use refresh() method. But, be carfull!! Anything you have assigned before will not be committed (it's overwritten by the returned value from registry).

And a very important point. When the instance of registry is destructed, or goes out of scope, then all the values you created from it will not have access to the registry. That is, all iterators on a given registry instance should be destructed before their parent registry instance. That's why I use this class as a member of another class. When the parent class is destroyed, it doesn't need access to the registry, neither is it possible at that point.

That's it. Hope it will be useful to someone.

A better example

#include "registry.h"
#include <iostream>

class my_app{
    registry reg;
    registry::iterator UserName;
    void SetUserName(const char * name){
        UserName = name;
    const char * GetUserName(){
        return (const char *) UserName;

int main (int, char *){
    my_app App;
    cout<<"The username is: "<<App.GetUserName()
        <<endl<<"Thanks for playing..."<<endl;
    return 0;


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author


Canada Canada
Big Grin | :-D

Comments and Discussions

GeneralSource does not work with STLPORT PinmemberRalfGriggel27-Nov-06 4:39 
GeneralRe: Source does not work with STLPORT Pinmember__PPS__27-Nov-06 5:57 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140421.2 | Last Updated 16 Aug 2004
Article Copyright 2004 by __PPS__
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid