Re: Odd behavior of getenv and putenv

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



On Tue, 2 May 2006 15:29:02 -0700, Yogi Watcher
<YogiWatcher@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

Hi,

Recently I have observed some odd behavior of getenv and putenv function. I
am developing some code that integrates with several other libraries. This
program is not using MFC. It is plain C and C++ code.

Some login and initialization function from one dll is setting an
environment variable ?MANUAL_LOGIN? to value ?TRUE?. I tried three different
cases and here is what I found:

Case 1: If I write following code in my function:

char *val = getenv (?MANUAL_LOGIN?) ;
printf (?MANUAL_LOGIN = [%s]\n?, val == NULL ? ?? : val) ;
system (?echo MANUAL_LOGIN = [%MANUAL_LOGIN%]?) ;

then I get following output on screen:

MANUAL_LOGIN = []
MANUAL_LOGIN = [TRUE]

This sounds like the modules (EXE, DLL) are using different CRTs and thus
have distinct C-level environments. Sticking to the Windows functions
GetEnvironmentVariable and SetEnvironmentVariable should work for you, or
you could link everyone to the same CRT DLL so they would share CRT state.

Case 2: If I use same code as in case 1, but define following environment
variable on the command line prior to running the program:
set MANUAL_LOGIN=SOME_JUNK

then I get following output on screen:

MANUAL_LOGIN = [SOME_JUNK]
MANUAL_LOGIN = [TRUE]

Right, the new process inherits the environment of the process that started
it, so this is expected.

Case 3: Now if I write following code in my function:

putenv (?MANUAL_LOGIN=SOME_JUNK?) ;
char *val = getenv (?MANUAL_LOGIN?) ;
printf (?MANUAL_LOGIN = [%s]\n?, val == NULL ? ?? : val) ;
system (?echo MANUAL_LOGIN = [%MANUAL_LOGIN%]?) ;

then I get following output on screen:

MANUAL_LOGIN = [SOME_JUNK]
MANUAL_LOGIN = [SOME_JUNK]

So it seems that getenv() and system() look at different environment. Can
somebody please explain me what is happening here?

If DLL X links to a different CRT than DLL Y, they will have separate
C-level environments, heap, errno, etc.

My goal is to get original
value of environment variable. Change it to different value and then do some
processing and reset it back to its original value. Any idea how I can
achieve that?

Link everyone to the same CRT DLL or use the Windows functions.

Here is another odd behavior that I saw, this time with putenv. Following
code does not unset the environment variable, even though there is nothing
after = sign:

putenv (?MANUAL_LOGIN=?) ;

This may or may not be related to what I have described above.

Any explanations?

Depending on where you're doing your getenv, it may well be related.

--
Doug Harrison
Visual C++ MVP
.



Relevant Pages

  • Re: Odd behavior of getenv and putenv
    ... Recently I have observed some odd behavior of getenv and putenv function. ... This sounds like the modules (EXE, DLL) are using different CRTs and thus ... you could link everyone to the same CRT DLL so they would share CRT state. ... So it seems that getenvand systemlook at different environment. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Odd behavior of getenv and putenv
    ... If DLL X links to different CRT than DLL Y, ... and If I change some environment variable in a function in DLL Y and then ... Recently I have observed some odd behavior of getenv and putenv function. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Umgebungsvariable aus C++/C heraus setzen?
    ... > dann müßte das mit der API-Funktion SetEnvironmentVariable gehen. ... Die EXE müsste also garantiert bevor die DLL auf die Idee kommt, ... die Environment variablen abzufragen, diese per SetEnviromentVariable ... Einfacher wird es wenn die CRT die shared DLL verwendet, ...
    (microsoft.public.de.vc)
  • Re: Odd behavior of getenv and putenv
    ... and If I change some environment variable in a function in DLL Y and then ... If you start the process using a CRT function, ... Windows-level environment, use CreateProcess directly. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: How do I set the environmet for a dll?
    ... KB-Q78542 (Retrieving MS-DOS Environment Vars from a Windows DLL) ... I don't think that setting things in the registry is going to ... > DllMain, I can't think of an elegant solution, but here are a couple of ...
    (microsoft.public.win32.programmer.kernel)