_putenv、_wputenv_putenv, _wputenv

创建、修改或删除环境变量。Creates, modifies, or removes environment variables. 提供这些函数的更多安全版本;请参阅 _putenv_s、_wputenv_sMore secure versions of these functions are available; see _putenv_s, _wputenv_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

int _putenv(
   const char *envstring
);
int _wputenv(
   const wchar_t *envstring
);

参数Parameters

envstringenvstring
环境字符串定义。Environment-string definition.

返回值Return Value

返回成功时为 0 或-1,如果出现错误。Return 0 if successful or -1 in the case of an error.

备注Remarks

_Putenv函数将新环境变量添加或修改现有环境变量的值。The _putenv function adds new environment variables or modifies the values of existing environment variables. 环境变量定义过程执行的环境(例如待与程序链接的库的默认搜索路径)。Environment variables define the environment in which a process executes (for example, the default search path for libraries to be linked with a program). _wputenv是宽字符版本的 _putenv; envstring参数 _wputenv是宽字符字符串。_wputenv is a wide-character version of _putenv; the envstring argument to _wputenv is a wide-character string.

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

Tchar.h 例程Tchar.h routine 未定义 _UNICODE 和 _MBCS_UNICODE and _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_tputenv_tputenv _putenv_putenv _putenv_putenv _wputenv_wputenv

Envstring参数必须是指向形式的字符串的指针varname=value_string,其中varname是要添加或修改的环境变量的名称和value_string是变量的值。The envstring argument must be a pointer to a string of the form varname=value_string, where varname is the name of the environment variable to be added or modified and value_string is the variable's value. 如果varname已属于该环境,其值将由替换value_string; 否则为新varname变量并将其value_string值添加到环境。If varname is already part of the environment, its value is replaced by value_string; otherwise, the new varname variable and its value_string value are added to the environment. 可以从环境中删除变量通过指定一个空value_string,或换而言之,通过仅指定varname=。You can remove a variable from the environment by specifying an empty value_string, or in other words, by specifying only varname=.

_putenv_wputenv影响本地到当前进程环境; 不能用于修改命令级别环境。_putenv and _wputenv affect only the environment that is local to the current process; you cannot use them to modify the command-level environment. 也就是说,这些函数只在可由运行时库访问的数据结构上操作,并不在操作系统为进程创建的环境段上操作。That is, these functions operate only on data structures accessible to the run-time library and not on the environment segment created for a process by the operating system. 在当前进程终止时,环境将还原到调用进程的级别(在大多数情况下,为操作系统级别)。When the current process terminates, the environment reverts to the level of the calling process (in most cases, the operating-system level). 但是,修改后的环境可以传递到创建的任何新进程 _spawn_exec,或系统,这些新进程将获取由添加任何新项 _putenv_wputenvHowever, the modified environment can be passed to any new processes created by _spawn, _exec, or system, and these new processes get any new items added by _putenv and _wputenv.

不要直接更改环境条目: 请改用 _putenv_wputenv若要更改它。Do not change an environment entry directly: instead, use _putenv or _wputenv to change it. 具体而言,直接释放的元素 _environ [] 全局数组可能会导致无效的内存进行寻址。In particular, direct freeing elements of the _environ[] global array might lead to invalid memory being addressed.

getenv_putenv使用全局变量 _environ访问环境表;_wgetenv_wputenv使用 _wenvirongetenv and _putenv use the global variable _environ to access the environment table; _wgetenv and _wputenv use _wenviron. _putenv_wputenv可能会更改的值 _environ_wenviron,因此失效 _envp自变量主要_wenvp参数wmain_putenv and _wputenv might change the value of _environ and _wenviron, thus invalidating the _envp argument to main and the _wenvp argument to wmain. 因此,它是安全使用 _environ_wenviron访问环境信息。Therefore, it is safer to use _environ or _wenviron to access the environment information. 有关关系的详细信息 _putenv_wputenv全局变量,请参阅_environ、 _wenvironFor more information about the relation of _putenv and _wputenv to global variables, 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.

要求Requirements

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

有关更多兼容性信息,请参阅 兼容性For more compatibility information, see Compatibility.

示例Example

有关如何使用的示例 _putenv,请参阅getenv、 _wgetenvFor a sample of how to use _putenv, see getenv, _wgetenv.

请参阅See also

进程和环境控制Process and Environment Control
getenv、_wgetenvgetenv, _wgetenv
_searchenv、_wsearchenv_searchenv, _wsearchenv