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 value
    • qgetenv("VARNAME") shows up empty

    The variable in question was set using SetEnvironmentVariableW on application startup before calling the QApplication constructor. Looking at the qgetenv implementation on Qt 5.6, it seems to use some kind of cache for the environment variables (in the file src\corelib\kernel\qfunctions_fake_env_p.h), does this cache somehow get initialized before creating the QApplication?



  • 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.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.