Menu

[r3166]: / trunk / Src / UConsoleApp.pas  Maximize  Restore  History

Download this file

406 lines (370 with data), 13.7 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
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
398
399
400
401
402
403
404
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2006-2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* A class that encapsulates and executes a command line application and
* optionally redirects the application's standard input, output and error.
}
unit UConsoleApp;
interface
uses
// Delphi
Classes, Windows;
const
// Constants for working in milliseconds
cOneSecInMS = 1000; // one second in milliseconds
cOneMinInMS = 60 * cOneSecInMS; // one minute in milliseconds
// Default values for some TConsoleApp properties
cDefTimeSlice = 50; // default time slice allocated to app
cDefMaxExecTime = cOneMinInMS; // maximum execution time of app
type
{
TConsoleApp:
Class that encapsulates and executes a command line application and
optionally redirects the application's standard input, output and error. The
application is excuted in time slices and the class triggers an event
between time slices.
}
TConsoleApp = class(TObject)
strict private
fOnWork: TNotifyEvent;
{References OnWork event handler}
fStdIn: THandle;
{Handle of Console app's redirected standard input or 0 if not redirected}
fStdOut: THandle;
{Handle of Console app's redirected standard output or 0 if not
redirected}
fStdErr: THandle;
{Handle of Console app's redirected standard error or 0 if not redirected}
fExitCode: LongWord;
{Exit code returned from console app}
fMaxExecTime: LongWord;
{Maximum execution time of console app}
fProcessHandle: THandle;
{Process handle of console app. 0 when no app is executing}
fErrorMessage: string;
{Description of any error that occured while trying to execute
application}
fErrorCode: LongWord;
{Code of any error that occured while trying to execute application}
fVisible: Boolean;
{Whether application is to be visible or hidden}
fTimeSlice: Integer;
{Time to let application run before generating each OnWork event}
function MonitorProcess: Boolean;
{Monitors a running process, triggering event at end of each timeslice.
@return True on successful completion or false if application times out.
}
function SetExitCode: Boolean;
{Sets ExitCode property to value returned from application.
@return True if exit code retrieved OK and false if we fail to retrieve
it.
}
procedure TriggerWorkEvent;
{Triggers OnWork event if assigned.
}
procedure SetMaxExecTime(const Value: LongWord);
{Sets MaxExecTime property.
@param Value Required time in miliseconds. If 0 then property's default
value is used.
}
procedure SetTimeSlice(const Value: Integer);
{Sets TimeSlice property.
@param Value Required time in miliseconds. If 0 then property's default
value is used.
}
strict protected
function StartProcess(const CmdLine, CurrentDir: string;
out ProcessInfo: TProcessInformation): Boolean;
{Starts a process and gets information about it from OS.
@param CmdLine Command line to be executed.
@param CurrentDir Application's current directory.
@param ProcessInfo Passes OS process info back to caller.
@return True if process created OK and false if process couldn't be
started.
}
procedure DoWork; virtual;
{Overridable method called between program timeslices and after
completion. Simply triggers OnWorkEvent.
}
procedure RecordAppError(const Code: LongWord; const Msg: string);
{Set error code and message to class-defined error. Error code has bit 29
set.
@param ErrorCode Required error code. Bit 29 will be forced set.
@param Msg Required error message.
}
procedure RecordWin32Error;
{Set error code message to the last-reported Windows error.
}
procedure ResetError;
{Resets error code and message to "no error" values of 0 and empty string.
}
public
constructor Create;
{Class constructor. Instantiates object.
}
function Execute(const CmdLine, CurrentDir: string): Boolean;
{Executes given command line and returns whether application succeeds
fails.
@param CmdLine Command line to execute.
@param CurrentDir Current directory of application.
@return True if command line application succeeds or false if it fails.
}
property StdIn: THandle read fStdIn write fStdIn default 0;
{Handle of console app's redirected standard input. Leave as 0 if standard
input is not to be redirected. Ensure handle is inheritable}
property StdOut: THandle read fStdOut write fStdOut default 0;
{Handle of console app's redirected standard output. Leave as 0 if
standard output not to be redirected. Ensure handle is inheritable}
property StdErr: THandle read fStdErr write fStdErr default 0;
{Handle of console app's redirected standard error. Leave as 0 if standard
error is not to be redirected. Ensure handle is inheritable}
property Visible: Boolean read fVisible write fVisible default False;
{Determines whether console app is to be displayed of not}
property MaxExecTime: LongWord read fMaxExecTime write SetMaxExecTime
default cDefMaxExecTime;
{Maximum execution time of console app in miliseconds}
property TimeSlice: Integer read fTimeSlice write SetTimeSlice
default cDefTimeSlice;
{Timeslice allocated to console app in miliseconds. The app is paused at
end of each time slice and Onwork event is triggered}
property ProcessHandle: THandle read fProcessHandle;
{Handle of executing console app. Zero when no application executing}
property ExitCode: LongWord read fExitCode;
{Exit code set by console app. Not valid if ErrorCode is non zero. Refer
to console application documentation for meaning of these codes}
property ErrorCode: DWORD read fErrorCode;
{Zero if application executes successfully and non-zero if there was an
error executing the application (e.g. if it timed out). Error codes either
correspond to Windows error or are set by the class. Class generated error
codes have bit 29 set}
property ErrorMessage: string read fErrorMessage;
{Error message corresponding to ErrorCode. '' if ErrorCode = 0}
property OnWork: TNotifyEvent read fOnWork write fOnWork;
{Event triggered each time console application signals the class.
Frequency of these events depends on TimeSlice}
end;
implementation
uses
// Delphi
SysUtils, Forms;
const
// Mask that is ORd with application error codes: according to Windows API
// docs, error codes with bit 29 set are reserved for application use.
cAppErrorMask = 1 shl 29;
resourcestring
// Error message
sErrTimeout = 'Application timed out';
{ TConsoleApp }
constructor TConsoleApp.Create;
{Class constructor. Instantiates object.
}
begin
inherited Create;
// Set default property values
fMaxExecTime := cDefMaxExecTime;
fTimeSlice := cDefTimeSlice;
fVisible := False;
fStdIn := 0;
fStdOut := 0;
fStdErr := 0;
end;
procedure TConsoleApp.DoWork;
{Overridable method called between program timeslices and after completion.
Simply triggers OnWorkEvent.
}
begin
TriggerWorkEvent;
end;
function TConsoleApp.Execute(const CmdLine, CurrentDir: string): Boolean;
{Executes given command line and returns whether application succeeds
fails.
@param CmdLine Command line to execute.
@param CurrentDir Current directory of application.
@return True if command line application succeeds or false if it fails.
}
var
ProcessInfo: TProcessInformation; // information about process
begin
fExitCode := 0;
ResetError;
Result := StartProcess(CmdLine, CurrentDir, ProcessInfo);
if Result then
begin
// Process started: monitor its progress
try
fProcessHandle := ProcessInfo.hProcess;
Result := MonitorProcess and SetExitCode;
finally
// Process ended: tidy up
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
fProcessHandle := 0;
end;
end
else
begin
// Couldn't start process: error
RecordWin32Error;
fProcessHandle := 0;
end;
end;
function TConsoleApp.MonitorProcess: Boolean;
{Monitors a running process, triggering event at end of each timeslice.
@return True on successful completion or false if application times out.
}
var
TimeToLive: Integer; // Milliseconds app has left before timing out
AppState: DWORD; // State of app after last wait
begin
Result := True;
TimeToLive := fMaxExecTime;
repeat
// Pause and wait for app - length determined by TimeSlice property
AppState := WaitForSingleObject(fProcessHandle, fTimeSlice);
Application.ProcessMessages;
Dec(TimeToLive, fTimeSlice);
if AppState = WAIT_FAILED then
begin
RecordWin32Error;
Result := False;
end
else
// All OK: do inter-timeslice processing
DoWork;
until (AppState <> WAIT_TIMEOUT) or (TimeToLive <= 0);
// App halted or timed out: check which
if AppState = WAIT_TIMEOUT then
begin
RecordAppError(1, sErrTimeout);
Result := False;
end;
end;
procedure TConsoleApp.RecordAppError(const Code: LongWord;
const Msg: string);
{Set error code and message to class-defined error. Error code has bit 29
set.
@param ErrorCode Required error code. Bit 29 will be forced set.
@param Msg Required error message.
}
begin
fErrorMessage := Msg;
fErrorCode := Code or cAppErrorMask;
end;
procedure TConsoleApp.RecordWin32Error;
{Set error code message to the last-reported Windows error.
}
begin
fErrorCode := GetLastError;
fErrorMessage := SysErrorMessage(fErrorCode);
end;
procedure TConsoleApp.ResetError;
{Resets error code and message to "no error" values of 0 and empty string.
}
begin
fErrorCode := 0;
fErrorMessage := '';
end;
function TConsoleApp.SetExitCode: Boolean;
{Sets ExitCode property to value returned from application.
@return True if exit code retrieved OK and false if we fail to retrieve it.
}
begin
Result := GetExitCodeProcess(fProcessHandle, fExitCode);
if not Result then
RecordWin32Error;
end;
procedure TConsoleApp.SetMaxExecTime(const Value: LongWord);
{Sets MaxExecTime property.
@param Value Required time in miliseconds. If 0 then property's default
value is used.
}
begin
if Value = 0 then
fMaxExecTime := cDefMaxExecTime
else
fMaxExecTime := Value;
end;
procedure TConsoleApp.SetTimeSlice(const Value: Integer);
{Sets TimeSlice property.
@param Value Required time in miliseconds. If 0 then property's default
value is used.
}
begin
if Value > 0 then
fTimeSlice := Value
else
fTimeSlice := cDefTimeSlice;
end;
function TConsoleApp.StartProcess(const CmdLine, CurrentDir: string;
out ProcessInfo: TProcessInformation): Boolean;
{Starts a process and gets information about it from OS.
@param CmdLine Command line to be executed.
@param CurrentDir Application's current directory.
@param ProcessInfo Passes OS process info back to caller.
@return True if process created OK and false if process couldn't be started.
}
const
// Maps Visible property to required wondows flags
cShowFlags: array[Boolean] of Integer = (SW_HIDE, SW_SHOW);
var
StartInfo: TStartupInfo; // information about process from OS
SafeCmdLine: string; // command line: safe for CreateProcessW
CreateFlags: DWORD; // process creation flags
begin
// Set up startup information structure
FillChar(StartInfo, Sizeof(StartInfo),#0);
with StartInfo do
begin
cb := SizeOf(StartInfo);
dwFlags := STARTF_USESHOWWINDOW;
if (fStdIn <> 0) or (fStdOut <> 0) or (fStdErr <> 0) then
dwFlags := dwFlags or STARTF_USESTDHANDLES; // we are redirecting
hStdInput := fStdIn; // std handles (non-zero => redirect)
hStdOutput := fStdOut;
hStdError := fStdErr;
wShowWindow := cShowFlags[fVisible]; // show or hide window
end;
// Make CmdLine parameter safe for passing to CreateProcess (Delphi 2009
// and later). Need to ensure memory space is writeable because of issue with
// CreateProcessW. Problem does not exist with CreateProcessA.
// Without the following code this problem would arise if this method was
// called with a constant or string with -1 reference count as the CmdLine
// parameter.
// See http://msdn.microsoft.com/en-us/library/ms682425.aspx for an
// explanation of the problem: look under the lpCommandLine parameter section.
// Remy Lebeau suggested the workaround used below in his post to
// https://forums.codegear.com/thread.jspa?threadID=12826
SafeCmdLine := CmdLine;
UniqueString(SafeCmdLine);
// Set up creation flags: special flag used to determine type of environment
// block passed to app: Unicode or ANSI.
CreateFlags := CREATE_UNICODE_ENVIRONMENT;
// Try to create the process
Result := CreateProcess(
nil,
PChar(SafeCmdLine),
nil,
nil,
True,
CreateFlags,
nil,
PChar(CurrentDir),
StartInfo,
ProcessInfo
);
end;
procedure TConsoleApp.TriggerWorkEvent;
{Triggers OnWork event if assigned.
}
begin
if Assigned(fOnWork) then
fOnWork(Self);
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.