Programming C++Builder Manipulate Windows Registry--Using TRegistryIniFile and TRegistry

Many 32-bit Windows applications store their information in the system Registry instead of ini files because the Registry is hierarchical and doesn't suffer from the size limitations of ini files. If you are accustomed to using ini files and want to move your configuration information to the Registry instead, you can use the TRegistryIniFile class. You may also want to use TRegistryIniFile in cross-platform applications if you want to use the system Registry on Windows and an ini file on Linux. You can write most of your application so that it uses the TCustomIniFile type. You need only conditionalize the code that creates an instance of TRegistryIniFile (on Windows) or TMemIniFile (on Linux) and assigns it to the TCustomIniFile your application uses.

TRegistryIniFile makes Registry entries look like ini file entries. All the methods from TIniFile and TMemIniFile (read and write) exist in TRegistryIniFile.
When you construct a TRegistryIniFile object, the parameter you pass to the constructor (corresponding to the filename for an IniFile or TMemIniFile object) becomes a key value under the user key in the registry. All sections and values branch from that root. TRegistryIniFile simplifies the Registry interface considerably, so you may want to use it instead of the TRegistry component even if you aren't porting existing code or writing a cross-platform application.

If you are writing a Windows-only application and are comfortable with the structure of the system Registry, you can use TRegistry. Unlike TRegistryIniFile, which uses the same properties and methods of other ini file components, the properties and methods of TRegistry correspond more directly to the structure of the system Registry. For example, TRegistry lets you specify both the root key and subkey, while TRegistry assumes HKEY_CURRENT_USER as a root key. In addition to methods for opening, closing, saving, moving, copying, and deleting keys, TRegistry lets you specify the access level you want to use.

Note: TRegistry is not available for cross-platform programming.

The following example retrieves a value from a registry entry:


AnsiString GetRegistryValue(AnsiString KeyName) {   AnsiString S;   TRegistry *Registry = new TRegistry(KEY_READ);
  try   {
    Registry->RootKey = HKEY_LOCAL_MACHINE;     // False because we do not want to create it if it doesn't exist     Registry->OpenKey(KeyName,false);     S = Registry->ReadString("VALUE1");   }   __finally   {    delete Registry;   }   return S; }

Add comment

Security code

Programming - C++Builder