QB CULT MAGAZINE
Issue #9 - February 2001

Appending Data To Your .EXEs

By Chris Charabaruk <evilbeaver@tekscode.com>

Sometimes, you'll want to have certain data for your program that you don't want to put into a seperate data file (configuration info, CRC signiture for the program part of the executable or other files, etc.). There is a method to check data at the end of your program's .exe file, and to write to it too.

Getting The EXE Header

The first step in the process is to get the EXE header so you know where the program ends in your executable. Of course you'll need to know the actual name of the executable in case someone renamed it, check my article of tips and tricks in issue 8.

The executable header type, in QuickBasic, would look as so:

TYPE EXEHEADER
    exSignature AS INTEGER
    exExtraBytes AS INTEGER ' Number of bytes in last page.
    exPages AS INTEGER      ' Number of whole and part pages.
    exRelocItems AS INTEGER
    exHeaderSize AS INTEGER
    exMinAlloc AS INTEGER
    exMaxAlloc AS INTEGER
    exInitSS AS INTEGER
    exInitSP AS INTEGER
    exCheckSum AS INTEGER
    exInitIP AS INTEGER
    exInitCS AS INTEGER
    exRelocTable AS INTEGER
    exOverlay AS INTEGER
END TYPE

The two entries exExtraBytes and exPages let you find where the end of executable code is, and where dataspace begins, using this formula:

IF exeheadblock.exPages MOD 512 <> 0 THEN
    size = exeheadblock.exPages * 512& - (512& - exeheadblock.exExtraBytes)
  ELSE  ' If no partial pages then execute the ELSE statement.
    size = exeheadblock.exPages * 512&
END IF

Note that exeheadblock is of type EXEHEADER. We extract the header from the program with this juicy tidbit of code:

DIM exeheadblock AS EXEHEADER
OPEN "test.exe" FOR BINARY AS #1
GET #1, , exeheadblock  ' This puts the executable header in
                        ' to the structure exeheadblock.

Don't forget to replace test.exe with the name of your program. Like I mentioned before, take a look at the tip in issue 8 about getting file and path name. If you don't want the program to crash if it's been renamed, you'll have to.

Now, to finally read and write to your program, start at size + 1, and GET or PUT all you need. You can store text, bsaved images, even other files. If I understood Descent's HOG format, I would make a HOG SFX module by now. I hope this article can help you out one day!