现在的位置: 首页 > 综合 > 正文

main函数第3个参数envp装的是什么

2013年12月10日 ⁄ 综合 ⁄ 共 3733字 ⁄ 字号 评论关闭

1.引入

 一般情况下,main函数带有两个参数,分别为int argc和char* argv[],各自表示参数个数、参数选项。比如在cmd窗口中运行ping www.csdn.net -t,那么传到ping程序的main函数的两个参数argc=3,argv[]={"ping","www.csdn.net","-t"}。

查看MSDN,可选的原型为int main(intargc,char*argv[],char*envp[]);。前两个参数比较熟悉,第3个参数就很陌生了。那么,envp装的是什么,有什么用?

2.打印环境变量

编写一个小程序,把envp里的数据打印出来,代码如下:

/*++ 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;
}

其结果为:

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:MOZ_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
请按任意键继续. . .

观察结果不难发现,envp里存放正是系统的环境变量,可以右键单击计算机->属性->高级系统设置->环境变量(Win7),打开环境变量设置窗口,如图1所示。

                                    图1 系统环境变量设置窗口

把图1所示窗口中的变量名称、值与程序打印出来的结果对比,它们是一样的。也就是说envp数组里保存的正式系统的环境变量。

3.envp的用途

MSDN上的解释是这样的:

Microsoft Specific

wmain( int argc, wchar_t *argv[ ], wchar_t *envp[ ] )

envp:

The envp array, which is a common extension in many 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:许多UNIX操作系统中普遍扩展了对envp数组的支持;

#2:它保存这用户环境中的变量字符串,以NULL结束。正因此,本文上面的例子用if(envp[i])来判断是否打印完毕;

#3:envp可以是char*[]类型也可以是char**类型,本文上面的例子使用的是前者;如果使用宽字符集,则应使用wmain代替main,并使用wchar*[]或wchar**类型的envp。本文上面的例子使用tchar.h,使用了通用文本编程;

#4:envp一旦传入,它就只是单纯的字符串数组而已,不会随着程序动态设置发生改变。可以使用putenv函数实时修改环境变量,也能使用getenv实时查看环境变量,但是envp本身不会发生改变;

#5:这个版本对ANSI C兼容,但对ANSI C++不兼容。

envp用的不多,因此对它的作用并没有argc和argv那样清楚的理解。不过依据它的表现,笔者能够想到的到envp的作用有两点:

(1)为程序提供参考:程序的运行过程中需要参考环境变量作出决定,比如安装程序必须知道系统默认的ProgramFiles,它可以通过envp查看到本系统的程序文件夹在ProgramFiles=C:\Program Files,于是默认安装目录就设置定为C:\ProogramFiles;

(2)如果程序在运行过程中对环境变量做了修改,在它退出时想要恢复,这时就可以参照envp中的数据将环境变量恢复到程序执行前的设置。

抱歉!评论已关闭.