delphi啟動外部程式執行結束
一、為什麼要啟動外部程式
也許,你想讓你的程式完成全部的功能。不過,無論從物力還是人力上,你都應養成資源共享的習慣。更好的考慮是,充分利用已有的程式,而讓你的程式專注於某一方面的功能。比如說,瀏覽器負責開啟網頁,讓人們瀏覽,當遇到下載的任務時,可以交給更專業的下載軟體去做。你也可能在你的程式裡留下了你的主頁和郵箱地址,你希望有人點選它們時就分別啟動瀏覽器和電子郵件。在某些情況下,你需要外部程式處理後,再進行下一步的工作,這時就會遇到啟動外部程式並等待它結束的問題。
二、預備知識
啟動外部程式我們可以使用函式Winexec、ShellExecute和ShellExecuteEx。我推薦大家使用函式ShellExecute,因為它既靈活,又簡單。看看下面的例子,用法就清楚了:
*: 啟動一個程式
ShellExecute(Handle,'open',PChar('c: estapp.exe'),
nil,nil,SW_SHOW);
* 啟動記事本 (因為記事本在系統路徑下,所以不必寫完整的路徑名了):
ShellExecute(Handle, 'open', PChar('notepad'),
nil, nil, SW_SHOW);
* 啟動記事本並載入一個純文字檔案:
ShellExecute(Handle, 'open', PChar('notepad'),
PChar('c: est eadme.txt', nil, SW_SHOW);
* 使用記事本開啟一個純文字檔案 (請確定*.txt檔案被關聯到記事本):
ShellExecute(Handle, 'open', PChar('c: est eadme.txt'),
nil, nil, SW_SHOW);
* 使用預設瀏覽器開啟網址:
nil, nil, SW_SHOW);
* 列印一個檔案:
ShellExecute(Handle, 'print', PChar('c: est eadme.txt'),
nil, nil, SW_SHOW);
* 用Windows Explorer開啟一個資料夾:
ShellExecute(Handle, 'explore', PChar('c:windows)',
nil, nil, SW_SHOW);
* 執行一個DOS命令並立即返回:
ShellExecute(Handle, 'open', PChar('command.com'),
PChar('/c copy file1.txt file2.txt'), nil, SW_SHOW);
* 執行一個DOS命令並保持DOS視窗開啟 ("stay in DOS"):
ShellExecute(Handle, 'open', PChar('command.com'),
PChar('/k dir'), nil, SW_SHOW);
啟動一個外部程式並不難吧?不過,我們如何知道它是否執行結束了呢?我們的程式又怎樣等待它結束呢?
三、啟動外部程式並等待它結束的函式
我們可以通過程序控制代碼(process handle)來檢視程序(程式)是否結束。為了得到程序控制代碼,有兩個Win32 API函式可以利用:ShellExecuteEx 或者CreateProces。解決這個問題最簡單的方法是,使用ShellExecuteEx啟動一個外部程式,然後使用WaitForSingleObject管理這個外部程式的程序控制代碼。我們可以這樣定義一個函式:
……
{ ExecAppWait:功能:執行外部程式並等待它結束。。
執行外部程式APPNAME,引數PARAMS;
Returns:如果外部程式出錯返回 FASLE
}
function ExecAppWait(AppName, Params: string): Boolean ;
……
function ExecAppWait(AppName, Params: string): Boolean;
var
// Structure containing and receiving info about application to start
ShellExInfo: TShellExecuteInfo;
begin
FillChar(ShellExInfo, SizeOf(ShellExInfo), 0);
with ShellExInfo do begin
cbSize := SizeOf(ShellExInfo);
fMask := see_Mask_NoCloseProcess;
Wnd := Application.Handle;
lpFile := PChar(AppName);
lpParameters := PChar(Params);
nShow := sw_ShowNormal;
end;
Result := ShellExecuteEx(@ShellExInfo);
if Result then
while WaitForSingleObject(ShellExInfo.HProcess, 100) = WAIT_TIMEOUT do
begin
Application.ProcessMessages;
if Application.Terminated then Break;
end;
end;
……
不難理解吧?
建立一個Unit ExecWait,把上面的程式碼輸進去。
四、例子
unit DemoUnit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, SHELLAPI;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
BtnExec: TButton;
CheckBoxWait: TCheckBox;
Label3: TLabel;
Label4: TLabel;
Edit3: TEdit;
Edit4: TEdit;
Label5: TLabel;
procedure BtnExecClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
execwait;
{$R *.DFM}
procedure TForm1.BtnExecClick(Sender: TObject);
var
Success: Boolean;
InstanceID: THandle;
begin
{ 最小化視窗,提醒發生的變化 }
Application.Minimize;
Success := False;
try
if CheckBoxWait.Checked then
Success := ExecAppWait(Edit1.Text, Edit2.Text)
else begin
InstanceID := ShellExecute(Handle, 'open', PChar(Edit1.Text),
PChar(Edit2.Text), nil, SW_SHOW);
Success := InstanceID >= 32; // 小於32可就錯了
end;
finally
// 可別忘了恢復我們的程式的視窗!
Application.Restore;
if not Success then
ShowMessage('Application 1 failed: ' + Edit1.Text + ' ' + Edit2.Text);
end;
try
if CheckBoxWait.Checked then
Success := ExecAppWait(Edit3.Text, Edit4.Text)
else begin
InstanceID := ShellExecute(Handle, 'open', PChar(Edit3.Text),
PChar(Edit4.Text), nil, SW_SHOW);
Success := InstanceID >= 32; //小於32可就錯了
end;
finally
//恢復我們的程式的視窗
Application.Restore;
if not Success then
ShowMessage('Application 2 failed: ' + Edit3.Text + ' ' + Edit4.Text);
end;
end;
end.
OK,沒有問題吧?你趕快試試吧,把它應用到你的程式裡。