-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathOSCmdLineExecutor.pas
More file actions
245 lines (207 loc) · 7.61 KB
/
OSCmdLineExecutor.pas
File metadata and controls
245 lines (207 loc) · 7.61 KB
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
unit OSCmdLineExecutor;
interface
uses windows,classes;
type
TOSCommandLineExecutor = class;
TOSCmdLineOutputProc = procedure(sender:TOSCommandLineExecutor; const txt:string) of object;
/// <summary>
/// Runs a command line command and captures its stdout/stderr
/// </summary>
TOSCommandLineExecutor = class(TComponent)
public
type TLogProc = reference to procedure(txt:string);
private
type
TPipeHandles = record
hRead, hWrite: THandle;
end;
var FCmdLine: string;
FWorkDir: string;
PipeStdOut,PipeStdErr,pipeStdIn: TPipeHandles;
StartupInfo: TStartupInfo;
FOnStdOut: TOSCmdLineOutputProc;
FOnStdErr: TOSCmdLineOutputProc;
FOnIdle : TNotifyEvent;
FAnonProcStdOut,FAnonProcStdErr:TLogProc;
procedure ClearPipe(var pipe:TPipeHandles);
procedure InitPipe(var Pipe:TPipeHandles;SecAttr : TSecurityAttributes);
procedure ClosePipe(var Pipe: TPipeHandles);
function ReadPipe(var Pipe: TPipeHandles):AnsiString;
procedure InitStartupInfo;
procedure LogOutput(var msg:AnsiString;IsError:boolean);
public
function Execute: DWORD; overload;
function Execute(StdErrProc,StdOutProc:TLogProc):DWORD; overload;
function Execute(OutProc:TLogProc):DWORD; overload;
published
property OnStdOut : TOSCmdLineOutputProc read FOnStdOut write FOnStdOut;
property OnStdErr : TOSCmdLineOutputProc read FOnStdErr write FOnStdErr;
property OnIdle : TNotifyEvent read FOnIdle write FOnIdle;
property WorkDir:string read FWorkDir write FWorkDir;
property CmdLine:string read FCmdLine write FCmdLine;
end;
procedure Register;
implementation
uses sysutils;
const BufSize = $4000;
procedure TOSCommandLineExecutor.ClearPipe(var pipe:TPipeHandles);
begin
pipe.hRead := 0;
pipe.hWrite := 0;
end;
procedure TOSCommandLineExecutor.InitPipe(var Pipe:TPipeHandles;SecAttr : TSecurityAttributes);
begin
if not CreatePipe (pipe.hRead, pipe.hWrite, @SecAttr, BufSize) then
Raise Exception.Create('Can''t Create pipe!');
end;
procedure TOSCommandLineExecutor.ClosePipe(var Pipe: TPipeHandles);
begin
if Pipe.hRead <> 0 then CloseHandle (Pipe.hRead);
if Pipe.hWrite <> 0 then CloseHandle (Pipe.hWrite);
ClearPipe(Pipe);
end;
function TOSCommandLineExecutor.ReadPipe(var Pipe: TPipeHandles):AnsiString;
var ReadBuf: array[0..BufSize] of AnsiChar;
BytesRead: Dword;
begin
result := '';
if not PeekNamedPipe(Pipe.hRead, nil, 0, nil, @BytesRead, nil) or (BytesRead <= 0) then exit;
ReadFile( Pipe.hRead, ReadBuf, BufSize, BytesRead, nil);
if BytesRead <= 0 then exit;
ReadBuf[BytesRead] := #0;
result := pAnsichar(@readbuf[0]);
end;
procedure TOSCommandLineExecutor.InitStartupInfo;
begin
FillChar(StartupInfo,SizeOf(TStartupInfo), 0);
StartupInfo.cb := SizeOf(TStartupInfo);
StartupInfo.dwFlags := STARTF_USESTDHANDLES;
StartupInfo.hStdOutput := PipeStdOut.hWrite;
StartupInfo.hStdError := PipeStdErr.hWrite;
StartupInfo.hStdInput := PipeStdIn.hRead;
StartupInfo.wShowWindow := SW_HIDE;
end;
procedure TOSCommandLineExecutor.LogOutput(var msg:AnsiString;IsError:boolean);
var newLinePosition:integer;
newLineSeparatorLength:integer;
procedure LocateNewLineSeparator;
var p0,p1,p2:integer;
begin
p0 := pos(AnsiString(#13#10),msg);
p1 := pos(AnsiChar(#13),msg);
p2 := pos(AnsiChar(#10),msg);
if (p1 >= p0) and (p2 >= p0) then begin
newLineSeparatorLength := 2;
newLinePosition := p0;
end else begin
newLineSeparatorLength := 1;
if p1 <=0 then newLinePosition := p2
else if p2 <=0 then newLinePosition := p1
else if p1 < p2 then newLinePosition := p1
else newLinePosition := p2;
end;
end;
var s:String;
begin
LocateNewLineSeparator;
while newLinePosition>=1 do begin
s := String(copy( msg,1,newLinePosition-1));
if IsError then begin
if Assigned(FOnStdErr) then
FOnStdErr(self,s);
if Assigned(FAnonProcStdErr) then
FAnonProcStdErr(s);
end;
if not IsError then begin
if Assigned(FOnStdOut) then
FOnStdOut(self,s);
if Assigned(FAnonProcStdOut) then
FAnonProcStdOut(s);
end;
msg := copy(msg,newLinePosition+newLineSeparatorLength,length(msg));
LocateNewLineSeparator;
end;
end;
function TOSCommandLineExecutor.Execute(StdErrProc,StdOutProc:TLogProc):DWORD; // returns the exit code of the process
var SecAttr : TSecurityAttributes;
function DoExecuteProcess:DWORD;
var strError,strOut:AnsiString;
ProcessInformation:TProcessInformation;
begin
if not CreateProcess(
nil, PChar(FCmdLine), nil, nil, True,
NORMAL_PRIORITY_CLASS or CREATE_NO_WINDOW,
nil, PChar(FWorkDir),
StartupInfo,
ProcessInformation )
then Raise Exception.create('Cannot create process for '+FCmdLine+ #13#10+SysErrorMessage(GetLastError));
result := STILL_ACTIVE;
strError := '';
strOut := '';
try
repeat
WaitForSingleObject(ProcessInformation.hProcess, 100);
GetExitCodeProcess(ProcessInformation.hProcess,result);
if assigned(FOnIdle) then
FOnIdle(self);
strError := strError + ReadPipe(PipeStdErr);
StrOut := StrOut + ReadPipe(PipeStdOut);
LogOutput(strError,true);
LogOutput(strOut,False);
until result <> STILL_ACTIVE;
if not GetExitCodeProcess(ProcessInformation.hProcess,result) then
Raise Exception.Create('Cannot get exit code!');
result := result;
finally
if result = STILL_ACTIVE then TerminateProcess(ProcessInformation.hProcess, 1);
CloseHandle(ProcessInformation.hProcess);
CloseHandle(ProcessInformation.hThread);
ProcessInformation.hProcess := 0;
end;
end;
begin
FAnonProcStdOut := StdOutProc;
FAnonProcStdErr := StdErrProc;
try
SecAttr.nLength := SizeOf(SecAttr);
SecAttr.lpSecurityDescriptor := nil;
SecAttr.bInheritHandle := TRUE;
ClearPipe(pipeStdIn);
ClearPipe(PipeStdErr);
ClearPipe(PipeStdOut);
try
InitPipe(PipeStdIn,SecAttr);
InitPipe(PipeStdErr,SecAttr);
InitPipe(PipeStdOut,SecAttr);
// child process must not inherit stdin/stdout/stderr stream handles from the current process
if not SetHandleInformation(pipeStdIn.hWrite, HANDLE_FLAG_INHERIT, 0) then
Raise Exception.Create('error calling SetHandleInformation for pipeStdIn.hWrite');
if not SetHandleInformation(pipeStdOut.hRead, HANDLE_FLAG_INHERIT, 0) then
Raise Exception.Create('error calling SetHandleInformation for pipeStdOut.hRead');
if not SetHandleInformation(PipeStdErr.hRead, HANDLE_FLAG_INHERIT, 0) then
Raise Exception.Create('error calling SetHandleInformation for PipeStdErr.hRead');
InitStartupInfo;
result := DoExecuteProcess;
finally
ClosePipe(PipeStdOut);
ClosePipe(PipeStdErr);
ClosePipe(PipeStdIn);
end;
finally
FAnonProcStdOut := nil;
FAnonProcStdErr := nil;
end;
end;
function TOSCommandLineExecutor.Execute: DWORD;
begin
result := Execute(nil,nil);
end;
function TOSCommandLineExecutor.Execute(OutProc:TLogProc):DWORD;
begin
result := Execute(OutProc,OutProc);
end;
procedure Register;
begin
// RegisterComponents('CSTools',[TOSCommandLineExecutor]);
end;
end.