G
Guest
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]
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]
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? 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?
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?
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]
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]
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? 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?
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?