如何在Inno Setup中使用PrepareToTinstall函数的NeedsRestart参数?

问题描述

AllPagesExample.iss示例文件中,有以下部分:

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;

如果PreparetoTinstall一个事件函数,而我自己没有调用它,该如何将NeedsRestart参数传递给它?

解决方法

NeedsRestart参数为passed by a referencevar关键字)。因此,从函数返回的值类似于函数本身的返回值(Result)。

documentation说:

返回一个非空字符串,以指示安装程序在Preparing to Install向导页面上停止,将返回的字符串显示为错误消息。如果需要重新启动,请将NeedsRestart设置为True(并返回一个非空字符串)。如果以这种方式停止安装程序,它将以专用退出代码退出,如Setup Exit Codes中所述。

因此,您要做的就是为参数分配一个值,一旦事件函数退出,Inno Setup就会相应地起作用。

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