-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadList.pas
53 lines (44 loc) · 1.18 KB
/
ThreadList.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
unit ThreadList;
interface
uses
PsAPI,
TlHelp32,
Windows,
SysUtils;
procedure KillAllThreads;
implementation
uses
Logger;
procedure KillAllThreads;
var
SnapProcHandle: THandle;
IsMore: Boolean;
ThreadEntry: TThreadEntry32;
PID: Cardinal;
Result: Boolean;
LastError: DWORD;
begin
PID := GetCurrentProcessId;
SnapProcHandle := CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); //Takes a snapshot of the all threads
Result := (SnapProcHandle <> INVALID_HANDLE_VALUE);
if Result then
try
ThreadEntry.dwSize := SizeOf(ThreadEntry);
// Get first thread
IsMore := Thread32First(SnapProcHandle, ThreadEntry);
while IsMore do
begin
if ThreadEntry.th32OwnerProcessID = PID then
if not TerminateThread(ThreadEntry.th32ThreadID, 0) then
begin
LastError := GetLastError();
Logger.Log('Cannot terminate thread #'+IntToStr(ThreadEntry.th32ThreadID)+' error: '+IntToStr(LastError));
end;
// Get next thread
IsMore := Thread32Next(SnapProcHandle, ThreadEntry);
end;
finally
CloseHandle(SnapProcHandle);//Close the Handle
end;
end;
end.