forked from gerald-lindsly/mongo-delphi-driver
-
Notifications
You must be signed in to change notification settings - Fork 5
/
uWinProcHelper.pas
73 lines (63 loc) · 1.85 KB
/
uWinProcHelper.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
unit uWinProcHelper;
{$i LibVer.inc}
interface
uses
Windows;
function KillProcess(const aProcess: string): Boolean;
function GetProcessHandle(const AProcess : string; dwDesiredAccess : DWORD): THandle;
function IsProcessRunning(const AProcess: string): Boolean;
implementation
uses
SysUtils, TLHelp32 {$IFDef LEVEL7} , Variants {$EndIf}; // This three units are safe because they not reference Classes
function KillProcess(const aProcess: string): Boolean;
var
ProcessHandle : THandle;
begin
ProcessHandle := GetProcessHandle (aProcess, PROCESS_TERMINATE);
try
Result := TerminateProcess (ProcessHandle, 0);
finally
if ProcessHandle <> 0
then CloseHandle (ProcessHandle);
end;
end;
function GetProcessHandle(const AProcess : string; dwDesiredAccess : DWORD): THandle;
var
SnapshotHandle : THandle;
ProcessEntry32 : TProcessEntry32;
ContinueLoop : Boolean;
ProcName : string;
ProcId : THandle;
begin
Result := 0;
SnapshotHandle := CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0);
try
try
ProcessEntry32.dwSize := SizeOf (ProcessEntry32);
ContinueLoop := Process32First (SnapshotHandle, ProcessEntry32);
while ContinueLoop do
begin
ProcName := ExtractFileName(ProcessEntry32.szExeFile);
if CompareText(ProcName, AProcess) = 0
then
begin
ProcId := ProcessEntry32.th32ProcessID;
Result := OpenProcess (dwDesiredAccess, False, ProcID);
exit;
end;
ContinueLoop := Process32Next (SnapshotHandle, ProcessEntry32);
end;
except
if Result <> 0
then CloseHandle (Result);
raise;
end;
finally
CloseHandle (SnapshotHandle);
end;
end;
function IsProcessRunning(const AProcess: string): Boolean;
begin
Result := (GetProcessHandle(AProcess, PROCESS_ALL_ACCESS)) <> 0;
end;
end.