P-Invoke in .net core with Linux
Asked Answered
I

2

22

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

Example :

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

If it is possible to use like this or it's not support to call native win api with linux using .net-core ?

[DllImport("MyLib.dll", CallingConvention = CallingConvention.StdCall)]
internal static extern long NativeMethod();
Illness answered 5/7, 2016 at 11:14 Comment(1)
Have you tried it? How did it fail?Samirasamisen
R
20

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.

Rival answered 5/7, 2016 at 18:30 Comment(0)
C
18

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

Consideration answered 8/2, 2017 at 16:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.