Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

P-Invoke in .net core with Linux

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();
like image 278
Elshan Avatar asked Jul 05 '16 11:07

Elshan


People also ask

Can you use .NET Core on Linux?

NET Core runtime allows you to run applications on Linux that were made with .

Is .NET framework compatible with Linux?

NET is cross-platform and runs on Linux, macOS, and Windows. . NET Framework only runs on Windows. .


2 Answers

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.

like image 164
Eric Mellino Avatar answered Sep 16 '22 16:09

Eric Mellino


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

like image 28
Marcus Couto Avatar answered Sep 17 '22 16:09

Marcus Couto