getenv、_wgetenvgetenv, _wgetenv

从当前环境中获取值。Gets a value from the current environment. 提供这些函数的更安全版本;请参阅 getenv_s、_wgetenv_sMore secure versions of these functions are available; see getenv_s, _wgetenv_s.

重要

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

语法Syntax

char *getenv(
   const char *varname
);
wchar_t *_wgetenv(
   const wchar_t *varname
);

参数Parameters

varnamevarname
环境变量名称。Environment variable name.

返回值Return Value

将指针返回到环境表条目包含varnameReturns a pointer to the environment table entry containing varname. 使用返回的指针修改环境变量并不安全。It is not safe to modify the value of the environment variable using the returned pointer. 使用_putenv函数修改环境变量的值。Use the _putenv function to modify the value of an environment variable. 返回值是NULL如果varname环境表中找不到。The return value is NULL if varname is not found in the environment table.

备注Remarks

Getenv函数将搜索的环境变量列表varnameThe getenv function searches the list of environment variables for varname. getenv不区分大小写,在 Windows 操作系统中。getenv is not case sensitive in the Windows operating system. getenv_putenv使用由全局变量指向该环境的副本 _environ来访问该环境。getenv and _putenv use the copy of the environment pointed to by the global variable _environ to access the environment. getenv操作仅在运行时库的访问的数据结构上而不是在环境"段"由操作系统对进程创建。getenv operates only on the data structures accessible to the run-time library and not on the environment "segment" created for the process by the operating system. 因此,程序使用envp参数主要wmain可能会检索无效信息。Therefore, programs that use the envp argument to main or wmain may retrieve invalid information.

如果varnameNULL,此函数调用无效参数处理程序中, 所述参数验证If varname is NULL, this function invokes an invalid parameter handler, as described in Parameter Validation. 如果允许执行继续,此函数将errnoEINVAL并返回NULLIf execution is allowed to continue, this function sets errno to EINVAL and returns NULL.

_wgetenv是宽字符版本的getenv; 的自变量和返回值 _wgetenv是宽字符字符串。_wgetenv is a wide-character version of getenv; the argument and return value of _wgetenv are wide-character strings. _Wenviron全局变量是宽字符版本的 _environThe _wenviron global variable is a wide-character version of _environ.

在 MBCS 程序中 (例如,在 SBCS ASCII 程序中), _wenviron最初NULL因为环境多字节字符字符串组成。In an MBCS program (for example, in an SBCS ASCII program), _wenviron is initially NULL because the environment is composed of multibyte-character strings. 然后,在首次调用_wputenv,或在首次调用上 _wgetenv如果 (MBCS) 环境已存在,创建并然后指向对应的宽字符字符串环境 _wenvironThen, on the first call to _wputenv, or on the first call to _wgetenv if an (MBCS) environment already exists, a corresponding wide-character string environment is created and is then pointed to by _wenviron.

同样,在 Unicode (_wmain) 程序中, _environ最初NULL因为环境宽字符字符串组成。Similarly in a Unicode (_wmain) program, _environ is initially NULL because the environment is composed of wide-character strings. 然后,在首次调用 _putenv,或在首次调用上getenv如果 (Unicode) 环境已存在,创建,然后由指向对应的 MBCS 环境 _environThen, on the first call to _putenv, or on the first call to getenv if a (Unicode) environment already exists, a corresponding MBCS environment is created and is then 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 may 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. 有关详细信息,请参阅 _environ、_wenvironFor more information, see _environ, _wenviron.

备注

_Putenv_getenv系列的函数不是线程安全。The _putenv and _getenv families of functions are not thread-safe. _getenv无法返回字符串指针时 _putenv正在修改的字符串,从而导致随机失败。_getenv could return a string pointer while _putenv is modifying the string, causing random failures. 确保对这些函数的调用同步。Make sure that calls to these functions are synchronized.

一般文本例程映射Generic-Text Routine Mappings

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_tgetenv_tgetenv getenvgetenv getenvgetenv _wgetenv_wgetenv

若要检查或更改的值TZ环境变量,使用getenv_putenv_tzset根据需要。To check or change the value of the TZ environment variable, use getenv, _putenv and _tzset as necessary. 有关详细信息TZ,请参阅_tzset_daylight、 timezone 和 _tznameFor more information about TZ, see _tzset and _daylight, timezone, and _tzname.

要求Requirements

例程Routine 必需的标头Required header
getenvgetenv <stdlib.h><stdlib.h>
_wgetenv_wgetenv <stdlib.h> 或 <wchar.h><stdlib.h> or <wchar.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.

示例Example

// crt_getenv.c
// compile with: /W3
// This program uses getenv to retrieve
// the LIB environment variable and then uses
// _putenv to change it to a new value.

#include <stdlib.h>
#include <stdio.h>

int main( void )
{
   char *libvar;

   // Get the value of the LIB environment variable.
   libvar = getenv( "LIB" ); // C4996
   // Note: getenv is deprecated; consider using getenv_s instead

   if( libvar != NULL )
      printf( "Original LIB variable is: %s\n", libvar );

   // Attempt to change path. Note that this only affects the environment
   // variable of the current process. The command processor's
   // environment is not changed.
   _putenv( "LIB=c:\\mylib;c:\\yourlib" ); // C4996
   // Note: _putenv is deprecated; consider using putenv_s instead

   // Get new value.
   libvar = getenv( "LIB" ); // C4996

   if( libvar != NULL )
      printf( "New LIB variable is: %s\n", libvar );
}
Original LIB variable is: C:\progra~1\devstu~1\vc\lib
New LIB variable is: c:\mylib;c:\yourlib

请参阅See also

进程和环境控制Process and Environment Control
_putenv、_wputenv_putenv, _wputenv
环境常量Environmental Constants