简体   繁体   English

如何通过JNI将大型结构从C传递到JAVA

[英]How to pass a large structure through JNI from C to JAVA

Is it possible to pass a large Structure through JNI from C to Java? 是否可以通过JNI将大型结构从C传递到Java?

Can somebody please give me the possible solutions? 有人可以给我可能的解决方案吗?

Declare a pointer to the struct in your java class like so: 在Java类中声明一个指向struct的指针,如下所示:

protected long ptrToX;

Next, to set it: 接下来,进行设置:

  • Get the field ID using (*env)->GetFieldID(...) 使用(*env)->GetFieldID(...)获取字段ID
  • Get the pointer using (*env)->GetLongField(...) 使用(*env)->GetLongField(...)获取指针
  • Set the pointer using (*env)->SetLongField(...) 使用(*env)->SetLongField(...)设置指针

To get it, just follow the first two steps mentioned above. 要获得它,只需按照上面提到的前两个步骤。

Always remember to include a finaliser that will take care of deallocating the pointer when the object is garbage-collected. 始终记住要包括一个终结器,该终结器将在对象被垃圾回收时负责释放指针。 Alternatively, if you do not want to incur the performance hit incurred by using finalisers, just provide a terminate() method that deallocates the pointer. 另外,如果您不想引起使用终结器引起的性能下降,只需提供一个终止指针的terminate()方法即可。

最好您切换到JNA,这是从C到JAVA编程的便捷方法。

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

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