inno-setuppascalscriptinno-setup-v6

How to change LoadStringFromFile function from Inno Setup 5 to Inno Setup 6 and make it work


I am trying to convert my code from Inno Setup 5 to 6. I can't make the following code work.

My old working code was:

procedure CurStepChanged(CurStep: TSetupStep);
var
  FileData: String;
begin
  if (CurStep = ssInstall) then
  begin
    LoadStringFromFile(ExpandConstant('{code:GetSvcDir|2}\PostSteps.ps1'), FileData);
    StringChange(FileData, 'ScvProdPath', ExpandConstant('{code:GetSvcDir|0}\'));
    StringChange(FileData, 'ProdSitePath', ExpandConstant('{code:GetWebDir|0}\'));
    StringChange(FileData, 'ProdAuthPath', ExpandConstant('{code:GetWebDir|1}\'));
    StringChange(FileData, '444', ExpandConstant('{code:GetConfig|4}'));
    StringChange(FileData, '8732', ExpandConstant('{code:GetConfig|3}'));
    StringChange(FileData, 'RV_ExceptionsPath', ExpandConstant('{code:GetSvcDir|2}\RVExceptions'));
    SaveStringToFile(ExpandConstant('{code:GetSvcDir|2}\PostSteps.ps1'), FileData, False);
  end;
end;

I tried to change it after read some articles like this, but i can't make it work.

procedure CurStepChanged(CurStep: TSetupStep);
var
  FileData: AnsiString;
  UnicodeStr: string;
begin
  if (CurStep = ssInstall) then
  begin
    LoadStringFromFile(ExpandConstant('{code:GetSvcDir|2}\PostSteps.ps1'), FileData);
    StringChangeEx(UnicodeStr, 'ScvProdPath', ExpandConstant('{code:GetSvcDir|0}\'), True);
    StringChangeEx(UnicodeStr, 'ProdSitePath', ExpandConstant('{code:GetWebDir|0}\'), True);
    StringChangeEx(UnicodeStr, 'ProdAuthPath', ExpandConstant('{code:GetWebDir|1}\'), True);
    StringChangeEx(UnicodeStr, '444', ExpandConstant('{code:GetConfig|4}'), True);
    StringChangeEx(UnicodeStr, '8732', ExpandConstant('{code:GetConfig|3}'), True);
    StringChangeEx(UnicodeStr, 'RV_ExceptionsPath', ExpandConstant('{code:GetSvcDir|2}\RVExceptions'), True);
    SaveStringToFile(ExpandConstant('{code:GetSvcDir|2}\PostSteps.ps1'), FileData, False);
  end;
end;

Text inside file is English, not something special. Could you please help me?


Solution

  • As I have commented to your previous question, this is not about upgrading from Inno Setup 5 to Inno Setup 6. Inno Setup 6 is compatible with Inno Setup 5 code. Your problem is that you have used legacy Ansi version of Inno Setup 5. And Inno Setup 6 has Unicode version only. You should have used Unicode version even with Inno Setup 5. See Upgrading from Ansi to Unicode version of Inno Setup (any disadvantages)

    In general, you should not use AnsiString. It's a legacy of the Ansi version. With AnsiString, your code will fail, if a user uses non-ASCII characters in the the paths.

    So in the end, your question is more or less duplicate of:
    Inno Setup replace a string in a UTF-8 file without BOM


    Anyway, to answer your literal question: You never assign the UnicodeStr variable.

    Also as with your previous question: Do not use ExpandConstant to call a function.

    procedure CurStepChanged(CurStep: TSetupStep);
    var
      FileData: AnsiString;
      UnicodeStr: string;
    begin
      if (CurStep = ssInstall) then
      begin
        LoadStringFromFile(GetSvcDir('2') + '\PostSteps.ps1', FileData);
        UnicodeStr := FileData;
        StringChangeEx(UnicodeStr, 'ScvProdPath', GetSvcDir('0') + '\', True);
        StringChangeEx(UnicodeStr, 'ProdSitePath', GetWebDir('0') + '\', True);
        StringChangeEx(UnicodeStr, 'ProdAuthPath', GetWebDir('1') + '\', True);
        StringChangeEx(UnicodeStr, '444', GetConfig('4'), True);
        StringChangeEx(UnicodeStr, '8732', GetConfig('3'), True);
        StringChangeEx(UnicodeStr, 'RV_ExceptionsPath', GetSvcDir('2') + '\RVExceptions', True);
        FileData := UnicodeStr;
        SaveStringToFile(GetSvcDir('2') + '\PostSteps.ps1', FileData, False);
      end;
    end;
    

    (untested)

    And you probably do not even need the functions then – you can inline them to the above code, unless you use them elsewhere.

    You will also find AddBackslash function useful.