2

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.

1
  • Hi, I am also facing the same issue. tried the below answers but no luck. Can you please tell me how I can make it functional?
    – A B
    Mar 4, 2019 at 8:48

3 Answers 3

1

Your log message TServiceApplication.Run - The parameter is incorrect means that StartThread.ReturnValue is 87 (ERROR_INVALID_PARAMETER). The only way that can happen is if StartServiceCtrlDispatcher() failed when called by TServiceStartThread.Execute(). StartServiceCtrlDispatcher() takes only 1 parameter - the ServiceStartTable array that TServiceApplication.Run() sets up. That array contains service names and Main() callbacks for receiving SCM messages. So something is wrong with that array and you need to find out what it is. It is probably not being populated correctly, or is being corrupted before StartServiceCtrlDispatcher() is called.

To diagnose your problem, you will have to use the IDE debugger to step through the TService startup code. That means using the debugger's Attach to Process feature, as the IDE cannot auto-start a service for debugging. You have to issue a start to the SCM first, and then attach the debugger to the resulting process (click on the "Show system processes" checkbox in the attach dialog). That also means tweaking your project code to make the startup delay until the debugger is attached. I usually handle that in the OnStart event, by calling IsDebuggerPresent() in a loop, but since your OnStart is not triggering, you will have to do the delay earlier in the startup logic, such as in the main DPR before TApplication starts initializing the TService object.

There is simply not enough information in this discussion so far for anyone here to diagnose the problem for you.

3
  • Remy, Thanks for you answer but I don't think that's the problem of my service. The only way I get this error, is after I modified it to use my own version of Vcl.SvcMgr, otherwise, it will work fine in my machine. I've commented it because I thought it might be useful for someone to know it. I think the real problem of the service is somewhere else. Probably, the problem with the array, is that some unit is using the original Vcl.SvcMgr. I know my attempt to fix it modifying it it's not the healthiest but i had to try it anyway ;) I'm kinda lost here... Feb 13, 2015 at 13:32
  • Let's work backwards. The only way that OnStart would not be triggered (assuming it is assigned) is if either the service's TServiceThread object is not created at all, or gets terminated before its Execute() method is called (TThread checks its Terminated property before calling Execute()). TServiceThread is not created if TService.Main() is not called, or if RegisterServiceCtrlHandler() fails when called by TService.Main()... Feb 13, 2015 at 19:10
  • ... Main() is not called if TServiceApplication.DispatchServiceMain() is not called at all, or does not find the TService object whose Name matches the name passed as a parameter to Main(). DispatchServiceMain() is not called if ServiceMain() is not called. And ServiceMain() is not called if StartServiceCtrlDispatcher() fails. Feb 13, 2015 at 19:11
1

I ran into similar problem:

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

  • the TService.OnStart or TService.OnExecute events where never fired.
  • the services's path to executables (services.msc -> right click on the service -> properties) was set like this: C:\test\TrucServer.exe /startedbyscm:A357F915-40E33CD5-trucServer

=> The solution was to reinstall the service (uninstall sc.exe delete "Truc Server", then install C:\test\TrucServer.exe -install). The services's path to executables is now set like this: C:\test\TrucServer.exe and the service starts normally.

I did not try to insert logs in TServiceApplication class, but your error message: TServiceApplication.Run - The parameter is incorrect seems to confirm my solution could correspond to your problem as in my case the /startedbyscm:A357F915-40E33CD5-trucServer parameter seems to be the culprit.

I did not try too to track the error message in Vcl.SvcMgr.

1
  • I don't remember exactly, but I've fixed this a long time ago. If I'm recall correctly, it was a problem with a component having a wrong parameter seting on design. My service connects to a server to do it's stuff. I think it was the URL or the port or something like that was wrong, As it was Fixed in a component at design, the service won't even start. Is there a way to close this question? Jun 24, 2015 at 15:33
0

In my case, the problem was TServiceDesc.displayName. I tried to /install and /uninstall and on uninstall the name could not be found. Because it could not be found on /uninstall, the server manager could not find it that it has started either. So the status in the "Services" was always "Starting". Why some names are OK and others not I cant tell. There was nothing special about it.

DisplayName := "Name DB Service";

It did not work. So I made the exe and displayName exactly the same. Not sure how this happens, but the reason for the error is the name of the form of the service, the name of the executable and the DisplayName. All this three together somehow get mixed up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.