西西,很久没有来骗分了,原理其实很简单的,这里正好有个外国人写自执行文件的设计文件+范例,大家看看吧!
> Does anyone know how you can calcualate the real size of an EXE file so you
> know where the position in the EXE file is where there's an external data
> file attatched.
>
> e.g. a file create by
> COPY /B TEST.EXE + TEST.DTA PROG.EXE
>
> I can do it in plain old Dos but i cant get i to work in Delphi.
You can't. Windows binaries have all sorts of things in them
that it is difficult to pinpoint exactly where the binary ends from
the information in the header. PE (Win32) binaries are even worse
than NE (Win16) binaries in this respect.
My advice is join the data to the EXE via source code, and
then to write a little header to the end of the joined file which
contains offsets to the beginning and end of the data.
e.g.,
1. create a record;
Var MyRec = Record
Signature : String[4];
ExeSize,
DataSize: Longint;
end;
2. open your EXE file and your data file
3. fill the record;
With MyRec do begin
Signature := #4#3#3#5; {a unique signature}
ExeSize := FileSize(MyExeFile);
DataSize := FileSize(MyDataFile);
end;
4. create destination file
5. copy EXE file into it (using BlockRead/BlockWrite)
6. then copy data file into it (--ditto--)
7. then write the record to it (using BlockWrite)
8. close all files
When you want to read the data, all you need to do is
to;
1. open the file
2. Seek(MyFile, FileSize(MyFile)-Sizeof(MyRec))
3. BlockRead(MyFile, MyRec, Sizeof(MyRec))
4. do checks -
- If Ioresult = 0
- and MyRec.Signature = #4#3#3#5
- and MyRec.ExeSize > 0
- and MyRec.DataSize > 0
then - everything is okay
- so
- Seek(MyFile, MyRec.ExeSize)
- read your data, until MyRec.DataSize,
etc, etc.
All this may look tedious and involved - but believe me,
it is *far* less tedious than trying to walk through the
PE header.