Exit from Inno Setup Installation from [code]

2019-01-12 07:21发布

问题:

Is it possible to exit the installation from a function in the [code] section of an installer created with inno setup?

I'm not interested in setting the exit code, what I want to do is perform a custom check for a requirement, and exit the installation if that requirement was not previously installed.

回答1:

You can use Abort() if you are in these events:

InitializeSetup
InitializeWizard
CurStepChanged(ssInstall)
InitializeUninstall
CurUninstallStepChanged(usAppMutexCheck)
CurUninstallStepChanged(usUninstall)


回答2:

The way I do it is:

procedure ExitProcess(exitCode:integer);
  external 'ExitProcess@kernel32.dll stdcall';

And the way of using it is:

[Code]
  if .... then begin
     ExitProcess(0);
  end;


回答3:

To prevent the installer from running, when prerequisites test fails, just return False from the InitializeSetup. This will exit the installer even before the wizard shows.

function InitializeSetup(): Boolean;
begin
  Result := True;

  if not PrerequisitesTest then
  begin                     
    SuppressibleMsgBox('Prerequisites test failed', mbError, MB_OK, MB_OK);
    Result := False;
  end;
end;


If you need to test prerequisites right before the installation starts only (i.e. the InitializeSetup is too early), you can call the Abort function from the CurStepChanged(ssInstall):

procedure CurStepChanged(CurStep: TSetupStep);
begin
  if CurStep = ssInstall then
  begin
    if not PrerequisitesTest then
    begin                     
      SuppressibleMsgBox('Prerequisites test failed', mbError, MB_OK, MB_OK);
      Abort;
    end;
  end;
end;


Though for this scenario, consider using the PrepareToInstall event function mechanism, instead of exiting the setup.

function PrepareToInstall(var NeedsRestart: Boolean): String;
begin
  Result := '';

  if not PrerequisitesTest then
  begin                     
    Result := 'Prerequisites test failed';
  end;
end;


If you need to force terminate the installer any other time, use the ExitProcess WinAPI call:

procedure ExitProcess(uExitCode: Integer);
  external 'ExitProcess@kernel32.dll stdcall';

function NextButtonClick(CurPageID: Integer): Boolean;
begin
  if CurPageID = wpReady then
  begin
    if not PrerequisitesTest then
    begin                     
      SuppressibleMsgBox('Prerequisites test failed', mbError, MB_OK, MB_OK);
      ExitProcess(1);
    end;
  end;
  Result := True;
end;

Though this is rather unsafe exit, so use it only as the last resort approach.




回答4:

Take a look at InitializeSetup and Abort in the InnoSetup help. As Cody said, it is possible. If you're having problems, post what you've done and the problem you're having.



回答5:

Somewhere in your code section you perform a check. Right? As result of that check you want to exit the installation. To perform the exit put the line:

PostMessage (WizardForm.Handle, $0010, 0, 0);  { quit setup, $0010=WM_CLOSE }

Hopefully this helps



标签: inno-setup