Using Windows Registry in Delphi VCL and FMX apps

On Microsoft Windows, the Registry represent the main component to store, retrieve and customize configurations and settings.

And on Delphi (in VCL and FMX), you can access and use Registry to give more advanced features. In this tutorial, we will enumerate some code snippets that give you the ability to perform some tasks like :

  • Enabling JavaScript for IE-based FMX Webbrowser.
  • Launching your app on the system startup.

First, you have to add the System.Win.Registry unit to the uses clauses. You can do this on your project’s main unit, or you can add a new unit just to add Windows only codes.

Be careful :

  • On VCL, you can simply add theSystem.Win.Registry unit without any harms
  • On FMX, you must put it inside conditional defines to avoid errors when compiling to other platforms, like the following :
uses
{$IF Defined(MSWINDOWS)}
   System.Win.Registry,
{$ENDIF}

The implementation of these codes consist in checking the existance of the key in the registry, and if it is not found, your app will write the key to the registry.

To use and apply these codes, you have to copy each code inside a routine (like procedures). Then, you call that routine on your app’s first launching (like in the FormCreate).

Enabling JavaScript for IE-based FMX Webbrowser

procedure ...();
{$IFDEF MSWINDOWS}
const
  cHomePath = 'SOFTWARE';
  cFeatureBrowserEmulation = 'Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION\';
  cIE11 = 11001;

var
  Reg: TRegIniFile;
  sKey: string;
{$ENDIF}

begin
{$IFDEF MSWINDOWS}

sKey := ExtractFileName(ParamStr(0));
Reg := TRegIniFile.Create(cHomePath);
try
   if Reg.OpenKey(cFeatureBrowserEmulation, True) 
   and not(TRegistry(Reg).KeyExists(sKey) and (TRegistry(Reg).ReadInteger(sKey) = cIE11)) 
   then TRegistry(Reg).WriteInteger(sKey, cIE11);
finally
   Reg.Free;
end;

{$ENDIF}
end;

Launching your app on the system startup

With this code, your app will be launched and shown on desktop screen everytime the system starts up :

procedure ...();
{$IFDEF MSWINDOWS}
const
  cHomePath = 'SOFTWARE';
  cWindowsStartup = 'Microsoft\Windows\CurrentVersion\Run\';

var
  Reg: TRegIniFile;
  sKey: string;
  sApp: string;
{$ENDIF}

begin
{$IFDEF MSWINDOWS}

sKey := ExtractFileName(ParamStr(0));
sApp := TPath.Combine(TPath.GetLibraryPath, ExtractFileName(ParamStr(0)));
Reg := TRegIniFile.Create(cHomePath);
try
   if Reg.OpenKey(cWindowsStartup, True) 
   and not(TRegistry(Reg).KeyExists(sKey) and (TRegistry(Reg).ReadString(sKey) = sApp))
   then TRegistry(Reg).WriteString(sKey, sApp);
finally
   Reg.Free;
end;

{$ENDIF}
end;

See also

Leave a Reply