Search code examples
windowsinstallationinno-setuppascalscript

Write serial number to file using Inno Setup


I am trying to get the following Inno Setup code to work:

[Setup]
UserInfoPage=yes

[Code]
function CheckSerial(Serial: String): Boolean;
begin
  Result := true;
  SaveStringToFile('c:\Registration.txt', Serial, False);
end;

The code is extremely simple when the file path is known at UserInfoPage. However it becomes extraordinarily complex when I need to write this file next to my application. Neither:

WizardDirValue();

nor

ExpandConstant('{app}');

do work. The first one is empty when called too early and the second one does not even run, I get:

Internal error: an attempt was made to expand the "app" constant before it was initialized.

How does one store the Serial value to a file which needs to resides next to the application (.exe file) ?


Solution

  • You can expand the {userinfoserial} constant to get the serial number that the user entered in the info page in some event fired after the application directory is chosen, e.g.:

    [Setup]
    AppName=My Program
    AppVersion=1.5
    UserInfoPage=yes
    DefaultDirName={pf}\My Program
    
    [Code]
    function CheckSerial(Serial: String): Boolean;
    begin
      Result := True;
    end;
    
    procedure CurStepChanged(CurStep: TSetupStep);
    begin
      if CurStep = ssPostInstall then
      begin
        SaveStringToFile(ExpandConstant('{app}\Serial.txt'),
          ExpandConstant('{userinfoserial}'), False);
      end;
    end;