Delphi 调用外部程序并等待其运行结束

function RunWait(FileName: string; Visibility: Integer): THandle;
var
    zAppName: array[0..512] of Char;
    zCurDir: array[0..255] of Char;
    WorkDir: string;
    StartupInfo: TStartupInfo;
    ProcessInfo: TProcessInformation;
begin
    try
      StrPCopy(zAppName, FileName);
      GetDir(0, WorkDir);
      StrPCopy(zCurDir, WorkDir);
      FillChar(StartupInfo, SizeOf(StartupInfo), #0);
      StartupInfo.cb := SizeOf(StartupInfo);
      StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
      StartupInfo.wShowWindow := Visibility;
      if not CreateProcess(nil, zAppName, nil, nil, false, Create_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo) then
      begin
        result := 0;

        Exit;
      end
      else
      begin
        WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
        GetExitCodeProcess(ProcessInfo.hProcess, result);
      end;
    finally
    end;
end;

其中 参数   Visibility 取值如下:


行号 参数 含义
1 SW_HIDE                        隐藏这个窗体,并激活其他窗体。
2 SW_MAXIMIZE                最大化指定的窗体。
3 SW_MINIMIZE                 最小化指定的窗体,并按顺序激活最上层的窗体。
4 SW_RESTORE               激活并显示窗体。如果窗体为最小化或者最大化,窗体恢复到原始大
                                          小和置。应用程序当恢复一个最小化的窗体时将指定标记。
5 SW_SHOW                      以当前的大小和位置激活并显示窗体。
6 SW_SHOWDEFAULT
7 SW_SHOWMAXIMIZED    激活并最大化显示窗体。
8 SW_SHOWMINIMIZED     激活并最小化现实窗体。
9 SW_SHOWMINNOACTIVE    最小化窗体,保持其激活状态。
10 SW_SHOWNA                     以当前状态显示窗体,保持其激活状态。
11 SW_SHOWNOACTIVATE   以当前的大小和位置显示窗体,并保持其激活状态。
12 SW_SHOWNORMAL 激活并显示一个窗体。如果窗体为最大化或者最小化,窗体恢复到原始的大小和位置。当窗体第一次显示的时候,应用程序记录标记。

 

引用2

如果在你的软件中留下了你的网页地址和邮件地址,你肯定希望人们点击它就会启动浏览器或者电子邮件软件。这其实就是如何启动外部软件的问题,很简单,不是吗?不过,如果我问你,如何启动外部程序并等待它结束,你还能告诉我吗?

其实,这是一个“古老”的话题,在WIN95时代就被讨论过了。不过,既然这么多人不知道,我感觉还是有必要再讨论一下。


一、为什么要启动外部程序

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


二、预备知识

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值