Passing an argument to the process example
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: Passing an argument to a process for processing
To show: Playing with Windows processe
#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;
}
// Loads and executes new child processes.
// We are going to run "ping 127.0.0.1 -t" command, passing the 127.0.0.1 -t argument....
if ((ret = _execl(av[1], av[1], "127.0.0.1", "-t", NULL, my_env)) == -1)
perror("perror says _execl() failed");
}
Output example:
(This program run at the command prompt)
F:\vc2005project\crtexecprog\debug>crtexecprog ping.exe
perror says _execl() failed: No such file or directory
F:\vc2005project\crtexecprog\debug>crtexecprog C:\windows\system32\ping.exe
F:\vc2005project\crtexecprog\debug>
Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
...
...
...