-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathWinProcess.pp
More file actions
201 lines (174 loc) · 6.12 KB
/
WinProcess.pp
File metadata and controls
201 lines (174 loc) · 6.12 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
{ Copyright (C) 2020-2026 by Bill Stewart (bstewart at iname.com)
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Lesser Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see https://www.gnu.org/licenses/.
}
{$MODE OBJFPC}
{$MODESWITCH UNICODESTRINGS}
unit WinProcess;
interface
uses
windows;
// Returns the process ID of the parent process of the specified process
function GetParentProcessID(const ProcessID: DWORD; out ParentProcessID: DWORD): DWORD;
// Returns true if the specified executable's name is the parent process of
// the specified process, or false otherwise
function IsParentProcessName(const ProcessID: DWORD; const ProcessName: string): Boolean;
// Returns true if the specified executable name is running in the specified
// process ID's process tree, or false otherwise
function IsProcessInTree(const ProcessID: DWORD; const ProcessName: string): Boolean;
// Returns true if the current process and parent process are both 32-bit
// or are both 64-bit, or false otherwise
function CurrentProcessMatchesProcessBits(const ProcessID: DWORD): Boolean;
implementation
uses
WindowsString; // https://github.com/Bill-Stewart/WindowsString
type
TProcessEntry = record
dwSize: DWORD;
cntUsage: DWORD;
th32ProcessID: DWORD;
th32DefaultHeapID: ULONG_PTR;
th32ModuleID: DWORD;
cntThreads: DWORD;
th32ParentProcessID: DWORD;
pcPriClassBase: LONG;
dwFlags: DWORD;
szExeFile: array[0..MAX_PATH - 1] of WCHAR;
end;
function CreateToolhelp32Snapshot(DwFlags: DWORD; th32ProcessID: DWORD): HANDLE;
stdcall; external 'kernel32.dll';
function Process32FirstW(hSnapshot: HANDLE; var lppe: TProcessEntry): BOOL;
stdcall; external 'kernel32.dll';
function Process32NextW(hSnapshot: HANDLE; var lppe: TProcessEntry): BOOL;
stdcall; external 'kernel32.dll';
// Gets the TProcessEntry for the specified process ID
function GetProcessEntry(const ProcessID: DWORD; out ProcessEntry: TProcessEntry): DWORD;
const
TH32CS_SNAPPROCESS = 2;
var
Snapshot: HANDLE;
OK: BOOL;
begin
Snapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, // DWORD dwFlags
0); // DWORD th32ProcessID
if Snapshot = INVALID_HANDLE_VALUE then
begin
result := GetLastError();
exit;
end;
result := ERROR_SUCCESS;
ProcessEntry.dwSize := SizeOf(TProcessEntry);
OK := Process32FirstW(Snapshot, // HANDLE hSnapshot
ProcessEntry); // LPPROCESSENTRY32W lppe
if OK then
begin
while OK do
begin
if ProcessEntry.th32ProcessID = ProcessID then
break;
OK := Process32NextW(Snapshot, // HANDLE hSnapshot
ProcessEntry); // LPPROCESSENTRY32W lppe
if not OK then
result := GetLastError();
end
end
else
result := GetLastError();
CloseHandle(Snapshot); // HANDLE hObject
end;
// Gets the TProcessEntry for the parent process of the specified process ID
function GetParentProcessEntry(const ProcessID: DWORD; out ProcessEntry: TProcessEntry): DWORD;
var
ProcEntry: TProcessEntry;
begin
result := GetProcessEntry(ProcessID, ProcEntry);
if result = ERROR_SUCCESS then
begin
result := GetProcessEntry(ProcEntry.th32ParentProcessID, ProcEntry);
if result = ERROR_SUCCESS then
ProcessEntry := ProcEntry;
end;
end;
function GetParentProcessID(const ProcessID: DWORD; out ParentProcessID: DWORD): DWORD;
var
ProcessEntry: TProcessEntry;
begin
result := GetParentProcessEntry(ProcessID, ProcessEntry);
if result = ERROR_SUCCESS then
ParentProcessID := ProcessEntry.th32ProcessID;
end;
function IsParentProcessName(const ProcessID: DWORD; const ProcessName: string): Boolean;
var
RC: DWORD;
ProcessEntry: TProcessEntry;
begin
result := false;
RC := GetParentProcessEntry(ProcessID, ProcessEntry);
if RC = ERROR_SUCCESS then
result := SameString(string(ProcessEntry.szExeFile), ProcessName);
end;
function IsProcessInTree(const ProcessID: DWORD; const ProcessName: string): Boolean;
var
RC: DWORD;
ProcessEntry: TProcessEntry;
begin
result := false;
RC := GetProcessEntry(ProcessID, ProcessEntry);
while RC = ERROR_SUCCESS do
begin
result := SameString(string(ProcessEntry.szExeFile), ProcessName);
if result then
break;
RC := GetProcessEntry(ProcessEntry.th32ParentProcessID, ProcessEntry);
end;
end;
function IsProcessor64Bit(): Boolean;
const
PROCESSOR_ARCHITECTURE_AMD64 = 9;
PROCESSOR_ARCHITECTURE_ARM64 = 12;
var
SystemInfo: SYSTEM_INFO;
Architecture: Word;
begin
GetNativeSystemInfo(@SystemInfo); // LPSYSTEM_INFO lpSystemInfo
Architecture := SystemInfo.wProcessorArchitecture;
result := (Architecture = PROCESSOR_ARCHITECTURE_AMD64) or
(Architecture = PROCESSOR_ARCHITECTURE_ARM64);
end;
function IsProcessWoW64(const ProcessID: DWORD): Boolean;
var
ProcessHandle: HANDLE;
IsWoW64: BOOL;
begin
result := false;
ProcessHandle := OpenProcess(PROCESS_QUERY_INFORMATION, // DWORD dwDesiredAccess
true, // BOOL bInheritHandle
ProcessID); // DWORD dwProcessId
if ProcessHandle <> 0 then
begin
if IsWow64Process(ProcessHandle, // HANDLE hProcess
@IsWoW64) then // PBOOL Wow64Process
begin
result := IsWoW64;
end;
CloseHandle(ProcessHandle);
end;
end;
function CurrentProcessMatchesProcessBits(const ProcessID: DWORD): Boolean;
var
CurrProcID: DWORD;
begin
CurrProcID := GetCurrentProcessId();
result := ((not IsProcessWoW64(CurrProcID)) and (not IsProcessWoW64(ProcessID))) or
(IsProcessWoW64(CurrProcID) and IsProcessWoW64(ProcessID));
end;
begin
end.