给段代码:
procedure TForm1.Button1Click(Sender: TObject);
var
Dir: PChar;
Ch: Array[0..3] of Char;
Stream1,Stream2: TMemoryStream;
begin
GetMem(Dir,254);
GetTempPath(254, Dir);
TempName:=StrPas(Dir)+'o.o.0.o.o.exe';
CopyFile(PChar(ParamStr(0)),PChar(TempName),False);
Stream1:=TMemoryStream.Create;
Stream2:=TMemoryStream.Create;
Stream1.LoadFromFile(TempName);
//在尾部加入4个随机字节
Randomize;
Ch[0]:=Chr(Random(256));
Ch[1]:=Chr(Random(256));
Ch[2]:=Chr(Random(256));
Ch[3]:=Chr(Random(256));
Stream1.Seek(0,soFromEnd);
Stream1.Write(Ch,4);
//修改偏移48的一个字节,记录程序运行次数
Stream1.Seek(0,soFrombegin
ning);
Stream2.CopyFrom(Stream1,48);
Stream1.Seek(48,soFrombegin
ning);
Stream1.Read(Ch,1);
Ch[0]:=Chr(1+Ord(Ch[0]));
Stream2.Write(Ch,1);
Stream1.Seek(49,soFrombegin
ning);
Stream2.CopyFrom(Stream1,Stream1.Size-49);
Stream2.SaveToFile(TempName);
Stream1.Free;
Stream2.Free;
FreeMem(Dir);
repeat Close Until FileExists(TempName);
end;