Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
754 views
in Technique[技术] by (71.8m points)

Inno Setup Exec() function Wait for a limited time

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.7k users

...