Forum > LCL

Tprocess - how to force close of child process

(1/1)

jaz2014:
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:
Hello. Here is a procedure that I use:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.AbortProcess(const AExeName: string);var  LStr: string;begin  DebugLn(Format('AbortProcess(%s)', [AExeName]));{$IFDEF MSWINDOWS}  DebugLn(Format('RunCommand=%d', [Ord(RunCommand('taskkill', ['/f', '/im', AExeName], LStr))]));{$ELSE}{$IFDEF LINUX}  DebugLn(Format('RunCommand=%d', [Ord(RunCommand('killall', [AExeName], LStr))]));{$ELSE}{$ENDIF}{$ENDIF}end; 
Maybe there are better solutions, but... it works.  :)

GetMem:
@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 ?
--- End quote ---

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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function FindProcessByParentID(const AParentID: DWORD): DWORD;var  Proc: TPROCESSENTRY32;  hSnap: HWND;  Looper: BOOL;begin  Result := 0;  Proc.dwSize := SizeOf(Proc);  hSnap := CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);  Looper := Process32First(hSnap, Proc);  while Integer(Looper) <> 0 do  begin    if Proc.th32ParentProcessID = AParentID then    begin      Result := Proc.th32ProcessID;      Break;    end;    Looper := Process32Next(hSnap, Proc);  end;  CloseHandle(hSnap);end; function KillProcess(const AProcessID: DWORD): Boolean;var  ProcHandle: THandle;begin  Result := False;  ProcHandle := OpenProcess(PROCESS_TERMINATE, False, AProcessID);  if ProcHandle > 0 then  try    Result := TerminateProcess(ProcHandle, 0);  finally    CloseHandle(ProcHandle);  end;end; procedure TForm1.Button1Click(Sender: TObject);var  CmdID: DWord;  FfmpegID: DWord;begin  CmdID := FindProcessByParentID(GetCurrentProcessId);  if CmdID > 0 then  begin    FfmpegID := FindProcessByParentID(CmdID);    if FfmpegID > 0 then      KillProcess(FfmpegID);    KillProcess(CmdID); //if needed  end;end;  

jaz2014:
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




Navigation

[0] Message Index

Go to full version