_environ、_wenviron_environ, _wenviron

_environ 变量是指向构成进程环境的多字节字符字符串的指针数组的指针。The _environ variable is a pointer to an array of pointers to the multibyte-character strings that constitute the process environment. 此全局变量已弃用,因为出现了更安全的函数版本 getenv_s、_wgetenv_s_putenv_s、_wputenv_s,应使用这两个版本来替换此全局变量。This global variable has been deprecated for the more secure functional versions getenv_s, _wgetenv_s and _putenv_s, _wputenv_s, which should be used in place of the global variable. _environ 在 Stdlib.h 中声明。_environ is declared in Stdlib.h.

重要

此 API 不能用于在 Windows 运行时中执行的应用程序。This API cannot be used in applications that execute in the Windows Runtime. 有关详细信息,请参阅 /ZW 不支持的 CRT 函数For more information, see CRT functions not supported with /ZW.

语法Syntax

extern char **_environ;  

备注Remarks

在使用 main 函数的程序中,_environ 在程序启动过程中根据来自操作系统环境的设置进行初始化。In a program that uses the main function, _environ is initialized at program startup according to settings taken from the operating-system environment. 环境包含窗体的一个或多个条目The environment consists of one or more entries of the form

ENVVARNAME =stringENVVARNAME =string

getenv_sputenv_s 使用 _environ 变量来访问和修改环境表。getenv_s and putenv_s use the _environ variable to access and modify the environment table. 当调用 _putenv 以添加或删除环境设置时,该环境表的大小发生更改。When _putenv is called to add or delete environment settings, the environment table changes size. 其在内存中的位置也可能更改,具体取决于程序的内存要求。Its location in memory may also change, depending on the program's memory requirements. _environ 的值会自动进行相应调整。The value of _environ is automatically adjusted accordingly.

_wenviron 变量在 Stdlib.h 中声明为:The _wenviron variable, declared in Stdlib.h as:

extern wchar_t **_wenviron;  

_environ 的宽字符版本。is a wide-character version of _environ. 在使用 wmain 函数的程序中,_wenviron 在程序启动过程中根据来自操作系统环境的设置进行初始化。In a program that uses the wmain function, _wenviron is initialized at program startup according to settings taken from the operating-system environment.

在使用 main 的程序中,_wenviron 最初为 NULL,因为环境是由多字节字符字符串组成的。In a program that uses main, _wenviron is initially NULL because the environment is composed of multibyte-character strings. 在首次调用 _wgetenv_wputenv 时,会由 _wenviron 创建并指向对应的宽字符字符串环境。On the first call to _wgetenv or _wputenv, a corresponding wide-character string environment is created and is pointed to by _wenviron.

同样,在使用 wmain 的程序中,_environ 最初是 NULL,因为环境是由宽字符字符串组成的。Similarly, in a program that uses wmain, _environ is initially NULL because the environment is composed of wide-character strings. 在首次调用 _getenv_putenv 时,会由 _environ 创建并指向对应的多字节字符字符串环境。On the first call to _getenv or _putenv, a corresponding multibyte-character string environment is created and is pointed to by _environ.

当程序中同时存在环境的两个副本(MBCS 和 Unicode)时,运行时系统必须保留这两个副本,而这将减慢执行时间。When two copies of the environment (MBCS and Unicode) exist simultaneously in a program, the run-time system must maintain both copies, resulting in slower execution time. 例如,当调用 _putenv 时,也会自动调用 _wputenv,以便两个环境字符串相对应。For example, whenever you call _putenv, a call to _wputenv is also executed automatically, so that the two environment strings correspond.

小心

在极少数情况下,当运行时系统同时保留环境的 Unicode 版本和多字节版本时,两个环境版本可能不完全对应。In rare instances, when the run-time system is maintaining both a Unicode version and a multibyte version of the environment, these two environment versions might not correspond exactly. 这是因为,虽然任何唯一的多字节字符字符串将映射到唯一的 Unicode 字符串,但从唯一的 Unicode 字符串到多字节字符字符串的映射却不一定是唯一的。This is because, although any unique multibyte-character string maps to a unique Unicode string, the mapping from a unique Unicode string to a multibyte-character string is not necessarily unique. 因此,两个不同的 Unicode 字符串可能会映射到同一个多字节字符串。Therefore, two distinct Unicode strings might map to the same multibyte string.

当使用 /MD/MDd 链接时,轮询 Unicode 上下文中的 _environ 没有任何意义。Polling _environ in a Unicode context is meaningless when /MD or /MDd linkage is used. 对于 CRT DLL,该程序的类型(宽字节或多字节)是未知的。For the CRT DLL, the type (wide or multibyte) of the program is unknown. 因为这是最可能的方案,所以仅创建多字节类型。Only the multibyte type is created because that is the most likely scenario.

下面的伪代码说明了这一点。The following pseudo-code illustrates how this can happen.

int i, j;  
i = _wputenv( "env_var_x=string1" );  // results in the implicit call:  
                                      // putenv ("env_var_z=string1")  
j = _wputenv( "env_var_y=string2" );  // also results in implicit call:  
                                      // putenv("env_var_z=string2")  

在用于此示例的表示法中,字符字符串不是 C 字符串文本;相反,它们是表示 _wputenv 调用中的 Unicode 环境字符串文本和 putenv 调用中多字节环境字符串的占位符。In the notation used for this example, the character strings are not C string literals; rather, they are placeholders that represent Unicode environment string literals in the _wputenv call and multibyte environment strings in the putenv call. 两个不同的 Unicode 环境字符串中的字符占位符“x”和“y”不会唯一映射到当前 MBCS 中的字符。The character placeholders 'x' and 'y' in the two distinct Unicode environment strings do not map uniquely to characters in the current MBCS. 相反,这两个占位符都会映射到某些 MBCS 字符“z”,这是尝试转换字符串的默认结果。Instead, both map to some MBCS character 'z' that is the default result of the attempt to convert the strings.

因此,在多字节环境中,当“env_var_z”的值设置为“string2”时,第一次隐式调用 putenv 之后“env_var_z”的值将为“string1”,但此值将在第二次隐式调用 putenv 时改写。Thus, in the multibyte environment, the value of "env_var_z" after the first implicit call to putenv would be "string1", but this value would be overwritten on the second implicit call to putenv, when the value of "env_var_z" is set to "string2". 因此,Unicode 环境(在 _wenviron 中)与多字节环境(在 _environ 中)将区分以下这一系列的调用。The Unicode environment (in _wenviron) and the multibyte environment (in _environ) would therefore differ following this series of calls.

另请参阅See Also

全局变量 Global Variables
getenv、_wgetenv getenv, _wgetenv
getenv_s、_wgetenv_s getenv_s, _wgetenv_s
_putenv、_wputenv _putenv, _wputenv
_putenv_s、_wputenv_s_putenv_s, _wputenv_s