I am trying to read a file into a byte array in Delphi XE2.
This is my current code:
function FileToBytes(const AName: string; var Bytes: TBytes): Boolean;
var
Ms: TMemoryStream;
begin
Result := False;
if not FileExists(AName) then
Exit;
Ms := TMemoryStream.Create;
try
Ms.LoadFromFile(AName);
if Ms.Size > 0 then
begin
Ms.Position := 0;
MS.ReadBuffer(Bytes[0], Ms.Size);
Result := True;
end;
finally
Ms.Free;
end;
end;
procedure runFile();
var
Bytes: TBytes;
OpFile: String;
begin
OpFile := 'C:\Users\Kenny\Documents\calc.exe';
Bytes := nil;
if FileToBytes(OpFile, Bytes) then
begin
//do someting with Bytes(array of Byte)
end;
end;
I am getting an error at this line:
MS.ReadBuffer(Bytes[0], Ms.Size);
The error is:
access violation at 0x00404727: write of address 0x00000008
Any help in solving this would be greatly appreciated.