简体   繁体   English

如何通过名单 <T> 从Java到使用JNA的C ++ std :: vector函数参数

[英]How to pass List<T> from Java to a C++ std::vector function parameter using JNA

I have a native function : 我有一个本机功能:

int NativeFunction(std::vector<MyObject>);

I am loading the Native dll using JNA and I am trying to call this function NativeFunction from Java like: 我正在使用JNA加载本机dll,并且尝试从Java调用此函数NativeFunction:

nativedlljnapointer.NativeFunction(List<MyObject>);

I am however running into "java.lang.IllegalArgumentException: Unsupported argument type ArrayList" exception. 但是,我遇到了“ java.lang.IllegalArgumentException:不支持的参数类型ArrayList”异常。

I al tried using java util vector when I am running into the same exception "java.lang.IllegalArgumentException: Unsupported argument type java.util.Vector" 我遇到相同的异常“ java.lang.IllegalArgumentException:参数类型不受支持的java.util.Vector”时,尝试使用Java util向量。

Can someone suggest me how I could pass List from my Java function to the native function which has vector<> as an argument. 有人可以建议我如何将List从Java函数传递给带有vector <>作为参数的本机函数。

Any help will be greatly appreciated. 任何帮助将不胜感激。

std::vector and java List are completely different types, it's normal for them not to work. std :: vector和Java List是完全不同的类型,它们不起作用是正常的。

Furthermore , Is MyObject a C++ defined object or a Java defined object (if you define one in each, they are again, completely different objects ! )? 此外,MyObject是C ++定义的对象还是Java定义的对象(如果每个定义一个,它们又是完全不同的对象!)?

The best and safest way to communicate via JNI is to use serialization, like you would between any two different environments. 通过JNI进行通信的最好,最安全的方法是使用序列化,就像在任何两个不同环境之间一样。

Granted, it takes a bit of extra work, but in the long run you end up with more robust code. 当然,这需要一些额外的工作,但是从长远来看,您最终将获得更强大的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM