kernel32.dll SetEnvironmentVariable 中的 EntryPointNotFoundException

EntryPointNotFoundException in kernel32.dll SetEnvironmentVariable

我是 运行 mac 上的一个 c# 应用程序,使用 mono 和 xamarin 作为 IDE。 在我的主要方法中,我有:

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling=false)]
 public static extern bool SetEnvironmentVariable(string lpName, string lpValue);

然后当我调用它时:

SetEnvironmentVariable("PATH", path);

我得到一个 EntryPointNotFoundException

System.EntryPointNotFoundException: SetEnvironmentVariable
at at (wrapper managed-to-native) EICService.Program:SetEnvironmentVariable (string,string)
at EICService.Program.Main (System.String[] rawArgs) [0x00031] in (*my path*)/Program.cs:34

现在我知道在 mac 上调用 kernel32.dll 是一个问题,但我认为它正在找到它或任何 mono 可以替代它的东西,因为如果我放一些虚拟 dll 文件那里的名字我得到一个 dll 未找到错误。

我是一个 c# 菜鸟,但这段代码在 windows 上运行,我们只想在 unix 上运行它 运行。

有什么想法吗?

这是一个 Windows API 函数。它根本不存在于 Mac 上。因此错误。

您不需要p/invoke此函数来设置进程环境中的变量。您可以使用本机 .net 功能。即Environment.SetEnvironmentVariable

此外,您应该在 Windows 上做完全相同的事情。 p/invoke 一个本机函数来执行 .net 运行时库提供的功能从来都不是一个好主意。