Esta documentación está archivada y no tiene mantenimiento.

_putenv_s, _wputenv_s

Create, modify, or remove environment variables. These are versions of _putenv, _wputenv with security enhancements as described in Security Enhancements in the CRT.

errno_t _putenv_s(
   const char *name,
   const char *value 
errno_t _wputenv_s(
   const wchar_t *name,
   const wchar_t *value



Environment variable name.


Value to set the environment variable to.

Return 0 if successful, or an error code.

Error Conditions
name value Return value







If one of the error conditions occurs, these functions will invoke an invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, these functions return EINVAL and set errno to EINVAL.

The _putenv_s 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_s is a wide-character version of _putenv_s; the envstring argument to _wputenv_s is a wide-character string.

Generic-Text Routine Mappings
TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined





name is the name of the environment variable to be added or modified and value is the variable's value. If name is already part of the environment, its value is replaced by value; otherwise, the new name variable and its value are added to the environment. You can remove a variable from the environment by specifying an empty string (that is, "") for value.

_putenv_s and _wputenv_s 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). However, 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_s and _wputenv_s.

Do not change an environment entry directly: instead, use _putenv_s or _wputenv_s to change it. In particular, direct freeing elements of the _environ[] global array may lead to invalid memory being addressed.

getenv and _putenv_s use the global variable _environ to access the environment table; _wgetenv and _wputenv_s use _wenviron. _putenv_s and _wputenv_s may change the value of _environ and _wenviron, thus invalidating the envp argument to main and the _wenvp argument to wmain. Therefore, it is safer to use _environ or _wenviron to access the environment information. For more information about the relation of _putenv_s and _wputenv_s to global variables, see _environ, _wenviron.


The _putenv_s and _getenv_s families of functions are not thread-safe. _getenv_s could return a string pointer while _putenv_s is modifying the string, causing random failures. Make sure that calls to these functions are synchronized.

Routine Required header Compatibility



Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003


<stdlib.h> or <wchar.h>

Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003

For additional compatibility information, see Compatibility in the Introduction.

For a sample of how to use _putenv_s, see getenv_s, _wgetenv_s.

Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.