Search code examples
c#linuxmonopinvokeshared-libraries

Calling UNIX and Linux shared object file .so from c#


Is there a way for a Shared Object file written in C and built on Unix to be called from C# P/Invoke?

Or do I need to use Java or something like that?


Solution

  • Mono has the ability to integrate with native libraries from within C# built on top of dlopen(3). You just have to use the DllImport statement with the name of the library (i.e. 'libform.so.5'), then wrap the native code and data types with a friendly C# class that takes care of all the low-level stuff. This page has a good overview with lots of information on how to deal with marshaling pointers and other unsafe types.

    Once you've got your wrapper class written, you can just use that without worrying about the fact that it's using a native shared library underneath.