【问题标题】:Inno Setup: Execute a Pascal function in [Run] sectionInno Setup:在 [Run] 部分执行 Pascal 函数
【发布时间】:2016-05-17 07:40:49
【问题描述】:

在安装结束时,我需要运行 Pascal 函数来更新飞行模拟器 .cfg 文件(在 Inno Setup 中称为 .ini 文件)。 Pascal 函数存在于其[Code] 部分中并且可以正确运行。我想在[Run] 部分运行这个Pascal 函数,使用StatusMsg 告诉用户发生了什么。

[Run]
Filename: {code:FsxEditSceneryFile|Add#<scenerySpec>}; StatusMsg: "Add scenery to FSX";
; <scenerySpec> is just a place holder of the actual scenery specification!

除了 Inno Setup 强制我使用 string 作为 Pascal 函数的返回值之外,一切都按预期工作。但是Filename 语句需要Boolean 作为返回值来指定执行是成功(True)还是失败(False)。这种类型不匹配会在Filename 语句的执行结束时产生一个错误消息框,说

创建过程失败;代码 87。错误的参数。

任何建议如何解决?我知道存在我可以使用的事件函数,例如CurStepChanged() 但我发现 StatusMsg 机制很好地告诉用户安装完成了什么。

【问题讨论】:

    标签: inno-setup pascalscript


    【解决方案1】:

    您正在滥用Filename 参数解析来执行某些代码。解析参数值时未记录。这使您的方法不可靠。在显示 StatusMsg 时,您无法知道该值已解析。而且,无论如何,该值必须解析为可执行路径。 Inno Setup 将尝试执行它(因此出现错误)。你可能不想要什么。不要那样做。


    相反,正如您已经建议的那样,使用CurStepChanged。您可以通过访问WizardForm.StatusLabel 来显示来自 Pascal 代码的状态消息。

    procedure CurStepChanged(CurStep: TSetupStep);
    begin
      if CurStep = ssPostInstall then
      begin
        WizardForm.StatusLabel.Caption := 'Installing something...';
        { Install something }
      end;
    end;
    

    【讨论】:

    • 非常感谢您的帮助!
    【解决方案2】:

    如果其他人遇到这个问题,这里有一个解决方案,可以让您“代替”[RUN] 部分中的任何语句运行 pascal 脚本过程。

    我需要将 powershell 脚本的执行移至 [CODE] 部分,因为 [RUN] 部分不提供对外部 cmd 程序的退出代码作出反应的方法。 由于我有一堆子安装程序,我需要在我的设置中按特定顺序执行,因此可以选择在[RUN] 部分中控制 pascal 脚本的位置,而不仅仅是在输入时[RUN] 部分设置步骤。

    它的工作原理是使用“虚拟”程序(如ping)作为[RUN] 语句的文件名,然后使用内置BeforeInstall 参数调用包含实际执行逻辑的pascal 脚本过程。

    [运行]部分

    Filename: "ping"; BeforeInstall: RunSQLSetupScript; Components: "thirds\db"; StatusMsg: "Installing SQL Server ..." ; Flags: runhidden
    

    包含实际执行逻辑的 Pascal 脚本过程

    (请注意,{tmp}\{#SQLServerInstallScript} 将是此示例中脚本的实际路径,因为通常建议避免使用硬编码路径并改用常量+临时目录。)

    //  runs the SQL setup script and terminates the setup process, if the script returns an exit code that indicates an error
    procedure RunSQLSetupScript();
    var
      runSuccess: Boolean;
      retVar: Integer;
      msgText: String;
    begin
      runSuccess := ShellExec('', 'powershell.exe', '-NoProfile -File ' + ExpandConstant('{tmp}\{#SQLServerInstallScript}'), '', SW_SHOW, ewWaitUntilTerminated, retVar);
          
      // the external script will return an exit code > 0 if an error occurred
      if (runSuccess = False) or (retVar > 0) then
      begin
        msgText := 'SQL Server setup script returned error code ' + IntToStr(retVar) + ', indicating an unsuccessful installation of SQL Server. Setup will now terminate.';
         
        MsgBox(msgText, mbCriticalError, MB_OK);
        // => further handle error case here, like cancelling the running setup or log the issue etc.
      end;
    end;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-27
      • 2016-11-12
      • 2021-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-15
      相关资源
      最近更新 更多