Java Native Access doesn#39;t do C++, right?(Java Native Access 不支持 C++,对吧?)
问题描述
I've found many references online (including some on stackoverflow) to JNA being used for C++ libraries, but nothing I can find in the JNA docs indicates that's possible. There doesn't seem to be any way to wrap a C++ class, in particular.
I need native access to use RTAudio, but all of RTAudio's functions are member functions of the RTAudio class. So just to confirm, JNA isn't the way to go right?
What this question amounts to is asking how to call C++ instance methods using JNA, and it's possible, but you're going to have to do some work. In particular, you'll need to write a wrapper which extern "C"
s any functions you actually need to invoke.
For any arbitrary type* function()
definition you can map the method using JNA as returning a com.sun.jna.Pointer
, but you won't be able to invoke methods on a C++ object from JNA.
A simple workaround for this would be to write a C interface library that simply invokes the method on the objects for you...so if you have some member function foo()
you could export a C method from your C++ code:
extern "C" void bar(type* var){
var->foo();
}
Obviously this will add some work for you...but I suspect the overhead for switching to JNI would be about the same.
JNA only cares about the way in which the method is exported in the DLL -- and that must be without C++ decorations (hence the extern "C"
), so you can do whatever you need to within any such method without exposing methods that you call.
In my contrived example above, this means that foo()
, as long as it is defined within the DLL does not in fact have to even be exposed. Since it's a C++ function, JNA cannot call it directly, but it can be called from within a function that JNA can call, which is why my proposed solution works.
So, yes, you can fully encapsulate calls to all the member functions (create, operate, destroy) in a single function and JNA won't care.
这篇关于Java Native Access 不支持 C++,对吧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java Native Access 不支持 C++,对吧?
基础教程推荐
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01