|
Subject: RE: Re: SetEnvironmentVariableA & getenv Newsgroups: gmane.comp.gnu.mingw.user Date: 2003-07-30 14:02:54 GMT (11 years, 39 weeks, 4 days and 23 minutes ago) From: Earnie Boyd [mailto:earnie_boyd@...] > Sam Steingold wrote: >> This is confusing. >> Are you saying that each process has two sets of environment variables? >> Actually, MSDN does not document getenv() at all. >> >> Could you please elaborate? > <quote> > [...] > </quote> To simplify a little: The OS maintains a process environment, which is maintained by the API calls {Get,Set}EnvironmentVariable[AW]. This is the fundamental level of program environment in Windows. However, the C runtime (MSVCRT) attempts to support the Unix style environment manipulation routines getenv/putenv. If it was this simple, there would be no duplication. However, Unix allows access to the environment via the envp argument to main() and the environ global variable. In order to support this usage (in such a way that getenv/putenv see changes to environ/envp as is required for Unix semantics) the C runtime takes a *copy* at process startup of the OS environment, and stores it in environ/envp. It is this copy that getenv/putenv manipulate. Hence, yes. There are 2 sets of environment variables. Things are, I believe, made mildly more confusing by attempts to keep the 2 in sync if possible, but as it's not always possible, this may do more harm than good. The basic rule is not to mix Windows API access to the environment and standard C/Unix compatibility access unless you are very sure you know what you are doing. [Disclaimer - this is a simplification, and it's from memory. Don't sue me if I got some points wrong |
|