Search code examples
inno-setupexit-code

How can I set the exit code from return code of my Execute File in Inno Setup?


I want to get return code (negative value) from my {app}\{#MyAppExeName} for setup exit code (MyAppExeName will run 20~30s)

I refer many example codes, and Exec can get result code but still don't know how to add to [Code] section for setup exit code (I have no idea about Pascal Script)

Below is [Run] section in my Inno Setup Script

[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent

How to change [Run] & [Code] section for my goal?

Please help me and give me example code

Thanks

BR, Alan


Solution

  • To run an external process and retrieve its exit code, using Exec support function.

    To modify installer's exit code, implement GetCustomSetupExitCode event function

    [Code]
    
    var
      ExitCode: Integer;
      
    procedure CurStepChanged(CurStep: TSetupStep);
    begin
      if CurStep = ssPostInstall then
      begin
        if Exec(
             ExpandConstant('{app}\{#MyAppExeName}'), '', '', SW_SHOW,
             ewWaitUntilTerminated, ExitCode) then
        begin
          Log(Format('Command finished, exit code is %d', [ExitCode]));
        end
          else
        begin
          Log('Failed to run command');
        end;
      end;
    end;
    
    function GetCustomSetupExitCode: Integer;
    begin
      if ExitCode <> 0 then
      begin
        Log(Format('Returning exit code %d', [ExitCode]));
      end;
      Result := ExitCode;
    end;
    

    Note that Windows process exit code cannot be negative. The exit code is an unsigned 32-bit integer.

    See how uExitCode parameter of ExitProcess and well as lpExitCode parameter of GetExitCodeProcess are of UINT and DWORD types respectively.

    It's just a common error/misconception that the exit code is interpreted as signed.

    The Inno Setup follows that misconception by using signed integer value in GetCustomSetupExitCode.