Environment variables are different for dll than exe

被刻印的时光 ゝ 提交于 2019-12-21 19:25:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!