Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to kill a running applicaton using the file name. Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Kill a running application using the file name. 26-Feb-04
Category
Win API
Language
Delphi 4.x
Views
508
User Rating
9
# Votes
2
Replies
0
Publisher:
Darley, F. Joe
Reference URL:
			This article will help you find and kill a running application by using the 
filename that is running. don't forget to add Tlhelp32 unit in the uses clause.  
please note this code has been only tested on windows 2000, I don't know if it will 
work on windows 95/98, but if it does work please email me.
1   
2   
3   uses 
4   Tlhelp32;
5   
6   type
7     TForm1 = class(TForm)
8       Button1: TButton;
9       Memo1: TMemo;
10      procedure Button1Click(Sender: TObject);
11    private
12      { Private declarations }
13      function KillTask(ExeFileName: string): integer;
14    public
15      { Public declarations }
16    end;
17  
18  var
19    Form1: TForm1;
20  
21  implementation
22  
23  {$R *.dfm}
24  
25  function TForm1.KillTask(ExeFileName: string): integer;
26  const
27  PROCESS_TERMINATE=$0001;
28  var
29  ContinueLoop: BOOL;
30  FSnapshotHandle: THandle;
31  
32  FProcessEntry32: TProcessEntry32;//used to store proccess information that are 
33  //currently running
34  
35  sFileName:string;
36  begin
37    result := 0;
38  
39    //Takes a snapshot of the processes and the heaps, modules, and threads 
40    //used by the processes
41    FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
42  
43    FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
44  
45    //Retrieves information about the first process encountered in a system snapshot
46    ContinueLoop := Process32First(FSnapshotHandle,FProcessEntry32);
47  
48   //stops until there is no more processes to check
49    while integer(ContinueLoop) <> 0 do  begin
50    sFileName:=  UpperCase(ExtractFileName(FProcessEntry32.szExeFile));
51  
52    //check if filename is the same to terminate it.
53    if (sFileName=UpperCase(ExeFileName)) then
54    Result := Integer(TerminateProcess(OpenProcess(
55    PROCESS_TERMINATE, BOOL(0),
56    FProcessEntry32.th32ProcessID), 0));
57  
58    //gets the next process encountered in a system snapshot
59    ContinueLoop := Process32Next(FSnapshotHandle,
60    FProcessEntry32);
61  end;
62  
63    CloseHandle(FSnapshotHandle);
64  end;
65  
66  procedure TForm1.Button1Click(Sender: TObject);
67  begin
68    KillTask('itemfinder.exe');
69  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC