[ Usenet FAQs | Web FAQs | Documents | RFC Index ]
    Search the FAQ Archives

Part1 - Part2 - Part3 - Single Page

Top Document: x86 Assembly Language FAQ - General Part 1/3
Previous Document: 7. How To Truncate A File
Next Document: 9. How To Determine The CPU Type


8. How Can STDERR Be Redirected To A File


I understand that 4DOS has this capability at its command line.  If you
are looking in the assembly language FAQ for this information, an
assembly language answer probably is desired.  Here it is.

You will need to write a short program that performs the STDERR
redirection before loading and executing the desired program.  This
loader program relies upon the fact that a child program inherits all
open files of the parent program unless the parent program opens a file
with the inheritance flag set to no.

Because the full code for such a program is too large for this FAQ, I
will give the salient specifications for such a program.

1.  The loader program accepts three command line arguments:
    a.  The full path and filename of the file into which STDERR is to
        be written.
    b.  The full path and filename of the program to be executed.
    c.  The command line for the program to be executed (should be
        delimited by double quotes to allow multiple arguments).  This
        argument is optional.
2.  Release all memory above the program using Int 21 function 4ah so
    that there will be room enough to load and execute the designated
    program.
3.  Open the file from step 1.a above into which STDERR is to be
    written.
4.  Duplicate STDERR filehandle, which is 2, using Int 21h function
    45h.
5.  Using Int 21h function 46h, force STDERR filehandle, again 2, to
    have the filehandle of the opened file from step 2.
6.  Use Int 21h function 4b00h to load and execute the program from
    step 1.a.  Use the default environment and the command line from
    step 1.c above.
7.  Upon return from the function 4b00h, close the file opened in step
    2.
8.  To restore STDERR, use Int 21h function 46h to force STDERR, again
    2, to point to the filehandle saved from step 3 above.

This same technique can be applied to any of the standard devices.

I have written a full featured demonstration program.  I believe that
asm programmers will find the source code useful even if they do not
want to redirect stderr to a file.  The URL to the file is:

    ftp://ftp.simtel.net/pub/simtelnet/msdos/asmutl/stderrf1.zip

Contributor: Raymond Moon, raymoon@moonware.dgsys.com
Last changed: 3 Jun 95



Top Document: x86 Assembly Language FAQ - General Part 1/3
Previous Document: 7. How To Truncate A File
Next Document: 9. How To Determine The CPU Type

Part1 - Part2 - Part3 - Single Page


[ Usenet FAQs | Web FAQs | Documents | RFC Index ]

Send corrections/additions to the FAQ Maintainer:
raymoon@moonware.dgsys.com

Last Update October 22 2009 @ 05:22 AM

Some parts © 2009 Advameg, Inc.