-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPasfmt.WinExec.pas
208 lines (178 loc) · 5.68 KB
/
Pasfmt.WinExec.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
unit Pasfmt.WinExec;
interface
uses
Winapi.Windows;
type
TReadPrepareOutputProc = reference to procedure(var Data: PByte; var DataCapacity: Cardinal);
TReadConsumeOutputProc = reference to procedure(Data: PByte; DataLen: Cardinal);
TOutputHandler = record
Prepare: TReadPrepareOutputProc;
Consume: TReadConsumeOutputProc;
end;
TWriteInputProc = reference to procedure(var Data: PByte; var DataLen: Cardinal; BytesWritten: Cardinal);
TInputHandler = record
Produce: TWriteInputProc;
end;
function RunWinProcess(
CommandLine: string;
StdIn: TInputHandler;
StdOut: TOutputHandler;
StdErr: TOutputHandler;
WorkingDirectory: string;
TimeoutMillis: Cardinal
): Cardinal;
implementation
uses
System.Classes,
System.SysUtils,
Pasfmt.Log;
type
TOutputPipeThread = class(TThread)
private
FPipe: THandle;
FOutputHandler: TOutputHandler;
FPipeDesc: string;
public
constructor Create(Pipe: THandle; OutputHandler: TOutputHandler; PipeDesc: string); overload;
procedure Execute; override;
end;
TInputPipeThread = class(TThread)
private
FPipe: THandle;
FInputHandler: TInputHandler;
FPipeDesc: string;
public
constructor Create(Pipe: THandle; InputHandler: TInputHandler; PipeDesc: string); overload;
procedure Execute; override;
end;
constructor TOutputPipeThread.Create(Pipe: THandle; OutputHandler: TOutputHandler; PipeDesc: string);
begin
inherited Create(False);
FPipe := Pipe;
FOutputHandler := OutputHandler;
FPipeDesc := PipeDesc;
end;
procedure TOutputPipeThread.Execute;
var
WasOK: Boolean;
Ptr: PByte;
DataLen: Cardinal;
BytesRead: FixedUInt;
begin
NameThreadForDebugging('pasfmt: read output pipe (' + FPipeDesc + ')');
repeat
FOutputHandler.Prepare(Ptr, DataLen);
WasOK := Winapi.Windows.ReadFile(FPipe, Ptr^, DataLen, BytesRead, nil);
FOutputHandler.Consume(Ptr, BytesRead);
until not WasOK or (BytesRead = 0);
end;
constructor TInputPipeThread.Create(Pipe: THandle; InputHandler: TInputHandler; PipeDesc: string);
begin
inherited Create(False);
FPipe := Pipe;
FInputHandler := InputHandler;
FPipeDesc := PipeDesc;
end;
procedure TInputPipeThread.Execute;
var
WasOK: Boolean;
Ptr: PByte;
DataLen: Cardinal;
BytesWritten: Cardinal;
begin
NameThreadForDebugging('pasfmt: write input pipe (' + FPipeDesc + ')');
try
BytesWritten := 0;
repeat
FInputHandler.Produce(Ptr, DataLen, BytesWritten);
WasOK := True;
if DataLen > 0 then
WasOK := Winapi.Windows.WriteFile(FPipe, Ptr^, DataLen, BytesWritten, nil);
until not WasOK or (BytesWritten = 0) or (DataLen = 0);
finally
if not CloseHandle(FPipe) then
RaiseLastOSError;
end;
end;
function RunWinProcess(
CommandLine: string;
StdIn: TInputHandler;
StdOut: TOutputHandler;
StdErr: TOutputHandler;
WorkingDirectory: string;
TimeoutMillis: Cardinal
): Cardinal;
var
SecAttrs: TSecurityAttributes;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
ProcHandle: Boolean;
StdOutPipeRead, StdOutPipeWrite: THandle;
StdErrPipeRead, StdErrPipeWrite: THandle;
StdInPipeRead, StdInPipeWrite: THandle;
StdInThread: TInputPipeThread;
StdOutThread: TOutputPipeThread;
StdErrThread: TOutputPipeThread;
begin
Result := High(Cardinal);
SecAttrs := Default(TSecurityAttributes);
SecAttrs.nLength := SizeOf(SecAttrs);
SecAttrs.bInheritHandle := True;
SecAttrs.lpSecurityDescriptor := nil;
CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SecAttrs, 0);
CreatePipe(StdErrPipeRead, StdErrPipeWrite, @SecAttrs, 0);
CreatePipe(StdInPipeRead, StdInPipeWrite, @SecAttrs, 0);
if not SetHandleInformation(StdOutPipeRead, HANDLE_FLAG_INHERIT, 0) then
RaiseLastOSError;
if not SetHandleInformation(StdErrPipeRead, HANDLE_FLAG_INHERIT, 0) then
RaiseLastOSError;
if not SetHandleInformation(StdInPipeWrite, HANDLE_FLAG_INHERIT, 0) then
RaiseLastOSError;
StartupInfo := Default(TStartupInfo);
StartupInfo.cb := SizeOf(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
StartupInfo.wShowWindow := SW_HIDE;
StartupInfo.hStdInput := StdInPipeRead;
StartupInfo.hStdOutput := StdOutPipeWrite;
StartupInfo.hStdError := StdErrPipeWrite;
Log.Debug('subprocess started at %s', [FormatDateTime('hh:mm:ss.zzz', Now)]);
ProcHandle :=
CreateProcess(nil, PChar(CommandLine), nil, nil, True, 0, nil, PChar(WorkingDirectory), StartupInfo, ProcessInfo);
if not ProcHandle then
RaiseLastOSError;
try
if not CloseHandle(StdOutPipeWrite) then
RaiseLastOSError;
if not CloseHandle(StdErrPipeWrite) then
RaiseLastOSError;
if not CloseHandle(StdInPipeRead) then
RaiseLastOSError;
StdInThread := TInputPipeThread.Create(StdInPipeWrite, StdIn, 'stdin');
StdOutThread := TOutputPipeThread.Create(StdOutPipeRead, StdOut, 'stdout');
StdErrThread := TOutputPipeThread.Create(StdErrPipeRead, StdErr, 'stderr');
try
case WaitForSingleObject(ProcessInfo.hProcess, TimeoutMillis) of
WAIT_TIMEOUT: begin
raise Exception.CreateFmt('subprocess timed out after %d ms: %s', [TimeoutMillis, CommandLine]);
end;
WAIT_FAILED:
RaiseLastOSError;
else
if not GetExitCodeProcess(ProcessInfo.hProcess, Result) then
RaiseLastOSError;
end;
Log.Debug('subprocess finished at %s', [FormatDateTime('hh:mm:ss.zzz', Now)]);
finally
StdInThread.WaitFor;
StdOutThread.WaitFor;
StdErrThread.WaitFor;
FreeAndNil(StdInThread);
FreeAndNil(StdOutThread);
FreeAndNil(StdErrThread);
end;
finally
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
end;
end;
end.