La aplicación funcina correctamente si se ejecuta bajo windows de forma normal.
Funciona también bien si se pone en el registro de windows para que arranque junto con windows, pero en este caso se inicia cuando se inicia una sesión y no antes.
Falla cuando la tengo como servicio de windows tanto al arrancar el equipo como cuando intento de forma manual iniciarlo yo, obteniendo del error "1053 El servicio no ha respondido a la petición o inicio del control en un tiempo adecuado."
El caso es que la aplicación (que hace de cliente) SÍ arranca y parece que comienza a comunicarse con la otra aplicación que tengo en otro equipo que hace de aplicación servidor, pero al poco cesa y se apaga.
Que hago mal?. He intentado crear otra aplicación completamente nueva que lo unico que haga es ejecutar la otra, a ver si colaba, pero sin exito.
¿Qué puedo hacer para que el servicio se mantenga activo?
Creación del servicio:
Código Delphi
[-]
procedure TMainForm.CrearServicio();
var
servicio: TServicio;
begin
servicio := TServicio.Create('miAplicacionCliente');
if not servicio.EstaInstalado() then servicio.Instalar(Application.Exename);
servicio.Free;
end;
Función INICIAR:
Código Delphi
[-]
function TServicio.Iniciar(): boolean;
var
i: integer;
argv, p: ^PChar;
hServicio, hManejador: cardinal;
begin
argv := nil;
result := false;
hManejador := self.AbrirManejador();
if (hManejador <> 0) then
begin
try
hServicio := self.AbrirServicio();
if (hServicio <> 0) then
begin
if (FArgumentos.Count > 0) then begin
GetMem(argv, FArgumentos.Count * Sizeof(PChar));
p := argv;
for i := 0 to FArgumentos.Count-1 do
begin
p^:= PChar(FArgumentos[i]);
Inc(p);
end;
end;
Result := StartService(hServicio,
FArgumentos.Count, argv^);
if (hServicio <> 0) then
CloseServiceHandle(hServicio);
end;
finally
if (hManejador <> 0) then
CloseServiceHandle(hManejador);
end;
end;
end;
Función INSTALAR:
Código Delphi
[-]
function TServicio.Instalar(ejecutable: string): boolean;
var
hServicio, hManejador: cardinal;
begin
FEjecutable := ejecutable;
if not FileExists(FEjecutable) then
raise EIOErrorServicio.Create(rsErrorSinEjecutable);
if not self.ValidarNombre() then
raise EIOErrorServicio.Create(rsErrorNombreInvalido);
if (Length(Trim(FDescripcion)) > 256) then
raise EIOErrorServicio.Create(rsErrorDescripcionInvalida);
hServicio := 0;
result := false;
hManejador := self.AbrirManejador(SC_MANAGER_CREATE_SERVICE);
if (hManejador <> 0) then begin
try
hServicio := CreateService(hManejador, Pchar(FNombre), Pchar(FDescripcion),
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL, PChar(FEjecutable), nil, nil, nil, nil, nil);
result := (hServicio <> 0);
finally
if (hServicio <> 0) then
CloseServiceHandle(hServicio);
if (hManejador <> 0) then
CloseServiceHandle(hManejador);
end;
end;
end;