2009年5月30日

Delphi JEDI CreateProcess 執行 Console 程式


需先安裝 JEDI VCL for Delphi
在 Form 上
放入 Jv Non-Visual -> TJvCreateProcess
放入 TEdit, TButton 來執行 CommandLine
放入 TMemo 來顯示 Console 訊息
放入 TActionManager 來建立一個 Action
在 ActionManager1 雙擊滑鼠左鍵開啟 Editing ActionManager
新增一個 New Action [Inc]
加入 Action1 OnExecute 事件
procedure TForm1.Action1Execute(Sender: TObject);
var
cmd: String;
path: String;
begin
with JvCreateProcess1 do
begin
case State of
psReady: begin
cmd := Edit1.Text;
commandLine := cmd;
path := ExtractFilePath(cmd);
CurrentDirectory := path;
Memo1.Clear;
Run;
end;
psRunning,
psWaiting: begin
JvCreateProcess1.CloseApplication;
end;
end;
end;
end;

加入 Action1 OnUpdate 事件
procedure TForm1.Action1Update(Sender: TObject);
begin
with JvCreateProcess1 do
begin
case State of
psReady: Button1.Caption := 'Run';
psRunning, psWaiting: Button1.Caption := 'Close';
end;
end;
end;


加入 JvCreateProcess1 OnRead 事件
procedure TForm1.JvCreateProcess1Read(Sender: TObject; const S: String;
const StartsOnNewLine: Boolean);
begin
Memo1.Lines.Add(S);
end;

修改屬性
Button1.Action = Action1
Edit1.Text = 'netstat -na'
JvCreateProcess1.StartupInfo.ShowWindow = swHide
JvCreateProcess1.StartupInfo.DefaultWindowState = False
JvCreateProcess1.ConsoleOptions = [coOwnerData, coRedirect]
Memo1.ScrollBars = ssBoth
Memo1.WordWrap = False

執行程式, 按下 Run Button, Memo1 會取得到 stdout
Active Connections

Proto Local Address Foreign Address State
TCP 0.0.0.0:135 0.0.0.0:0 LISTENING
TCP ...

沒有留言:

網誌存檔