Microsoft KB Archive/58437

From BetaArchive Wiki
< Microsoft KB Archive
Revision as of 14:15, 21 July 2020 by X010 (talk | contribs) (Text replacement - "&" to "&")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Accessing Child Process Exit Code from 16-Bit Parent Process

Q58437



The information in this article applies to:


  • The C Run-Time (CRT), included with:
    • Microsoft C for MS-DOS, versions 5.1, 6.0, 6.0a, and 6.0ax
    • Microsoft C/C++ for MS-DOS, version 7.0
    • Microsoft Visual C++, versions 1.0, 1.5, 1.51, 1.52





SUMMARY

In the Microsoft C Compiler, there is no C run-time function that will return the system exit code from a child process. However, Interrupt 21h SubFunction 4Dh can be used to return it.



MORE INFORMATION

Immediately after a child process terminates, the child exit code and the system exit code are in the AL and AH registers, respectively. For example:

                    _________________________
                    |           |           |
      AX Register   |     |     |     |     |
                    |           |           |
                    -------------------------
                         AH     |    AL
                         /            \ 
                        /              \ 
         System Exit Code               Child Exit Code 

When the spawn() family of functions is called with a mode flag of P_WAIT, only the child exit code is returned. To read the system exit code, a call to Interrupt 21h SubFunction 4Dh is needed.

It is important to get and store the return codes immediately upon returning from the child process, because another function may modify them.

The following code samples demonstrate how to get the exit code from the child process within the parent process with C.

Sample Code 1

/* Compile options needed: none
*/ 

/* Call this prog1.c  */ 
#include <dos.h>
#include <stdio.h>
#include <process.h>

union REGS inregs, outregs, tempreg;
int retcode;
unsigned char syscode;
void main (void)
{
   printf ("In program I\n");
   retcode = spawnl (P_WAIT, "sp2.exe", "sp2", NULL);

   /* Call int 21h function 4Dh to obtain exit codes */ 

   inregs.h.ah = 0x4d;
   intdos (&inregs, &outregs);

   /* System Exit Code will be in AH. */ 

   syscode = outregs.h.ah;

   printf ("Child exit code: %d\n", retcode);
   printf ("Child process ");
   switch (syscode)
   {
      case 0 : printf ("terminated normally\n");
               break;
      case 1 : printf ("exit with a Control-C\n");
               break;
      case 2 : printf ("exit with a hard error\n");
               break;
      case 3 : printf ("exit as a TSR program\n");
               break;
   }
} 

Sample Code 2

/* Compile options needed: none
*/ 

/* Call this sp2.c */ 
#include <stdio.h>

void main (void)
{
   printf ("In program II\n");
   exit (77);
} 

Because C version 6.0 and later have the feature of using inline assembly, the AX register can be accessed directly without using any interrupts. The following line of code can be used in place of the interrupt call: '

   _asm mov syscode, ah 

Additional query words: kbinf 5.10 6.00 6.00a 6.00ax 7.00 1.00 1.50

Keywords : kb16bitonly
Issue type :
Technology : kbVCsearch kbAudDeveloper kbCRT


Last Reviewed: May 4, 2001
© 2001 Microsoft Corporation. All rights reserved. Terms of Use.