Example for KbmMW as a Windows Service

Home Forums kbmMW Example for KbmMW as a Windows Service

Viewing 4 reply threads
  • Author
    Posts
    • #57362
      jet2050
      Participant

      Hello dear Kim, I kindly request your help with an example of how to use kbmMW in a Windows service.

      Thank you in advance for your time and help.

      Regards, Job Espejel.

    • #57363
      Ismet Sonmez
      Participant

      unit MainSvc;

      interface

      uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.SvcMgr, Vcl.Dialogs,
      System.Win.Registry;

      const
      STR_VERSION_MAIN = ‘1.0’;
      STR_NAME_COMPANY = ‘Bolergo’;
      STR_NAME_APPLICATION_SHORT = ‘BLTicketSvc’;
      STR_NAME_APPLICATION = STR_NAME_COMPANY+’ ‘+STR_NAME_APPLICATION_SHORT;
      STR_NAME_SVC = ‘BLTicketSvc’;

      // service main registry keys
      STR_REGKEY_SOFTWARE = ‘\Software’;
      STR_REGKEY_COMPANY = ‘\’+STR_NAME_COMPANY;
      STR_REGKEY_APPLICATION = ‘\’+STR_NAME_APPLICATION_SHORT;
      STR_REGKEY_FULL = STR_REGKEY_SOFTWARE+STR_REGKEY_COMPANY+STR_REGKEY_APPLICATION+’\’+STR_VERSION_MAIN;
      STR_REGKEY_EVENTMSG = ‘\SYSTEM\CurrentControlSet\Services\EventLog\Application\’+STR_NAME_APPLICATION;
      STR_REGKEY_SVC = ‘\SYSTEM\CurrentControlSet\Services\’+STR_NAME_SVC;

      STR_REGVAL_INSTALLDIR = ‘InstallDir’;
      STR_REGVAL_IMAGEPATH = ‘ImagePath’;
      STR_REGVAL_DEPENDONSERVICE = ‘DependOnService’;
      STR_REGVAL_DESCRIPTION = ‘Description’;
      STR_REGVAL_EVENTMESSAGEFILE = ‘EventMessageFile’;
      STR_REGVAL_TYPESSUPPORTED = ‘TypesSupported’;

      STR_INFO_SVC_DESC = STR_NAME_APPLICATION+’ ‘+STR_VERSION_MAIN;
      STR_INFO_SVC_REGFAIL = ‘Registry is not initialized for service’;
      STR_INFO_SVC_START = ‘Starting service…’;
      STR_INFO_SVC_STARTED = ‘Service has been started’;
      STR_INFO_SVC_STARTFAIL = ‘Service failed to start : “%s”‘;
      STR_INFO_SVC_STOP = ‘Stopping service…’;
      STR_INFO_SVC_STOPPED = ‘Service has been stopped’;
      STR_INFO_SVC_STOPPEDWITHERROR = ‘Service stopped due to main thread error, please check debug log’;
      STR_INFO_SVC_STOPFAIL = ‘Service failed to stop : “%s”‘;

      STR_INFO_SVC_NOCONFIG = ‘Unable to load service configuration file’;

      type
      TBolergoService = class(TService)
      procedure ServiceStart(Sender: TService; var Started: Boolean);
      procedure ServiceStop(Sender: TService; var Stopped: Boolean);
      procedure ServiceExecute(Sender: TService);
      procedure ServiceAfterInstall(Sender: TService);
      private
      { Private declarations }
      public
      function GetServiceController: TServiceController; override;
      { Public declarations }
      end;

      var
      BolergoService: TBolergoService;

      implementation

      {$R *.dfm}

      uses MainModule;

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

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

      procedure TBolergoService.ServiceAfterInstall(Sender: TService);
      var
      Reg: TRegistry; //declared in System.Win.Registry;
      begin
      Reg := TRegistry.Create(KEY_READ or KEY_WRITE);
      try
      Reg.RootKey := HKEY_LOCAL_MACHINE;
      if Reg.OpenKey(‘\SYSTEM\CurrentControlSet\Services\’ + name, False {do not create if not exists}) then
      begin
      Reg.WriteString(‘Description’, ‘BLTicket Service’);
      Reg.CloseKey;
      end;
      finally
      Reg.Free;
      end;
      end;

      procedure TBolergoService.ServiceExecute(Sender: TService);
      begin
      while not Terminated do
      begin
      Sleep(1000);
      ServiceThread.ProcessRequests(False);
      end;
      end;

      procedure TBolergoService.ServiceStart(Sender: TService; var Started: Boolean);
      begin
      ModuleMain.StartServer;
      end;

      procedure TBolergoService.ServiceStop(Sender: TService; var Stopped: Boolean);
      begin
      ModuleMain.StopServer;
      end;

      end.

    • #57364
      Ismet Sonmez
      Participant

      procedure TModuleMain.StartServer;
      var
      filelogmgr:IkbmMWLocalFileLogManager;
      begin
      filelogmgr:=TkbmMWLocalFileLogManager.Create(‘.\log.txt’,’.\audit.txt’);
      filelogmgr.FlushInterval:=0;
      Log.LogManager:=filelogmgr;
      //Log.TimeStart(‘Total’);
      //Log.TimeEnd(‘Total’,’Total time’);

      mwIndyStd.Bindings.Items[0].Port := StrToInt(ConfApp.StdPort);
      mwIndyRest.Bindings.Items[0].Port := StrToInt(ConfApp.RestPort);

      mwServer.RegisterServiceByName(‘KBMMW_QUERY’, TServiceQuery, false);
      mwServer.RegisterServiceByName(‘SECURITYSVC’, TServiceSecurity, false);
      mwServer.RegisterServiceByName(‘TICKETSVC’, TServiceTicket, false);
      mwServer.RegisterServiceByName(‘DEVICESVC’, TServiceDevice, false);
      // mwServer.RegisterServiceByName(‘REPORTSVC’, TServiceReport, false);

      mwServer.AutoRegisterServices;

      try
      ModuleMainDb.StopPool;
      ModuleMainDb.StartPool;
      ModuleScheduler.StartScheduler;
      // ModuleSecurity.CloseSessionAll(2);
      mwServer.Active := true;
      except
      on e: exception do
      Log.LogException(e);
      end;
      end;

      procedure TModuleMain.StopServer;
      begin
      try
      mwServer.Active:=False;
      mwServer.Shutdown;
      // mwServer.Active:=False;
      ModuleScheduler.StopScheduler;
      ModuleMainDb.StopPool;
      except
      on e: exception do
      Log.LogException(e);
      end;
      end;

    • #57369
      jet2050
      Participant

      “Ismet, thank you very much for your assistance and time. I will try it as soon as possible. Thanks again, and happy holidays!” 🎄🌟

    • #57374
      Ismet Sonmez
      Participant

      I am sending the server codes I use in the running system. You can change it according to yourself.
      https://mega.nz/file/j0ZGlazK#Nj74Eh45wJfXvGXp_xzaF6qPALnswGkhnFeHvbUAi8k

Viewing 4 reply threads
  • You must be logged in to reply to this topic.