Search code examples
inno-setuppascalscript

Passing values into a procedure from NextButtonClicked method Inno Setup


Currently I'm trying to make an installer utility that sets the static IP for the Wi-Fi on Inno Setup. I'm having trouble finding a way to make the correct command insert when the Next button of custom page is pressed. The NextButtonClick method is called correctly however when I actually hit the next button on my page it merely exits without executing the query.

procedure InitializeWizard();
var
  Page: TInputQueryWizardPage;
  ipAddress, subnetMask, defaultGateway, prefferredDNSServer: String;
  ResultCode: Integer;
begin
  Page := CreateInputQueryPage(wpWelcome,
    'Set Network Config', 'A window for setting the wifi configuration',
    'Please indicate the IP address and press next when finished.');

  { Add items (False means it's not a password edit) }
  Page.Add('IP Address:', False);
  Page.Add('Subnet mask:', False);
  Page.Add('Default gateway:', False);
  Page.Add('Preferred DNS server:', False);

  { Set initial values (optional) }
  Page.Values[0] := ExpandConstant('0.0.0.0');

  ipAddress := Page.Values[0]

  Page.Values[1] := ExpandConstant('0.0.0.0');

  subnetMask :=  Page.Values[1]

  Page.Values[2] := ExpandConstant('0.0.0.0');

  defaultGateway :=  Page.Values[2]

  Page.Values[3] := ExpandConstant('0.0.0.0');

  prefferredDNSServer :=  Page.Values[3]

  if NextButtonClick(Page.ID) then
  begin
    Exec('cmd.exe',
         '/k ' + 'netsh interface ip set address "Wi-Fi" static ' + ipAddress + ' ' +
           subnetMask + ' ' + defaultGateway + ' ' + prefferredDNSServer,
         '', SW_SHOW, ewWaitUntilTerminated, ResultCode)
  end;
end; 

function NextButtonClick(CurPageID: Integer): Boolean;
var
  ipAddress, subnetMask, defaultGateway, prefferredDNSServer: String;
  ResultCode: Integer;
begin
  Result := True

  Log('NextButtonClick(' + IntToStr(CurPageID) + ') called');
  case CurPageID of
    100:
      { ipAddress := getParams(ipAddress); }
      { setWifi(ipAddress, subnetMask, defaultGateway, prefferredDNSServer); }
      Result:= True;
  end;
end;

Solution

  • You do not call the NextButtonClick function yourself. It's an event function, so it's called by Inno Setup.

    The code should be like:

    var
      NetPage: TInputQueryWizardPage;
    
    procedure InitializeWizard();
    begin
      NetPage :=
        CreateInputQueryPage(wpWelcome,
          'Set Network Config', 'A window for setting the wifi configuration',
          'Please indicate the IP address and press next when finished.');
    
      { Add items (False means it's not a password edit) }
      NetPage.Add('IP Address:', False);
      NetPage.Add('Subnet mask:', False);
      NetPage.Add('Default gateway:', False);
      NetPage.Add('Preferred DNS server:', False);
    
      { Set initial values (optional) }
      NetPage.Values[0] := ExpandConstant('0.0.0.0');
      NetPage.Values[1] := ExpandConstant('0.0.0.0');
      NetPage.Values[2] := ExpandConstant('0.0.0.0');
      NetPage.Values[3] := ExpandConstant('0.0.0.0');
    end; 
    
    function NextButtonClick(CurPageID: Integer): Boolean;
    var
      ipAddress, subnetMask, defaultGateway, prefferredDNSServer: String;
      ResultCode: Integer;
      Command: string;
    begin
      Result := True;
      
      if CurPageID = NetPage.ID then
      begin
        ipAddress := NetPage.Values[0];
        subnetMask := NetPage.Values[1];
        defaultGateway := NetPage.Values[2];
        prefferredDNSServer := NetPage.Values[3];
       
        Command :=
          'netsh interface ip set address "Wi-Fi" static ' +
          ipAddress + ' ' + subnetMask + ' ' + defaultGateway + ' ' + prefferredDNSServer;
        Exec('cmd.exe', '/C ' + Command, '', SW_SHOW, ewWaitUntilTerminated, ResultCode)
      end;
    end;
    

    Though, in general, you should do any changes to the target system, until the user confirms the installation by clicking "Install" on the "Ready to Install" page.

    While you can use CurPageID = wpReady for that, a more idiomatic approach is using CurStepChanged(ssInstall) (or ssPostInstall):

    procedure CurStepChanged(CurStep: TSetupStep);
    var
      ipAddress, subnetMask, defaultGateway, prefferredDNSServer: String;
      ResultCode: Integer;
      Command: string;
    begin
      if CurStep = ssInstall then
      begin
        ipAddress := NetPage.Values[0];
        subnetMask := NetPage.Values[1];
        defaultGateway := NetPage.Values[2];
        prefferredDNSServer := NetPage.Values[3];
       
        Command :=
          'netsh interface ip set address "Wi-Fi" static ' +
          ipAddress + ' ' + subnetMask + ' ' + defaultGateway + ' ' + prefferredDNSServer;
        Exec('cmd.exe', '/C ' + Command, '', SW_SHOW, ewWaitUntilTerminated, ResultCode)
      end;
    end;