The _exec family C program example, executing other processes

 

 

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: _exec family C code examples, executing other processes

To show: Playing with Windows processes

 

 

// Project name: crtexecprog, source file name: crtexecsrc.cpp generating crtexecprog.exe

// Run the following program at command prompt to execute cplus.exe.

/* Illustrates the different versions of exec, including

* _execl(), _execle(), _execlp(), _execlpe()

* _execv(), _execve(), _execvp(), _execvpe()

*

* Although crtexecsrc.cpp can execute any program, you can verify how

* the different versions handle arguments and environment by

* compiling and specifying the sample program cplus.exe. See

* "_spawn(), _wspawn() functions" for examples of the similar spawn functions.

*/

#include <windows.h>

#include <stdio.h>

#include <stdlib.h>

#include <process.h>

 

/* Environment for exec?e */

char *my_env[] =

{

// NAME=value

"THIS=environment will be",

"PASSED=to new process by",

"the EXEC=functions",

NULL

};

 

int main(int ac, char* av[])

{

// An array pointers to strings...

char *args[4];

 

// If command line arguments not properly supplied...

if (ac != 3)

{

fprintf(stderr, "Usage: %s <program_name> <number>\n", av[0]);

return 0;

}

 

// otherwise...

/* Arguments for _execv? */

args[0] = av[1];

args[1] = "exec??";

args[2] = "two";

args[3] = NULL;

switch(atoi(av[2]))

{

case 1:

{

_execl(av[1], av[1], "_execl", "two", NULL);

printf("_execl(av[1], av[1], \"_execl\", \"two\", NULL)\n");

}

break;

case 2:

_execle(av[1], av[1], "_execle", "two", NULL, my_env);

break;

case 3:

_execlp(av[1], av[1], "_execlp", "two", NULL);

break;

case 4:

_execlpe(av[1], av[1], "_execlpe", "two", NULL, my_env);

break;

// ==========================================================

case 5:

_execv(av[1], args);

break;

case 6:

_execve(av[1], args, my_env);

break;

case 7:

_execvp(av[1], args);

break;

case 8:

_execvpe(av[1], args, my_env);

break;

default:

break;

}

/* This point is reached only if exec fails. */

printf("\nProcess was not exec'ed, error if any: %d.", GetLastError());

// exit peacefully...

exit(0);

}

 

Output example:

(This program run at the command prompt)

 

F:\vc2005project\crtexecprog\debug>crtexecprog

Usage: crtexecprog <program_name> <number>

 

F:\vc2005project\crtexecprog\debug>crtexecprog F:\vc2005project\cplus\debug\cplus 1

F:\vc2005project\crtexecprog\debug>

Command-line arguments:

argv[0] F:\vc2005project\cplus\debug\cplus.

argv[1] _execl.

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\crtexecprog\debug>crtexecprog F:\vc2005project\cplus\debug\cplus 2

F:\vc2005project\crtexecprog\debug>

Command-line arguments:

argv[0] F:\vc2005project\cplus\debug\cplus.

argv[1] _execle.

argv[2] two.

Environment variables:

THIS=environment will be.

PASSED=to new process by.

the EXEC=functions.

SystemRoot=C:\WINDOWS.

 

 

F:\vc2005project\crtexecprog\debug>crtexecprog F:\vc2005project\cplus\debug\cplus 3

F:\vc2005project\crtexecprog\debug>

Command-line arguments:

argv[0] F:\vc2005project\cplus\debug\cplus.

argv[1] _execlp.

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\crtexecprog\debug>crtexecprog F:\vc2005project\cplus\debug\cplus 4

F:\vc2005project\crtexecprog\debug>

Command-line arguments:

argv[0] F:\vc2005project\cplus\debug\cplus.

argv[1] _execlpe.

argv[2] two.

Environment variables:

THIS=environment will be.

PASSED=to new process by.

the EXEC=functions.

SystemRoot=C:\WINDOWS.

 

 

F:\vc2005project\crtexecprog\debug>crtexecprog F:\vc2005project\cplus\debug\cplus 5

F:\vc2005project\crtexecprog\debug>

Command-line arguments:

argv[0] F:\vc2005project\cplus\debug\cplus.

argv[1] exec??.

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\crtexecprog\debug>crtexecprog F:\vc2005project\cplus\debug\cplus 6

F:\vc2005project\crtexecprog\debug>

Command-line arguments:

argv[0] F:\vc2005project\cplus\debug\cplus.

argv[1] exec??.

argv[2] two.

Environment variables:

THIS=environment will be.

PASSED=to new process by.

the EXEC=functions.

SystemRoot=C:\WINDOWS.

 

 

F:\vc2005project\crtexecprog\debug>crtexecprog F:\vc2005project\cplus\debug\cplus 7

F:\vc2005project\crtexecprog\debug>

Command-line arguments:

argv[0] F:\vc2005project\cplus\debug\cplus.

argv[1] exec??.

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\crtexecprog\debug>crtexecprog F:\vc2005project\cplus\debug\cplus 8

F:\vc2005project\crtexecprog\debug>

Command-line arguments:

argv[0] F:\vc2005project\debug\debug\cplus.

argv[1] exec??.

argv[2] two.

Environment variables:

THIS=environment will be.

PASSED=to new process by.

the EXEC=functions.

SystemRoot=C:\WINDOWS.

F:\vc2005project\crtexecprog\debug>

 

 

C and C++ Programming Resources | C & C++ Code Example Index