Search code examples
installationinno-setuppascalscript

How to force a reboot in post-install in Inno Setup


In my setup I have to install an external driver.
In some rare cases the installation fails and I have to remove the old driver and reboot before I can try again.

I install the external driver in the ssPostInstall.

procedure CurStepChanged(CurStep: TSetupStep);
begin
  if CurStep = ssPostInstall then
  begin
    if Exec(ExpandConstant('{app}\external.exe'), '-install', '', SW_SHOW,
            ewWaitUntilTerminated, ResultCode) then
    begin
      { handle success if necessary; ResultCode contains the exit code }
    end
    else begin
      { handle failure if necessary; ResultCode contains the error code }
      bReboot := true;
    end;
  end;

function NeedRestart(): Boolean;
begin
  Result := bReboot;
end;

Unfortunately this does not work since NeedRestart is called before ssPostInstall.

Is there any other way to trigger a reboot?
I don't want to set AlwaysRestart = yes

I could let pop up a MsgBox to inform the user and tell them what to do. But it would be much nicer if it could be handled within of the setup automatically.


Solution

  • You can do the installation sooner. For example immediately after the external.exe is installed using AfterInstall:

    [Files]
    Source: "external.exe"; DestDir: "{app}"; AfterInstall: InstallDriver
    
    [Code]
    procedure InstallDriver;
    begin
      if Exec(ExpandConstant('{app}\external.exe'), '-install', '', SW_SHOW,
              ewWaitUntilTerminated, ResultCode) then
      begin
        { handle success if necessary; ResultCode contains the exit code }
      end
        else
      begin
        { handle failure if necessary; ResultCode contains the error code }
        bReboot := true;
      end;
    end;
    

    Another option is to use ssInstall step (or even PrepareToInstall event) and extract the file programmatically using ExtractTemporaryFile.


    Btw, if external.exe is only an installer, you may want to "install" it to {tmp} (to get it automatically deleted).