Solved

Kill a process started from a Delphi application

Posted on 2015-01-14
5
1,439 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 25

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 25

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 25

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now