Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inno Setup Exec() function Wait for a limited time

Tags:

inno-setup

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);

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

Is there any solution for that?

like image 641
user739711 Avatar asked Jun 05 '12 06:06

user739711


1 Answers

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 '[email protected] stdcall';
function TerminateProcess(hProcess: THandle; uExitCode: UINT): BOOL;
  external '[email protected] 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)

like image 182
TLama Avatar answered Nov 28 '22 16:11

TLama