Solved

Kill a process started from a Delphi application

Posted on 2015-01-14
5
1,804 Views
Last Modified: 2015-01-21
Hello,
I have a Delphi application from where the user has the option to start  an external application.
In fact, I use ShellExecute with action "open" on a PDF file.
When this is performed, Adobe Reader opens the file in a separated window.
I want to know if, from the Delphi application, I can later on kill (close ?) that external window one way or another.
Maybe I shall use another function than ShellExecute ?
Thanks
0
Comment
Question by:LeTay
  • 3
5 Comments
 
LVL 3

Expert Comment

by:lopem
ID: 40549613
Try this:

function KillTask(ExeFileName: string): integer;

const
  PROCESS_TERMINATE=$0001;
var
  ContinueLoop: BOOL;
  FSnapshotHandle: THandle;
  FProcessEntry32: TProcessEntry32;
begin
  result := 0;
  FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
  ContinueLoop := Process32First(FSnapshotHandle,FProcessEntry32);

  while integer(ContinueLoop) <> 0 do
  begin
  if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(ExeFileName))
  or (UpperCase(FProcessEntry32.szExeFile) = UpperCase(ExeFileName))) then
    Result := Integer(TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0),FProcessEntry32.th32ProcessID), 0));
    ContinueLoop := Process32Next(FSnapshotHandle,FProcessEntry32);
  end;
CloseHandle(FSnapshotHandle);
end;

I think you need to include the shellapi in your uses clause.

regards
Lopem
0
 
LVL 26

Accepted Solution

by:
Sinisa Vuk earned 500 total points
ID: 40549858
using shellexecuteex windows api you can get handle to process which you can use then to terminate that specific process...
function StartApp(apchOperation, apchFileName, apchParameters, apchDirectory: PChar;
  awrdShowCmd: Word): Cardinal;
var
  lseiInfo: TShellExecuteInfo;
begin
  Result := 0;

  FillChar(lseiInfo, SizeOf(lseiInfo), Chr(0));
  lseiInfo.cbSize := SizeOf(lseiInfo);
  lseiInfo.fMask := SEE_MASK_NOCLOSEPROCESS;
  lseiInfo.lpVerb := apchOperation;
  lseiInfo.lpFile := apchFileName;
  lseiInfo.lpParameters := apchParameters;
  lseiInfo.lpDirectory := apchDirectory;
  lseiInfo.nShow := awrdShowCmd;

  if Boolean(ShellExecuteEx(@lseiInfo)) then
    Result := lseiInfo.hProcess;
end;

procedure DestroyProcess(hProcess: Cardinal);
Var
  ovExitCode: LongWord;
begin
  try
    if hProcess<>0 then
    begin
      GetExitCodeProcess(hProcess, ovExitCode);
      if (ovExitCode = STILL_ACTIVE) or (ovExitCode <> WAIT_OBJECT_0) then
        TerminateProcess(hProcess, ovExitCode);
      CloseHandle(hProcess);
    end;
  except
  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  hApp := StartApp('open', 'd:\Workers.pdf', '', '', SW_SHOWNORMAL);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  if hApp > 0 then
    DestroyProcess(hApp);
end;

Open in new window

0
 

Author Comment

by:LeTay
ID: 40556486
I give the point but I tried that DestroyProcess and it was indeed not killed !
0
 
LVL 26

Expert Comment

by:Sinisa Vuk
ID: 40556546
Strange, works for me. Is hApp > 0 before call DestroyProcess? Does StartApp start pdf viewer?
I need to know if process is started, if pid is returned.
0
 
LVL 26

Expert Comment

by:Sinisa Vuk
ID: 40561911
there is similar request where I put similar solution using wmi
Q_28596320 (at bottom)
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question