views:

416

answers:

4

In the simplest possible terms (I'm an occasional programmer who lacks up-to-date detailed programming knowledge) can someone explain the simplest way to make use of the registry in codegear C++ (2007).

I have a line of code in an old (OLD!) program I wrote which is causing a significant delay in startup...

DLB->Directory=pIniFile->ReadString("Options","Last Directory","no key!");

The code is making use of an ini file. I would like to be able to use the registry instead (to write variables such as the last directory the application was using)

But the specifics are not important. I'd just like a generic how-to about using the registry that's specific to codegear c++ builder.

I've googled this, but as usual with this type of thing I get lots of pages about c++ builder and a few pages about the windows registry, but no pages that explain how to use one with the other.

+6  A: 

Use the TRegistry class... (include registry.hpp)

//Untested, but something like...
TRegistry *reg = new TRegistry;
reg->RootKey = HKEY_CURRENT_USER; // Or whatever root you want to use
reg->OpenKey("theKey",true);
reg->ReadString("theParam",defaultValue);
reg->CloseKey();

Note, opening and reading a ini file is usually pretty fast, so maybe you need to test your assumption that the reading of the ini is actually your problem, I don't think that just grabbing your directory name from the registry instead is going to fix your problem.

Tim Jarvis
+1  A: 

Please see the documentation for the QSettings class from the Qt 4.5 library. It will allow you to load and store your program's configuration settings easily and in a cross-platform manner. The Windows implementation uses the Windows registry for loading and storing your program's configuration data. On other platforms, the platform's preferred, native mechanism for storing configuration data will be used. This is far better than interacting with the Windows registry directly, as you will not be tied to a specific platform.

Michael Aaron Safyan
+2  A: 

Tim is right but an even simpler class to use is TIniRegFile but it is also more limited in what you can do.

Toby Allen
+1  A: 

Include the Registry.hpp file:

#include <Registry.hpp>

Then in any function you have, you can write the following to read the value:

String __fastcall ReadRegistryString(const String &key, const String &name, 
    const String &def)
{
    TRegistry *reg = new TRegistry();
    String result;

    try {
        reg->RootKey = HKEY_CURRENT_USER;

        if (reg->OpenKeyReadOnly(key)) {
            result = reg->ReadString(name, def);
            reg->CloseKey();
        }
    }
    __finally {
        delete reg;
    }

    return result;
}

So reading the value should be as easy as:

ShowMessage(ReadRegistryString("Options", "Last Directory", "none"));

You can use the following to write the value:

void __fastcall WriteRegistryString(const String &key, const String &name, 
    const String &value)
{
    TRegistry *reg = new TRegistry();

    try {
        reg->RootKey = HKEY_CURRENT_USER;

        if (reg->OpenKey(key, true)) {
            reg->WriteString(name, value);
            reg->CloseKey();
        }
    }
    __finally {
        delete reg;
    }
}

Should be self explaining, remembering the try ... finally is actually really helpful when using the VCL TRegistry class.

Edit

I've heard that .ini files are stored in the registry in Windows, so if you want the speed advantage of ini files you should call them something else - like .cfg

This is something I've heard from an although reliable source, I haven't tested it myself.

TommyA