[net.micro.pc] More on the EXEC function call

starr@shell.UUCP (Bob Starr) (02/12/85)

~r submit.02

starr@shell.UUCP (Bob Starr) (02/12/85)

I am writing a program which must be able to execute another
program from within my program, based upon a user supplied
command.
 
Using DOS function 4B works fine. For a while, I was merely
using it to invoke a second copy of COMMAND.COM (god, I wish
MicroSoft had seperated the code & data segments so this wasn't
necessary). Anyhow, if I use function 4B to invoke a second
COMMAND.COM, my specified command can use redirection.
 
BUT, I decided I would try to eliminate the overhead of loading
COMMAND.COM when a program was being invoked. Well, suprise! If
you run function 4B to load a program which has redirection on
the command line, it doesn't work!
 
Could someone please tell me how I can inform DOS apriori that I
wish all standard output (or input) to be redirected? Is IOCTL the
way to go? If so, how is it done?
 
Any help would be much appreciated. Thanks.
 
Bob Starr @ Shell Development

roy@gitpyr.UUCP (Roy J. Mongiovi) (02/15/85)

> Could someone please tell me how I can inform DOS apriori that I
> wish all standard output (or input) to be redirected? Is IOCTL the
> way to go? If so, how is it done?

To redirect stdin, you close handle 0 (which is standard input),
and then open the file you want to be stdin.  That file is allocated
the first free file handle (in this case 0 since we just freed it),
and voila! stdin is redirected.  Likewise for stdout and handle 1.

Then you load and exec your program.  To get stdin/stdout back to
the console, you can dup handle 2 (stderr) unless you have changed
it also (in which case you can open "con:").
-- 
Roy J. Mongiovi.	Office of Computing Services.		User Services.
Georgia Institute of Technology.	Atlanta GA  30332.	(404) 894-6163
 ...!{akgua, allegra, amd, hplabs, ihnp4, masscomp, ut-ngp}!gatech!gitpyr!roy

	  Who me?  I'm not even a REAL modo, I'm only a quasi-modo.

jchapman@watcgl.UUCP (john chapman) (02/15/85)

> I am writing a program which must be able to execute another
> program from within my program, based upon a user supplied
> command.
>  
> Using DOS function 4B works fine. For a while, I was merely
> using it to invoke a second copy of COMMAND.COM (god, I wish
> MicroSoft had seperated the code & data segments so this wasn't
> necessary). Anyhow, if I use function 4B to invoke a second
> COMMAND.COM, my specified command can use redirection.
>  
> BUT, I decided I would try to eliminate the overhead of loading
> COMMAND.COM when a program was being invoked. Well, suprise! If
> you run function 4B to load a program which has redirection on
> the command line, it doesn't work!
>  
> Could someone please tell me how I can inform DOS apriori that I
> wish all standard output (or input) to be redirected? Is IOCTL the
> way to go? If so, how is it done?
>  
> Any help would be much appreciated. Thanks.
>  
> Bob Starr @ Shell Development
  
  I too was wanting to redirect io for an invoked command; this
  was mentioned in only one place in my MS-DOS documentation
  something to the effect "you can change the definition of
  stdin & stdout and then invoke a program, this is extremely
  powerful" and then no mention anywhere of how to do it.
  Anyway after a while I figured it out (I think  - I have only
  done minor testing but it seems to work the way you would
  expect); I'm doing this from memory so I can't give you the
  function numbers but they are in the doc.  Sooo, if you want
  to redirect stdin to file XXX do something like the following:
 
   1. open a file handle to XXX
   2. do a force duplicate file handle (force duplicate not
      just duplicate file handle), setting cx=1 just before.
      For this call cx is the number of the new file handle
      to be the duplicate. 
   3. close the original file handle to XXX
   4. EXEC your program.
 
 The mapping of handles to devices seems to be:
 Handle     Device/File
  1 		stdin
  2		stdout
  3		auxin
  4		auxout
  5		stdlst
 
 Some points to note:
 1. this requires a free file handle to do plus the five    
     handles above so your config.sys FILES=n should 
     have n=6+number of other simultaneously open files
 2. this affects not only i/o from the handles 1-5 but the
    device specific function calls to con:,prn: etc (functions
    1-9).
 
 Hope this is what you wanted!
 
 John Chapman
 ...!watmath!watcgl!jchapman

jchapman@watcgl.UUCP (john chapman) (02/15/85)

 a p.s. to my previous followup
 
 It sounds like you are expecting msdos to parse the command
 line which you pass to it - it doesn't, the only use is to
 put the image of the line where the program being invoked
 expects to find it (80h in the program segment), that is
 why you also have to explicitly give the name of the program
 to be executed as well.

cjn@druxm.UUCP (NetterCJ) (02/19/85)

John was almost right, the standard MsDos file handles are:

	0 .... Std input device
	1 .... Std output device
	2 .... Std error output device
	3 .... Std auxiliary device (typically com port)
	4 .... Std printer device (lpt1)

Chris Netter
AT&T Information Systems
Denver, Co
druxm!cjn