Solved

Kill a process started from a Delphi application

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Simple Delphi Question 9 83
Tviruailstringtree sort multi columns on header click 1 53
Firemonkey DbLookupComboBox equivalent ? 2 36
RESTRequest Parameter 4 12
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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

12 Experts available now in Live!

Get 1:1 Help Now