How to use the registry?

In the simplest possible terms (I'm an occasional programmer who lacks detailed modern programming knowledge), someone can explain the easiest way to use the registry in C ++ (2007) codec.

I have a line of code in an old (OLD!) Program that I wrote, which causes a significant delay on startup ...

DLB-> Directory = pIniFile-> ReadString ("Options", "Last Directory", "No Key!");

The code uses an INI file. I would like to be able to use the registry instead (for writing variables like the last directory that the application used)

But the features are not important. I would just like to get a general way to use the codegear C ++ builder specific registry.

I searched for this, but as usual with this type I get a lot of pages about C ++ builder and a few pages about the Windows registry, but not pages explaining how to use them with another.

+4
source share
4 answers

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 an ini file is usually pretty quick, so maybe you need to check your assumption that reading ini is actually your problem, I don’t think that just grabbing your directory name from the registry will fix your problem.

+10
source

Include the Registry.hpp file:

 #include <Registry.hpp> 

Then, in any function, 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; } 

Therefore, reading the value should be as simple as:

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

You can use the following to record 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; } } 

There has to be self-updating, remembering the attempt ... is finally really useful when using the VCL class TRegistry.

Edit

I heard that .ini files are stored in the registry on Windows, so if you need the speed advantage of ini files, you should call them something else - for example .cfg

This is what I heard from a reliable source, I have not tested it myself.

+3
source

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

+2
source

See the documentation for the QSettings class from the Qt 4.5 Library . This will allow you to easily and easily download and save your program configuration settings and cross-platform. The Windows implementation uses the Windows registry to load and store program configuration data. Other platforms will use the preferred platform, their own mechanism for storing configuration data. This is much better than interacting with the Windows registry directly, as you will not be tied to a specific platform.

+1
source

All Articles