使用 Linux 在 .net 核心中调用 P-Invoke
P-Invoke in .net core with Linux
有没有办法在 Linux 上的 .NET Core 中实现 P/Invoke (dllimport)
?
示例:
我用 .net 框架编译了 C++ MyLib.dll。
如果可以这样使用,或者不支持使用 .net-core 调用本机 win api 和 linux?
[DllImport("MyLib.dll", CallingConvention = CallingConvention.StdCall)]
internal static extern long NativeMethod();
PInvoke 当然受支持(这就是与 OS 接口的所有代码的工作方式),但在您列出的特定情况下不支持。您不能 PInvoke 到 Linux 上的 win32 二进制文件,除非您自己以某种方式为 Linux 构建了它的功能兼容版本。更实际的是,您需要从其他公开类似功能的系统二进制文件中找到一个 Linux 函数,然后改用它。
即使 dll 仅存在于 Windows OS,您可以在 .NET Core 中使用 PInvoke 导入和使用一些 linux API。
样本:
[DllImport("libc")]
static extern int read(int handle, byte[] buf, int n);
[DllImport("libc.so.6")]
private static extern int getpid();
[DllImport("libgtk-x11-2.0.so.0")]
static extern IntPtr gtk_message_dialog_new(IntPtr parent_window, DialogFlags flags, MessageType type, ButtonsType bt, string msg, IntPtr args);
请看https://developers.redhat.com/blog/2016/09/14/pinvoke-in-net-core-rhel/
https://gist.github.com/martinwoodward/f5b195aa53b4ed52cabaf701486baa79
有没有办法在 Linux 上的 .NET Core 中实现 P/Invoke (dllimport)
?
示例:
我用 .net 框架编译了 C++ MyLib.dll。
如果可以这样使用,或者不支持使用 .net-core 调用本机 win api 和 linux?
[DllImport("MyLib.dll", CallingConvention = CallingConvention.StdCall)]
internal static extern long NativeMethod();
PInvoke 当然受支持(这就是与 OS 接口的所有代码的工作方式),但在您列出的特定情况下不支持。您不能 PInvoke 到 Linux 上的 win32 二进制文件,除非您自己以某种方式为 Linux 构建了它的功能兼容版本。更实际的是,您需要从其他公开类似功能的系统二进制文件中找到一个 Linux 函数,然后改用它。
即使 dll 仅存在于 Windows OS,您可以在 .NET Core 中使用 PInvoke 导入和使用一些 linux API。
样本:
[DllImport("libc")]
static extern int read(int handle, byte[] buf, int n);
[DllImport("libc.so.6")]
private static extern int getpid();
[DllImport("libgtk-x11-2.0.so.0")]
static extern IntPtr gtk_message_dialog_new(IntPtr parent_window, DialogFlags flags, MessageType type, ButtonsType bt, string msg, IntPtr args);
请看https://developers.redhat.com/blog/2016/09/14/pinvoke-in-net-core-rhel/
https://gist.github.com/martinwoodward/f5b195aa53b4ed52cabaf701486baa79