-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathISExec.dpr
88 lines (76 loc) · 2.07 KB
/
ISExec.dpr
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
library ISExec;
uses
System.SysUtils,
System.Classes,
Winapi.Windows,
OtlTask,
OtlTaskControl,
OtlCommon;
{$R *.res}
type
TCancelProc = function: Boolean of object;
procedure ExecTask(const task: IOmniTask);
function _Win32Check(RetVal: BOOL): BOOL;
begin
if not RetVal then
begin
OutputDebugString(PChar(SysErrorMessage(GetLastError)));
end;
Result := RetVal;
end;
var
SI: TStartupInfo;
PI: TProcessInformation;
CmdLine: UnicodeString;
CancelProc: TMethod;
begin
CancelProc := task.Param['Callback'].ToRecord<TMethod>;
CmdLine := task.Param['ExeName'];
UniqueString(CmdLine);
FillChar(SI, SizeOf(SI), 0);
FillChar(PI, SizeOf(PI), 0);
SI.cb := SizeOf(SI);
SI.dwFlags := STARTF_USESHOWWINDOW;
if task.Param['Visible'] then
SI.wShowWindow := SW_SHOWNORMAL
else
SI.wShowWindow := SW_HIDE;
SetLastError(ERROR_INVALID_PARAMETER);
{$WARN SYMBOL_PLATFORM OFF}
_Win32Check(CreateProcess(nil, PChar(CmdLine), nil, nil, False, CREATE_DEFAULT_ERROR_MODE {$IFDEF UNICODE}or CREATE_UNICODE_ENVIRONMENT{$ENDIF}, nil, nil, SI, PI));
{$WARN SYMBOL_PLATFORM ON}
WaitForInputIdle(PI.hProcess, INFINITE);
CloseHandle(PI.hThread);
while (WaitForSingleObject(PI.hProcess, 10) <> WAIT_OBJECT_0) do
begin
if TCancelProc(CancelProc) then
begin
TerminateProcess(PI.hProcess, 0);
task.Terminate;
end;
end;
CloseHandle(PI.hProcess);
end;
procedure Exec(aEXEName: WideString; aVisible: Boolean; aCallback: TMethod); stdcall;
var
lTask: IOmniTaskControl;
Msg: TMsg;
begin
lTask := CreateTask(ExecTask)
.SetParameter('ExeName', aEXEName)
.SetParameter('Callback', TOmniValue.FromRecord<TMethod>(aCallback))
.SetParameter('Visible', aVisible)
.Run;
while not lTask.WaitFor(10) do
begin
while PeekMessage(Msg, 0, 0, 0, PM_REMOVE) do
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
end;
end;
exports
Exec;
begin
end.