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

半腔热情 提交于 2020-12-06 15:42:19

问题


I have two possible build options. As I don't want my clients to start the installer with some parameters, I'd better pass them to the compiler and do all the job in my code.

Let's say I have the variable UNION which may take two values: 0 and 1. I have to analyze the value of that variable in my code and depending on the result to include some files or not. I know how to pass parameterrs to the installer itself, but how can I pass them to the compiler?

Here's some code:

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 is the function that should analyze the parameter taken from the command line and then do its job depending on the result.


回答1:


Compiler (or technically the preprocessor) has /D command-line switch, which you can use to set a preprocessor variable.

For example this...

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

... has the the same effect, as if you use #define directive in the script itself, like this:

#define BinaryName "MyProg.exe"

So you can use it the same way in the script:

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

You can use a variable even in conditions like:

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

Though for the same effect you can use just a variable existence, like:

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

This is also covered in these questions:

  • How do I build two different installers from the same script in Inno Setup?
  • Compile Inno Setup installer for specific component only

You can use the preprocessor directives anywhere, even in the [Code] section.

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;

or even:

#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

The preprocesor does not care, it kicks in as the very first step and treats the .iss file as a plain text file. Pretty much like C/C++ preprocessor. It does not care (much) about the sections or code structure. You can even to things like:

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

Add SaveToFile to the end of the script to see the generated code.




回答2:


If you are looking more comfortable solution see this extension for Visual Studio: https://marketplace.visualstudio.com/items?itemName=unSignedsro.VisualInstaller

You can easily set multiple symbols/configurations in Project Properties and build your installers with various configurations/outputs etc.

(Feel free to contact me if you have any questions, I am author of this extension).



来源:https://stackoverflow.com/questions/60888905/how-can-i-pass-command-line-parameters-with-a-value-to-the-inno-setup-compiler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!