Inno Setup: How to pass variable from [Code] to [Run] (or other section)

wcc picture wcc · Oct 10, 2013 · Viewed 10.8k times · Source

How can I pass a variable from [Code] section to parameters in [Run] section in Inno Setup?

Basically, I want to do the following.

  1. Get and save user input to a variable in a procedure InitializeWizard.
  2. Pass the user input to an executable in [Run] section

Here is my code.

[Run]
Filename: "someProgram.exe"; Parameters: ??userInput??

[Code]
procedure InitializeWizard;
var 
  ConfigPage: TInputQueryWizardPage;
  UserInput: String;
begin
  { Create the page }
  ConfigPage :=
    CreateInputQueryPage(
      wpWelcome, 'User input', 'User input',
      'Please specify the following information, then click Next.');

  { Add items (False means it's not a password edit) }
  ConfigPage.Add('Input here:', False);
  { Set initial values (optional) }
  ConfigPage.Values[0] := ExpandConstant('hello');
  { Read values into variables }
  UserInput := ConfigPage.Values[0];
end;

Thanks.

Answer

TLama picture TLama · Oct 10, 2013

You're looking for the scripted constant. See the following example:

[Run]
Filename: "SomeProgram.exe"; Parameters: {code:GetParams}

[Code]
var
  ConfigPage: TInputQueryWizardPage;

function GetParams(Value: string): string;
begin
  Result := ConfigPage.Values[0];
end;

procedure InitializeWizard;
begin
  { Create the page }
  ConfigPage :=
    CreateInputQueryPage(
      wpWelcome, 'User input', 'User input',
      'Please specify the following information, then click Next.');
  { Add items (False means it's not a password edit) }
  ConfigPage.Add('Input here:', False);
  { Set initial values (optional) }
  ConfigPage.Values[0] := ExpandConstant('hello');
end;