1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| unit Unit3;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,tlhelp32;
type
TForm3 = class(TForm)
ListBox1: TListBox;
Label1: TLabel;
procedure ListBox1Click(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure ListBox1DblClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
function UpString(Str: string): string;
var i: integer;
begin
result:='';
for i:=1 to length(str) do
result:=result+UpCase(str[i]);
end;
//fonction qui retourne l'id d'un process fournit en parametre
function GetProcessId(ProgName:string):cardinal;
var Snaph:thandle;
Proc:tprocessentry32;
PId:cardinal;
begin
PId:=0;
Proc.dwSize:=sizeof(Proc);
Snaph:=createtoolhelp32snapshot(TH32CS_SNAPALL,0); //recupere un capture de process
process32first(Snaph,Proc); //premeir process de la list
if UpString(extractfilename(Proc.szExeFile))=UpString(ProgName) then //test pour savoir si le process correspond
PId:=Proc.th32ProcessID // recupere l'id du process
else
begin
while process32next(Snaph,Proc) do //dans le cas contraire du test on continue à cherche le process en question
begin
if extractfilename(Proc.szExeFile)=ProgName then
PId:=Proc.th32ProcessID;
end;
end;
Closehandle(Snaph);
result:=PId;
end;
//fonction qui marche comme GetProcessId, sauf que la elle retourne une liste de process
function GetProcessList():TStringList;
var Snaph:thandle;
Proc:tprocessentry32;
PList:TStringList;
begin
PList:= TStringList.Create();
PList.Clear;
Proc.dwSize:=sizeof(Proc);
Snaph:=createtoolhelp32snapshot(TH32CS_SNAPALL,0);
process32first(Snaph,Proc);
PList.Add(extractfilename(proc.szExeFile));
while process32next(Snaph,Proc) do
PList.Add(extractfilename(Proc.szExeFile));
result:=PList;
Closehandle(Snaph);
end;
procedure TForm3.FormActivate(Sender: TObject);
var TSL : TStringList;
begin
Listbox1.Clear;
TSL := GetProcessList;
Try Listbox1.Items.Assign(TSL);
Finally
TSL.Free;
end;
end ;
procedure TForm3.ListBox1DblClick(Sender: TObject);
var Proch:thandle;
PId:cardinal;
begin
PId:=GetProcessId(Listbox1.items[Listbox1.itemindex]);
Proch:=openprocess(PROCESS_ALL_ACCESS ,true,PId); //handel du process
if not terminateprocess(Proch,PId) then //terminer le process
showmessage('Impossible d''arrete '+Listbox1.items[Listbox1.itemindex])
else
begin
Listbox1.Clear;
Listbox1.Items:=GetProcessList();
end;
Closehandle(Proch);
end;
end. |
Partager