Environment variables on Windows
-
Hi all,
I just ran into a weird problem with environment variables on Windows:
QProcessEnvironment::systemEnvironment().value("VARNAME")
shows the correct valueqgetenv("VARNAME")
shows up empty
The variable in question was set using
SetEnvironmentVariableW
on application startup before calling theQApplication
constructor. Looking at theqgetenv
implementation on Qt 5.6, it seems to use some kind of cache for the environment variables (in the filesrc\corelib\kernel\qfunctions_fake_env_p.h
), does this cache somehow get initialized before creating theQApplication
?Chees,
Bart
-
Hi, Qt has no cache for normal Windows (that fake API stuff you see is just for pretending there's an environment for WinCE Qt apps).
I think instead you are seeing an old Windows gotcha , what I mean, getenv()/putenv() works nice together but do not play well with SetEnvironmentVariable().
Also, putenv()/getenv() works fine regardless of QApplication has been constructed or not :-)
Edit: actually you could say there's a cache, or copy of the environment variables stored in the process, which getenv()/putenv() uses but SetEnvironmentVariable() does not. It's part of the design of Windows apps.
-
Allright, thanks a lot, that explains this behavior.