6

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?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
user739711
  • 1,842
  • 1
  • 25
  • 30
  • And what will you do after that 10 seconds ? – TLama Jun 05 '12 at 06:56
  • Maybe he wants to kill the process? I suppose you could exec it normally and create simple timer which kills the process after 10 secs. – Slappy Jun 05 '12 at 07:40
  • @Slappy, you could use e.g. [`Sleep`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms686298%28v=vs.85%29.aspx) function and then kill the process. The problem is that you don't know what process and as far as I know, from the available InnoSetup functions no one returns executed process handle, which you need for process termination. And if you would know the process handle, it's better to use the [`WaitForSingleObject`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms687032%28v=vs.85%29.aspx) function for waiting. See below for a code sample. – TLama Jun 06 '12 at 11:10

1 Answers1

8

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)

Community
  • 1
  • 1
TLama
  • 75,147
  • 17
  • 214
  • 392