|< Windows Process & Threads: C Run-Time 1 | Main | Windows Process & Threads: C Run-Time 3 >| Site Index | Download |
MODULE R1
PROCESSES AND THREADS: C RUN-TIME
Part 2: Story And Program Examples
My Training Period: hours
Note:
This is a continuation from the previous Module. Working program examples compiled using Visual C++ .Net (Visual studio .Net 2003). It is low-level programming and the .Net used is Unmanaged (/clr is not set: Project menu → your_project_name Properties… sub menu → Configuration Properties folder → General subfolder → Used Managed Extension setting set to No). This also applied to other program examples in other Modules of tenouk.com Tutorial that mentioned “compiled using Visual C++ .Net”. Other settings are default. Machine’s OS is standalone Win Xp SP2. Program examples have been tested for Non Destructive Test :o). All information recomposed for Win 2000 (NT5.0) above.
WARNING
Wrongly modified and run the program examples presented here might collapse your Windows machine or disturb its integrity. So for your Windows machine safety, make sure you fulfill the following conditions:
Abilities
▪ Able to understand the basic of Windows process and thread concepts.
▪ Able to appreciate the using of C, C++ and MFC to implement process and thread.
▪ Able to understand and manipulate process and thread functions mainly the exec() and spawn() family from C Run-Time Library.
▪ Able to understand the basic of multithreading.
▪ Able to understand, execute, modify and develop a process and thread programs.
|
|
// 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 running the program at command prompt:
F:\myproject\crtprocess\Release>crtprocess test command line argument and environment variable strings
Command-line arguments: argv[0] crtprocess argv[1] test argv[2] command argv[3] line argv[4] argument argv[5] and argv[6] environment argv[7] variable argv[8] strings
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 the crtprocess.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>crtexecprog c:\windows\system32\crtprocess 2
F:\myproject\crtexecprog\Release>
Command-line arguments:
argv[0] c:\windows\system32\crtprocess
argv[1] _execle
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 3
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");
}

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;
}

Continue on next Module...
Further reading and digging:
Check the best selling C / C++ and Windows books at Amazon.com.
|
Search books and compare the prices. Get the cheapest C/C++ books from the cheap! Not just C/C++ books... |
||
|
|
||
|
|
|< Windows Process & Threads: C Run-Time 1 | Main | Windows Process & Threads: C Run-Time 3 >| Site Index | Download |