This code is running on Delphi XE4 perfectly well:
var
b: byte;
fl: TFileStream;
filename:string;
begin
b:= $2F;
filename:='C:\test.exe';
fl:= tFileStream.Create(filename, 0,fmOpenReadWrite);
fl.Position:= $C;
fl.WriteBuffer(b,sizeof(b));
fl.free
end;
However, when I run exactly the same code on Delphi XE7 on the same PC, it fails with the error "Stream write error".
In the
TFileStreamconstructor, you are setting theModeparameter to 0 (fmOpenRead) and theRightsparameter tofmOpenReadWrite. You need to swap them:Or simply:
When the
fmCreateflag is not present in theModeparameter,TFileStreamcallsFileOpen()instead ofFileCreate().In XE4, the
ModeandRightsparameters areOR'ed together whenTFileStreamcallsFileOpen()on Windows:That is why your code works in XE4. You are opening the file in a read/write mode.
In XE7, the
Rightsparameter is ignored whenTFileStreamcallsFileOpen()on every platform:That is why your code does not work in XE7. You are opening the file in a read-only mode.