Recent

Author Topic: Tprocess - how to force close of child process  (Read 877 times)

jaz2014

  • New Member
  • *
  • Posts: 14
Tprocess - how to force close of child process
« on: January 13, 2022, 05:25:43 pm »
I have a Tprocess running fine and everything works.
The process runs a cmd.exe shell which in turn runs ffmpeg

Iwant to be able to force closure of the child shell (killing ffmpeg obviously) but cant seem
to see how to do that.
Ideally I want to do it whether I have a console window open or not.

I've tried windows messaging and everything else I can find here but nothing seems
to reference killing a child program.

Can anyone tell me the simplest way to do his ?

thanks

Roland57

  • Sr. Member
  • ****
  • Posts: 423
    • msegui.net
Re: Tprocess - how to force close of child process
« Reply #1 on: January 13, 2022, 06:00:55 pm »
Hello. Here is a procedure that I use:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.AbortProcess(const AExeName: string);
  2. var
  3.   LStr: string;
  4. begin
  5.   DebugLn(Format('AbortProcess(%s)', [AExeName]));
  6. {$IFDEF MSWINDOWS}
  7.   DebugLn(Format('RunCommand=%d', [Ord(RunCommand('taskkill', ['/f', '/im', AExeName], LStr))]));
  8. {$ELSE}
  9. {$IFDEF LINUX}
  10.   DebugLn(Format('RunCommand=%d', [Ord(RunCommand('killall', [AExeName], LStr))]));
  11. {$ELSE}
  12. {$ENDIF}
  13. {$ENDIF}
  14. end;
  15.  

Maybe there are better solutions, but... it works.  :)
My projects are on Gitlab and on Codeberg.

balazsszekely

  • Guest
Re: Tprocess - how to force close of child process
« Reply #2 on: January 13, 2022, 06:01:26 pm »
@jaz2014
Quote
Iwant to be able to force closure of the child shell (killing ffmpeg obviously) but cant seem
to see how to do that.
Ideally I want to do it whether I have a console window open or not.

I've tried windows messaging and everything else I can find here but nothing seems
to reference killing a child program.

Can anyone tell me the simplest way to do his ?

Your application is the parent process. Iterate through all processes, compare each process parent id with your application process id. If there is a match, you found cmd.ex.  Do the same for cmd to find ffmpeg. Something like this(not tested):
Code: Pascal  [Select][+][-]
  1. function FindProcessByParentID(const AParentID: DWORD): DWORD;
  2. var
  3.   Proc: TPROCESSENTRY32;
  4.   hSnap: HWND;
  5.   Looper: BOOL;
  6. begin
  7.   Result := 0;
  8.   Proc.dwSize := SizeOf(Proc);
  9.   hSnap := CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
  10.   Looper := Process32First(hSnap, Proc);
  11.   while Integer(Looper) <> 0 do
  12.   begin
  13.     if Proc.th32ParentProcessID = AParentID then
  14.     begin
  15.       Result := Proc.th32ProcessID;
  16.       Break;
  17.     end;
  18.     Looper := Process32Next(hSnap, Proc);
  19.   end;
  20.   CloseHandle(hSnap);
  21. end;
  22.  
  23. function KillProcess(const AProcessID: DWORD): Boolean;
  24. var
  25.   ProcHandle: THandle;
  26. begin
  27.   Result := False;
  28.   ProcHandle := OpenProcess(PROCESS_TERMINATE, False, AProcessID);
  29.   if ProcHandle > 0 then
  30.   try
  31.     Result := TerminateProcess(ProcHandle, 0);
  32.   finally
  33.     CloseHandle(ProcHandle);
  34.   end;
  35. end;
  36.  
  37. procedure TForm1.Button1Click(Sender: TObject);
  38. var
  39.   CmdID: DWord;
  40.   FfmpegID: DWord;
  41. begin
  42.   CmdID := FindProcessByParentID(GetCurrentProcessId);
  43.   if CmdID > 0 then
  44.   begin
  45.     FfmpegID := FindProcessByParentID(CmdID);
  46.     if FfmpegID > 0 then
  47.       KillProcess(FfmpegID);
  48.     KillProcess(CmdID); //if needed
  49.   end;
  50. end;  
« Last Edit: January 13, 2022, 06:06:56 pm by GetMem »

jaz2014

  • New Member
  • *
  • Posts: 14
Re: Tprocess - how to force close of child process
« Reply #3 on: January 13, 2022, 09:14:42 pm »
I have to say those are the 2 most pleasing responses I think I've had in any forum for a long time.
Thank you both.

I'm going with GetMem's functions as they are probably safer and more comprehensive
for use in my application (which is getting a bit complex)

I'd never heard of createtoolhelp32snapshot or (as I've discovered) jwaTLHelp32 before and thought
they were some sort of commercial tools I couldnt find anywhere when I saw them in the wiki !
Lazarus docs cripple me once again...

GetMem you'll be pleased to know I did a simple cut and paste to relevant places
and it all worked first time - no changes needed.
(Impressive given the issue)

A double thanks for taking the trouble to use my program names  - a very nice helpful touch.
I'm guessing I can find more uses for this - it's a shame it coldn't be built into TProcess
to do other things with child situations

Anyway thats got me out of that hole - now to go dig another ...

Thank you both
jaz




« Last Edit: January 13, 2022, 09:17:27 pm by jaz2014 »

 

TinyPortal © 2005-2018