inno-setup

Constant defined using Inno Setup preprocessor #define is not recognised in ExpandConstant


I'm trying to set up an install file that (optionally) installs .NET 5 if it's not already installed.
However, I'm having trouble defining the version of .NET to install in a constant.

My script is set up like this

#define DotNetVersion "5"
...
[Tasks]   
Name: "dotnet"; Description: "{cm:DotNet}"; GroupDescription: "{cm:Prerequisites}"
...

[Files]
Source: "..\Dependencies\{#DotNetInstallFile}"; DestDir: {tmp}; \
    Flags: deleteafterinstall; AfterInstall: InstallDotNet; \
    Check: NetNotInstalled(ExpandConstant('{DotNetVersion}')); Tasks: "dotnet"

When I try running the resulting install file I get the following error:

Internal error: Expression error 'Internal error: Unknown constant "DotNetVersion"'

Error box stating: 'Internal error: Unknown constant "DotNetVersion"'

The function NetNotInstalled works correctly if I replace ExpandConstant('{DotNetVersion}') with '5', but I want to easily be able to change this without modifying more than the defined constants.

I don't get what's wrong here. The Inno Setup docs state that this should be valid.
Using the same constant for any other function seems to work flawlessly.


Solution

  • A variable defined using Inno Setup preprocessor is not Inno Setup constant. Calling ExpandConstant function on it has no effect.

    To expand preprocessor variable (or any expression), you can use {#VariableOrExpression} syntax. It's an inline preprocessor directive call, where, when no directive is explicitly specified, the emit is implied. So the {#VariableOrExpression} is the same as {#emit VariableOrExpression}. And as every preprocessor construct, it's evaluated on compile time (contrary to ExpandConstant).

    You actually do that correctly already with {#DotNetInstallFile}, so do the same with DotNetVersion:

    Source: "..\Dependencies\{#DotNetInstallFile}"; \
        DestDir: {tmp}; Flags: deleteafterinstall; AfterInstall: InstallDotNet; \
        Check: NetNotInstalled('{#DotNetVersion}'); Tasks: "dotnet"
    

    See also How to use variables \ macros with Inno Setup?