Solved

Kill a process started from a Delphi application

Posted on 2015-01-14
5
2,204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 27

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 27

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 27

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

724 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