0

I am creating a Windows Service Application with Delphi and I have a big problem:

The service runs, connects to a server via Winsock.. The server, when the client connects send a text to the client(the service) and then the client reads and execute and action. RIGHT AFTER this action is done, the service STOPs..

What I want is: Even when the Client Winsock receive the data and make the action, I would like the service to keep RUNNING and the WINSOCK LISTENING.

I have tested if the Client Winsock can receive data if the service is stopped, but cant.

This is my code for the SERVICE.pas:

unit WebLauncher;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,
  ScktComp, Registry, ShellApi, DSiWin32;

type
  TiGunBoundWebLauncher = class(TService)
    WebLauncherConector: TClientSocket;
    procedure ServiceExecute(Sender: TService);
    procedure WebLauncherConectorError(Sender: TObject;
      Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
      var ErrorCode: Integer);
    procedure WebLauncherConectorRead(Sender: TObject;
      Socket: TCustomWinSocket);
  private
    { Private declarations }
  public
    function GetServiceController: TServiceController; override;
    { Public declarations }
  end;

var
  iGunBoundWebLauncher: TiGunBoundWebLauncher;

implementation

{$R *.DFM}

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

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

procedure TiGunBoundWebLauncher.ServiceExecute(Sender: TService);
begin
   WebLauncherConector.Active := True;
    ServiceThread.ProcessRequests(False);
end;

procedure TiGunBoundWebLauncher.WebLauncherConectorError(Sender: TObject;
  Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
  var ErrorCode: Integer);
  var error: integer;
begin

   error := ErrorCode; {prevent exception from being thrown}
   ErrorCode := 0;

   if error = 10053 then begin
   end;

end;

procedure TiGunBoundWebLauncher.WebLauncherConectorRead(Sender: TObject;
  Socket: TCustomWinSocket);
  var
  Data: string;
  GunBoundPath: string;
  handle: HWND;
begin

   Data := Socket.ReceiveText;

   If Pos('Launch', Data) > 0 then begin

        GunBoundPath := DSiReadRegistry('\Software\IGunBound\Seasson2',
  'Location', '', HKEY_LOCAL_MACHINE, KEY_QUERY_VALUE OR KEY_WOW64_64KEY);
        ShellExecute(handle,'open',PChar(GunBoundPath + '\Launcher.exe'), '', PChar(GunBoundPath), SW_SHOWNORMAL);

   end;

end;

end.
  • 1
    Do not implement service execute and spawn a second thread where you activate the socket. You can find a skeleton implementation [here](http://stackoverflow.com/a/10538102/800214) – whosrdaddy Jan 31 '14 at 06:15
  • Have you done any debugging. I don't mean in the IDE necessarily. What happens in the service? How far does the code get? Does Launcher.exe show GUI? Because that is doomed to fail inside a service. – David Heffernan Jan 31 '14 at 07:30
  • @whosrdaddy so I should delete the ServiceExecute event? What if a set the Active control of the Winsock to TRUE.. when the service starts it will connect! so then what do i do for my thread to dont terminate? – Thiago Marquezini Jan 31 '14 at 07:36
  • David, the service stops right after the Launcher.exe is executed. – Thiago Marquezini Jan 31 '14 at 07:37
  • @whosrdaddy in that code you sent the link there is code like this _italic_ **bold** `procedure TMyService.ServiceStart(Sender: TService; var Started: Boolean); begin Started := False; try MyThread := TMyThread.Create; MyThread.Resume; NTEventLog.Add(Eventlog_Success, STR_INFO_SVC_STARTED); Started := True; except on E : Exception do begin // add event in eventlog with reason why the service couldn't start NTEventLog.Add(Eventlog_Error_Type, Format(STR_INFO_SVC_STARTFAIL, [E.Message])); end; end; end; ` – Thiago Marquezini Jan 31 '14 at 07:38
  • This is starting a new thread.. but whatever I do in this thread, when it finishes, the service stops.. how could I make an infinite thread? I mean, one that doesnt Terminates (so then the service should keep running) – Thiago Marquezini Jan 31 '14 at 07:41
  • that is up to you, you can loop the thread like: `while not terminated do begin end` – whosrdaddy Jan 31 '14 at 12:16

1 Answers1

1

The service stops right after the Launcher.exe is executed.

This is likely the crux of the issue. Most likely Launcher.exe is an interactive program that cannot be shown in the non-interactive session 0 where sessions run. Perhaps you are hoping that the new process will appear on the interactive desktop (it won't).

I think you'll need to completely re-think your design once you come to terms with the fact that Services cannot launch GUI.

Note that there are ways to get a service to start an app on the interactive desktop (http://msdn.microsoft.com/en-us/magazine/cc163486.aspx) but I could not, in all honesty, recommend them.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490