This repository has been archived by the owner on Nov 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
EvilWorks.Vcl.ConsoleIO.pas
397 lines (353 loc) · 10.9 KB
/
EvilWorks.Vcl.ConsoleIO.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
unit EvilWorks.Vcl.ConsoleIO;
interface
uses
WinApi.Windows,
WinApi.Messages,
System.Classes,
System.SysUtils,
Vcl.Forms;
const
MIO_OFFSET = $1911;
MIO_RECEIVE_OUTPUT = WM_USER + MIO_OFFSET + 0;
MIO_RECEIVE_ERROR = WM_USER + MIO_OFFSET + 1;
MIO_ERROR = WM_USER + MIO_OFFSET + 2;
MIO_PROCESS_TERMINATED = WM_USER + MIO_OFFSET + 3;
type
TReceiveEvent = procedure(Sender: TObject; const Cmd: string) of object;
TProcessStatusChangeEvent = procedure(Sender: TObject; IsRunning: Boolean) of object;
TSplitMode = (smNone, sm0D0A, smSplitchar);
{ TConsoleIO }
TConsoleIO = class(TComponent)
private
FWindowHandle : HWND;
InputReadPipe, InputWritePipe : THandle;
OutputReadPipe, OutputWritePipe: THandle;
ErrorReadPipe, ErrorWritePipe : THandle;
FProcessHandle : THandle;
FTerminateCommand : string;
FEnableKill : Boolean;
FWaitTimeout : Integer;
FStopProcessOnFree : Boolean;
FOutputBuffer : string;
FSplitReceive : Boolean;
FSplitSend : Boolean;
FSplitChar : Char;
FSplitMode : TSplitMode;
FOnReceiveError : TReceiveEvent;
FOnReceiveOutput : TReceiveEvent;
FOnError : TReceiveEvent;
FOnProcessStatusChange : TProcessStatusChangeEvent;
function GetIsRunning: Boolean;
procedure SetProcessHandle(const Value: THandle);
procedure ReceiveOutput(Buf: Pointer; Size: Integer);
procedure ReceiveError(Buf: Pointer; Size: Integer);
procedure Error(const Msg: string);
procedure ReaderProc(Handle: THandle; MessageCode: Integer);
procedure ProcessTerminated;
procedure CloseProcessHandle;
function SplitSendAvail: string;
property ProcessHandle: THandle read FProcessHandle write SetProcessHandle;
property OutputBuffer: string read FOutputBuffer write FOutputBuffer;
protected
procedure WndProc(var Msg: TMessage);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure ClosePipes;
procedure SendInput(Msg: string);
procedure RunProcess(const CmdLine: string; CurrentDir: string = ''; ShowWindow: Boolean = False);
procedure StopProcess;
published
property EnableKill : Boolean read FEnableKill write FEnableKill default False;
property IsRunning : Boolean read GetIsRunning;
property SplitChar : Char read FSplitChar write FSplitChar default #10;
property SplitMode : TSplitMode read FSplitMode write FSplitMode default sm0D0A;
property SplitReceive : Boolean read FSplitReceive write FSplitReceive default True;
property SplitSend : Boolean read FSplitSend write FSplitSend default True;
property StopProcessOnFree: Boolean read FStopProcessOnFree write FStopProcessOnFree default True;
property TerminateCommand : string read FTerminateCommand write FTerminateCommand;
property WaitTimeout : Integer read FWaitTimeout write FWaitTimeout default 1000;
property OnError : TReceiveEvent read FOnError write FOnError;
property OnProcessStatusChange: TProcessStatusChangeEvent read FOnProcessStatusChange write FOnProcessStatusChange;
property OnReceiveError : TReceiveEvent read FOnReceiveError write FOnReceiveError;
property OnReceiveOutput : TReceiveEvent read FOnReceiveOutput write FOnReceiveOutput;
end;
implementation
{ Win API wrappers }
procedure WinCheck(Result: Boolean);
begin
if not Result then
RaiseLastOSError;
end;
procedure InprocessDuplicateHandle(Source: THandle; var Destination: THandle);
var
CurrentProcess: THandle;
begin
CurrentProcess := GetCurrentProcess;
WinCheck(DuplicateHandle(CurrentProcess, Source, CurrentProcess, @Destination, 0, False, DUPLICATE_SAME_ACCESS));
end;
procedure CloseAndZeroHandle(var Handle: THandle);
var
SaveHandle: THandle;
begin
SaveHandle := Handle;
Handle := 0;
WinCheck(CloseHandle(SaveHandle));
end;
function ToPChar(const St: string): PChar;
begin
if (St = EmptyStr) then
Result := nil
else
Result := PChar(St);
end;
{ Thread functions }
procedure IOReadOutput(Handler: TConsoleIO);
begin
Handler.ReaderProc(Handler.OutputReadPipe, MIO_RECEIVE_OUTPUT);
end;
procedure IOReadError(Handler: TConsoleIO);
begin
Handler.ReaderProc(Handler.ErrorReadPipe, MIO_RECEIVE_ERROR);
end;
procedure WaitProcess(Handler: TConsoleIO);
begin
if WaitForSingleObject(Handler.ProcessHandle, INFINITE) = WAIT_OBJECT_0 then
SendMessage(Handler.FWindowHandle, MIO_PROCESS_TERMINATED, 0, 0);
end;
{ TConsoleIO }
constructor TConsoleIO.Create(AOwner: TComponent);
begin
inherited;
FTerminateCommand := 'quit';
FSplitChar := #10;
FSplitMode := sm0D0A;
FSplitReceive := True;
FSplitSend := True;
FStopProcessOnFree := True;
FWaitTimeout := 1000;
FWindowHandle := AllocateHWnd(WndProc);
end;
destructor TConsoleIO.Destroy;
begin
if StopProcessOnFree then
StopProcess;
CloseProcessHandle;
DeallocateHWnd(FWindowHandle);
FWindowHandle := 0;
inherited;
end;
procedure TConsoleIO.ClosePipes;
begin
CloseAndZeroHandle(InputReadPipe);
CloseAndZeroHandle(OutputWritePipe);
CloseAndZeroHandle(ErrorWritePipe);
CloseAndZeroHandle(InputWritePipe);
CloseAndZeroHandle(OutputReadPipe);
CloseAndZeroHandle(ErrorReadPipe);
end;
procedure TConsoleIO.ReceiveOutput(Buf: Pointer; Size: Integer);
var
Cmd : string;
TastyStrPos: Integer;
begin
if (Size <= 0) then
Exit;
SetString(Cmd, PAnsiChar(Buf), Size);
OutputBuffer := OutputBuffer + Cmd;
if not Assigned(FOnReceiveOutput) then
Exit;
if not SplitReceive or (SplitMode = smNone) then
begin
FOnReceiveOutput(Self, OutputBuffer);
OutputBuffer := '';
end
else if SplitMode = sm0D0A then
repeat
TastyStrPos := Pos(#13#10, OutputBuffer);
if TastyStrPos = 0 then
Break;
FOnReceiveOutput(Self, Copy(OutputBuffer, 1, TastyStrPos - 1));
OutputBuffer := Copy(OutputBuffer, TastyStrPos + 2, Length(OutputBuffer));
until False
else if SplitMode = smSplitchar then
repeat
TastyStrPos := Pos(SplitChar, OutputBuffer);
if TastyStrPos = 0 then
Break;
FOnReceiveOutput(Self, Copy(OutputBuffer, 1, TastyStrPos - 1));
OutputBuffer := Copy(OutputBuffer, TastyStrPos + 1, Length(OutputBuffer));
until False;
end;
procedure TConsoleIO.ReceiveError(Buf: Pointer; Size: Integer);
var
Cmd: string;
begin
if (Size <= 0) then
Exit;
if not Assigned(FOnReceiveOutput) then
Exit;
SetString(Cmd, PAnsiChar(Buf), Size);
FOnReceiveError(Self, Cmd);
end;
procedure TConsoleIO.RunProcess(const CmdLine: string; CurrentDir: string = ''; ShowWindow: Boolean = False);
var
buffer: array [0 .. MAX_PATH] of Char;
SA : TSecurityAttributes;
SI : TStartupInfo;
PI : TProcessInformation;
InputWriteTmp: THandle;
OutputReadTmp: THandle;
ErrorReadTmp : THandle;
ThreadId: Cardinal;
begin
SA.nLength := SizeOf(SA);
SA.lpSecurityDescriptor := nil;
SA.bInheritHandle := True;
WinCheck(CreatePipe(InputReadPipe, InputWriteTmp, @SA, 0));
WinCheck(CreatePipe(OutputReadTmp, OutputWritePipe, @SA, 0));
WinCheck(CreatePipe(ErrorReadTmp, ErrorWritePipe, @SA, 0));
InprocessDuplicateHandle(InputWriteTmp, InputWritePipe);
InprocessDuplicateHandle(OutputReadTmp, OutputReadPipe);
InprocessDuplicateHandle(ErrorReadTmp, ErrorReadPipe);
CloseAndZeroHandle(InputWriteTmp);
CloseAndZeroHandle(OutputReadTmp);
CloseAndZeroHandle(ErrorReadTmp);
FillChar(SI, SizeOf(SI), $00);
SI.cb := SizeOf(SI);
SI.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
SI.hStdInput := InputReadPipe;
SI.hStdOutput := OutputWritePipe;
SI.hStdError := ErrorWritePipe;
if (ShowWindow) then
SI.wShowWindow := SW_SHOW
else
SI.wShowWindow := SW_HIDE;
ZeroMemory(@buffer, SizeOf(buffer));
CopyMemory(@buffer[0], @CmdLine[1], Length(CmdLine) * StringElementSize(CmdLine));
WinCheck(CreateProcess(nil, buffer, @SA, nil, True, CREATE_NEW_CONSOLE, nil, ToPChar(CurrentDir), SI, PI));
CloseAndZeroHandle(PI.hThread);
ProcessHandle := PI.hProcess;
WinCheck(BeginThread(nil, 0, @IOReadOutput, Self, 0, ThreadId) <> 0);
WinCheck(BeginThread(nil, 0, @IOReadError, Self, 0, ThreadId) <> 0);
WinCheck(BeginThread(nil, 0, @WaitProcess, Self, 0, ThreadId) <> 0);
end;
procedure TConsoleIO.SendInput(Msg: string);
var
BytesWritten: Cardinal;
bfr : ansistring;
begin
Msg := Msg + SplitSendAvail;
bfr := ansistring(Msg);
WinCheck(WriteFile(InputWritePipe, bfr[1], Length(Msg), BytesWritten, nil));
end;
procedure TConsoleIO.WndProc(var Msg: TMessage);
var
Unhandled: Boolean;
begin
with Msg do
begin
Unhandled := False;
try
case Msg of
MIO_RECEIVE_OUTPUT:
ReceiveOutput(Pointer(wParam), LParam);
MIO_RECEIVE_ERROR:
ReceiveError(Pointer(wParam), LParam);
MIO_PROCESS_TERMINATED:
ProcessTerminated;
MIO_ERROR:
Error(string(Pointer(wParam)))
else
Unhandled := True;
end;
except
Application.HandleException(Self);
end;
if Unhandled then
Result := DefWindowProc(FWindowHandle, Msg, wParam, LParam);
end;
end;
procedure TConsoleIO.Error(const Msg: string);
begin
if Assigned(FOnError) then
FOnError(Self, Msg);
end;
procedure TConsoleIO.ReaderProc(Handle: THandle; MessageCode: Integer);
var
Buf : array [0 .. 1024] of Char;
BytesRead: Cardinal;
Err : string;
begin
repeat
if not ReadFile(Handle, Buf, SizeOf(Buf), BytesRead, nil) then
try
if not IsRunning then
Exit;
RaiseLastOSError;
except
on E: Exception do
begin
Err := E.Message;
SendMessage(FWindowHandle, MIO_ERROR, Integer(PChar(Err)), Length(Err) + 1);
end;
end;
if BytesRead > 0 then
SendMessage(FWindowHandle, MessageCode, Integer(@Buf), BytesRead);
until False;
end;
procedure TConsoleIO.ProcessTerminated;
begin
if Assigned(FOnReceiveOutput) then
FOnReceiveOutput(Self, OutputBuffer);
OutputBuffer := '';
CloseProcessHandle;
ClosePipes;
end;
function TConsoleIO.GetIsRunning: Boolean;
begin
Result := ProcessHandle <> 0;
end;
procedure TConsoleIO.SetProcessHandle(const Value: THandle);
begin
if FProcessHandle = Value then
Exit;
Assert((ProcessHandle = 0) or (Value = 0));
FProcessHandle := Value;
if Assigned(FOnProcessStatusChange) then
FOnProcessStatusChange(Self, IsRunning);
end;
procedure TConsoleIO.CloseProcessHandle;
begin
if ProcessHandle = 0 then
Exit;
WinCheck(CloseHandle(ProcessHandle));
ProcessHandle := 0;
end;
procedure TConsoleIO.StopProcess;
begin
if not IsRunning then
Exit;
if TerminateCommand <> '' then
SendInput(TerminateCommand);
if not EnableKill then
Exit;
if TerminateCommand <> '' then
if WaitForSingleObject(ProcessHandle, WaitTimeout) = WAIT_OBJECT_0 then
Exit;
TerminateProcess(ProcessHandle, 4);
end;
function TConsoleIO.SplitSendAvail: string;
begin
Result := '';
if not SplitSend then
Exit;
if SplitMode = smNone then
Exit;
if SplitMode = sm0D0A
then
Result := #$0D#$0A
else
Result := SplitChar
end;
end.