Path: utzoo!censor!geac!yunexus!maccs!cs4g6ag
From: cs4g6ag@maccs.dcss.mcmaster.ca (Stephen M. Dunn)
Newsgroups: comp.lang.pascal
Subject: Re: Calling alien programs in Turbo Pascal, how?
Summary: it can be done, but it's icky
Keywords: Alien Pascal
Message-ID: <251C5706.20720@maccs.dcss.mcmaster.ca>
Date: 24 Sep 89 04:24:37 GMT
References: <5644@decvax.dec.com>
Reply-To: cs4g6ag@maccs.dcss.mcmaster.ca (Stephen M. Dunn)
Organization: McMaster University, Hamilton, Ontario
Lines: 67


   You can call other programs from Turbo Pascal 3, but it's not the most
pleasant task in the civilized world.  Turbo 3 doesn't have the EXEC
function that Turbo 4 and above includes, so you'll have to use the DOS
function 4Bh.

   To use 4Bh, you must set AL to 0; DS:DX must point to an ASCIIZ string
giving the complete filespec of the program you want to execute (including
the path to it if it isn't in the current directory, and the extension
.COM or .EXE; the EXEC call doesn't handle searching the PATH); ES:BX must
point to the parameter block.

   The parameter block is as follows:

Byte  0- 1:  segment of environment block (if zero, DOS will give the
             program a copy of your program's environment; this is probably
	     quite adequate)
Byte  2- 3:  Offset of command tail
Byte  4- 5:  Segment of command tail (described below)
Byte  6- 7:  Offset of first FCB for new program
Byte  8- 9:  Segment of first FCB for new program
Byte 10-11:  Offset of second FCB for new program
Byte 12-13:  Segment of second FCB for new program

   The command tail is the list of parameters you want to pass to the
program (e.g. a filename and/or some option switches).  It consists of
a count byte, then the list of parameters, then a carriage return (which
is not to be included in the count).

   The two FCBs are a relic of DOS 1.x; most programs in DOS 2 and higher
don't use FCBs.  However, DOS still sets them up just in case the program
expects them (historically, DOS 1 used to parse the first two parameters
and set up FCBs for them, assuming they were filenames; more recent versions
still do this for backward compatibility).  Chances are, the program
you're invoking won't care whether you set these up or not; if it does, you'll
have to use function 29h ... but don't bother with this unless you find
the program complaining.

   The EXEC function clears the carry flag if it worked; otherwise, it
sets the carry flag and returns an error code in AX:

 2:  if file not found or path invalid
 5:  if access denied
 8:  if insufficient memory to load the program
10:  if environment invalid
11:  if format invalid

(note that these numbers are in decimal as I've written them)

   You'll have to determine how much memory your main program needs, and
then go into the Options - COM file section and set the Maximum Dynamic
Memory option (or whatever they call it - essentially, heap + stack).
COM files are given the entirety of memory when they're invoked and unless
you tell your program to give some of this memory back, you won't have any
memory left to run your child process in!

   The information on the EXEC function came from _Advanced MS-DOS_ by
Ray Duncan (Microsoft Press) - an excellent book; similar information can
also be found in _Programmer's Guide to the IBM PC_ by Peter Norton (also
Microsoft Press) - another excellent book.

   Hope this helps!  Feel free to write or post if you have any questions.
-- 
Stephen M. Dunn                         cs4g6ag@maccs.dcss.mcmaster.ca
**********************************************************************
        = "\nI'm only an undergraduate!!!\n";
"VM is like an orgasm:  the less you have to fake, the better." - S.C.