簡體   English   中英

如何將帶有值的命令行參數傳遞給 Inno Setup Compiler,以便我可以在我的代碼中使用它們?

[英]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是應該分析從命令行獲取的參數然后根據結果執行其工作的函數。

編譯器(或技術上的預處理器)具有/D命令行開關,您可以使用它來設置預處理器變量。

例如這個...

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

...具有相同的效果,就像您在腳本本身中使用#define指令一樣,如下所示:

#define BinaryName "MyProg.exe"

所以你可以在腳本中以同樣的方式使用它:

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

您甚至可以在以下情況下使用變量:

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

雖然為了同樣的效果,你可以只使用變量存在,例如:

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

這些問題也涵蓋了這一點:


您可以在任何地方使用預處理器指令,甚至在[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++ 預處理器 它不關心(很多)部分或代碼結構。 您甚至可以執行以下操作:

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

SaveToFile添加到腳本末尾以查看生成的代碼。

如果您正在尋找更舒適的解決方案,請參閱 Visual Studio 的此擴展: https : //marketplace.visualstudio.com/items?itemName=unSignedsro.VisualInstaller

您可以在項目屬性中輕松設置多個符號/配置項目屬性 並使用各種配置/輸出等構建您的安裝程序。

(如果您有任何問題,請隨時與我聯系,我是此擴展程序的作者)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM