-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunexternal.pas
259 lines (204 loc) · 7.3 KB
/
runexternal.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
unit RunExternal;
{$mode ObjFPC}{$H+}
interface
uses
{$ifdef unix}cthreads,{$endif} Classes, SysUtils,Process,Dialogs,debugUtils;
{TO DO:
* procedure Run(); change to func():boolean ,with check if exe file exist.
}
{ Run external application ( in new TProcess ) and store output in TStream ( copping is done in new TThread ).
You can get output anytime you want.
// Example for writing output in real-time.
var RE : TRunExternal;
var strBuff : string;
begin
RE := TRunExternal.Create(PATH,ARGS);
RE.Run(); // now process runs asynchronously in another thread
// to get process output:
while ( not RE.IsStreamFinished() ) do begin
if RE.ReadOutputChunk(1000,strBuff ) then writeln(strBuff);
sleep(500);
end;
end;
// Or wait till process is finished and it's Output copied to Stream, and then grab all output at once
var RE : TRunExternal;
begin
RE := TRunExternal.Create(PATH,ARGS);
RE.Run(); // now process runs asynchronously in another thread
// to get process output:
while ( not RE._PipeFinished ) do begin
sleep(500);
end;
writeln(ReadOutputAll());
end;
}
type TStreamThread = class;
type TRunExternal = class
public
constructor Create(path : string; args :string);
destructor Destroy(); override;
procedure Run();
function ReadOutputAll(var OutBuffer): LongInt;
function ReadOutputAll(): string; overload;
function ReadOutputChunk(var OutBuffer; Count: LongInt): LongInt;
function ReadOutputChunk(Count: LongInt; var ResultString : string): boolean; overload;
function IsStreamFinished() : Boolean; // Stream is finished when you've read the last chunk of data using ReadOutputChunk(). WARNING: Calling ReadOutputAll() doesn't make it finished.
function IsProcessFinished() : Boolean;
public
StreamPosTracker : LongInt; // This tracks position in Stream at which last ReadOutputChunk() ended. In this class it's used for reading from Stream to Main application
// it's different from TStream.position, which is changed by TStream.Write/Read calls and in this class used internally for writing from Process Output to Stream
Updated : Boolean; // This flag informs if a new input was loaded into the stream since the last read() function.
_PipeFinished : Boolean; // It's TRUE when all Process Output was loaded into Stream ; and Process is not running
//private
_Proc : TProcess;
_CriticalSection: TRTLCriticalSection;
_Stream : TMemoryStream;
_Thread : TStreamThread;
end;
type TStreamThread = class(TThread)
procedure Execute(); override;
Constructor Create(CreateSuspended : boolean; var RunExternal : TRunExternal);
private
RE : TRunExternal;
end;
implementation
Constructor TRunExternal.Create(path : string; args :string);
begin
_Stream := TMemoryStream.Create; // Stream for holding process output
_Proc := TProcess.Create(nil);
_Thread := TStreamThread.Create(TRUE , self );
InitCriticalSection(_CriticalSection);
_Proc.Executable := path;
_Proc.Parameters.Add(args);
_Proc.Options := [poUsePipes , poStderrToOutPut];
_Proc.ShowWindow:=swoHide;
StreamPosTracker := 0;
end;
destructor TRunExternal.Destroy();
begin
_Stream.Clear();
_Stream.Free();
_Proc.Free();
//_Thread.Free(); // ?? ACCESS VIOLATION
DoneCriticalsection(_CriticalSection);
end;
procedure TRunExternal.Run() ;
begin
_Thread.Start;
end;
function TRunExternal.IsStreamFinished() : Boolean;
begin
Result := _PipeFinished and (StreamPosTracker >= _Stream.Position ) ;
end;
function TRunExternal.ReadOutputChunk(var OutBuffer; Count: LongInt): LongInt;
var tempPosContainer : Int64;
begin
Result := 0;
if (StreamPosTracker >= _Stream.Position) then
begin
Updated := False;
end else begin
EnterCriticalSection(_CriticalSection);
Try
tempPosContainer := _Stream.Position;
_Stream.Seek(StreamPosTracker,soBeginning);
Result := _Stream.Read(OutBuffer,Count);
_Stream.Seek(tempPosContainer,soBeginning);
Finally
LeaveCriticalSection(_CriticalSection);
StreamPosTracker := StreamPosTracker + Result;
end;
end;
end;
function TRunExternal.ReadOutputChunk(Count: LongInt; var ResultString : string): boolean; overload;
var size:longInt;
var buf : PChar;
begin
Result := FALSE;
Updated := False;
buf := stralloc(count);
size:= ReadOutputChunk(buf^,Count);
if size>0 then
begin
(buf+size)^ := #0;
ResultString := '' + buf;
Result :=TRUE;
end;
StrDispose(buf);
end;
function TRunExternal.ReadOutputAll(var OutBuffer): LongInt;
var tempPosContainer : Int64;
begin
Result := 0;
EnterCriticalSection(_CriticalSection);
Try
tempPosContainer := _Stream.Position;
_Stream.Seek(0,soBeginning);
Result := _Stream.Read(OutBuffer,_Stream.Size);
_Stream.Seek(tempPosContainer,soBeginning);
Finally
LeaveCriticalSection(_CriticalSection);
end;
Updated := False;
end;
function TRunExternal.ReadOutputAll(): string; overload;
var size:longInt;
var buf : PChar;
begin
buf := stralloc(_Stream.Size);
size := ReadOutputAll(buf^);
Result := '' + buf;
StrDispose(buf);
end;
function TRunExternal.IsProcessFinished() : Boolean;
begin
Result := not _Proc.Running;
end;
// ---------------- TStreamThread -------------------
Constructor TStreamThread.Create(CreateSuspended : boolean; var RunExternal : TRunExternal);
begin
inherited Create(CreateSuspended);
FreeOnTerminate := True;
RE := RunExternal;
end;
procedure TStreamThread.Execute();
const
BUF_SIZE = 2048;
var
BytesRead : longint;
Buffer : array[1..BUF_SIZE] of byte;
begin
try
RE._Proc.Execute();
except
on E: Exception do
begin
ShowMessage( 'Error: '+ E.ClassName + #13#10 + E.Message + #13#10 + 'Failed to execute: '+RE._Proc.Executable );
RE.Free;
self.free;
exit;
end;
end;
while (not Terminated)and(RE._Proc.Running) do begin
if RE._Proc.Output.NumBytesAvailable > 0 then begin
BytesRead := RE._Proc.Output.Read(Buffer, BUF_SIZE);
RE._Stream.Write(Buffer, BytesRead);
RE.Updated:=True;
end;
sleep(100); // When process is not sending data we are relaxing
// Now the external process is done. But make sure to check if anything has left in the pipe
sleep(500);
if RE._Proc.Output.NumBytesAvailable > 0 then begin
repeat
BytesRead := RE._Proc.Output.Read(Buffer, BUF_SIZE);
RE._Stream.Write(Buffer, BytesRead);
sleep(50);
/// Showmessage(ByteArrayToHexString(Buffer,' '));
until RE._Proc.Output.NumBytesAvailable = 0;
RE.Updated:=True;
end;
end;
RE._PipeFinished := True;
end;
begin
end.