问题
I'm debugging an 64-bit application where c# exe is using native c++ dll, on Windows 7. It seems environment variables are different for these two, even though they are both executing in the same process. How is it possible that calling System.Environment.SetEnvironmentVariable has no effect on values returned by getenv()?
回答1:
The environment variables are just a blob of data which gets passed by windows to the process when it starts. The runtime functions you are using (The BCL for System.Environment and the CRT for getenv) are probably making copies of the environment during startup, which means they are not operating on the same "environment" variables.
Conceptually they must do this because otherwise there would need to be some way to synchronize them accessing the environment.
回答2:
You can try using the below function. You need to pass the EnvironmentVariableTarget as http://msdn.microsoft.com/en-us/library/system.environmentvariabletarget.aspx basing on your requirement.
public SetEnvironmentVariable( string variable, string value, EnvironmentVariableTarget target )
来源:https://stackoverflow.com/questions/5153547/environment-variables-are-different-for-dll-than-exe