LoadStringFromFile and StringChangeEx from Unicode Inno Setup (Ansi file)
Asked Answered
E

1

14

I'm trying to update one of my scripts to use the Unicode version of Inno Setup. Unfortunately I'm running into a problem where StringChangeEx is expecting to see a unicode String instead of the AnsiString that I need (mismatched type). Is there an alternate way to replace matching AnsiString from Unicode Inno Setup?

I'm using WizardDirValue() to avoid having to type ExpandConstant('{app}\') all the time, in case anyone wonders why.

var
  AnsiData: AnsiString;
begin
  LoadStringFromFile(WizardDirValue() + '\legacyansi.conf', AnsiData)
  // Type mismatch, StringChangeEx expects String which is Unicode
  StringChangeEx(AnsiData, 'current', 'replace', True);
  SaveStringToFile(WizardDirValue() + '\legacyansi.conf', AnsiData)
end;
Epigone answered 3/1, 2014 at 20:53 Comment(1)
The answer my @TLama is a quick solution. But if you actually need to read "Unicode" file, see Inno Setup Reading file in Ansi and Unicode encoding.Throng
U
28

Since parameters of LoadStringFromFile as well as of StringChangeEx functions are declared, they expect the exact type to be passed, so there's not much to do with it. You will need just to declare another variable just for your StringChangeEx function call and typecast between ANSI & Unicode string types:

var
  UnicodeStr: string;
  ANSIStr: AnsiString;
begin
  if LoadStringFromFile('C:\File.txt', ANSIStr) then
  begin
    UnicodeStr := String(ANSIStr);
    if StringChangeEx(UnicodeStr, 'FromStr', 'ToStr', True) > 0 then
      SaveStringToFile('C:\File.txt', AnsiString(UnicodeStr), False);
  end;
end;

Annoying, isn't it ?

Un answered 3/1, 2014 at 21:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.