The C Run-Time, CRT: Spawning a process using the _spawn C functions family
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows Xp Pro SP2
Target platform: none, just for learning and fun
Header file: Standard and Windows
Additional library: none
Additional project setting: Set project to be compiled as C
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)
Other info: non-CLR or unmanaged.
To do: Spawning a process using _spawn C functions family
To show: Playing with Windows processes
// Project name: myspawnpro, source file name: myspawnsrc.cpp
/* This program accepts a number in the range
* 1-8 from the command line. Based on the number it receives,
* it executes one of the eight different procedures that
* spawn the process named clus (child). For some of these procedures,
* the cplus.exe file must be in the same directory; for
* others, it only has to be in the same path. Here we provide full path to the cplus.exe
* Each of the _spawn() functions creates and executes a new process
*/
#include <stdio.h>
#include <process.h>
char *my_env[] =
{
"This=environment will be",
"passed=to child.exe by the",
"_spawnle()=and",
"_spawnlpe()=and",
"_spawnve()=and",
"_spawnvpe()=functions",
NULL
};
int main(int argc, char *argv[])
{
/* Array storage for argument strings... */
char *args[4];
/* Set up parameters to be sent: */
args[0] = "child";
args[1] = "spawn??";
args[2] = "two";
args[3] = NULL;
if (argc <= 2)
{
/* argv[0] argv[1] argv[2] */
printf("Usage: %s <child_program_name> <1-8>.\n", argv[0]);
exit(1);
}
/* Based on first letter of argument */
switch (argv[2][0])
{
case '1':
_spawnl(_P_WAIT, argv[1], argv[1], "_spawnl", "two", NULL);
break;
case '2':
_spawnle(_P_WAIT, argv[1], argv[1], "_spawnle", "two", NULL, my_env);
break;
case '3':
_spawnlp(_P_NOWAIT, argv[1], argv[1], "_spawnlp", "two", NULL);
break;
case '4':
_spawnlpe(_P_NOWAIT, argv[1], argv[1], "_spawnlpe", "two", NULL, my_env);
break;
case '5':
_spawnv(_P_OVERLAY, argv[1], args);
break;
case '6':
_spawnve(_P_OVERLAY, argv[1], args, my_env);
break;
case '7':
_spawnvp(_P_OVERLAY, argv[1], args);
break;
case '8':
_spawnvpe(_P_OVERLAY, argv[1], args, my_env);
break;
default:
printf("");
printf("Usage: %s <child_program_name> <number [1-8]>.\n", argv[0]);
exit (1);
}
return 0;
}
Output examples:
(This program run at the command prompt)
F:\vc2005project>cd myspawnpro
F:\vc2005project\myspawnpro>cd debug
F:\vc2005project\myspawnpro\debug>myspawnpro
Usage: myspawnpro <child_program_name> <1-8>.
F:\vc2005project\myspawnpro\debug>myspawnpro F:\vc2005project\cplus\debug\cplus 1
Command-line arguments:
argv[0] F:\vc2005project\cplus\debug\cplus.
argv[1] _spawnl.
argv[2] two.
Environment variables:
ALLUSERSPROFILE=C:\Documents and Settings\All Users.WINDOWS.
APPDATA=C:\Documents and Settings\Johnny\Application Data.
CLIENTNAME=Console.
CommonProgramFiles=C:\Program Files\Common Files.
COMPUTERNAME=MYPERSONAL.
ComSpec=C:\WINDOWS\system32\cmd.exe.
FP_NO_HOST_CHECK=NO.
HOMEDRIVE=C:.
HOMEPATH=\Documents and Settings\Johnny.
INCLUDE=c:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\include\.
LIB=c:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Lib\.
LOGONSERVER=\\MYPERSONAL.
NUMBER_OF_PROCESSORS=2.
OS=Windows_NT.
Path=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Common Files\Adobe\AGL;C:\Prog
ram Files\Microsoft SQL Server\90\Tools\binn\.
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH.
PROCESSOR_ARCHITECTURE=x86.
PROCESSOR_IDENTIFIER=x86 Family 15 Model 3 Stepping 4, GenuineIntel.
PROCESSOR_LEVEL=15.
PROCESSOR_REVISION=0304.
ProgramFiles=C:\Program Files.
PROMPT=$P$G.
SESSIONNAME=Console.
SystemDrive=C:.
SystemRoot=C:\WINDOWS.
TEMP=C:\DOCUME~1\Johnny\LOCALS~1\Temp.
TMP=C:\DOCUME~1\Johnny\LOCALS~1\Temp.
USERDOMAIN=MYPERSONAL.
USERNAME=Johnny.
USERPROFILE=C:\Documents and Settings\Johnny.
VS71COMNTOOLS=c:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools\.
VS80COMNTOOLS=C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\.
windir=C:\WINDOWS.
F:\vc2005project\myspawnpro\debug>myspawnpro F:\vc2005project\cplus\debug\cplus 2
Command-line arguments:
argv[0] F:\vc2005project\cplus\debug\cplus.
argv[1] _spawnle.
argv[2] two.
Environment variables:
This=environment will be.
passed=to child.exe by the.
_spawnle()=and.
_spawnlpe()=and.
_spawnve()=and.
_spawnvpe()=functions.
SystemRoot=C:\WINDOWS.
F:\vc2005project\myspawnpro\debug>myspawnpro F:\vc2005project\cplus\debug\cplus 3
F:\vc2005project\myspawnpro\debug>
Command-line arguments:
argv[0] F:\vc2005project\cplus\debug\cplus.
argv[1] _spawnlp.
argv[2] two.
Environment variables:
ALLUSERSPROFILE=C:\Documents and Settings\All Users.WINDOWS.
APPDATA=C:\Documents and Settings\Johnny\Application Data.
CLIENTNAME=Console.
CommonProgramFiles=C:\Program Files\Common Files.
COMPUTERNAME=MYPERSONAL.
ComSpec=C:\WINDOWS\system32\cmd.exe.
FP_NO_HOST_CHECK=NO.
HOMEDRIVE=C:.
HOMEPATH=\Documents and Settings\Johnny.
INCLUDE=c:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\include\.
LIB=c:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Lib\.
LOGONSERVER=\\MYPERSONAL.
NUMBER_OF_PROCESSORS=2.
OS=Windows_NT.
Path=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Common Files\Adobe\AGL;C:\Program Files\Microsoft SQL Server\90\
Tools\binn\.
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH.
PROCESSOR_ARCHITECTURE=x86.
PROCESSOR_IDENTIFIER=x86 Family 15 Model 3 Stepping 4, GenuineIntel.
PROCESSOR_LEVEL=15.
PROCESSOR_REVISION=0304.
ProgramFiles=C:\Program Files.
PROMPT=$P$G.
SESSIONNAME=Console.
SystemDrive=C:.
SystemRoot=C:\WINDOWS.
TEMP=C:\DOCUME~1\Johnny\LOCALS~1\Temp.
TMP=C:\DOCUME~1\Johnny\LOCALS~1\Temp.
USERDOMAIN=MYPERSONAL.
USERNAME=Johnny.
USERPROFILE=C:\Documents and Settings\Johnny.
VS71COMNTOOLS=c:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools\.
VS80COMNTOOLS=C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\.
windir=C:\WINDOWS.
F:\vc2005project\myspawnpro\debug>myspawnpro F:\vc2005project\cplus\debug\cplus 4
F:\vc2005project\myspawnpro\debug>
Command-line arguments:
argv[0] F:\vc2005project\cplus\debug\cplus.
argv[1] _spawnlpe.
argv[2] two.
Environment variables:
This=environment will be.
passed=to child.exe by the.
_spawnle()=and.
_spawnlpe()=and.
_spawnve()=and.
_spawnvpe()=functions.
SystemRoot=C:\WINDOWS.
F:\vc2005project\myspawnpro\debug>myspawnpro F:\vc2005project\cplus\debug\cplus 5
F:\vc2005project\myspawnpro\debug>
Command-line arguments:
argv[0] child.
argv[1] spawn??.
argv[2] two.
Environment variables:
ALLUSERSPROFILE=C:\Documents and Settings\All Users.WINDOWS.
APPDATA=C:\Documents and Settings\Johnny\Application Data.
CLIENTNAME=Console.
CommonProgramFiles=C:\Program Files\Common Files.
COMPUTERNAME=MYPERSONAL.
ComSpec=C:\WINDOWS\system32\cmd.exe.
FP_NO_HOST_CHECK=NO.
HOMEDRIVE=C:.
HOMEPATH=\Documents and Settings\Johnny.
INCLUDE=c:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\include\.
LIB=c:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Lib\.
LOGONSERVER=\\MYPERSONAL.
NUMBER_OF_PROCESSORS=2.
OS=Windows_NT.
Path=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Common Files\Adobe\AGL;C:\Prog
ram Files\Microsoft SQL Server\90\Tools\binn\.
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH.
PROCESSOR_ARCHITECTURE=x86.
PROCESSOR_IDENTIFIER=x86 Family 15 Model 3 Stepping 4, GenuineIntel.
PROCESSOR_LEVEL=15.
PROCESSOR_REVISION=0304.
ProgramFiles=C:\Program Files.
PROMPT=$P$G.
SESSIONNAME=Console.
SystemDrive=C:.
SystemRoot=C:\WINDOWS.
TEMP=C:\DOCUME~1\Johnny\LOCALS~1\Temp.
TMP=C:\DOCUME~1\Johnny\LOCALS~1\Temp.
USERDOMAIN=MYPERSONAL.
USERNAME=Johnny.
USERPROFILE=C:\Documents and Settings\Johnny.
VS71COMNTOOLS=c:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools\.
VS80COMNTOOLS=C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\.
windir=C:\WINDOWS.
F:\vc2005project\myspawnpro\debug>myspawnpro F:\vc2005project\cplus\debug\cplus 6
F:\vc2005project\myspawnpro\debug>
Command-line arguments:
argv[0] child.
argv[1] spawn??.
argv[2] two.
Environment variables:
This=environment will be.
passed=to child.exe by the.
_spawnle()=and.
_spawnlpe()=and.
_spawnve()=and.
_spawnvpe()=functions.
SystemRoot=C:\WINDOWS.
F:\vc2005project\myspawnpro\debug>myspawnpro F:\vc2005project\cplus\debug\cplus 7
F:\vc2005project\myspawnpro\debug>
Command-line arguments:
argv[0] child.
argv[1] spawn??.
argv[2] two.
Environment variables:
ALLUSERSPROFILE=C:\Documents and Settings\All Users.WINDOWS.
APPDATA=C:\Documents and Settings\Johnny\Application Data.
CLIENTNAME=Console.
CommonProgramFiles=C:\Program Files\Common Files.
COMPUTERNAME=MYPERSONAL.
ComSpec=C:\WINDOWS\system32\cmd.exe.
FP_NO_HOST_CHECK=NO.
HOMEDRIVE=C:.
HOMEPATH=\Documents and Settings\Johnny.
INCLUDE=c:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\include\.
LIB=c:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Lib\.
LOGONSERVER=\\MYPERSONAL.
NUMBER_OF_PROCESSORS=2.
OS=Windows_NT.
Path=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Common Files\Adobe\AGL;C:\Prog
ram Files\Microsoft SQL Server\90\Tools\binn\.
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH.
PROCESSOR_ARCHITECTURE=x86.
PROCESSOR_IDENTIFIER=x86 Family 15 Model 3 Stepping 4, GenuineIntel.
PROCESSOR_LEVEL=15.
PROCESSOR_REVISION=0304.
ProgramFiles=C:\Program Files.
PROMPT=$P$G.
SESSIONNAME=Console.
SystemDrive=C:.
SystemRoot=C:\WINDOWS.
TEMP=C:\DOCUME~1\Johnny\LOCALS~1\Temp.
TMP=C:\DOCUME~1\Johnny\LOCALS~1\Temp.
USERDOMAIN=MYPERSONAL.
USERNAME=Johnny.
USERPROFILE=C:\Documents and Settings\Johnny.
VS71COMNTOOLS=c:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools\.
VS80COMNTOOLS=C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\.
windir=C:\WINDOWS.
F:\vc2005project\myspawnpro\debug>myspawnpro F:\vc2005project\cplus\debug\cplus 8
F:\vc2005project\myspawnpro\debug>
Command-line arguments:
argv[0] child.
argv[1] spawn??.
argv[2] two.
Environment variables:
This=environment will be.
passed=to child.exe by the.
_spawnle()=and.
_spawnlpe()=and.
_spawnve()=and.
_spawnvpe()=functions.
SystemRoot=C:\WINDOWS.
F:\vc2005project\myspawnpro\debug>