如何在代码中使用 [Setup] 部分中的值?

X-R*_*Ray 3 inno-setup

如何在代码中使用 [Setup] 部分中的值?

我怀疑我使用该工具的方式不正确;也许我应该以完全不同的方式来做这件事。

[Setup]
MyValue=some value

[code]

function InitializeSetup(): Boolean;
begin
  // blank
  MsgBox(GetEnv('MyValue'), mbError, MB_OK);

  // no expansion occurs
  MsgBox(ExpandConstant('MyValue'), mbError, MB_OK);

  // unknown constant "MyValue".
  MsgBox(ExpandConstant('{MyValue}'), mbError, MB_OK);

  Result := true;
end;
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助!

TLa*_*ama 6

您不能在该部分中声明变量[Setup]。此部分可能仅包含一组预定义指令。如果您的目标是定义一个可在脚本部分条目以及脚本编码[Code]部分中使用的常量,那么您正在寻找由#define指令声明的预处理器变量。例如:

#define MyValue "some value"

[Setup]
AppName={#MyValue}
AppVersion=1.5
DefaultDirName={pf}\My Program

[INI]
Filename: "MyProg.ini"; Section: "InstallSettings"; Key: "InstallPath"; String: "{#MyValue}"

[Code]
function InitializeSetup: Boolean;
begin
  Result := True;
  MsgBox('{#MyValue}', mbInformation, MB_OK);
end;
Run Code Online (Sandbox Code Playgroud)

语句后面实际发生的事情{#MyValue}是预处理器将emits定义的常量的值传递MyValue给最终的脚本。