-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsproc.pas
433 lines (387 loc) · 11.5 KB
/
jsproc.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
unit jsproc;
interface
uses
windows, sysutils, xsuperobject, Winapi.TlHelp32,
Winapi.PsAPI, System.Generics.Collections;
type
Tprocmeta = packed record
id: Int64;
running: Boolean;
pid: DWORD;
handle: Thandle;
thread: Thandle;
end;
type
Twinmeta = packed record
hwnd: DWORD;
objclass: string;
ancestor: string;
caption: string;
pid: DWORD;
path: string;
fullscreen: Boolean;
end;
Tapp = packed record
path: string;
pid: DWORD;
end;
type
TQueryFullProcessImageName = function(hProcess: Thandle; dwFlags: DWORD; lpExeName: PChar; nSize: PDWORD): BOOL; stdcall;
function EnumProc: widestring;
function ProcmetaAsJson(meta: Tprocmeta): string;
function WinmetaAsJson(meta: Twinmeta): string;
function RunProcess(FileName: string; const param: string = ''; const hide: Boolean = false): Tprocmeta;
function getWinmeta(window: hwnd): Twinmeta;
function pingProcess(id: Int64): Boolean;
function releaseProcess(id: Int64): integer;
function GetAppExecutable(hwndFG: hwnd): Tapp;
function isfullscreen(h: DWORD): Boolean;
function GetDosOutput(CommandLine: string; Work: string = ''): string;
function GetOutputStdIn(app, command: string; const dir: string = ''; const delimiter: char = #10): string;
var
ProcList: TDictionary<integer, Tprocmeta>;
QueryFullProcessImageName: TQueryFullProcessImageName;
pc: Int64;
implementation
uses
desktools, psyapi;
function isfullscreen(h: DWORD): Boolean;
var
rect: Trect;
height: integer;
width: integer;
begin
if h <= 0 then
begin
Result := false;
exit;
end;
GetWindowRect(h, rect);
height := rect.Bottom - rect.Top;
width := rect.Right - rect.Left;
Result := false;
if (IsWindowVisible(h) and not IsIconic(h)) then
begin
// if (rect.Left <= 0) and (rect.Top <= 0) and (width >= screen.width) and (height >= screen.height) and (h <> GetDesktopWindow) then
Result := True;
end;
end;
function EnumProc: widestring;
var
MyHandle: Thandle;
Struct: TProcessEntry32;
e: string;
begin
Result := '[';
try
MyHandle := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
Struct.dwSize := Sizeof(TProcessEntry32);
if Process32First(MyHandle, Struct) then
e := Struct.szExeFile;
// if e <> '[System Process]' then
Result := Result + quotedstr(makePath(e)) + ',';
while Process32Next(MyHandle, Struct) do
begin
e := Struct.szExeFile;
// if e <> '[System Process]' then
Result := Result + quotedstr(makePath(e)) + ',';
end;
delete(Result, length(Result), 1);
Result := Result + ']';
except
on exception do
// Sleep(1);
end
end;
function GetDosOutput(CommandLine: string; Work: string = ''): string;
var
SA: TSecurityAttributes;
SI: TStartupInfo;
PI: TProcessInformation;
StdOutPipeRead, StdOutPipeWrite: THandle;
WasOK: Boolean;
Buffer: array[0..255] of AnsiChar;
BytesRead: Cardinal;
WorkDir: string;
Handle: Boolean;
begin
Result := '';
with SA do
begin
nLength := SizeOf(SA);
bInheritHandle := True;
lpSecurityDescriptor := nil;
end;
CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SA, 0);
try
with SI do
begin
FillChar(SI, SizeOf(SI), 0);
cb := SizeOf(SI);
dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
wShowWindow := SW_HIDE;
hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdin
hStdOutput := StdOutPipeWrite;
hStdError := StdOutPipeWrite;
end;
WorkDir := Work;
if WorkDir = '' then
WorkDir := GetCurrentDir;
Handle := CreateProcess(nil, PChar('cmd.exe /C ' + CommandLine), nil, nil, True, 0, nil, PChar(WorkDir), SI, PI);
CloseHandle(StdOutPipeWrite);
if Handle then
try
repeat
WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil);
if BytesRead > 0 then
begin
Buffer[BytesRead] := #0;
Result := Result + Buffer;
end;
until not WasOK or (BytesRead = 0);
WaitForSingleObject(PI.hProcess, INFINITE);
finally
CloseHandle(PI.hThread);
CloseHandle(PI.hProcess);
end;
finally
CloseHandle(StdOutPipeRead);
end;
end;
function GetChildProcess(CurrentProcessId: dword): DWORD;
var
HandleSnapShot: THandle;
EntryParentProc: TProcessEntry32;
HandleParentProc: THandle;
ParentProcessFound: Boolean;
ParentProcPath: string;
begin
result := 0;
ParentProcessFound := False;
HandleSnapShot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //enumerate the process
if HandleSnapShot <> INVALID_HANDLE_VALUE then
begin
EntryParentProc.dwSize := SizeOf(EntryParentProc);
if Process32First(HandleSnapShot, EntryParentProc) then //find the first process
begin
repeat
if EntryParentProc.th32ParentProcessID = CurrentProcessId then
begin
if EntryParentProc.szExeFile <> 'conhost.exe' then
begin
Result := EntryParentProc.th32ProcessID;
break;
end;
end;
until not Process32Next(HandleSnapShot, EntryParentProc);
end;
CloseHandle(HandleSnapShot);
end;
end;
function GetOutputStdIn(app, command: string; const dir: string = ''; const delimiter: char = #10): string;
//launches an app, writes something to stdin and terminates application on delimiter received
var
SA: TSecurityAttributes;
SI: TStartupInfo;
PI: TProcessInformation;
StdOutPipeRead, StdOutPipeWrite: THandle;
InputPipeRead, InputPipeWrite: THandle;
WasOK: Boolean;
Buffer: array[0..255] of AnsiChar;
BytesRead: Cardinal;
WorkDir: string;
Handle: Boolean;
dos: string;
DosSize: Integer;
cWritten: Cardinal;
child: dword;
cmd: ansistring;
r: char;
i: integer;
begin
Result := '';
with SA do
begin
nLength := SizeOf(SA);
bInheritHandle := True;
lpSecurityDescriptor := nil;
end;
CreatePipe(InputPipeRead, InputPipeWrite, @SA, 0);
CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SA, 0);
try
with SI do
begin
FillChar(SI, SizeOf(SI), 0);
cb := SizeOf(SI);
dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
wShowWindow := SW_HIDE;
hStdInput := InputPipeRead;
hStdOutput := StdOutPipeWrite;
hStdError := StdOutPipeWrite;
end;
if dir = '' then
WorkDir := GetCurrentDir
else
WorkDir := dir;
SetLength(dos, 255);
DosSize := GetEnvironmentVariable('COMSPEC', @dos[1], 255);
SetLength(dos, DosSize);
Handle := CreateProcess(nil, PChar(dos + ' /C ' + app), @SA, @SA, true, SYNCHRONIZE, nil, PChar(WorkDir), SI, PI);
cmd := command + #13#10;
WriteFile(InputPipeWrite, cmd[1], Length(cmd), cWritten, nil);
CloseHandle(StdOutPipeWrite);
if Handle then
try
repeat
WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil);
if BytesRead > 0 then
begin
Buffer[BytesRead] := #0;
Result := Result + Buffer;
for i := 0 to Length(Result) do
begin
if result[i] = delimiter then
begin
child := GetChildProcess(PI.dwProcessId);
terminateprocess(OpenProcess(PROCESS_TERMINATE, false, child), 0);
// GenerateConsoleCtrlEvent(CTRL_C_EVENT, child);
// SetConsoleCtrlHandler(nil, true);
end;
end;
end;
Sleep(1);
until not WasOK or (BytesRead = 0);
WaitForSingleObject(PI.hProcess, INFINITE);
finally
CloseHandle(PI.hThread);
CloseHandle(PI.hProcess);
end;
finally
CloseHandle(StdOutPipeRead);
CloseHandle(InputPipeWrite);
CloseHandle(InputPipeRead);
end;
result := trim(Result);
end;
function getWinmeta(window: hwnd): Twinmeta;
var
anc: Thandle;
app: Tapp;
begin
anc := GetAncestor(window, 3);
app := (GetAppExecutable(anc));
Result.hwnd := window;
Result.objclass := getwindowclass(window);
Result.caption := getwindowtitle(window);
Result.ancestor := getwindowclass(anc);
Result.fullscreen := isfullscreen(window);
Result.path := makePath(app.path);
Result.pid := app.pid;
end;
function GetAppExecutable(hwndFG: hwnd): Tapp;
var
hProc: Thandle;
hMod: array[0..0] of HMODULE;
dwPID: DWORD;
dwSize: DWORD;
dwCount: DWORD;
nSize: cardinal;
sciezka: array[0..MAX_PATH - 1] of Char;
begin
Result.path := '';
// SetLength(result.exe, 0);
if (hwndFG <> 0) then
begin
if (GetWindowThreadProcessID(hwndFG, @dwPID) <> 0) then
begin
Result.pid := dwPID;
hProc := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, True, dwPID);
if (hProc <> 0) then
begin
try
dwCount := 0;
if EnumProcessModules(hProc, @hMod, Sizeof(HMODULE), dwCount) then
begin
SetLength(Result.path, Succ(MAX_PATH));
dwSize := GetModuleFileNameEx(hProc, hMod[0], Pointer(Result.path), MAX_PATH);
SetLength(Result.path, dwSize);
end
else
begin
nSize := MAX_PATH;
ZeroMemory(@sciezka, MAX_PATH);
if QueryFullProcessImageName(hProc, 0, sciezka, @nSize) then
Result.path := trim(sciezka);
if Result.path = '' then
Result.path := '<unknown>';
end;
finally
CloseHandle(hProc);
end;
end;
end;
end;
end;
function releaseProcess(id: Int64): integer;
var
Proc: Tprocmeta;
begin
if ProcList.TryGetValue(id, Proc) then
begin
OutputDebugString(PChar('Releasing handle: ' + inttostr(id) + '/' + inttostr(Proc.handle)));
CloseHandle(Proc.handle);
CloseHandle(Proc.thread);
ProcList.Remove(id);
end;
end;
function WinmetaAsJson(meta: Twinmeta): string;
begin
Result := TJSON.Stringify<Twinmeta>(meta);
end;
function ProcmetaAsJson(meta: Tprocmeta): string;
begin
Result := TJSON.Stringify<Tprocmeta>(meta);
end;
function pingProcess(id: Int64): Boolean;
var
Proc: Tprocmeta;
begin
if ProcList.TryGetValue(id, Proc) then
begin
if WaitForSingleObject(Proc.handle, 1) = WAIT_TIMEOUT then
Result := True
else
Result := false;
end;
end;
function RunProcess(FileName: string; const param: string = ''; const hide: Boolean = false): Tprocmeta;
var
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
begin
FillChar(StartupInfo, Sizeof(StartupInfo), #0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK;
if hide then
StartupInfo.wShowWindow := SW_HIDE
else
StartupInfo.wShowWindow := SW_SHOWNORMAL;
if CreateProcess(nil, @FileName[1], nil, nil, false, CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo) then
begin
Result.running := True;
Result.pid := ProcessInfo.dwProcessId;
Result.handle := ProcessInfo.hProcess;
Result.thread := ProcessInfo.hThread;
Result.id := pc;
ProcList.Add(Result.id, Result);
Inc(pc);
end
else
Result.running := false;
end;
initialization
ProcList := TDictionary<integer, Tprocmeta>.Create;
@QueryFullProcessImageName := GetProcAddress(GetModuleHandle('kernel32'), 'QueryFullProcessImageNameW');
finalization
ProcList.Free;
end.