I have a Windows Service written in Delphi which runs a number of programs.
On Stopping the service, I want to also close these programs. When the service was originally written, this worked fine, but I think I’ve updated the tProcess component and now – The subordinate programs are not being closed.
in tProcess – Here’s the code which starts the new processes.
if CreateProcess( nil , PChar( FProcess.Command ) , nil , nil , False , NORMAL_PRIORITY_CLASS , nil , Directory , StartupInfo , ProcessInfo ) then begin if FProcess.Wait then begin WaitForSingleObject( ProcessInfo.hProcess , Infinite ); GetExitCodeProcess( ProcessInfo.hProcess , ExitCode ); if Assigned( FProcess.FOnFinished ) then FProcess.FOnFinished( FProcess , ExitCode ); end; CloseHandle( ProcessInfo.hProcess ); CloseHandle( ProcessInfo.hThread ); end;
Each of the executables called by this are Windows GUI Programs (With a close button at the top).
When I stop the service, I also want to stop (not kill) the programs I’ve started up via the createProcess procedure.
How would you do this?
You want to enumerate open windows that match your launched ProcessId and tell those windows to close. Here’s some sample code for that:
Then you’ll want to utilize this in your code when you want to shut down the launched applications: