有没有人用Java或.NET成功“包装”了C ++ API? 我有一个应用程序提供了用于编写插件的C ++ API。 我想要做的是从.NET或Java访问该API。
我需要使用COM吗,还是有更简单/更好的选择?
Has anyone successfully "wrapped up" a C++ API in Java or .NET? I have an application that provides a C++ API for writing plug-ins. What I'd like to do is access that API from .NET or Java.
Would I need to use COM, or are there simpler/better alternatives?
最满意答案
如果你有一个非常直接的方法签名(即接受和返回基本类型,如int,char [],void * ...等)的方法,在.NET中这样做相当容易,但仍然可能,但有点用JNI在Java中更难。
但是,如果您的类方法使用现代C ++编程技术,例如boost共享指针和STL容器,那么这是一个非常不同的故事。 在这种情况下,你需要非常小心内存管理。
编辑:如果方法有C ++模板参数,它会更有趣,因为C ++模板系统与C#或Java泛型非常不同,因为它只是编译时机制。 基本上这意味着每次将不同的数据类型传递给模板参数时,方法或类的签名都不相同。 这使得该方法无法用C#或Java进行封装。
If you have a very straight forward method signatures (i.e. methods that takes and returns primitive types such as int, char[], void* ... etc), it is reasonably easy to do so in .NET and still possible but a bit harder in Java with JNI.
However, if your class methods uses modern C++ programming techniques such as boost shared pointers and STL containers then it is a very different story. You will need to be really careful with memory management in this case.
EDIT: It is gonna be even more interesting if the method has C++ template arguments because C++ template system is very different from C# or Java generics in that it is only a compile time mechanism. Basically this means the signature of the method or class is different every time you pass a different data type to the template argument. This made the method impossible to wrap in C# or Java.
更多推荐
发布评论