John
John

Reputation: 961

How to use Inno Setup preprocessor directive in [Code] section?

I feel like this has got to be something simple I'm missing - in Inno Setup, if I have passed in a directive variable, how do I use it inside the [Code] section?

Say I pass in /DMYVAR=1 to the Inno Setup engine.

In my .iss file I can have something like:

[Setup]
AppName=MyApp v{#MYVAR}

Down in my [Code] section I'd like to be able to use it like this:

function IsVersionOne(param: String): boolean;
begin
    Result := {#MYVAR} == "1";
end;

This doesn't work :(

Upvotes: 4

Views: 3703

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202642

Preprocessor does not care, where its directives are expanded.

So {#name} syntax (inline preprocessor directive call) works everywhere, including Pascal Script.

Though, if you want to compare a define value as a string, you have to, of course, enclose it to quotes to make it a string. Also note that your == "1" syntax is wrong, Pascal uses single = and single quotes.

function IsVersionOne(param: String): boolean;
begin
  Result := '{#MYVAR}' = '1';
end;

#expr SaveToFile(AddBackslash(SourcePath) + "Preprocessed.iss")

Run the compiler with /DMYVAR=1, and check the generated Preprocessed.iss. It will show:

function IsVersionOne(param: String): boolean;
begin
  Result := '1' = '1';
end;

As the value is a number, you can, of course, use numerical comparison too:

function IsVersionOne(param: String): boolean;
begin
  Result := {#MYVAR} = 1;
end;

For a related question with more detailed information, see Evaluate preprocessor macro on run time in Inno Setup Pascal Script.


While the above answered your literal question, your function signature actually suggests that you want to implement a Check function to test a value of a compile-time directive, like:

[Files]
Source: "MYPROG.EXE"; DestDir: "{app}"; Check: IsVersionOne

That's an inefficient overkill.

Use a preprocessor #if directive instead:

[Files]
#if MYVAR == "1"
Source: "MYPROG.EXE"; DestDir: "{app}"
#endif

#expr SaveToFile(AddBackslash(SourcePath) + "Preprocessed.iss")

If you run the compiler with /DMYVAR=1, Preprocessed.iss will show:

[Files]
Source: "MYPROG.EXE"; DestDir: "{app}"

If you run the compiler with a different value of DMYVAR, Preprocessed.iss will show:

[Files]

If you need the script to compile even without /DMYVAR= switch, define a default value at the top of the script, like:

#ifndef MYVAR
#define MYVAR "0"
#endif

Upvotes: 8

Related Questions