How does one write to a binary file in Inno Setup script? It's a configuration file I want to edit in the PrepareToInstall
step. The problem is that I'm looking at the support functions:
TStream = class(TObject)
function Read(Buffer: String; Count: Longint): Longint;
function Write(Buffer: String; Count: Longint): Longint;
function Seek(Offset: Longint; Origin: Word): Longint;
procedure ReadBuffer(Buffer: String; Count: Longint);
procedure WriteBuffer(Buffer: String; Count: Longint);
function CopyFrom(Source: TStream; Count: Longint): Longint;
property Position: Longint; read write;
property Size: Longint; read write;
end;
And it seems even the most basic write function writes strings. Should I just do it in a batch script?
Consider the
string
in theTStream
interface as a buffer of chars/bytes.With Ansi version of Inno Setup, it's quite easy, as there one byte equals one char.
It's bit more complicated with Unicode version of Inno Setup, where the string is an array of 2-byte chars.
See also Read bytes from file at desired position with Inno Setup.
To convert a hex string to the actual binary data, you can use the
CryptStringToBinary
Windows API function.The following code works both in Ansi and Unicode version of Inno Setup (You should use the Unicode version, if possible).
Use it like: