加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

inno-setup – Inno Setup Exec()函数等待有限的时间

发布时间:2020-12-15 09:31:32 所属栏目:大数据 来源:网络整理
导读:在我的Inno安装脚本中,我正在执行第三方可执行文件.我正在使用Exec()函数,如下所示: Exec(ExpandConstant('{app}SomeExe.exe'),'',SW_HIDE,ewWaitUntilTerminated,ErrorCode); 通过提及ewWaitUntilTerminated,它等待SomeExe.exe不退出.我想等待10秒钟. 那
在我的Inno安装脚本中,我正在执行第三方可执行文件.我正在使用Exec()函数,如下所示:

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

通过提及ewWaitUntilTerminated,它等待SomeExe.exe不退出.我想等待10秒钟.

那有什么解决方案吗?

解决方法

假设您要执行外部应用程序,等待指定时间终止,如果它没有自行终止,请从安装程序中删除它,请尝试以下代码.对于此处使用的神奇常数,在 WaitForSingleObject函数中用作参数的3000是设置等待进程终止的时间(以毫秒为单位).如果它本身没有在那个时间终止,它将被 TerminateProcess函数杀死,其中666值是进程退出代码(在这种情况下非常邪恶:-)

[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从this post开始使用条件定义来进行Windows API函数声明)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读