Kamis, 25 Januari 2024

Creating Linux Daemon or Windows Service with Lazarus

Daemon Application in Linux or Service Application in Windows is an application that running in the background, usually automatically started when the operating system started even if user is not logged in. In Windows Service Application you cannot access Windows GUI library.

To create Daemon with Lazarus, first you need to create new project and select Daemon (service) application. After you click the OK button the IDE will create a new lazarus project with a daemon mapper unit dan a daemon class unit. Then save the project to the specified project name and folder.

Then you need to give the daemon class name. In this example the daemon class is named FirebirdRelayDaemon and the unit file saved as uFirebirdRelayDaemon.pas. 

The daemon class will never be registered to the operating system or started unless you register it in the Daemon Mapper. To register the daemon class you need to open the daemonmapperunit1.pas.

Then click the object inspector and then click the ellipsis button in the DaemonDefs property to open the daemon definitions editor. Then click Add button in the daemon definitions editor to add new daemon definition.


After you click Add button the object inspector will display the new daemon definition. You need to edit DaemonClassName, Display and Name properties. 

  • DaemonClassName: TFirebirdRelayDaemon.
  • DisplayName: Firebird Relay Server.
  • Name: firebirdrelayd.

After the daemon class is registered, you can place the code for your daemon in the daemon class. You can place objects initialization in the OnCreate event and objects finalization in the OnDestroy event. The code to start the daemon thread is placed in the OnStart event and code to terminate daemon thread is placed in the OnStop event. 


This is the example of the daemon class declaration:

  1. type

  2.   { TFirebirdRelayDaemon }

  3.   TFirebirdRelayDaemon = class(TDaemon)
  4.     procedure DataModuleCreate(Sender: TObject);
  5.     procedure DataModuleDestroy(Sender: TObject);
  6.     procedure DataModuleStart(Sender: TCustomDaemon; var OK: Boolean);
  7.     procedure DataModuleStop(Sender: TCustomDaemon; var OK: Boolean);
  8.   private
  9.     { private declarations }
  10.     FirebirdRelayServer: TFirebirdRelayServer;
  11.   public
  12.     { public declarations }
  13.   end;

This is the example of the daemon class definition:

  1. { TFirebirdRelayDaemon }

  2. procedure TFirebirdRelayDaemon.DataModuleCreate(Sender: TObject);
  3. begin
  4.   FirebirdRelayServer := TFirebirdRelayServer.Create;
  5. end;

  6. procedure TFirebirdRelayDaemon.DataModuleDestroy(Sender: TObject);
  7. begin
  8.   FirebirdRelayServer.Free;
  9. end;

  10. procedure TFirebirdRelayDaemon.DataModuleStart(Sender: TCustomDaemon;
  11.   var OK: Boolean);
  12. begin
  13.   FirebirdRelayServer.LoadFromIniFile(ExtractFilePath(ParamStr(0))+'konfig.ini',
  14.     'FirebirdRelayServer');
  15.   FirebirdRelayServer.StartThread;
  16.   OK := True;
  17. end;

  18. procedure TFirebirdRelayDaemon.DataModuleStop(Sender: TCustomDaemon;
  19.   var OK: Boolean);
  20. begin
  21.   FirebirdRelayServer.TerminateThread;
  22.   OK := True;
  23. end;

Output of the compiled daemon:

The daemon is installed in the windows services list:

You can start the service from command line using "net start firebirdrelayd" and stop the service using "net stop firebirdrelayd".

Thankyou for reading this article and I hope this article can be useful.

Compress and Decompress Text or File using ZLib in Delphi or Free Pascal Lazarus

When I was in my college I work as part time programmer in a software house. I got a task to create a software updater. The software updater work by copying new files to the already installed application in the client's computers. To make update file easier to distribute I need to compress the new files and combine them into a file.

In Delphi or Lazarus we can use TFileStream class to access files and using TCompressionStream in the zstream unit to compress data from memory buffer to output stream that can be any TStream object including TFileStream or TMemoryStream. To decompress later you can use TDecompressionStream class.

If ZLib compression is not enough you can use BZip2 library for stronger compression.

Bellow is an example of using ZLib TCompressionStream and TDecompressionStream class:

  1. program TestCompressAndDecompress;
  2. {$MODE DELPHI}
  3. uses SysUtils, Classes, zstream;
  4. var
  5.   F: TMemoryStream;
  6.   Comp: TCompressionStream;
  7.   Decomp: TDecompressionStream;
  8.   X, Y: AnsiString;
  9. begin
  10.   F := TMemoryStream.Create;
  11.   try
  12.     Comp := TCompressionStream.Create(clMax, F);
  13.    try
  14.       X := 'Compress and Decompress array of bytes or a file using ZLib in Delphi or Free Pascal Lazarus';
  15.       Comp.Write(X[1], Length(X));
  16.     finally
  17.       Comp.Free;
  18.     end;
  19.     WriteLn('Original Text: "',X,'"');
  20.     WriteLn('Original Size: ',Length(X),' bytes.');
  21.     WriteLn('Compressed Size: ',F.Size,' bytes.');
  22.     F.Position := 0;
  23.     Decomp := TDecompressionStream.Create(F);
  24.     try
  25.        SetLength(Y, Length(X));
  26.        Decomp.Read(Y[1], Length(Y));
  27.     finally
  28.       Decomp.Free;
  29.     end;
  30.     WriteLn('Decompressed Text: "',Y,'"');
  31.   finally
  32.     F.Free;
  33.   end;
  34. end.

Creating Linux Daemon or Windows Service with Lazarus

Daemon Application in Linux or Service Application in Windows is an application that running in the background, usually automatically starte...