?
Solved

A challenging Question about ending a process

Posted on 2003-03-17
19
Medium Priority
?
240 Views
Last Modified: 2010-04-04
hello
     How do i end a windows process, say if the system runs a process called myfirst.exe and this can be viewed in the Task Manager > Processes, now the problem is i have to end this process and call another, i am struck with this plz help me ..............         Sam
0
Comment
Question by:samuel_chinnu
[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
  • 8
  • 6
  • 4
  • +1
19 Comments
 
LVL 17

Expert Comment

by:geobul
ID: 8151392
Hi,

It would be easier if that exe has a window and you know the class name or the caption of that window. Is this your case?

Regards, Geo
0
 
LVL 4

Accepted Solution

by:
cjm30305 earned 300 total points
ID: 8151835
function TForm1.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;


Cheers!!!
0
 
LVL 4

Expert Comment

by:cjm30305
ID: 8151846
BTW - This will kill ALL processes with that name.  If you are running one session of Notepad.exe or 20 Notepad.exe, all will be gone....

Have fun!!!
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 17

Expert Comment

by:geobul
ID: 8152035
cjm30305,

Your code will NOT work under Windows NT.

Regards, Geo
0
 
LVL 4

Expert Comment

by:cjm30305
ID: 8152268
I do not see where there is a requirement for NT.  This WILL work on 2000 and XP.  
0
 
LVL 4

Expert Comment

by:cjm30305
ID: 8152368
I do not see where there is a requirement for NT.  This WILL work on 2000 and XP.  
0
 
LVL 4

Expert Comment

by:cjm30305
ID: 8152392
Also, NT will be desupported in June of this year.  Why would someone be doing new development in OS that is to be desupported?
0
 
LVL 17

Expert Comment

by:geobul
ID: 8152638
I prefer posting WM_QUIT message to the main window of the app:

var
  h: HWND;
begin
  h := FindWindow('ClassName', 'Title'); // one of them or both
  if IsWindow(h) then PostMessage(h, WM_CLOSE, 0, 0);
end;

TollHelp32 is not available in NT 4.
 
Another thing is that OpenProcess API creates a handle which has to be freed by calling CloseHandle API.

Regards, Geo
0
 

Expert Comment

by:trident_job
ID: 8153097
From Smurff
-----------------------------------------
procedure KillApp(title : string);
var h : HWND;

begin
h:=FindWindow(nil,@(title)[1]);
if h<>0 then
       begin
       SendMessage(h,WM_CLOSE,0,0);
       Showmessage('App terminated.')
       end
       else
       Showmessage('failed kill app.')
end;

//to close notepad,
KillApp('Notepad');
Hope this helps
0
 
LVL 4

Expert Comment

by:cjm30305
ID: 8153276
Okay all, FindWindow will find the specified window by it's title.  You have both provided a solution for a process that has an open window.

However, FindWindow will not kill an app that is listed in the task manager list that does not have an associated window.  And yes, you must use the CloseHandle API, as the above example shows, but this is the only way to kill an app that has no window, but an open process, per the initial requirement from samuel_chinnu.

Cheers.
0
 
LVL 4

Expert Comment

by:cjm30305
ID: 8153412
Example, close any windows explorer sessions, and then pass explorer.exe into your function/procedure, then pass it into mine.  Mine will work and kill explorer.exe (you will loose your desktop), however FindWindow reports 'Failed kill app'.
0
 

Author Comment

by:samuel_chinnu
ID: 8157078
Hi cjm30305
Thanks for ur valuable answer, but i ended up with some errors while compiling , it would be great if u can clear the errors for me waiting for ur relpy........Sam
0
 

Author Comment

by:samuel_chinnu
ID: 8157089
Hi cjm30305
Thanks for ur valuable answer, but i ended up with some errors while compiling , it would be great if u can clear the errors for me waiting for ur relpy........Sam
0
 

Author Comment

by:samuel_chinnu
ID: 8157131
Hi cjm30305
Thanks for ur valuable answer, but i ended up with some errors while compiling , it would be great if u can clear the errors for me waiting for ur relpy........Sam
0
 
LVL 4

Expert Comment

by:cjm30305
ID: 8158806
What errors did you encounter?  The only thing that I can think of is you may need Tlhelp32 in your uses clause.

If that does not work, please send me the text of the errors you got when compiling.

What version of Delphi are you using?  This bit of code was first written by me in Delphi 5 and has been ported to 6 and 7.

Jake
0
 
LVL 17

Expert Comment

by:geobul
ID: 8159479
Hi,

In order to close all opened handles correctly, change:
---
Result := Integer(TerminateProcess(OpenProcess(
                       PROCESS_TERMINATE, BOOL(0),
                       FProcessEntry32.th32ProcessID), 0));
---

to:

---
var hPID: THandle;
...
hPID := OpenProcess(PROCESS_TERMINATE, false,        FProcessEntry32.th32ProcessID);
if hPID <> 0 then begin
  try
    result := Integer(TerminateProcess(hPID, 0));
  finally
    CloseHandle(hPID);
  end;
end;
---
 
Regards, Geo
0
 

Author Comment

by:samuel_chinnu
ID: 8163554
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
function TForm1.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;

end.


Errors:
[Error] Unit1.pas(23): Undeclared identifier: 'KillTask'
[Error] Unit1.pas(29): Undeclared identifier: 'TProcessEntry32'
[Error] Unit1.pas(31): Undeclared identifier: 'result'
[Error] Unit1.pas(33): Undeclared identifier: 'CreateToolhelp32Snapshot'
[Error] Unit1.pas(34): Undeclared identifier: 'TH32CS_SNAPPROCESS'
[Error] Unit1.pas(36): Missing operator or semicolon
[Error] Unit1.pas(38): Undeclared identifier: 'Process32First'
[Error] Unit1.pas(43): ')' expected but identifier 'szExeFile' found
[Error] Unit1.pas(62): Statement expected but end of file found

Hi cjm30305
the errors i have stated could be silly but im just new to this language and im using delphi 7 waiting for ur reply .....sam
0
 

Author Comment

by:samuel_chinnu
ID: 8163580
Hi cjm30305
Thanks for ur valuable answer, as u said the error was is didnt add Tlhelp32 in uses clause tx very much.....sam
0
 

Author Comment

by:samuel_chinnu
ID: 8178469
Hi cjm30305

One more query the codes above works perfect with normal process if its a system process how do i deal it...Sam
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month11 days, 7 hours left to enroll

752 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