-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuProcessusManagment.pas
366 lines (322 loc) · 9.53 KB
/
uProcessusManagment.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
unit uProcessusManagment;
interface
uses
Windows, SysUtils, Classes, AclApi, EncdDecd, Messages, TLHelp32, ShellAPI,
Dialogs, PsAPI;
{Pour un même nom de processus on peut récupérer plusieurs PID
autant de fois que le processus est lancé, la valeur est retournée dans le Result
Attention il faut caster Les items de la liste des PIDs en CARDINAL pour les
utiliser !!!
}
function PidsByProcessName(const AProcessName: string; var PIDs : string):Integer;
function ProcessNameByHandle(Handle:HWND):string;
function ProcessNameByPID( PID: Cardinal):string;
function HandleFromProcessName(const AProcessName: string; var AHandle: THandle;
num : Integer = 0):Boolean;
function EnumProcessus:string;
function EnumProcessusName:string;
function EnumModules:string;
function EnumThreads:string;
function IsActivProcessus(const AProcessName: string):Boolean;
{Active windows : GetForegroundWindow:HWND}
procedure MinimizeActiveWindows;
procedure RestoreActiveWindows;
procedure CloseActiveWindows;
implementation
function ProcessNameByHandle(Handle:HWND):string;
var
Pid : DWord;
SnapShot : HWND;
Module : TModuleEntry32;
begin
Result := '';
if not IsWindow(Handle) then exit;
GetWindowThreadProcessId(Handle, @Pid); // récupere le pid
Snapshot := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, Pid); // creer un snapshot sur le pid
try
if Snapshot<>-1 then
begin
Module.dwSize := SizeOf(TModuleEntry32);
if Module32First(Snapshot,Module) then result := Module.szModule; //szExePath; // recupere l'exe path
end
finally
CloseHandle(Snapshot);
end;
end;
{..............................................................................}
type
TWinInfo = record
PID: Cardinal;
Handle: THandle;
Found: Boolean;
end;
function EnumWindowsProcForHandle(Wnd: HWND; lParam: lParam): BOOL; stdcall;
var
AID : Cardinal;
begin
Result := True;
if (IsWindowVisible(Wnd) or IsIconic(wnd)) and
((GetWindowLong(Wnd, GWL_HWNDPARENT) = 0) or
(GetWindowLong(Wnd, GWL_HWNDPARENT) = Integer(GetDesktopWindow))) and
(GetWindowLong(Wnd, GWL_EXSTYLE) and WS_EX_TOOLWINDOW = 0) then
begin
GetWindowThreadProcessId(Wnd, @AID);
if AID = TWinInfo(Pointer(lParam)^).PID then begin
TWinInfo(Pointer(lParam)^).Handle := Wnd;
TWinInfo(Pointer(lParam)^).Found := True;
end;
end;
end;
{..............................................................................}
type
PFindWindowsStruct = ^TFindWindowsStruct;
TFindWindowsStruct = record
ProcessID: DWORD;
HandleList: TList;
end;
function EnumWindowsProc_PC(hwnd: HWND; lParam: LPARAM): boolean; stdcall;
var
dwProcessId: DWORD;
begin
Result:= false;
if lParam <> 0 then begin
GetWindowThreadProcessId(hwnd, dwProcessId);
with PFindWindowsStruct(lParam)^ do if dwProcessID = ProcessID then
HandleList.Add(Pointer(hwnd));
Result:= true;
end
end;
procedure FindProcessWindows(ProcessID: Integer; Handles: TList);
var
findWindowsStruct: TFindWindowsStruct;
begin
findWindowsStruct.ProcessID:= ProcessID;
findWindowsStruct.HandleList:= Handles;
EnumWindows(@EnumWindowsProc_PC, Integer(@findWindowsStruct));
end;
{..............................................................................}
function GetProcessID(const ProcessName: string; var ProcessIDs: string): Integer;
{Caster les ProcessIDs en integer pour les utiliser en dehors de la routine}
var
C : Boolean;
FH : THandle;
FP : TProcessEntry32;
begin
FH := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FP.dwSize := Sizeof(FP);
C := Process32First(FH, FP);
with TStringList.Create do
try
while C do begin
if (UpperCase(FP.szExeFile) = UpperCase(ProcessName)) then
Add( Format('%d', [FP.th32ProcessID]) );
C := Process32Next(FH, FP);
end;
ProcessIDs := Text;
Result := Count;
CloseHandle(FH);
finally
Free
end;
end;
function PidsByProcessName(const AProcessName: string; var PIDs: string):Integer;
begin
Result := GetProcessID(AProcessName, PIDs);
end;
{..............................................................................}
function ProcessNameByPID( PID: Cardinal):string;
var
SnapShot: Cardinal;
ProcessEntry: TProcessEntry32;
begin
Result := EmptyStr;
SnapShot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if SnapShot = 0 then Exit;
ProcessEntry.dwSize := SizeOf(ProcessEntry);
if Process32First(SnapShot, ProcessEntry) then begin
if ProcessEntry.th32ProcessID = PID then begin
Result := ProcessEntry.szExeFile;
Exit
end;
while Process32Next(SnapShot, ProcessEntry) do
if ProcessEntry.th32ProcessID = PID then begin
Result := ProcessEntry.szExeFile;
Break
end;
end;
CloseHandle(SnapShot);
end;
{..............................................................................}
function GetPathFromPID(PID: cardinal; ProcessName: string = ''): string;
var
hProcess: THandle;
Path: array[0..MAX_PATH - 1] of char;
begin
hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, PID);
Result := ProcessName;
if hProcess <> 0 then
try
if GetModuleFileNameEx(hProcess, 0, Path, MAX_PATH) <> 0 then Result := Path
finally
CloseHandle(hProcess)
end
end;
{..............................................................................}
function EnumModules:string;
var
SnapShot: Cardinal;
ModuleEntry: TModuleEntry32;
C: Boolean;
begin
with TStringList.Create do
try
SnapShot := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, 0);
if SnapShot = 0 then Exit;
ModuleEntry.dwSize := SizeOf(ModuleEntry);
C := Module32First(SnapShot, ModuleEntry);
while C do begin
with ModuleEntry do Add(Format('%s; %d; %s', [szModule, modBaseSize, szExePath]));
C := Module32Next(SnapShot, ModuleEntry);
end;
CloseHandle(SnapShot);
// Sort;
Result := Text
finally
Free
end
end;
function EnumProcessus:string;
var
SnapShot: Cardinal;
ProcessEntry: TProcessEntry32;
C: Boolean;
H : THandle;
begin
with TStringList.Create do
try
SnapShot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if SnapShot = 0 then Exit;
ProcessEntry.dwSize := SizeOf(ProcessEntry);
C := Process32First(SnapShot, ProcessEntry);
while C do begin
with ProcessEntry do begin
HandleFromProcessName( szExeFile, H );
if IsWindowVisible( H ) and not IsIconic( H ) (*and (GetWindowLong(H, GWL_EXSTYLE) and WS_EX_TOOLWINDOW = 0) and
((GetWindowLong(H, GWL_HWNDPARENT) = 0) or (GetWindowLong(H, GWL_HWNDPARENT) = Int(GetDesktopWindow)))*)
then
Add(Format('%s; %d; %d; %d; %s',
[szExeFile,
th32ParentProcessID,
th32ProcessID,
cntThreads,
GetPathFromPID(th32ProcessID, szExeFile)
]));
end;
C := Process32Next(SnapShot, ProcessEntry);
end;
CloseHandle(SnapShot);
// Sort;
Result := Text;
finally
Free
end
end;
function EnumProcessusName:string;
var
SnapShot: Cardinal;
ProcessEntry: TProcessEntry32;
C: Boolean;
begin
with TStringList.Create do
try
SnapShot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if SnapShot = 0 then Exit;
ProcessEntry.dwSize := SizeOf(ProcessEntry);
C := Process32First(SnapShot, ProcessEntry);
while C do begin
with ProcessEntry do Add(szExeFile);
C := Process32Next(SnapShot, ProcessEntry);
end;
CloseHandle(SnapShot);
// Sort;
Result := Text;
finally
Free
end
end;
function EnumThreads:string;
var
SnapShot: Cardinal;
ThreadEntry: TThreadEntry32;
C: Boolean;
begin
with TStringList.Create do
try
SnapShot := CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if SnapShot = 0 then Exit;
ThreadEntry.dwSize := SizeOf(ThreadEntry);
C := Thread32First(SnapShot, ThreadEntry);
while C do with ThreadEntry do begin
Add( Format('%s; %d; %d',
[ProcessNameByPID( ThreadEntry.th32OwnerProcessID ),
th32OwnerProcessID,
tpBasePri
] ));
C := Thread32Next(SnapShot, ThreadEntry);
end;
CloseHandle(SnapShot);
// Sort;
Result := Text
finally
Free
end
end;
function IsActivProcessus(const AProcessName: string):Boolean;
begin
with TStringList.Create do
try
Text := EnumProcessusName;
Result := IndexOf(AProcessName) > -1
finally
Free
end
end;
{..............................................................................}
function HandleFromProcessName(const AProcessName: string; var AHandle: THandle;
num : Integer):Boolean;
var
WinInfo : TWinInfo;
PIDs : string;
begin
Result := False;
AHandle := 0;
if GetProcessID(AProcessName, PIDs) > 0 then with TStringList.Create do
try
Text := PIDs;
if num < Count then begin
WinInfo.Found := False;
WinInfo.PID := StrToInt( Strings[num] );
EnumWindows(@EnumWindowsProcForHandle, Integer(@WinInfo));
Result := WinInfo.Found;
if Result then AHandle := WinInfo.Handle;
end;
finally
Free
end
end;
{...............................................................................
Active windows : GetForegroundWindow:HWND
...............................................................................}
procedure MinimizeActiveWindows;
begin
SendMessage(GetForegroundWindow, WM_SYSCOMMAND, SC_MINIMIZE, 0)
end;
procedure RestoreActiveWindows;
begin
SendMessage(GetForegroundWindow, WM_SYSCOMMAND, SC_RESTORE, 0)
end;
procedure CloseActiveWindows;
begin
SendMessage(GetForegroundWindow, WM_SYSCOMMAND, SC_CLOSE, 0)
end;
end.