How to Shell Out to System Prompt from Within a C Program

Last reviewed: July 17, 1997
Article ID: Q69989
6.00 6.00a 6.00ax 7.00 | 6.00 6.00a | 1.00 1.50 1.51 1.52
MS-DOS                 | OS/2       | WINDOWS
kbprg

The information in this article applies to:

  • The C Run-time (CRT) included with:

        - Microsoft C for MS-DOS, versions 6.0, 6.0a, and 6.0ax
        - Microsoft C for OS/2, versions 6.0, and 6.0a
        - Microsoft C/C++ for MS-DOS, version 7.0
        - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, and
          1.52
    

SUMMARY

In Microsoft C, the spawnlp() function may be used to shell out to the MS- DOS or OS/2 command prompt from within a C program by spawning a copy of the MS-DOS or OS/2 command interpreter. This method is demonstrated in the sample program below. The P_WAIT mode must be used under MS-DOS and is recommended under OS/2 as well.

MORE INFORMATION

The spawnl() function may also be used if a path to the command interpreter is given explicitly in argument 1 of the parameter list. See the documentation or online help supplied with your version of the compiler for more information about the spawn family of functions.

Sample Code

/* Compile options needed: none
*/

#include <process.h>
#include <stdio.h>
#include <stdlib.h>

void main(void)
{
   if ( _osmode == DOS_MODE )   /* Running under MS-DOS */
      {
       printf("Shelling out to DOS, type 'EXIT' to return\n");
       spawnlp(P_WAIT, "COMMAND.COM", "COMMAND.COM", NULL);
      }
   else   /* Running under OS/2 */
      {
       printf("Shelling out to OS/2, type 'EXIT' to return\n");
       spawnlp(P_WAIT, "CMD.EXE", "CMD.EXE", NULL);
      }
   printf("Back from Shell\n");
}

NOTE: For more information on spawning applications in Win32 operating systems, refer to Knowledge Base article Q125213.


Additional reference words: kbinf 6.00 6.00a 6.00ax 7.00 1.00 1.50 spawning
exec
KBCategory: kbprg
KBSubcategory: CRTIss
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.