如何将带有值的命令行参数传递给 Inno Setup 编译器,以便我可以在我的代码中使用它们?

How can I pass command line parameters with a value to the Inno Setup Compiler, so I can use them in my code?

我有两个可能的构建选项。因为我不希望我的客户使用一些参数启动安装程序,所以我最好将它们传递给编译器并在我的代码中完成所有工作。

假设我有变量 UNION,它可能有两个值:01。我必须在我的代码中分析该变量的值,并根据结果来包含或不包含一些文件。 我知道如何将参数传递给安装程序本身,但如何将它们传递给编译器?

这是一些代码:

procedure CurStepChanged(CurStep: TSetupStep);
var
  Code: Integer;
begin
  if CurStep = ssDone then
    begin
      if not IsUnion then
        begin
          DeleteFile(ExpandConstant('{app}')+'\Locale\C4Union.UKR');
          DeleteFile(ExpandConstant('{app}')+'\Locale\C4Union.ENU');  
        end;
    end;
end;

IsUnion 是应该分析从命令行获取的参数然后根据结果执行其工作的函数。

编译器(或技术上的 preprocessor) has /D command-line switch,您可以使用它来设置预处理器变量。

例如这个...

ISCC.exe Example1.iss /DBinaryName=MyProg.exe

... 具有相同的效果,就像您在脚本本身中使用 #define directive 一样:

#define BinaryName "MyProg.exe"

因此您可以在脚本中以相同的方式使用它:

[Files]
Source: "{#BinaryName}"; DestDir: "{app}"

您甚至可以在 conditions 中使用变量,例如:

ISCC.exe Example1.iss /DMode=Install
#if Mode == "Install"
[Files]
Source: "MyProg.exe"; DestDir: "{app}"
#elif Mode == "Delete"
[InstallDelete]
Type: files; Name: "{app}\MyProg.exe"
#else
#error Unknonn mode
#endif

尽管您可以只使用 variable existence 以获得相同的效果,例如:

ISCC.exe Example1.iss /DInstall /DDelete
#ifdef Install
[Files]
Source: "MyProg.exe"; DestDir: "{app}"
#endif

#ifdef Delete
[InstallDelete]
Type: files; Name: "{app}\MyProg.exe"
#endif

这也包含在这些问题中:

  • How do I build two different installers from the same script in Inno Setup?

您可以在任何地方使用预处理器指令,甚至在 [Code] 部分。

procedure CurStepChanged(CurStep: TSetupStep);
begin
  if CurStep = ssDone then
  begin
    #ifdef Delete
    DeleteFile(ExpandConstant('{app}')+'\Locale\C4Union.UKR');
    DeleteFile(ExpandConstant('{app}')+'\Locale\C4Union.ENU');  
    #endif
  end;
end;

甚至:

#ifdef Delete
procedure CurStepChanged(CurStep: TSetupStep);
begin
  if CurStep = ssDone then
  begin
    DeleteFile(ExpandConstant('{app}')+'\Locale\C4Union.UKR');
    DeleteFile(ExpandConstant('{app}')+'\Locale\C4Union.ENU');  
  end;
end;
#endif

预处理器不关心,它作为第一步启动并将 .iss 文件视为纯文本文件。很像 C/C++ preprocessor。它不(太多)关心部分或代码结构。您甚至可以执行以下操作:

DeleteFile(
  ExpandConstant(
    #ifdef DeleteFromUserData
    '{userappdata}\MyProg'
    #else
    '{app}'
    #endif
    )+'\Locale\C4Union.UKR');

Add SaveToFile to the end of the script 查看生成的代码。

如果您正在寻找更舒适的解决方案,请参阅 Visual Studio 的扩展: https://marketplace.visualstudio.com/items?itemName=unSignedsro.VisualInstaller

您可以轻松地在项目属性 中设置多个 symbols/configurations 并使用各种 configurations/outputs 等构建您的安装程序

(如有任何疑问,请随时与我联系,我是此扩展程序的作者)。