Search code examples
inno-setuputf-16pascalscript

Reading UTF-16 file in Inno Setup Pascal script


I have an .inf file exported from Resource Hacker. The file is in UTF-16 LE encoding.

EXTRALARGELEGENDSII_INI TEXTFILE "Data.bin"

LARGEFONTSLEGENDSII_INI TEXTFILE "Data_2.bin"

NORMALLEGENDSII_INI TEXTFILE "Data_3.bin"

THEMES_INI TEXTFILE "Data_4.bin" 

When I load it using the LoadStringFromFile function:

procedure LoadResources;
var
  RESOURCE_INFO: AnsiString;
begin
  LoadStringFromFile(ExpandConstant('{tmp}\SKINRESOURCE - INFO.inf'), RESOURCE_INFO);
  Log(String(RESOURCE_INFO));
end;

I am getting this in the Debug Output:

E

Please tell me how to fix this issue.

Thanks in advance.


Solution

  • It seems the file you are trying to log is a Windows Unicode (UTF-16LE) Encoded text file.

    You can use iConv Command Line and convert your file to Windows UTF-8 Encoded File.

    The LoadStringFromFile Support Function does not load Unicode Strings well and it only supports loading ANSI and UTF-8 Encoded Text Files.

    Inno Setup Compiler Debug Output stops logging the Text File because it finds a Character it can't load (NULL) and that's why you're getting only "E" in Compiler Debug Output even LoadStringFromFile loads the text file completely.


    You need to download the Setup Program of the iConv as shown below to get iConv Executable File and some DLLs used to convert between character encoding.

    enter image description here

    After downloaded, install GnuWin32 (LibIconv for Windows) and go to the installation folder.

    Copy following four files inside the sub directory in installation folder called "bin".

    They are:

    libcharset1.dll

    libiconv2.dll

    iconv.exe

    libintl3.dll

    copy these files to the directory where you store files of your Inno Setup Project.

    Then use following code to do the Conversion.

    [Files]
    Source: "libcharset1.dll"; Flags: dontcopy
    Source: "iconv.exe"; Flags: dontcopy
    Source: "libiconv2.dll"; Flags: dontcopy
    Source: "libintl3.dll"; Flags: dontcopy
    
    [Code]
    function InitializeSetup(): Boolean
    var
      ErrorCode: Integer;
    begin
      ExtractTemporaryFile('iconv.exe');
      ExtractTemporaryFile('libcharset1.dll');
      ExtractTemporaryFile('libintl3.dll');
      ExtractTemporaryFile('libiconv2.dll');
      ShellExec('Open', ExpandConstant('CMD.exe'), ExpandConstant('/C iConv -f UTF-16LE -t UTF-8 < SKINRESOURCE-INFO.inf > SKINRESOURCE-INFO-ANSI.inf'), ExpandConstant('{tmp}'), SW_HIDE, ewWaitUntilTerminated, ErrorCode); 
      DeleteFile(ExpandConstant('{tmp}\SKINRESOURCE-INFO.inf')); 
    

    Now LoadStringFromFile should load the text file correctly as now it has the Windows UTF-8 Encoding.

    You may also log it after converting it into a Unicode String like Log(String(RESOURCE_INFO)), if you are using Unicode Inno Setup.