Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to call a shared library (.so) on linux platform from .NET core?

I understand that aim of the .NET core is for cross platform development, but I am looking for backward compatibility. If I have a linux library available (maybe, legacy) and I want its functions to be called from .NET core application for linux platform. Is it possible?

I am not talking about ASP.NET core, I need it for a desktop application.

like image 485
Maulik Soni Avatar asked Oct 30 '22 21:10

Maulik Soni


2 Answers

Yes: https://github.com/dotnet/corefx/issues/343

That being said, I'll admit to not having actually tried it yet. I have used this a far bit on Mono, for what it's worth, and it's definitely worth reading their documentation on this: http://www.mono-project.com/docs/advanced/pinvoke/ ... because .net core seems to take much the same approach (eg: leave off the file extension for the p-invoked library, so the system can take care of linking to the right version etc...)

Eg how bits of .net core itself do this:

const string LIBC = "libc";

[DllImport(LIBC, EntryPoint = "getenv")]
private static extern IntPtr getenv_core(string name);

https://github.com/dotnet/corefx/blob/79d44f202e4b6d000ca6c6885a24549a55db38f1/src/System.Console/src/Interop/Interop.manual.Unix.cs

like image 60
piers7 Avatar answered Nov 15 '22 11:11

piers7


Look at SWIG. It is a simple way to wrap a shared library and it generates all of the P/Invoke code for you as well as maps any header files (think structs) between your .so library and your C# code. We created a simple cpp wrapper around the .so library that we wanted to use and then let SWIG do its magic. We now have a C# library that can call from our .Net code (a simple console app) that invokes our linux library functions. We have done this on a PI, on unix hosts running Ubuntu, CentOS, and Debian with great success. http://www.swig.org/

like image 43
twreynol Avatar answered Nov 15 '22 09:11

twreynol