【问题标题】:P-Invoke in .net core with Linux使用 Linux 的 .net 核心中的 P-Invoke
【发布时间】:2016-07-05 11:14:18
【问题描述】:

有没有办法在 Linux 上的 .NET Core 中实现 P/Invoke (dllimport)

例子:

我有使用 .net 框架编译的 C++ MyLib.dll。

如果可以这样使用或者不支持使用.net-core在linux上调用本机win api?

[DllImport("MyLib.dll", CallingConvention = CallingConvention.StdCall)]
internal static extern long NativeMethod();

【问题讨论】:

  • 你试过了吗?它是如何失败的?

标签: pinvoke .net-core


【解决方案1】:

当然支持 PInvoke(这是与操作系统接口的所有代码的工作方式),但在您列出的特定情况下不支持。你不能在 Linux 上 PInvoke 到一个 win32 二进制文件,除非你自己为 Linux 构建了一个功能兼容的版本。更实际的是,您需要从其他一些公开了类似功能的系统二进制文件中找到一个 Linux 函数,然后使用它来代替。

【讨论】:

    【解决方案2】:

    即使 dll 仅存在于 Windows 操作系统中,您也可以在 .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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多