我有一个简单的 C# 应用程序,它绑定(bind)到一个使用 mingnu 编译器工具集编译的库。我可以毫无问题地轻松调用库中的函数。
然而,库调用 getenv
来设置自己,需要设置此环境变量才能使库正常工作,所以我正在使用 Environment.SetEnvironmentVariable
但是库不能检索我设置的值。
最佳答案
getenv
在启动时复制进程的环境变量 block 。通过 SetEnvironmentVariable
进行的任何后续更改都不会反射(reflect)在 getenv
使用的变量 block 中。您将需要 PInvoke setenv
函数以使调整后的值反射(reflect)在后续的 putenv
调用中。
参见:http://msdn.microsoft.com/en-us/library/tehxacec(VS.71).aspx
getenv and _putenv use the copy of the environment pointed to by the global variable _environ to access the environment. getenv operates only on the data structures accessible to the run-time library and not on the environment "segment" created for the process by the operating system. Therefore, programs that use the envp argument to main or wmain may retrieve invalid information.
关于c# - 通过 SetEnvironmentVariable 进行的更改不会在使用 getenv 的库中生效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4788398/