JConstantine
JConstantine

Reputation: 1070

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

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.

Upvotes: 6

Views: 3110

Answers (2)

Martin Prikryl
Martin Prikryl

Reputation: 202642

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 Unknown 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:


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 do 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.

Upvotes: 9

Slappy
Slappy

Reputation: 5472

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 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).

Upvotes: 0

Related Questions