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 uses4 Tlhelp32;
5 6 type7 TForm1 = class(TForm)
8 Button1: TButton;
9 Memo1: TMemo;
10 procedure Button1Click(Sender: TObject);
11 private12 { Private declarations }13 function KillTask(ExeFileName: string): integer;
14 public15 { Public declarations }16 end;
17 18 var19 Form1: TForm1;
20 21 implementation22 23 {$R *.dfm}24 25 function TForm1.KillTask(ExeFileName: string): integer;
26 const27 PROCESS_TERMINATE=$0001;
28 var29 ContinueLoop: BOOL;
30 FSnapshotHandle: THandle;
31 32 FProcessEntry32: TProcessEntry32;//used to store proccess information that are 33 //currently running34 35 sFileName:string;
36 begin37 result := 0;
38 39 //Takes a snapshot of the processes and the heaps, modules, and threads 40 //used by the processes41 FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
42 43 FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
44 45 //Retrieves information about the first process encountered in a system snapshot46 ContinueLoop := Process32First(FSnapshotHandle,FProcessEntry32);
47 48 //stops until there is no more processes to check49 while integer(ContinueLoop) <> 0 dobegin50 sFileName:= UpperCase(ExtractFileName(FProcessEntry32.szExeFile));
51 52 //check if filename is the same to terminate it.53 if (sFileName=UpperCase(ExeFileName)) then54 Result := Integer(TerminateProcess(OpenProcess(
55 PROCESS_TERMINATE, BOOL(0),
56 FProcessEntry32.th32ProcessID), 0));
57 58 //gets the next process encountered in a system snapshot59 ContinueLoop := Process32Next(FSnapshotHandle,
60 FProcessEntry32);
61 end;
62 63 CloseHandle(FSnapshotHandle);
64 end;
65 66 procedure TForm1.Button1Click(Sender: TObject);
67 begin68 KillTask('itemfinder.exe');
69 end;