thomh 0 Posted February 22, 2021 Hi, we have a bunch of customers running our Delphi coded RealThinClient server as a service in a domain controlled server environment. They have set Windows Update to check for updates but not to automatically install them. When the server does a Windows update check it happens often that our Delphi services stop responding. If we stop the services we cannot start them again until we restart the server. Something happens in a domain controlled server environment when this Windows update check is done that messes up the services. Our .Net services are not affected and keeps on running. Has anybody seen or heard of this problem and is there a solution? Thanks, Thom Share this post Link to post
David Heffernan 2345 Posted February 22, 2021 What happens in a do nothing Delphi service? Do they stop too? If not then the problem is likely in your service code. Share this post Link to post
thomh 0 Posted February 22, 2021 Hi David, Delphi service that does nothing stops also. It might have something to do with how I start the service. This is straight from the RTC demo app. Is there a more "modern" way of starting the service? function ServiceStart(const aMachine, aService: String) : Boolean; var schm, schs: SC_Handle; ss: TServiceStatus; psTemp: PChar; dwChkP: DWord; begin ss.dwCurrentState := 1; schm := OpenSCManager(PChar(aMachine), nil, SC_MANAGER_CONNECT); if (schm > 0) then begin schs := OpenService(schm, PChar(aService), SERVICE_START or SERVICE_QUERY_STATUS); if (schs > 0) then begin psTemp := nil; if (StartService(schs, 0, psTemp)) then begin if (QueryServiceStatus(schs, ss)) then begin while (SERVICE_RUNNING <> ss.dwCurrentState) do begin dwChkP := ss.dwCheckPoint; Sleep(ss.dwWaitHint); if (not QueryServiceStatus(schs, ss)) then Break; if (ss.dwCheckPoint < dwChkP) then Break; end; end; end; CloseServiceHandle(schs); end; CloseServiceHandle(schm); end; Result := SERVICE_RUNNING = ss.dwCurrentState; end; Share this post Link to post
Cristian Peța 103 Posted February 22, 2021 (edited) I think a standard Delphi service would be using TService. http://docwiki.embarcadero.com/RADStudio/Sydney/en/Service_Applications Just create a service project and put this in OnExecute event: procedure TService1.Service1Execute(Sender: TService); begin while not Terminated do ServiceThread.ProcessRequests(True); end; Edited February 22, 2021 by Cristian Peța Share this post Link to post
Lars Fosdal 1792 Posted February 23, 2021 Our services runs on domain controlled servers and are descendents from TService, but they must be configured to run with a specific AD user (i.e. not the default system user) that has the necessary rights to access it's own file, and to start and stop services to be able to upgrade themselves. Share this post Link to post
Guest Posted February 23, 2021 I would be rather surprised if this has anything to do with RTC itself. However, building service code (or DI stuff) on demos can bite back. I do know that some of the RTC demos are compatible back to... well very long. So maybe you found a demo that was written with very long backwards compatibility in mind.. RTC do not make any presumptions on how you write your service DM. I'd suggest you delve deep into TService and all that. Also - this is a painful methodology, eps if the fault cannot be simulated in you dev environ - start to rip things out and see it it starts working. HTH Share this post Link to post
aehimself 396 Posted March 5, 2021 On 2/23/2021 at 1:06 PM, Lars Fosdal said: Our services runs on domain controlled servers and are descendents from TService, but they must be configured to run with a specific AD user (i.e. not the default system user) that has the necessary rights to access it's own file, and to start and stop services to be able to upgrade themselves. It's not related to the topic, but may I ask how you implemented the update mechanism? Without a "loader" (e.g. actual code is in a DLL, service only loads and executes the DLL,) I never managed to achieve this. I never figured out how to restart the service from within the service. Share this post Link to post
mvanrijnen 123 Posted March 5, 2021 (edited) 1 hour ago, aehimself said: It's not related to the topic, but may I ask how you implemented the update mechanism? Without a "loader" (e.g. actual code is in a DLL, service only loads and executes the DLL,) I never managed to achieve this. I never figured out how to restart the service from within the service. I usually use a seperate .exe located next to the service dll. The exutable stops the serive, does the updates, and restars the service. The executable is ofcourse called by the service. Another options is to create a seperate maintance service. Edited March 5, 2021 by mvanrijnen Share this post Link to post
mvanrijnen 123 Posted March 5, 2021 (edited) 1 minute ago, mvanrijnen said: - double psot Edited March 5, 2021 by mvanrijnen Share this post Link to post
Cristian Peța 103 Posted March 5, 2021 1 hour ago, aehimself said: I never figured out how to restart the service from within the service. It's not the best method but I run a batch file with net stop ServiceName net start ServiceName Share this post Link to post
ioan 45 Posted March 5, 2021 3 hours ago, aehimself said: I never figured out how to restart the service from within the service. I never tried this, but there are some options for service recovery. If you exit the service with an error code, windows should restart it for you right away: Share this post Link to post
aehimself 396 Posted March 5, 2021 Thank you all for the replies guys but I don't want to hijack OP's topic. The methods here seem to be "hacky" (to crash the service and let Windows to restart it - it's a tricky approach I have to admit, though!) or already tried (creating a batch file to sleep for 5 seconds, and issue the NET TOP/START commands. Issue is, as parent program stops, it stops the child processes and therefore the batch file execution too). I was curious if there is a trick to restart a service from within the service itself without the need of a loader, maintenance service - an other executable in general. As it is not that important for me at the moment I'll just keep my eyes open to see if such method was unearthed already. Share this post Link to post
Cristian Peța 103 Posted March 5, 2021 11 minutes ago, aehimself said: creating a batch file to sleep for 5 seconds, and issue the NET STOP/START commands. Issue is, as parent program stops, it stops the child processes and therefore the batch file execution too Why to sleep 5 second? And ShellExecute() doesn't create a child processes. Share this post Link to post
Fr0sT.Brutal 900 Posted March 9, 2021 On 3/5/2021 at 10:41 PM, aehimself said: Issue is, as parent program stops, it stops the child processes and therefore the batch file execution too Why do you think so? Share this post Link to post
Lars Fosdal 1792 Posted March 9, 2021 https://pastebin.com/YCiqiNAq Pretty simple but effective. Hasn't failed me yet. 1 1 Share this post Link to post
aehimself 396 Posted March 9, 2021 3 hours ago, Fr0sT.Brutal said: Why do you think so? 1 hour ago, Lars Fosdal said: Pretty simple but effective. Hasn't failed me yet. I tried creating a batch file from within the service. It slept 5 seconds (so I can see the process start) called NET STOP, slept 5 seconds (giving time for the service to stop) and NET START. When I called this batch from within the service I remember seeing the process starting and ending when the service terminated. This is why I never used this method. As it works for others I am sure I did something wrong; unfortunately I don't have the code snipplet to debug now. @Lars Fosdal this looks promising, I'll make a dummy service app to test it 🙂 Share this post Link to post
Lars Fosdal 1792 Posted March 10, 2021 @aehimself You need to have the unit as high as possible in the .dpr uses list to ensure that finalization is run as late as possible. Share this post Link to post
Guest Posted March 10, 2021 WOW! @Lars Fosdal, i used that quite succesfully in an application after you mentioned it last time (some ½ year?). I had no ide it is possible to do that in a service. If i can get this to work, then i could skip almost all 5 different VPN implementations on my own machines 🙂 Share this post Link to post
Lars Fosdal 1792 Posted March 10, 2021 Looking forward to learn how it works out for you. Share this post Link to post