forked from groehner/JavaEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUNTProcess.pas
More file actions
231 lines (204 loc) · 5.31 KB
/
UNTProcess.pas
File metadata and controls
231 lines (204 loc) · 5.31 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
unit UNTProcess;
interface
uses Windows;
type
TNTProcess = class
private
FPid: Integer;
procedure SetPID(Value: Integer);
function GetBaseName: string;
public
property PID: Integer read FPid write SetPID;
property BaseName: string read GetBaseName;
end;
TNTProcessList = class
private
FAllOk: Boolean;
FPIDList: PInteger;
FPIDCount: Integer;
FPSApiHandle: Integer;
function GetPID(Index: Integer): Integer;
public
constructor Create;
destructor Destroy; override;
procedure Refresh;
procedure GetProcess(Num: Integer; var Process: TNTProcess);
property AllOk: Boolean read FAllOk;
property Count: Integer read FPIDCount;
property PID[Index: Integer]: Integer read GetPID;
end;
procedure ProcessTerminate(const TermProcessID: string);
procedure ProcessTerminateID(ProcessID: LongInt);
implementation
uses
SysUtils,
TlHelp32;
type
TEnumProc = function(PidList: PInteger; Cbi: Integer; var CBNeeded: Integer)
: Boolean; stdcall;
TGetBase = function(HProcess: THandle; Module: HINST; BaseName: PChar;
Size: Integer): Integer; stdcall;
var
EnumProcessesF: TEnumProc;
GetModuleBaseNameF: TGetBase;
constructor TNTProcessList.Create;
begin
inherited Create;
FAllOk := True;
FPSApiHandle := LoadLibrary('PSAPI.DLL');
if FPSApiHandle <> 0 then
begin
@EnumProcessesF := GetProcAddress(FPSApiHandle, 'EnumProcesses');
@GetModuleBaseNameF := GetProcAddress(FPSApiHandle, 'GetModuleBaseNameA');
end
else
FAllOk := False;
if FAllOk and not Assigned(@EnumProcessesF) then
FAllOk := False;
if AllOk and not Assigned(@GetModuleBaseNameF) then
FAllOk := False;
end;
destructor TNTProcessList.Destroy;
begin
FreeLibrary(FPSApiHandle);
ReallocMem(FPIDList, 0);
inherited;
end;
procedure TNTProcessList.Refresh;
var
CBNeeded: Integer;
begin
ReallocMem(FPIDList, 65536);
if not EnumProcessesF(FPIDList, 65536, CBNeeded) then
CBNeeded := 0;
ReallocMem(FPIDList, CBNeeded);
FPIDCount := CBNeeded div SizeOf(Integer);
end;
function TNTProcessList.GetPID(Index: Integer): Integer;
begin
if (Index >= 0) and (Index < Count) then
Result := PInteger(PChar(FPIDList) + Index * SizeOf(Integer))^
else
raise ERangeError.Create('PID Index out of range');
end;
procedure TNTProcessList.GetProcess(Num: Integer; var Process: TNTProcess);
begin
Process.PID := PID[Num];
end;
procedure TNTProcess.SetPID(Value: Integer);
begin
if FPid <> Value then
FPid := Value;
end;
function TNTProcess.GetBaseName: string;
var
Handle: THandle;
SZName: array [0 .. MAX_PATH - 1] of Char;
begin
Result := '';
Handle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,
False, PID);
if Handle <> 0 then
try
if GetModuleBaseNameF(Handle, 0, SZName, SizeOf(SZName)) > 0 then
Result := SZName
else
Result := 'System';
finally
CloseHandle(Handle);
end
else if PID = 0 then
Result := 'Idle';
end;
procedure ProcessTerminate(const TermProcessID: string);
const
PROCESS_TERMINATE = 1;
type
CharArray = array[0 .. MAX_PATH] of Char;
var
ProcHandle: THandle;
ProcessID: LongInt;
procedure AddProcess(PE32: TProcessEntry32; Process: string; Path: CharArray);
begin
StrCopy(Path, PE32.szExeFile);
var Str := UpperCase(Path);
if Pos(Process, Str) > 0 then
ProcessID := PE32.th32ProcessID;
end;
procedure Win9xGetProcessID(const Process: string);
var
Path: CharArray;
Snap: THandle;
PE32: TProcessEntry32;
begin
Snap := 0;
try
Snap := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if Snap <> 0 then
begin
PE32.dwSize := SizeOf(TProcessEntry32);
if Process32First(Snap, PE32) then
begin
AddProcess(PE32, Process, Path);
while Process32Next(Snap, PE32) do
AddProcess(PE32, Process, Path);
end;
end;
finally
CloseHandle(Snap);
end;
end;
procedure WinNTGetProcessID(const Process: string);
const
PROCESS_TERMINATE = 1;
var
Str: string;
NTProcessList: TNTProcessList;
NTProcess: TNTProcess;
begin
NTProcessList := TNTProcessList.Create;
if not NTProcessList.AllOk then
begin
ProcessID := 55555;
Exit;
end;
NTProcess := TNTProcess.Create;
NTProcessList.Refresh;
for var I := 0 to NTProcessList.Count - 1 do
begin
NTProcessList.GetProcess(I, NTProcess);
Str := UpperCase(NTProcess.BaseName);
if Str = Process then
ProcessID := NTProcess.PID;
end;
NTProcessList.Destroy;
NTProcess.Destroy;
end;
begin
if Win32Platform >= VER_PLATFORM_WIN32_NT then
WinNTGetProcessID(TermProcessID)
else
Win9xGetProcessID(TermProcessID);
ProcHandle := OpenProcess(PROCESS_TERMINATE, False, DWORD(ProcessID));
try
if (ProcHandle <> 0) and TerminateProcess(ProcHandle, $FFFFFFFF) then
WaitForSingleObject(ProcHandle, INFINITE);
finally
CloseHandle(ProcHandle);
end;
end;
procedure ProcessTerminateID(ProcessID: LongInt);
const
PROCESS_TERMINATE = 1;
var
ProcHandle: THandle;
begin
ProcHandle := OpenProcess(PROCESS_TERMINATE, False, DWORD(ProcessID));
try
if (ProcHandle <> 0) and TerminateProcess(ProcHandle, $FFFFFFFF) then
WaitForSingleObject(ProcHandle, INFINITE);
finally
CloseHandle(ProcHandle);
end;
end;
end.