Inno Setup Exec() function Wait for a limited time

前端 未结 1 1770
孤街浪徒
孤街浪徒 2020-12-02 00:34

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



        
相关标签:
1条回答
  • 2020-12-02 01:07

    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;
    

    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)

    0 讨论(0)
提交回复
热议问题