1. Introduction
Generally, the main function has two parameters: int argc and char * argv [], respectively, indicating the number of parameters and Parameter options. For example, if you run Ping www.csdn.net-T in the CMD window, the two parameters argc = 3, argv [] = {"ping", "www.csdn.net ", "-T "}.
Check msdn. The optional prototype is int main (intargc, char * argv [], char * envp []). The first two parameters are familiar, and the first 3rd parameters are unfamiliar. So what is envp installed and what is its purpose?
2. Print Environment Variables
Compile a small program to print the data in envp. The Code is as follows:
/*++ envp.cpp - print environment viaries *decription:include<tchar.h> so as to support generic-text mapping *created:2011-08-14 16:58 *author:btwsmile--*/#include<tchar.h>#include<cstdio>#include<cstdlib>using namespace std;int _tmain(int argc,_TCHAR* argv[],_TCHAR* envp[]){for(int i=0;;++i){if(envp[i]){_tprintf_s(_TEXT("%d:%s\n"),i,envp[i]);}else break;}system("pause");return 0;}
The result is:
0: allusersprofile = c: \ programdata
1: appdata = c: \ Users \ Root \ appdata \ roaming
2: ASL. log = Destination = File
3: commonprogramfiles = c: \ Program Files \ common files
4: computername = ROOT-PC
5: comspec = c: \ windows \ system32 \ cmd.exe
6: dxsdk_dir = c: \ Program Files \ Microsoft DirectX SDK (June 2010 )\
7: fp_no_host_check = No
8: homedrive = C:
9: homepath = \ Users \ Root
10: localappdata = c: \ Users \ Root \ appdata \ Local
11: logonserver \\ ROOT-PC
12: pai_plugin_path = c: \ Program Files \ Foxit Software \ Foxit Reader \ plugins \
13: number_of_processors = 4
14: OS = windows_nt
15: pathext =. com;. EXE;. BAT;. CMD;. vbs;. VBE;. js;. JSE;. WSF;. wsh;. MSC
16: processor_architecture = x86
17: processor_identifier = x86 family 6 model 15 stepping 11, genuineintel
18: processor_level = 6
19: processor_revision = 0f0b
20: programdata = c: \ programdata
21: ProgramFiles = c: \ Program Files
22: psmodulepath = c: \ windows \ system32 \ windowspowershell \ V1.0 \ modules \
23: Public = c: \ Users \ public
24: sessionname = console
25: systemdrive = C:
26: systemroot = C: \ WINDOWS
27: temp = c: \ Users \ Root \ appdata \ Local \ Temp
28: TMP = c: \ Users \ Root \ appdata \ Local \ Temp
29: userdomain = root-PC
30: username = root
31: USERPROFILE = c: \ Users \ Root
32: visualstudiodir = c: \ Users \ Root \ Documents \ Visual Studio 2010
33: vs100comntools = D: \ Program Files \ Microsoft Visual Studio 10.0 \ common7 \ tools \
34: WINDIR = C: \ WINDOWS
35: Path = c: \ windows \ system32; C: \ WINDOWS; c: \ windows \ system32 \ WBEM; C: \ Program Files
\ Common files \ thunder Network \ Kankan \ codecs; D: \ Program Files \ Microsoft Visual St
UDIO 10.0 \; D: \ Program Files \ Microsoft Visual Studio 10.0 \ Vc \ bin
Press any key to continue...
It is not difficult to find that the environment variables of the system are stored in envp. You can right-click computer> Properties> advanced system Settings> environment variables (win7) to open the environment variable settings window, 1.
Figure 1 system environment variable setting window
The variable names and values in the window shown in Figure 1 are the same as those printed by the program. That is, the environment variables of the formal system saved in the envp array.
3. Use of envp
The msdn explanation is as follows:
Microsoft specific
Wmain (INT argc, wchar_t * argv [], wchar_t * envp [])
Envp:
The envp array, which is a common extension in UNIX systems [#1], is used in Microsoft C ++. it is an array of strings representing the variables set in the user's environment. this array is terminated by a null entry [#2 ].
It can be declared as an array of pointers to Char (char * envp []) or as a pointer to pointers to Char (char ** envp ). if your program uses wmain instead of main, use thewchar_t data type instead ofchar [#3 ].
The environment block passed to main and wmain is a "Frozen" copy of the current environment [#4 ]. if you subsequently change the environment via a call to putenv or _ wputenv, the current environment (as returned by getenv/_ wgetenv and the _ environ/_ wenviron
Variable) will change, but the block pointed to by envp will not change. see customizing Command Line Processing for information on suppressing environment processing. this argument is ANSI compatible in C, but not in C ++ [#5 ].
#1: many UNIX operating systems have extended support for the envp array;
#2: It stores the variable string in the user environment and ends with null. Therefore, the above example uses if (envp [I]) to determine whether the print is complete;
#3: envp can be char * [] or char **. The preceding example uses the former. If the wide character set is used, use wmain instead of main, use the envp of the wchar * [] or wchar ** type. The above example uses tchar. h and general text programming;
#4: Once the envp is passed in, it is just a string array and will not change with the dynamic settings of the program. You can use the putenv function to modify environment variables in real time, or use getenv to view environment variables in real time, But envp itself will not change;
#5: This version is compatible with ansi c but not ansi c ++.
Envp is rarely used, so it is not as clear as argc and argv in its role. However, based on its performance, I can think of two functions of envp:
(1) provide a reference for the Program: The program must be determined by referring to the environment variables during running. For example, the installer must know the default ProgramFiles of the system, it can view the program folder of the system in ProgramFiles = C: \ Program Files Through envp, so the default installation directory is set to c: \ proogramfiles;
(2) If the program modifies the environment variable during running and wants to restore it when it exits, you can refer to the data in envp to restore the environment variable to the setting before program execution.