Inno Setup Pascal Script - Reading UTF-16 file

前端 未结 2 1313
伪装坚强ぢ
伪装坚强ぢ 2020-12-06 14:17

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

EXTRALARGELEGENDSII_INI TEXTFILE \"Data.bin\"

LARGEFONTSL         


        
2条回答
  •  [愿得一人]
    2020-12-06 14:50

    The file is in the UTF-16 LE encoding.

    The LoadStringFromFile does not support any Unicode encoding. It loads the file as is, to a byte array (the AnsiString is effectively used as a byte array).

    As the Unicode string (in Unicode version of Inno Setup) actually uses the UTF-16 LE encoding, all you need to do is to copy the byte array bit-wise to the (Unicode) string. And trim the UTF-16 LE BOM (FEFF).

    procedure RtlMoveMemory(Dest: string; Source: PAnsiChar; Len: Integer);
      external 'RtlMoveMemory@kernel32.dll stdcall';
    
    function LoadStringFromUTF16LEFile(FileName: string; var S: string): Boolean;
    var
      A: AnsiString;
    begin
      Result := LoadStringFromFile(FileName, A);
      if Result then
      begin
        SetLength(S, Length(A) div 2);
        RtlMoveMemory(S, A, Length(S) * 2);
        { Trim BOM, if any }
        if (Length(S) >= 1) and (Ord(S[1]) = $FEFF) then
          Delete(S, 1, 1);
      end;
    end;
    

    See also Inno Setup Reading file in Ansi and Unicode encoding.

提交回复
热议问题