P-Invoke in .net core with Linux

前端 未结 2 1240
日久生厌
日久生厌 2020-12-06 05:18

Is there way to implement P/Invoke (dllimport) in .NET Core on Linux ?

Example :

I have C++ MyLib.dll compiled with .net framework.

If

相关标签:
2条回答
  • 2020-12-06 05:57

    PInvoke is certainly supported (that is how all of the code that interfaces with the OS works), but not in the specific case you listed. You cannot PInvoke to a win32 binary on Linux, unless you have somehow built a function-compatible version of it for Linux yourself. More realistically, you need to find a Linux function from some other system binary which exposes similar functionality, and then use that instead.

    0 讨论(0)
  • 2020-12-06 05:58

    Even dlls exists only in Windows OS, you can import and use some linux APIs using PInvoke in .NET Core.

    Samples:

    [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);
    

    Please see https://developers.redhat.com/blog/2016/09/14/pinvoke-in-net-core-rhel/

    https://gist.github.com/martinwoodward/f5b195aa53b4ed52cabaf701486baa79

    0 讨论(0)
提交回复
热议问题