如何使用 Inno Setup 保存带有 BOM 文件的 UTF-16

How to save a UTF-16 with BOM file with Inno Setup

如何将字符串保存到带有 BOM 的 UTF-16 (UCS-2) 编码的文本文件中?

SaveStringsToUTF8File 保存为 UTF-8。

使用流将其保存为 ANSI。

var
  i:integer;
begin
  for i := 1 to length(aString) do begin
    Stream.write(aString[i],1);
    Stream.write(#0,1);
  end;
  stream.free;
end;

作为 Unicode string(在 – the only version as of Inno Setup 6) actually uses the UTF-16 LE encoding, all you need to do is to copy the (Unicode) string to a byte array (AnsiString) bit-wise. And add the UTF-16 LE BOM (FEFF):

procedure RtlMoveMemoryFromStringToPtr(Dest: PAnsiChar; Source: string; Len: Integer);
  external 'RtlMoveMemory@kernel32.dll stdcall';
  
function SaveStringToUFT16LEFile(FileName: string; S: string): Boolean;
var
  A: AnsiString;
begin
  S := #$FEFF + S; 
  SetLength(A, Length(S) * 2);
  RtlMoveMemoryFromStringToPtr(A, S, Length(S) * 2);
  Result := SaveStringToFile(FileName, A, False);
end;

这正好与:相反。