Inno Setup Exec()函数等待有限的时间 [英] Inno Setup Exec() function Wait for a limited time

查看:479
本文介绍了Inno Setup Exec()函数等待有限的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Inno Setup脚本中,我正在执行第三方可执行文件.我正在使用Exec()函数,如下所示:

In my Inno Setup script I am executing 3rd party executable. I am using the Exec() function as below:

Exec(ExpandConstant('{app}\SomeExe.exe'), '', '', SW_HIDE, ewWaitUntilTerminated, ErrorCode);

通过提及ewWaitUntilTerminated,它会一直等到SomeExe.exe不退出.我只想等待10秒.

By mentioning ewWaitUntilTerminated it waits until the SomeExe.exe doesn't quit. I want to wait only for 10 secs.

有什么解决办法吗?

推荐答案

假定您要执行外部应用程序,请等待它终止指定的时间,如果它本身没有终止,请从安装程序中将其杀死,请尝试以下代码.对于此处使用的神奇常量,在

Assuming you want to execute external application, waiting for its termination for a specified time and if it's not terminated by itself killing it from setup try the following code. To the magical constants used here, 3000 used as the parameter in the WaitForSingleObject function is the time in milliseconds for how long the setup will wait for the process to terminate. If it doesn't terminate in that time by itself, it is killed by the TerminateProcess function, where the 666 value is the process exit code (quite evil in this case :-)

[Code]
#IFDEF UNICODE
  #DEFINE AW "W"
#ELSE
  #DEFINE AW "A"
#ENDIF

const
  WAIT_TIMEOUT = $00000102;
  SEE_MASK_NOCLOSEPROCESS = $00000040;

type
  TShellExecuteInfo = record
    cbSize: DWORD;
    fMask: Cardinal;
    Wnd: HWND;
    lpVerb: string;
    lpFile: string;
    lpParameters: string;
    lpDirectory: string;
    nShow: Integer;
    hInstApp: THandle;    
    lpIDList: DWORD;
    lpClass: string;
    hkeyClass: THandle;
    dwHotKey: DWORD;
    hMonitor: THandle;
    hProcess: THandle;
  end;

function ShellExecuteEx(var lpExecInfo: TShellExecuteInfo): BOOL; 
  external 'ShellExecuteEx{#AW}@shell32.dll stdcall';
function WaitForSingleObject(hHandle: THandle; dwMilliseconds: DWORD): DWORD; 
  external 'WaitForSingleObject@kernel32.dll stdcall';
function TerminateProcess(hProcess: THandle; uExitCode: UINT): BOOL;
  external 'TerminateProcess@kernel32.dll stdcall';

function NextButtonClick(CurPageID: Integer): Boolean;
var
  ExecInfo: TShellExecuteInfo;
begin
  Result := True;

  if CurPageID = wpWelcome then
  begin
    ExecInfo.cbSize := SizeOf(ExecInfo);
    ExecInfo.fMask := SEE_MASK_NOCLOSEPROCESS;
    ExecInfo.Wnd := 0;
    ExecInfo.lpFile := 'calc.exe';
    ExecInfo.nShow := SW_HIDE;

    if ShellExecuteEx(ExecInfo) then
    begin
      if WaitForSingleObject(ExecInfo.hProcess, 3000) = WAIT_TIMEOUT then
      begin
        TerminateProcess(ExecInfo.hProcess, 666);
        MsgBox('You just killed a little kitty!', mbError, MB_OK);
      end
      else
        MsgBox('The process was terminated in time!', mbInformation, MB_OK);
    end;
  end;
end;

我已经在Windows 7上使用Inno Setup 5.4.3 Unicode和ANSI版本测试过的代码(感谢kobik,他的想法是使用来自

The code I've tested with Inno Setup 5.4.3 Unicode and ANSI version on Windows 7 (thanks to kobik for his idea to use conditional defines for Windows API function declarations from this post)

这篇关于Inno Setup Exec()函数等待有限的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆