How to create Service Application with Delphi.

create Service Application















Source code

unit awMain;
{
 Service for backup database ...
 Create by: Samrid Somboon
 Date:      23/04/2018
}
interface

uses
  Windows,
  Messages,
  inifiles,
  SysUtils,
  Classes,
  Graphics,
  Controls,
  SvcMgr,
  Dialogs,
  ShellAPI,
  ExtCtrls;

type
  TAwTruckService = class(TService)
    Timer1: TTimer;
    procedure Timer1Timer(Sender: TObject);
    procedure ServiceStop(Sender: TService; var Stopped: Boolean);
    procedure ServiceStart(Sender: TService; var Started: Boolean);
    procedure ServiceCreate(Sender: TObject);
  private
    { Private declarations }
    dayIndex: byte;
    AMin: Word;
    H, M, S, Ms: Word;
  public
    function GetServiceController: TServiceController; override;
    { Public declarations }
  end;

var
  AwTruckService: TAwTruckService;
  FOCUS_TIME : byte = 12;
  FOCUS_MIN  : byte = 30;

implementation

{$R *.DFM}

procedure LogMsg(const AFilename, AMessage: string);
var
  f: TextFile;
begin
  try
    AssignFile(f, AFilename);
    if FileExists(AFilename) then
      Append(f)
    else
      ReWrite(f);
    WriteLn(f, FormatDateTime('yyyy-mm-dd hh:nn:ss', Now), ' ', AMessage);
    CloseFile(f);
  except
  end;
end;

function DayOfTheWeek(const AValue: TDateTime): Word;
begin
  Result := (DateTimeToTimeStamp(AValue).Date - 1) mod 7 + 1;
end;

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
  AwTruckService.Controller(CtrlCode);
end;

function TAwTruckService.GetServiceController: TServiceController;
begin
  Result := ServiceController;
end;

procedure TAwTruckService.Timer1Timer(Sender: TObject);
const
  DayName : array [0..6] of string = (
   'mon.bat',
   'tue.bat',
   'wed.bat',
   'thu.bat',
   'fri.bat',
   'sat.bat',
   'sun.bat');
var
  i: Byte;
  batchfile: string;
begin
  DecodeTime(now, H, M, S, Ms);
  if AMin = M then
    exit;

  Timer1.Enabled := false;
  try
      if ((H = FOCUS_TIME) and (M = FOCUS_MIN)) then
      begin
         dayIndex  := DayOfTheWeek(Date);
         batchfile := ExtractFilePath(paramstr(0))+DayName[dayIndex-1];

         ShellExecute (0, 'open', 'cmd.exe', PChar('/c '+batchfile), nil, SW_SHOWNORMAL);
         LogMsg(
          ExtractFilePath(paramstr(0))+'bakuplog.txt',
          format('backup db on day: %s',[Copy(DayName[dayIndex-1],1,3)])
          );
         AMin := M;
      end;
  finally
    Timer1.Enabled := true;
  end;
end;

procedure TAwTruckService.ServiceStop(Sender: TService;
  var Stopped: Boolean);
begin
  Timer1.Enabled := false;
end;

procedure TAwTruckService.ServiceStart(Sender: TService;
  var Started: Boolean);
var
  Aw1: TIniFile;
begin
  Timer1.Enabled := true;
  Aw1:= TIniFile.Create(ExtractFilePath(paramstr(0))+'Aw1.ini');
  try
     FOCUS_TIME:= Aw1.ReadInteger('BackupTime', 'Hour', 12);
     FOCUS_MIN := Aw1.ReadInteger('BackupTime', 'Min', 30);
  finally
     Aw1.free;
  end;
end;

procedure TAwTruckService.ServiceCreate(Sender: TObject);
begin
  AMin := 99;
  M    := 0;
end;

end.
How to install service







Service


How to uninstall service.










Comments