Registry Example Source Code (16 and 32-bit)

Here is an example of registry management code I wrote to play with
registry management..

unit Reg;

interface

Uses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, Buttons, ExtCtrls, Tabs, TypeDefs;

Var
   bSaveWindowPos         :  Boolean;
   bUsePassword           :  Boolean;

Const
   RootKey                = 'Software\Some_Software\Some_Product';

function  BuildRegistryTree( hKey : Longint; var aList : TStringList; szKey :
PChar ) : Boolean;
procedure ChangeRegistryKey( aKey : String; aValue : String );
procedure GetRegistryKey( aKey : String; var aValue : String; Default :
String );
function  GetRegistryInt( aKey : String; Default : Integer ) : Integer;

implementation

procedure ChangeRegistryKey( aKey : String; aValue : String );
Var
   szKey, szValue   : PStr;
begin
  szKey := StrAlloc( Length( aKey ) + 1 );
  szValue := StrAlloc( Length( aValue ) + 1 );
  StrPCopy( szKey, aKey );
  StrPCopy( szValue, aValue );
  RegSetValue( HKEY_CLASSES_ROOT, szKey, REG_SZ, szValue, StrLen( szValue ));
  StrDispose( szKey );
  StrDispose( szValue );
end;

procedure GetRegistryKey( aKey : String; var aValue : String; Default :
String );
Var
   szKey, szValue   : PStr;
   nRet, nSize      : LongInt;
begin
  szKey := StrAlloc( Length( aKey ) + 1 );
  szValue := StrAlloc( 1000 );
  StrPCopy( szKey, aKey );
  StrPCopy( szValue, aValue );
  nSize := 1000;
  nRet := RegQueryValue( HKEY_CLASSES_ROOT, szKey, szValue, nSize );
  if (nRet = ERROR_SUCCESS) then
     aValue := StrPas( szValue )
    else
     aValue := Default;
  StrDispose( szKey );
  StrDispose( szValue );
end;

function GetRegistryInt( aKey : String; Default : Integer ) : Integer;
Var
   szKey, szValue   : PStr;
   aString          : String;
   nRet, nSize      : LongInt;
begin
  szKey := StrAlloc( Length( aKey ) + 1 );
  szValue := StrAlloc( 32 );
  StrPCopy( szKey, aKey );
  nSize := 32;
  nRet := RegQueryValue( HKEY_CLASSES_ROOT, szKey, szValue, nSize );
  aString := StrPas( szValue );
  StrDispose( szKey );
  StrDispose( szValue );
  if (nRet = ERROR_SUCCESS) then
     GetRegistryInt := StrToInt( aString )
    else
     GetRegistryInt := Default;
end;

function BuildRegistryTree( hKey : LongInt; var aList : TStringList; szKey :
PChar ) : Boolean;
var
  hRoot          : LongInt;
  lItem          : LongInt;
  hError         : LongInt;
  pData          : PChar;
  aString        : String;

begin
  hRoot := 0;
  lItem := 0;
  pData := StrAlloc( 1024 );

  hError := ERROR_SUCCESS;

  if (RegOpenKey( hKey, szKey, hRoot ) = ERROR_SUCCESS) then
  begin
     while (hError = ERROR_SUCCESS) do
     begin
        hError := RegEnumKey( hRoot, lItem, pData, 1024 );

        if (hError = ERROR_SUCCESS) then
        begin
           aList.Add( StrPas( pData ));
           Inc(lItem);
        end;

     end;
     RegCloseKey( hRoot );
  end;

  StrDispose( pData );
end;

var
   RootString : String;
   aString    : String;

begin
     RootString := RootKey + '\Defaults';

     {********************************************************************
     *
     *  Get the bSaveWindowPos value
     *
     ********************************************************************}

     GetRegistryKey( RootString + '\SaveWindowPos', aString, 'False' );
     if (UpperCase(aString) = 'TRUE' ) then
        bSaveWindowPos := True
       else
        bSaveWindowPos := False;

     {********************************************************************
     *
     *  Get the bSaveWindowPos value
     *
     ********************************************************************}

     GetRegistryKey( RootString + '\UseDefaultWidth', aString, 'False' );
     if (UpperCase(aString) = 'TRUE' ) then
        bUsePassword := True
       else
        bUsePassword := False;
end.

Randy Dryburgh
Code...@aol.com
CDevelo...@gnn.com

The opinions expressed herein are mine and do not necessarily reflect that of
my employer, its assigns or partners.