读取数据调用代码:
procedure TForm1.FormCreate(Sender: TObject);
var
s:TExeStream;
a:Char;
k:Byte;
begin
s:=TExeStream.Create(Application.ExeName,True);
repeat
k:=s.Read(a,1);
if k<>0 then Memo1.Text:=Memo1.Text+a;
until k=0;
s.Free;
end;
写数据调用代码:
procedure TForm1.Button1Click(Sender: TObject);
var
s:TExeStream;
a:Char;
j:Integer;
begin
if not SaveD.Execute then Exit;
if UpperCase(ExtractFileExt(SaveD.FileName))<>'.EXE' then
SaveD.FileName:=SaveD.FileName+'.exe';
CopyFile(PChar(ExtractFilePath(Application.ExeName)+'Project2.exe'),PChar(SaveD.FileName),False);
s:=TExeStream.Create(SaveD.FileName,False);
j:=1;
while j<=Length(Memo1.Text) do
begin
a:=Memo1.Lines.Text[j];
s.Write(a,1);
Inc(j);
end;
ShowMessage(IntToStr(s.Size)+' bytes saved.');
s.Free;
end;
EXE尾部数据读写类:
unit ExeStr;
interface
uses Classes, SysUtils;
type
TExeStream=class(TStream)
private
fStart:Integer;
fMark:Cardinal;
fStream:TFileStream;
fReadOnly:Boolean;
protected
//procedure SetSize(NewSize: Longint); override;
public
constructor Create(const FileName:TFileName;ReadOnly:Boolean);
destructor Destroy; override;
function Read(var Buffer;Count:Longint):Longint; override;
function Write(const Buffer;Count:Longint):Longint; override;
function Seek(const Offset:Int64;Origin:TSeekOrigin):Int64; override;
end;
implementation
constructor TExeStream.Create(const FileName:TFileName;ReadOnly:Boolean);
var
f:Word;
c:Cardinal;
begin
inherited Create;
fMark:=1349469005;
fReadOnly:=ReadOnly;
if ReadOnly then f:=fmOpenRead or fmShareDenyWrite else f:=fmOpenReadWrite;
fStream:=TFileStream.Create(FileName,f);
fStream.Seek(-4,soEnd);
fStream.Read(c,4);
if c=fMark then
begin
fStream.Seek(-8,soEnd);
fStream.Read(c,4);
fStart:=fStream.Size-c-8;
fStream.Seek(fStart,soBeginning);
end
else if ReadOnly then
raise EStreamError.Create(FileName+' does not contains any data written by TExeStream.')
else
begin
fStart:=fStream.Size;
c:=0;
fStream.Write(c,4);
fStream.Write(fMark,4);
fStream.Seek(fStart,soBeginning);
end;
end;
destructor TExeStream.Destroy;
begin
fStream.Free;
inherited Destroy;
end;
function TExeStream.Read(var Buffer;Count:Longint):Longint;
var k:Longint;
begin
if Count+fStream.Position>fStream.Size-8 then k:=fStream.Size-fStream.Position-8 else k:=Count;
if k<=0 then
Result:=0
else
Result:=fStream.Read(Buffer,k);
end;
function TExeStream.Write(const Buffer;Count:Longint):Longint;
var
k:Longint;
j:Cardinal;
begin
if fReadOnly then
raise EStreamError.Create('Can not write data to readonly stream.')
else
begin
Result:=fStream.Write(Buffer,Count);
k:=fStream.Position;
if k>fStream.Size-8 then
begin
j:=k-fStart;
fStream.Write(j,4);
fStream.Write(fMark,4);
fStream.Seek(k,soBeginning);
end;
end;
end;
function TExeStream.Seek(const Offset:Int64;Origin:TSeekOrigin):Int64;
begin
case Origin of
soBeginning:fStream.Seek(fStart,soBeginning);
soEnd:fStream.Seek(-8,soEnd);
end;
if Offset+fStream.Position-fStart<0 then
Result:=fStream.Seek(fStart,soBeginning)-fStart
else if Offset+fStream.Position>fStream.Size-8 then
Result:=fStream.Seek(-8,soEnd)-fStart
else
Result:=fStream.Seek(Offset,soCurrent)-fStart;
end;
end.