Search code examples
inno-setuppascalscript

How to use NeedsRestart parameter of PrepareToTinstall function in Inno Setup?


In AllPagesExample.iss example file there's this part:

function PrepareToInstall(var NeedsRestart: Boolean): String;
begin
  if SuppressibleMsgBox('Do you want to stop Setup at the Preparing To Install wizard page?', mbConfirmation, MB_YESNO, IDNO) = IDYES then
    Result := 'Stopped by user';
end;

If PrepareToTinstall is an event function and I don't call it myself, how can I pass NeedsRestart parameter to it?


Solution

  • The NeedsRestart parameter is passed by a reference (the var keyword). So its value is returned from the function similarly to the return value of the function itself (Result).

    As the documentation says:

    Return a non empty string to instruct Setup to stop at the Preparing to Install wizard page, showing the returned string as the error message. Set NeedsRestart to True (and return a non empty string) if a restart is needed. If Setup is stopped this way, it will exit with a dedicated exit code as described in Setup Exit Codes.

    So, what you want to do, is to assign a value to the parameter and Inno Setup will act accordingly once the event function exits.

    function PrepareToInstall(var NeedsRestart: Boolean): String;
    begin
      Result := '';
      if DoWeNeedRestartBeforeInstalling then
      begin
        Result := 'You need to restart your machine before installing';
        NeedsRestart := True;
      end;
    end;
    

    enter image description here