adminysh

[系统应用] 修改自身exe文件大小的范例

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    StatusBar1: TStatusBar;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  TempName: string;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
  Sr: TSearchRec;
  size : Longint;
begin
  FindFirst(ParamStr(0),$27,Sr);
  size:=Sr.Size;
  FindClose(Sr);
  StatusBar1.SimpleText:='本程序大小为 '+IntToStr(size)+' 字节';
end;

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,soFromBeginning);
  Stream2.CopyFrom(Stream1,48);
  Stream1.Seek(48,soFromBeginning);
  Stream1.Read(Ch,1);
  Ch[0]:=Chr(1+Ord(Ch[0]));
  Stream2.Write(Ch,1);
  Stream1.Seek(49,soFromBeginning);
  Stream2.CopyFrom(Stream1,Stream1.Size-49);
  Stream2.SaveToFile(TempName);
  Stream1.Free;
  Stream2.Free;
  FreeMem(Dir);
  repeat Close Until FileExists(TempName);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  WinExec(PChar(TempName+'  '+ParamStr(0)), SW_SHOWNORMAL);
end;

end.

#1楼
发帖时间:2014-2-25   |   查看数:0   |   回复数:0
游客组