Is there a way for a Shared Object file written in C and built on Unix to be called from C# P/Invoke?
有没有办法用C语言编写并在Unix上构建的共享对象文件从C#P / Invoke调用?
Or do I need to use Java or something like that?
或者我需要使用Java或类似的东西?
2 个解决方案
#1
7
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.
Mono能够与构建在dlopen(3)之上的C#中的本机库集成。您只需使用带有库名称的DllImport语句(即'libform.so.5'),然后使用友好的C#类包装本机代码和数据类型,该类负责处理所有低级别的东西。这个页面有一个很好的概述,有很多关于如何处理编组指针和其他不安全类型的信息。
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
1
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.
我会说至少可能没有简单的方法,特别是如果你的意思是Windows上的C#。在这种情况下,您需要能够解码共享对象并获取其中的代码的东西,类似于GNU / linux的ABI的重新实现。此外,任何其他库也必须存在并可用,例如C运行时库等。这可能是一项非常重要的工作。
As for doing it directly under linux/Mono, see this answer: Calling UNIX and Linux shared object file .so from c# .
至于在linux / Mono下直接进行,请参阅以下答案:从c#调用UNIX和Linux共享对象文件.so。
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.
您还可以尝试查看开放式办公室是否有用,http://packages.debian.org/lenny/cli-uno-bridge可能会有所帮助;但这更像是一个界面而不是直接将两者联系在一起。
#1
7
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.
Mono能够与构建在dlopen(3)之上的C#中的本机库集成。您只需使用带有库名称的DllImport语句(即'libform.so.5'),然后使用友好的C#类包装本机代码和数据类型,该类负责处理所有低级别的东西。这个页面有一个很好的概述,有很多关于如何处理编组指针和其他不安全类型的信息。
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
1
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.
我会说至少可能没有简单的方法,特别是如果你的意思是Windows上的C#。在这种情况下,您需要能够解码共享对象并获取其中的代码的东西,类似于GNU / linux的ABI的重新实现。此外,任何其他库也必须存在并可用,例如C运行时库等。这可能是一项非常重要的工作。
As for doing it directly under linux/Mono, see this answer: Calling UNIX and Linux shared object file .so from c# .
至于在linux / Mono下直接进行,请参阅以下答案:从c#调用UNIX和Linux共享对象文件.so。
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.
您还可以尝试查看开放式办公室是否有用,http://packages.debian.org/lenny/cli-uno-bridge可能会有所帮助;但这更像是一个界面而不是直接将两者联系在一起。