1 | unit UFormConsole;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
|
---|
9 | ExtCtrls, Process;
|
---|
10 |
|
---|
11 | type
|
---|
12 |
|
---|
13 | { TFormConsole }
|
---|
14 |
|
---|
15 | TFormConsole = class(TForm)
|
---|
16 | ButtonAbort: TButton;
|
---|
17 | EditCommand: TEdit;
|
---|
18 | Memo1: TMemo;
|
---|
19 | Timer1: TTimer;
|
---|
20 | procedure ButtonAbortClick(Sender: TObject);
|
---|
21 | procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
|
---|
22 | procedure FormCreate(Sender: TObject);
|
---|
23 | procedure FormDestroy(Sender: TObject);
|
---|
24 | procedure FormShow(Sender: TObject);
|
---|
25 | procedure Timer1Timer(Sender: TObject);
|
---|
26 | private
|
---|
27 | Process: TProcess;
|
---|
28 | { private declarations }
|
---|
29 | public
|
---|
30 | Executable: string;
|
---|
31 | Parameters: TStringList;
|
---|
32 | WorkingDir: string;
|
---|
33 | EnvironmentVariables: TStringList;
|
---|
34 | Aborted: Boolean;
|
---|
35 | Log: TStringList;
|
---|
36 | procedure Perform;
|
---|
37 | end;
|
---|
38 |
|
---|
39 | var
|
---|
40 | FormConsole: TFormConsole;
|
---|
41 |
|
---|
42 | implementation
|
---|
43 |
|
---|
44 | {$R *.lfm}
|
---|
45 |
|
---|
46 | { TFormConsole }
|
---|
47 |
|
---|
48 | procedure TFormConsole.FormCreate(Sender: TObject);
|
---|
49 | begin
|
---|
50 | Parameters := TStringList.Create;
|
---|
51 | Log := TStringList.Create;
|
---|
52 | EnvironmentVariables := TStringList.Create;
|
---|
53 | end;
|
---|
54 |
|
---|
55 | procedure TFormConsole.FormDestroy(Sender: TObject);
|
---|
56 | begin
|
---|
57 | EnvironmentVariables.Free;
|
---|
58 | Log.Free;
|
---|
59 | Parameters.Free;
|
---|
60 | end;
|
---|
61 |
|
---|
62 | procedure TFormConsole.FormShow(Sender: TObject);
|
---|
63 | begin
|
---|
64 | ButtonAbort.Enabled := False;
|
---|
65 | Timer1.Enabled := True;
|
---|
66 | end;
|
---|
67 |
|
---|
68 | procedure TFormConsole.Timer1Timer(Sender: TObject);
|
---|
69 | begin
|
---|
70 | Timer1.Enabled := False;
|
---|
71 | Perform;
|
---|
72 | end;
|
---|
73 |
|
---|
74 | procedure TFormConsole.ButtonAbortClick(Sender: TObject);
|
---|
75 | begin
|
---|
76 | if Assigned(Process) then begin
|
---|
77 | Aborted := True;
|
---|
78 | Process.Terminate(0);
|
---|
79 | end;
|
---|
80 | end;
|
---|
81 |
|
---|
82 | procedure TFormConsole.FormClose(Sender: TObject; var CloseAction: TCloseAction
|
---|
83 | );
|
---|
84 | begin
|
---|
85 | ButtonAbort.Click;
|
---|
86 | end;
|
---|
87 |
|
---|
88 | procedure TFormConsole.Perform;
|
---|
89 | var
|
---|
90 | Buffer: string;
|
---|
91 | Count: Integer;
|
---|
92 | Text: string;
|
---|
93 | Line: string;
|
---|
94 | CommandLine: string;
|
---|
95 | I: Integer;
|
---|
96 | begin
|
---|
97 | Text := '';
|
---|
98 | try
|
---|
99 | Process := TProcess.Create(nil);
|
---|
100 | Aborted := False;
|
---|
101 | ButtonAbort.Enabled := True;
|
---|
102 | Log.Clear;
|
---|
103 | Memo1.Clear;
|
---|
104 | if WorkingDir <> '' then
|
---|
105 | Process.CurrentDirectory := WorkingDir;
|
---|
106 | Process.Environment.Assign(EnvironmentVariables);
|
---|
107 | Process.Parameters.Assign(Parameters);
|
---|
108 | Process.Executable := Executable;
|
---|
109 | CommandLine := Executable + ' ' + StringReplace(Parameters.Text, LineEnding, ' ', [rfReplaceAll]);
|
---|
110 | if CommandLine[Length(CommandLine)] = LineEnding then
|
---|
111 | SetLength(CommandLine, Length(CommandLine) - 1);
|
---|
112 | EditCommand.Text := CommandLine;
|
---|
113 | Process.Options := [poUsePipes, poNoConsole];
|
---|
114 | Process.Execute;
|
---|
115 | Application.ProcessMessages;
|
---|
116 | while Process.Running or (Process.Output.NumBytesAvailable > 0) or
|
---|
117 | (Process.Stderr.NumBytesAvailable > 0) do
|
---|
118 | begin
|
---|
119 | if Process.Output.NumBytesAvailable > 0 then begin
|
---|
120 | SetLength(Buffer, 1000);
|
---|
121 | Count := Process.Output.Read(Buffer[1], Length(Buffer));
|
---|
122 | SetLength(Buffer, Count);
|
---|
123 | Text := Text + Buffer;
|
---|
124 | while Pos(LineEnding, Text) > 0 do begin
|
---|
125 | Line := Copy(Text, 1, Pos(LineEnding, Text) - 1);
|
---|
126 | Delete(Text, 1, Length(Line) + Length(LineEnding));
|
---|
127 | if Visible then Memo1.Lines.Add(Line);
|
---|
128 | Log.Add(Line);
|
---|
129 | end;
|
---|
130 | end;
|
---|
131 |
|
---|
132 | if Process.Stderr.NumBytesAvailable > 0 then begin
|
---|
133 | SetLength(Buffer, 1000);
|
---|
134 | Count := Process.Stderr.Read(Buffer[1], Length(Buffer));
|
---|
135 | SetLength(Buffer, Count);
|
---|
136 | Text := Text + Buffer;
|
---|
137 | while Pos(LineEnding, Text) > 0 do begin
|
---|
138 | Line := Copy(Text, 1, Pos(LineEnding, Text) - 1);
|
---|
139 | Delete(Text, 1, Length(Line) + Length(LineEnding));
|
---|
140 | if Visible then Memo1.Lines.Add(Line);
|
---|
141 | Log.Add(Line);
|
---|
142 | end;
|
---|
143 | end;
|
---|
144 | Sleep(10);
|
---|
145 | Application.ProcessMessages;
|
---|
146 | end;
|
---|
147 | finally
|
---|
148 | if Visible then Memo1.Lines.Add(Text);
|
---|
149 | Log.Add(Text);
|
---|
150 | FreeAndNil(Process);
|
---|
151 | end;
|
---|
152 | ButtonAbort.Enabled := False;
|
---|
153 | //ModalResult := mrOK;
|
---|
154 | //Close;
|
---|
155 | end;
|
---|
156 |
|
---|
157 | end.
|
---|
158 |
|
---|