How to Shell Out to System Prompt from Within a C ProgramID: Q69989
|
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.
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.
/* 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 query words: kbinf 6.00 6.00a 6.00ax 7.00 1.00 1.50 spawning exec
Keywords : kb16bitonly
Version :
Platform :
Issue type :
Last Reviewed: August 5, 1999