How to Append Data to the End of an .EXE File

Article translations Article translations
Article ID: 84062 - View products that this article applies to.
This article was previously published under Q84062
Expand all | Collapse all

SUMMARY

Appending data to the end of an executable file may be useful when you need to include data with your program but do not want to have extra data files. The method shown in this article involves reading the executable file header to determine the program image (the actual size of the executable part of the file) at run time to determine the position of the data at the end of the executable file. Once the position of the data has been determined, the executable file can OPEN itself for BINARY access and read the data.

Although this method has been tested in limited use with Microsoft Basic products, it is not officially supported or guaranteed by Microsoft, and has not been extensively tested.

This information applies to Microsoft QuickBasic version 4.5 for MS-DOS and to Microsoft Basic PDS versions 7.0 and 7.1 for MS-DOS.

MORE INFORMATION

The first step in this process is to get the executable header information. This is documented in the "Microsoft MS-DOS Programmers Reference" (published by Microsoft Press). The following is the format of an executable header as defined by a QuickBasic program:
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 fields exExtraBytes and exPages can be used to compute the size of the image area of the executable, and thus the beginning of the data area.

The following is a small program that will create a data file containing a graphics image created with the PRINT and LINE statements in Basic:
SCREEN 12
COLOR 1          'Select Blue for the color
DIM box%(1 TO 1792) 'Dimension an array big enough for the GET
                    'statement

LOCATE 2, 2      'Locate the "Hello" text
PRINT "Hello";
LINE (1, 15)-(55, 30), , B 'Draw a box
GET (1, 1)-(55, 30), box%  'Get the box
OPEN "DATAFILE" FOR BINARY AS #1
FOR i% = 1 TO 1792

     PUT #1, , box%(i%) 'Save image to disk

NEXT i%
CLOSE #1
END
				
The next example should be compiled and have the data file from the previous program appended on the end of the executable. Type in the following program, and save it with the name TEST.BAS:
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

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

CLS
SCREEN 12
DIM box%(1 TO 1792)
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

GET #1, size + 1, box%(1) 'Read each element of the array
FOR i% = 2 TO 1792
    GET #1, , box%(i%)

NEXT i%
CLOSE #1
PUT (100, 100), box%      'Put the graphics image to the screen
SLEEP
END
				
After compiling this program into TEST.EXE, use the MS-DOS TYPE command to append the data file to the end of the executable as follows:
type datafile >> test.exe
Run the program and you will see the image that was created in first program displayed in the second program.

For more information about the format used by the graphics PUT and GET statements to store images into an array, query on the following words in the Microsoft Knowledge Base:
get and put and array and graphics and pixels and image

Properties

Article ID: 84062 - Last Review: August 16, 2005 - Revision: 2.1
APPLIES TO
  • Microsoft QuickBasic 4.5 for MS-DOS
  • Microsoft BASIC Professional Development System 7.0
  • Microsoft BASIC Professional Development System 7.1
Keywords: 
KB84062
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com