My Training Period: xx hours. Before you begin, read someinstruction here.
The expected abilities:
// File name: crtprocesssrc.cpp Project name: crtprocess, generating crtprocess.exe /* Illustrates the following variables used for accessing *command-line arguments and environment variables: * argc argv envp * This program will be executed by crtexecprog which presented in the next example */
#include <stdio.h>
int main(int argc,/* Number of strings in array argv */ char *argv[ ], /* Array of command-line argument strings */ char **envp) /* Array of environment variable strings */ { int count; /* Display each command-line argument. */ printf("\nCommand-line arguments:\n"); for (count = 0; count < argc; count++) printf(" argv[%d] %s.\n", count, argv[count]); /* Display each environment variable. */ printf("\nEnvironment variables:\n"); while (*envp != NULL) printf(" %s.\n", *(envp++)); return 0; }
The following is a sample output when running the program at command prompt:
|
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=1
OS=Windows_NT
Path=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Common Files\Adobe\AGL
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\
windir=C:\WINDOWS
F:\myproject\crtprocess\Release>
Then copies the Release version of the crtprocess.exe to Windows system directory (for Tenouk’s Windows Xp machine it is C:\WINDOWS\System32) to prepare for our next program example or you can provide the full path of thecrtprocess.exe for the following program execution.
// Project name: crtexecprog, file name: crtexecsrc.cpp generating crtexecprog.exe
// Run the following program at command prompt to execute the crtprocess.exe.
/* Illustrates the different versions of exec, including
* _execl _execle _execlp _execlpe
* _execv _execve _execvp _execvpe
*
* Although crtexecsrc.cpp can exec any program, you can verify how
* the different versions handle arguments and environment by
* compiling and specifying the sample program crtprocess.cpp. 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 (1-8)>\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);
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 execed, error if any: %d.", GetLastError());
// exit peacefully...
exit(0);
}
The following are sample outputs (our previous program crtprocess.exe has been copied to Windows system directory or where ever it is, you can provide the full path of the crtprocess.exe) and some of the output have been trimmed:
F:\myproject\crtexecprog\Release>crtexecprog
Usage: crtexecprog <program_name> <number (1-8)>
F:\myproject\crtexecprog\Release>crtexecprog crtprocess 1
Process was not execed, error if any: 2.
F:\myproject\crtexecprog\Release>crtexecprog c:\windows\system32\crtprocess 1
F:\myproject\crtexecprog\Release>
Command-line arguments:
argv[0] c:\windows\system32\crtprocess
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=1
OS=Windows_NT
Path=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Common Files\Adobe\AGL
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\
windir=C:\WINDOWS
|
F:\myproject\crtexecprog\Release>
Command-line arguments:
argv[0] c:\windows\system32\crtprocess
argv[1] _execlp
argv[2] two
Environment variables:
ALLUSERSPROFILE=C:\Documents and Settings\All Users.WINDOWS
APPDATA=C:\Documents and Settings\Johnny\Application Data
...[trimmed]
...
USERPROFILE=C:\Documents and Settings\Johnny
VS71COMNTOOLS=c:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools\
windir=C:\WINDOWS
F:\myproject\crtexecprog\Release>crtexecprog c:\windows\system32\crtprocess 4
F:\myproject\crtexecprog\Release>
Command-line arguments:
argv[0] c:\windows\system32\crtprocess
argv[1] _execlpe
argv[2] two
Environment variables:
THIS=environment will be
PASSED=to new process by
the EXEC=functions
F:\myproject\crtexecprog\Release>crtexecprog c:\windows\system32\crtprocess 5
F:\myproject\crtexecprog\Release>
Command-line arguments:
argv[0] c:\windows\system32\crtprocess
argv[1] exec??
argv[2] two
Environment variables:
ALLUSERSPROFILE=C:\Documents and Settings\All Users.WINDOWS
...[trimmed]
...
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\
windir=C:\WINDOWS
F:\myproject\crtexecprog\Release>crtexecprog c:\windows\system32\crtprocess 6
F:\myproject\crtexecprog\Release>
Command-line arguments:
argv[0] c:\windows\system32\crtprocess
argv[1] exec??
argv[2] two
Environment variables:
THIS=environment will be
PASSED=to new process by
the EXEC=functions
F:\myproject\crtexecprog\Release>crtexecprog c:\windows\system32\crtprocess 7
F:\myproject\crtexecprog\Release>
Command-line arguments:
argv[0] c:\windows\system32\crtprocess
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
...[trimmed]
...
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\
windir=C:\WINDOWS
F:\myproject\crtexecprog\Release>crtexecprog c:\windows\system32\crtprocess 8
F:\myproject\crtexecprog\Release>
Command-line arguments:
argv[0] c:\windows\system32\crtprocess
argv[1] exec??
argv[2] two
Environment variables:
THIS=environment will be
PASSED=to new process by
the EXEC=functions
F:\myproject\crtexecprog\Release>
The following is a smaller program example version using _execl().
#include <windows.h>
#include <stdio.h>
#include <process.h>
/* Environment for exec?e */
char *my_env[ ] =
{
// Just as an example here: NAME=value
"ComSpec=C:\\WINDOWS\\system32\\cmd.exe",
"Path=C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS",
NULL
};
int main(int ac,char* av[ ])
{
int ret;
// If command line arguments not properly supplied...
if (ac != 2)
{
fprintf(stderr, "Usage: %s <program_name>.\n", av[0]);
return 0;
}
// We are going to run "ping 127.0.0.1 -t" command....
if ((ret = _execl(av[1], av[1], "127.0.0.1", "-t", NULL, my_env)) == -1)
perror("perror says _execl() failed");
}
A sample output:
Another example.
#include <windows.h>
#include <stdio.h>
#include <process.h>
/* Environment for exec?e */
char *my_env[ ] =
{
// Just as an example here: NAME=value
"ComSpec=C:\\WINDOWS\\system32\\cmd.exe",
"Path=C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS",
NULL
};
int main(int ac,char* av[ ])
{
int ret;
// An array pointers...
char *args[3];
/* Arguments for _execv? */
args[0] = av[1];
args[1] = "/D";
args[2] = NULL;
// If command line arguments not properly supplied...
if (ac != 2)
{
fprintf(stderr, "Usage: %s <program_name>.\n", av[0]);
return 0;
}
// We are going to run "mem /D" command....
if ((ret = _execve(av[1], args, my_env)) == -1)
perror("perror says _execve() failed");
return 0;
}
A sample output:
Further reading and digging:
Microsoft C references, online MSDN.
Microsoft Visual C++, online MSDN.
ReactOS - Windows binary compatible OS - C/C++ source code repository, Doxygen.
Linux Access Control Lists (ACL) info can be found atAccess Control Lists.
Check the best selling C / C++ and Windows books at Amazon.com.