您的位置:首页 > 编程语言 > Delphi

delphi启动外部程序执行结束

2016-06-23 16:14 501 查看
一、为什么要启动外部程序

也许,你想让你的程序完成全部的功能。不过,无论从物力还是人力上,你都应养成资源共享的习惯。更好的考虑是,充分利用已有的程序,而让你的程序专注于某一方面的功能。比如说,浏览器负责打开网页,让人们浏览,当遇到下载的任务时,可以交给更专业的下载软件去做。你也可能在你的程序里留下了你的主页和邮箱地址,你希望有人点击它们时就分别启动浏览器和电子邮件。在某些情况下,你需要外部程序处理后,再进行下一步的工作,这时就会遇到启动外部程序并等待它结束的问题。

二、预备知识

启动外部程序我们可以使用函数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);

* 使用默认浏览器打开网址:

ShellExecute(Handle, 'open', PChar('http://www.festra.com/'),

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,没有问题吧?你赶快试试吧,把它应用到你的程序里。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: