Calling UNIX and Linux shared object file .so from

2019-01-26 12:28发布

问题:

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?

回答1:

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.



回答2:

I would say at the least there's likely to be no easy way, especially if you mean C# on Windows. In that case you would need something that would be able to decode the shared object and get to the code in it, sort of a re-implementation of the ABI for GNU/linux. Also, any other libraries would have to be present and usable as well, such as the C runtime library and the like. This would likely be a very significant effort.

As for doing it directly under linux/Mono, see this answer: Calling UNIX and Linux shared object file .so from c# .

You could also try to see if what open office does, http://packages.debian.org/lenny/cli-uno-bridge could be helpful; but this is more of an interface rather than directly linking the two together.