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();
NET Core runtime allows you to run applications on Linux that were made with .
NET is cross-platform and runs on Linux, macOS, and Windows. . NET Framework only runs on Windows. .
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With