HMM

 BBS: Inland Empire Archive
Date: 05-12-92 (21:51)             Number: 18
From: DOUGLAS LUSHER               Refer#: NONE
  To: RICK PEDLEY                   Recvd: NO  
Subj: HMM                            Conf: (2) Quik_Bas
 I'm afraid, Mr. Parisien that someone gave you some bad info a couple
 of days ago in this echo about how to get the length of an .EXE file
 from its header. If you tested what you were given I'm sure you found,
 as I did, that it sometimes gave incorrect results. I am posting to you
 herein (and to anyone else who cares to make use of them) a couple of
 functions that you may find useful if you pursue the idea of appending
 data to the end of the program file that will use the data. One function
 corrects the info that you were given previously, returning the correct
 file size for the .EXE file name passed to it. (At least it gave the
 correct size in all my tests.) The second function returns the name of
 the currently executing program, including the disk drive and complete
 path. If you have appended data to the end of an .EXE file, your program
 must be able to find that file to read the data or write new data. If
 you just embed the name that you intend to give the program in the code,
 you may face some problems. First, a user may rename the file (you know
 those users...), and second, a user may have your program in a subdirectory
 listed in his/her path and run your program while logged into another
 subdirectory - how then would your program "find itself"? The function
 I have provided solves both these problems. Below is a sample program
 demonstrating these functions and how they can be used. My program does
 only string data, but of course data of all types may be appended to
 an .EXE file and manipulated using these techniques.
 Enjoy, everyone.

 DECLARE FUNCTION EXEFileLength& (FileName$)
 DECLARE FUNCTION ProgramName$ ()
 REM $INCLUDE: 'qb.bi'

 DEFINT A-Z
 DIM SHARED Register AS RegType
 CLS

 PN$ = ProgramName$
 L& = EXEFileLength&(PN$)

 Poem$(1) = "Mary had a little lamb"
 Poem$(2) = "  Whose fleece was white as snow,"
 Poem$(3) = "And everywhere that Mary went"
 Poem$(4) = "  The lamb was sure to go."

 OPEN PN$ FOR APPEND AS #1
 FOR Z = 1 TO 4
   PRINT #1, Poem$(Z)
 NEXT
 CLOSE #1

 OPEN PN$ FOR INPUT AS #1
 SEEK #1, L& + 1
 FOR Z = 1 TO 4
  LINE INPUT #1, Z$: PRINT Z$
 NEXT
 CLOSE #1
 PRINT

 TYPE Sample
   em AS STRING * 33
 END TYPE

 DIM Po AS Sample

 Poem$(1) = "Mary had another lamb            "
 Poem$(2) = "  Whose fleece was black as soot,"
 Poem$(3) = "And everywhere that Mary went    "
 Poem$(4) = "  His sooty foot he put.         "

 OPEN PN$ FOR BINARY AS #1
 PUT #1, L& + 1, Poem$(1)
 PUT #1, , Poem$(2)
 PUT #1, , Poem$(3)
 PUT #1, , Poem$(4)
 CLOSE #1

 OPEN PN$ FOR BINARY AS #1
 SEEK #1, L& + 1
 FOR Z = 1 TO 4
   GET #1, , Po: PRINT Po.em
 NEXT
 CLOSE #1

 END

 FUNCTION EXEFileLength& (FileName$) STATIC
   File = FREEFILE
   OPEN FileName$ FOR INPUT AS File
     A$ = INPUT$(6, File)
   CLOSE File
   Pages& = ASC(MID$(A$, 6)) * 256& + ASC(MID$(A$, 5))
   Extra& = ASC(MID$(A$, 4)) * 256& + ASC(MID$(A$, 3))
   IF Extra& THEN Pages& = Pages& - 1
   EXEFileLength& = Pages& * 512& + Extra&
 END FUNCTION

 FUNCTION ProgramName$ STATIC
         'find Program Segment Prefix
   Register.AX = &H6200
   CALL Interrupt(&H21, Register, Register)
         'change to that segment
   DEF SEG = Register.BX
         'from the PSP get the DOS environment segment and change to it
   DEF SEG = PEEK(45) * &H100 + PEEK(44)
         'now scan for two consecutive null bytes
   Ptr = 0
   DO WHILE PEEK(Ptr) OR PEEK(Ptr + 1): Ptr = Ptr + 1: LOOP
         'the program name is stored three bytes beyond the two nulls
   Ptr = Ptr + 4
         'now find the length of the program name string by scanning for the
         'null byte that terminates it
   Length = 0
   DO WHILE PEEK(Ptr + Length): Length = Length + 1: LOOP
         'pull out the program name string and exit
   Temp$ = SPACE$(Length)
   FOR X = 1 TO Length
     MID$(Temp$, X) = CHR$(PEEK(Ptr + X - 1))
   NEXT
   DEF SEG
   ProgramName$ = Temp$: Temp$ = ""
 END FUNCTION


--- TMail v1.28
 * Origin: TC-AMS MLTBBS 2.2 - Minnetonka, MN (612)-938-4799 (1:282/7)
Outer Court
Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET