Toda Raba
Toda Raba

Reputation: 614

How can I set the exit code in Inno Setup?

I want to set the exit code for my installation, this way I will know why the installation was aborted. I'm using Inno Setup.

Upvotes: 8

Views: 9500

Answers (3)

Magus
Magus

Reputation: 65

Did have this same question and found a way to it:

[Code]
var CustomExitCode: integer;

procedure ExitProcess(exitCode:integer);
    external '[email protected] stdcall';

procedure DeinitializeSetup();
begin
    if (CustomExitCode <> 0) then
    begin
        DelTree(ExpandConstant('{tmp}'), True, True, True);
        ExitProcess(CustomExitCode);
    end;
end;

And now at any point of your setup just set CustomExitCode to the code you want. Example:

function InitializeSetup: Boolean;
begin
    // Some check did fail, exiting with custom code
    CustomExitCode = -1;
    
    // Let's just close the setup
    Result := false;
end;

This way the setup will not terminate abruptly and you can customize the exit code no matter what state the wizard did exit.

Upvotes: 0

Paul Lammertsma
Paul Lammertsma

Reputation: 38252

From the Inno Setup Help document (from the article "Setup Exit Codes"):

Beginning with Inno Setup 3.0.3, the Setup program may return one of the following exit codes:

0 Setup was successfully run to completion.

1 Setup failed to initialize.

2 The user clicked Cancel in the wizard before the actual installation started, or chose "No" on the opening "This will install..." message box.

3 A fatal error occurred while preparing to move to the next installation phase (for example, from displaying the pre-installation wizard pages to the actual installation process). This should never happen except under the most unusual of circumstances, such as running out of memory or Windows resources.

4 A fatal error occurred during the actual installation process.

Note: Errors that cause an Abort-Retry-Ignore box to be displayed are not fatal errors. If the user chooses Abort at such a message box, exit code 5 will be returned.

5 The user clicked Cancel during the actual installation process, or chose Abort at an Abort-Retry-Ignore box.

6 The Setup process was forcefully terminated by the debugger (Run | Terminate was used in the IDE).

You can easily check if the setup ran successfully by confirming that the exit code is 0. Furthermore:

Any non-zero exit code indicates that Setup was not run to completion.

To answer your question more specifically, you can determine the installation was canceled by observing exit code 2 or 5.

If you wish to return a custom exit code when Inno would otherwise return 0, you can define the following event function:

function GetCustomSetupExitCode: Integer;

From the help document (from the article "Pascal Scripting: Event Functions"):

function GetCustomSetupExitCode: Integer;

Return a non zero number to instruct Setup to return a custom exit code. This function is only called if Setup was successfully run to completion and the exit code would have been 0.

Upvotes: 15

lepe
lepe

Reputation: 25200

Use:

[Code]
procedure ExitProcess(exitCode:integer);
  external '[email protected] stdcall';

procedure SomeEventHere();
begin
  if someerror then begin
    ExitProcess(9); //Your custom exit code
  end;
end;

Upvotes: 8

Related Questions