c# - 通过 SetEnvironmentVariable 进行的更改不会在使用 getenv 的库中生效

标签 c# environment-variables pinvoke

我有一个简单的 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/

相关文章:

c# - 在 XNA、C# 中使用矩阵时对操作顺序感到困惑

c# - 不可删除/不可见的 .text 文件

java - 如何在 macos 上设置 LD_LIBRARY_PATH/DYLD_LIBRARY_PATH

c# - p/invoke 的结构变量的内存对齐 - 字符串丢失最后一个字符

c# - 从其他窗口获取 ListView 项目

c# - 在 wcf 服务中传递可选参数

c# - 对与指定绑定(bind)约束匹配的类型调用构造函数引发异常

python - 子进程运行结束后如何获取环境变量?

environment-variables - 使用 0.22.1 版错误时尝试从 0.19.1 版中解压缩估算器 SVC

c# - 如何固定 'unmanaged' 指针?