I have a DLL obtaining startup options using getenv (<stdlib.h>) call. I would like to set that variable in the same process, before opening the DLL, so that it is accessible via getenv. Which function should I use to set it?
I learned that there are two sets of env vars under windows: one is manipulated via win32 API (GetEnvironemntVariable, SetEnvironmentVariable), another one can be read using getenv, and probably set via _putenv, is that the one I should use?
Is this function accessible from python, perhaps via ctypes?
The current ~VS2019 situation appears to be:
getenv function retrieve the values from a block the MS CRT initializes at load time.GetEnvironmentVariable retrieve the value from the process environment block.SetEnvironmentVariable update only the value from the process environment block. These changes will not be seen by getenv.putenv update both the value in the CRT block and also additionally call Win32 SetEnvironmentVariable to update the process environment block.So, actually regardless of what you use it for:
_wputenv_s to set the env var - it will update both.::GetEnvironmentVariableW to read: It will read from the environment block, which will contain the value regardless of which method was used.getenvIf the code using getenv is using the same MS CRT that your code -- that is, the code is dynamically linked to the CRT and actually uses the same version as you, then you can always use putenv.
If the DLL (AND its CRT) is dynamically or delay-loaded and you are able to call putenv before the dll is loaded, then you can use it.
(This is conjecture, I have not tested this exactly:) If the DLL is already loaded in you process and it uses a statically linked CRT, or another CRT than the one you are using, than the environment data for its getenv call is already loaded up, an nothing that you do on your side of the DLL boundary will change that. Out of luck in this case I guess.
Ref:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With