I have a problem with a service I coded in Delphi. It's not starting and I can't seem to find the problem with it. It was working well 2 weeks ago, and without any change to the service, it suddenly stopped working. The service is in charge of updating a huge system, but it's independent of if. My service gets the URL of a hotfix, downloads it and executes it. Nothing more, nothing less. The system it is supposed to update has some other services and stuff running in the background, so it has a little tool to restart all the background stuff all at once (including my service). At my office, we have like 70 or so computers running everyday the hole system with my service included and it was working fine. We have almost all versions of windows (XP, Windows 7, Windows 8, Windows Server 2003, and even Windows 10). One day suddenly, it stopped restarting with this little tool I told you about. The first thing that came into my mind was to try to start it manually, but haven't had any luck either. My biggest problem, is that I have no clue of what can be happening. The only place where I can see some kind of log is in the windows event manager, and it's not helpful at all. The error says:
The XXX service failed to start due to the following error:
The service did not respond to the start or control request in a timely fashion.
I've searched the web and couldn't find anything useful. All the pages related to this error says that this error has something to do with the windows services time out, or that it has something to do with NetFramework 1.1 and windows 2003 server. It's neither the case, we have instaled NetFramework 4 with the last update in some of the computers where it's crashing and as I told you, it's crashing in may different versions of windows. About the timeout, the error is shown right after I hit start in the service manager, it won't wait the 30 seconds timeout.
I have a log machine in the service but it's of no use, the error is happening somewhere before the "ServiceStart" method. I have this code:
procedure TMyService.ServiceStart(Sender: TService; var Started: Boolean);
begin
try
LogFile.Write('TTMyService.ServiceStart'); //This is my logging machine
Started := True;
InitializeService; //Sets some variables
InitializeTimer; //Start a timer where all the magic happens
except
on e:Exception Do
LogFile.Write('TTMyService.ServiceStart - ' + e.Message);
end;
end;
This is not logging anything, not even "TMyService.ServiceStart". One idea I had but it's not working either, is to replace the TApplication VCL class. I copied all the content of the "Vcl.SvcMgr" file to a new one, modifying it like this (I only added the LogFile.Write Stuff):
procedure TServiceApplication.Run;
var
ServiceStartTable: TServiceTableEntryArray;
ServiceCount, i, J: Integer;
StartThread: TServiceStartThread;
begin
{$IF NOT DEFINED(CLR)}
AddExitProc(DoneServiceApplication);
{$ENDIF}
if FindSwitch('INSTALL') then
RegisterServices(True, FindSwitch('SILENT'))
else if FindSwitch('UNINSTALL') then
RegisterServices(False, FindSwitch('SILENT'))
else
begin
Vcl.Forms.Application.OnException := OnExceptionHandler;
ServiceCount := 0;
for i := 0 to ComponentCount - 1 do
if Components[i] is TService then Inc(ServiceCount);
SetLength(ServiceStartTable, ServiceCount + 1);
{$IF NOT DEFINED(CLR)}
FillChar(ServiceStartTable[0], SizeOf(TServiceTableEntry) * (ServiceCount + 1), 0);
{$ENDIF}
J := 0;
for i := 0 to ComponentCount - 1 do
if Components[i] is TService then
begin
{$IF DEFINED(CLR)}
ServiceStartTable[J].lpServiceName := Components[i].Name;
ServiceStartTable[J].lpServiceProc := ServiceMainDelegate;
{$ELSE}
ServiceStartTable[J].lpServiceName := PChar(Components[i].Name);
ServiceStartTable[J].lpServiceProc := @ServiceMain;
{$ENDIF}
Inc(J);
end;
StartThread := TServiceStartThread.Create(ServiceStartTable);
try
while not Vcl.Forms.Application.Terminated do
try
Vcl.Forms.Application.HandleMessage;
except
on E: Exception do
begin
LogFile.Write('TServiceApplication.Run - ' + e.Message);
DoHandleException(E);
end;
end;
Vcl.Forms.Application.Terminate;
{$IF DEFINED(CLR)}
DoneServiceApplication;
{$ENDIF}
if StartThread.ReturnValue <> 0 then
begin
LogFile.Write('TServiceApplication.Run - ' + SysErrorMessage(StartThread.ReturnValue));
FEventLogger.LogMessage(SysErrorMessage(StartThread.ReturnValue));
end;
finally
StartThread.Free;
end;
end;
end;
Sorry, I don't know how to mark my changes in bold or in red to make it easier for you to see them
This is working, now I can get some "useful" log, but it's not helping either. I get:
TServiceApplication.Run - The parameter is incorrect
And the service won't anymore in my machine (where it was still working). So far, I know for sure of 15 machines where it's not working, and I have no access to try in the other 65. Somehow, in my machine, where I'm developing the service, it's still working if I comment the "Tapplication" changes.
Another problem I have, is that I can't do as many tests as I would like in the machines where it's breaking, because they are being used by other people and every test I ask them to do, stop the from working...
Before I forget, don't know if it's relevant, but to get the URL of the hotfix, the service connects (in my machine), to a local web service hosted on IIS.
Any Ideas? Is this clear enough? Any doubt you have, please ask them! :D I'm really lost, and this have high priority :'(
Thanks in advance! Nicolás López
New data 1:
In a New test, Reinstalling the service in one of the machines, seams to fix the problem. But Without knowing the cause of the problem, that's useless.