问题
I've a question about the #define in Inno Setup. I've make a setup who's working well and i'm thinking of the future of app if i have some modification to do. For example, if i change the version (Major, Minor, Build, Patch,...) i doesn't want to change all the line (like Registry) at each time.
So i try to make something like that :
#define MyAppMajor "5"
#define MyAppMinor "5"
#define MyAppBuild "1"
#define MyAppPatch "1"
...
[Files]
Source: "D:\ProgramFiles\..."; DestDir: "{app}\Program\{MyAppMajor}.{MyAppMinor}\"; Flags: ignoreversion;
[Registry]
Root: "HKLM32"; Subkey: "Software\program\"; ... ; ValueData: "{MyAppMajor}.{MyAppMinor}.{MyAppBuild}.{MyAppPatch}";
But that doesn't compile and it says :
"Unknown constant "MyAppMajor".Use two consecutive "{" characters if you are trying to embed a single "{" and not a constant".
Is there a way to do something like that for versionning or another constant ?
回答1:
You missed to use the # char (or #emit, which is the longer version of the same), that is used to inline a defined variable into the script, e.g.:
#define MyAppMajor "5"
#define MyAppMinor "5"
#define MyAppBuild "1"
#define MyAppPatch "1"
[Files]
...; DestDir: "{app}\Program\{#MyAppMajor}.{#MyAppMinor}\"; Flags: ignoreversion;
[Registry]
...; ValueData: "{#MyAppMajor}.{#MyAppMinor}.{#MyAppBuild}.{#MyAppPatch}";
When missing this, the compiler expected (built-in) constants called MyAppMajor, MyAppMinor etc., whose doesn't exist; hence the error.
But, what you're trying to replicate is probably built-in in Inno Setup as the AppVersion directive. It might be useful e.g. in conjunction of reading the version of the application binary from its included file version information:
#define AppVersion GetFileVersion('C:\MyApp.exe')
[Setup]
...
AppVersion={#AppVersion}
回答2:
Although I haven't found something in Inno's documentation explicitly stating it, there is an example where the + operator is used to concatenate strings. I think you can get by with something like this:
Source: "D:\ProgramFiles\..."; DestDir: "{app}\Program\" + MyAppMajor + "." + MyAppMinor + "\"; Flags: ignoreversion;
If that doesn't work, there is a StringChange preprocessor macro that should work but will be uglier, especially in strings with multiple substitutions:
#define MyAppMajor "5"
#define MyAppMinor "5"
#define InstallDirectory StringChange(StringChange("{app}\Program\%major%.%minor%", "%major", MyAppMajor), "%minor%", MyAppMinor)
Source: "D:\ProgramFiles\..."; DestDir: InstallDirectory; Flags: ignoreversion;
来源:https://stackoverflow.com/questions/27985739/use-define-and-constant-in-inno-setup