-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnapTask.pas
102 lines (90 loc) · 2.11 KB
/
SnapTask.pas
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
unit SnapTask;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Tlhelp32;
type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
hPro: THandle;
implementation
{$R *.dfm}
function ListarProcessos : TStringList;
var
mHandle : THandle;
mStruct : PROCESSENTRY32;
begin
Result:= TStringList.Create;
try
mhandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
mStruct.dwSize := SizeOf(PROCESSENTRY32);
if Process32First(mHandle, mStruct) then
begin
Result.Add(mStruct.szExeFile);
end;
while(Process32Next(mHandle, mStruct)) do
begin
Result.Add(mStruct.szExeFile);
end;
except;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox1.Items := ListarProcessos();
end;
function getPid(nomPro : string) : DWORD;
var
mHandle : Thandle;
mStruct :PROCESSENTRY32;
begin
Result:= 0;
try
mhandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
mStruct.dwSize := SizeOf(PROCESSENTRY32);
if Process32First(mHandle, mStruct) then
begin
if string(mStruct.szExeFile) = String(nomPro) then
begin
Result:= mStruct.th32ProcessID;
end;
end;
while(Process32Next(mHandle, mStruct)) do
begin
if string(mStruct.szExeFile) = nomPro then
begin
Result:= mStruct.th32ProcessID;
end;
end;
except;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
pid : DWORD;
begin
pid := getPid(ListBox1.Items[ListBox1.ItemIndex]);
hPro := Openprocess(PROCESS_TERMINATE,false, pid);
if hpro <> 0 then
begin
ShowMessage('Processo aberto! Manipulador: ' + IntToStr(hpro));
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
TerminateProcess(hpro, 0);
end;
end.