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.
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.