Search code examples
inno-setuppascalscript

Retrieve values for a combobox from the registry and fill it by overwriting the default value for the second run of the installer


I already have a combobox in a TInputQueryWizardPage page but the problem is I don’t know how to retrieve the selected value from the registry after writing from the first run.

My code for combobox is:

  AuthComboBox := TNewComboBox.Create(ReportPage);
  AuthComboBox.Parent := ReportPage.Edits[1].Parent;
  AuthComboBox.Left := ReportPage.Edits[1].Left;
  AuthComboBox.Top := ReportPage.Edits[1].Top;
  AuthComboBox.Width := ReportPage.Edits[1].Width;
  AuthComboBox.Height := ReportPage.Edits[1].Height;
  AuthComboBox.TabOrder := ReportPage.Edits[1].TabOrder;
  AuthComboBox.Items.Add('Password Authentication');          
  AuthComboBox.Items.Add('Windows Authentication');
  AuthComboBox.ItemIndex := 0;
  { Hide the original edit box }
  ReportPage.PromptLabels[1].FocusControl := AuthComboBox;
  ReportPage.Edits[1].Visible := False;
  AuthComboBox.OnChange := @ComboBoxChange;

Values behind AuthComboBox.Items.Add are:

function GetAuthCombo(Param: String): String;
begin
  case AuthComboBox.ItemIndex of
    0: Result := 'False';
    1: Result := 'True';
  end;
end;

I write them to the registry with the following code:

if (CurStep=ssPostInstall) then 
  begin
     RegWriteStringValue(HKEY_LOCAL_MACHINE, 'Software\RiskValue',
    'ReportProdAuthType', ExpandConstant('{code:GetAuthCombo}'));
  end;

If I choose the second choice Windows Authentication from combobox I expect to have the same value (Windows Authentication) as default value now when I run the installer for the second time.


Solution

  • Replace this:

      AuthComboBox.ItemIndex := 0;
    

    with:

    var
      S: string;
    begin
      { ... }
      if RegQueryStringValue(HKLM, 'Software\RiskValue', 'ReportProdAuthType', S) and
         SameText(S, 'True') then
      begin
        AuthComboBox.ItemIndex := 1;
      end
        else
      begin
        AuthComboBox.ItemIndex := 0;
      end;
      { ... }
    end;
    

    Also the use of ExpandConstant to get the value for the registry key is over engineered.

    Either use it from [Registry] section (what scripted constants are intended for):

    [Registry]
    Root: HKLM; Subkey: "Software\RiskValue"; ValueType: string; \
        ValueName: "ReportProdAuthType"; ValueData: "{code:GetAuthCombo}"
    

    Or, if you want to use Pascal Script, use GetAuthCombo directly:

    if (CurStep=ssPostInstall) then 
    begin
      RegWriteStringValue(HKEY_LOCAL_MACHINE, 'Software\RiskValue',
        'ReportProdAuthType', GetAuthCombo(''));
    end;
    

    Then you can even remove the Param: String, or actually even inline the GetAuthCombo function completely, unless you use it elsewhere.

    var
      S: string;
    begin
      { ... }
      if (CurStep=ssPostInstall) then 
      begin
        case AuthComboBox.ItemIndex of
          0: S := 'False';
          1: S := 'True';
        end;
        RegWriteStringValue(HKEY_LOCAL_MACHINE, 'Software\RiskValue', 'ReportProdAuthType', S);
      end;
    end;