getenv, _wgetenv 获取环境变量

原创 2006年05月30日 17:46:00

getenv, _wgetenv

Get a value from the current environment.

char *getenv( const char *varname );

wchar_t *_wgetenv( const wchar_t *varname );

Routine Required Header Compatibility
getenv <stdlib.h> ANSI, Win 95, Win NT
_wgetenv <stdlib.h> or <wchar.h> Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.


LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

Each of these functions returns 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. Use the _putenv function to modify the value of an environment variable. The return value is NULL if varname is not found in the environment table.



Environment variable name


The getenv function searches the list of environment variables for varname. getenv is not case sensitive in Windows NT and Windows 95. getenv and _putenv use the copy of the environment pointed to by the global variable _environ to access the environment. 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. Therefore, programs that use the envp argument to main or wmain may retrieve invalid information.

_wgetenv is a wide-character version of getenv; the argument and return value of _wgetenv are wide-character strings. The _wenviron global variable is a wide-character version of _environ.

In an MBCS program (for example, in an SBCS ASCII program), _wenviron is initially NULL because the environment is composed of multibyte-character strings. Then, 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.

Similarly in a Unicode (_wmain) program, _environ is initially NULL because the environment is composed of wide-character strings. Then, 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.

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. For example, whenever you call _putenv, a call to _wputenv is also executed automatically, so that the two environment strings correspond.

Caution   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. 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. For more information, see _environ, _wenviron.

Generic-Text Routine Mappings

TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_tgetenv getenv getenv _wgetenv

To check or change the value of the TZ environment variable, use getenv, _putenv and _tzset as necessary. For more information about TZ, see _tzset and see _daylight, timezone, and _tzname.


/* GETENV.C: 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>

void main( void )
   char *libvar;

   /* Get the value of the LIB environment variable. */
   libvar = getenv( "LIB" );

   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" );

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

   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


相关函数:putenv, setenv, unsetenv 头文件:#include 定义函数:char * getenv(const char *name); 函数说明:gete...
  • Lina_ACM
  • Lina_ACM
  • 2016-07-19 10:56:32
  • 629


配置文件读取操作重要流程  1.获取配置文件的全路径  在本文中,配置文件存放的全路径为:/home/zhou/zhouzx/GetConfig/ Config.ini。实现获取配置文件的全路径的...
  • u013845722
  • u013845722
  • 2015-05-10 13:03:53
  • 2928


头文件: char *getenv(const char* env_var); 作用: 搜索与env_var字符串相匹配的主机环境(操作系统)提供的环境列表,返回该列表成员。 (C++ 1...
  • huhaoxuan2010
  • huhaoxuan2010
  • 2017-08-08 11:28:09
  • 657


getenv()读取环境变量的当前值的函数 原形:char *getenv(const char *name) 用法:s=getenv("环境变量名");    需先定义char *s; 功能:返回一...
  • god9me
  • god9me
  • 2010-01-06 17:14:00
  • 22818


  • Lina_ACM
  • Lina_ACM
  • 2016-07-19 10:55:25
  • 627


在PHP中getenv(参数)函数是一个用于获取环境变量的函数,根据提供不同的参数可以获取不同的环境变量,具体如下:   “PHP_SELF”   当前正在执行脚本的文件名,与docume...
  • xiaoxiaoniaoer1
  • xiaoxiaoniaoer1
  • 2012-07-05 11:20:11
  • 1257


C语言标准库函数getenv可获取环境参数(根据参数名称), 函数声明:char* getenv(char*name); 其实现如下: ---------------glibc-...
  • aweii
  • aweii
  • 2016-09-10 14:02:57
  • 1373

getenv, _wgetenv 获取环境变量

 getenv, _wgetenvGet a value from the current environment.char *getenv( const char *varname );wchar_...
  • justtodo
  • justtodo
  • 2006-05-30 17:46:00
  • 2405


_beep _sleep _seterrormode 这些函数在以前版本的 CRT 中已过时。 此外,对应 Win32 API 不可用于 UWP 应用。 无解决方法。 chdir _c...
  • lengjianjun
  • lengjianjun
  • 2016-05-31 22:40:09
  • 1489

getenv、setenv函数(获取和设置系统环境变量) 与 环境变量

1、getenv() 函数名: getenv 功 能: 从环境中取字符串,获取环境变量的值 头文件: stdlib.h 用 法:char *getenv(char *envvar); 函数...
  • qq_26093511
  • qq_26093511
  • 2017-05-25 19:57:51
  • 844
您举报文章:getenv, _wgetenv 获取环境变量