Modify the (basic type) parameters in JNI and return to the Java layer for use

Recently, I encountered a problem in JNI related projects: pass in multiple int type parameters in the Java layer, and return to the Java layer after the JNI layer modifies the parameter value or address. This should be regarded as a basic knowledge, especially the parameters of basic types often seem simple, so I took it all before learning JNI. As a result, I suddenly encounter this problem and need to find materials to learn. Therefore, I will record this after reviewing the basic knowledge again this week. No matter how far you go, don't forget the way under your feet.

Let's use the demo to explain the requirements and corresponding solutions

Two methods are written in the Java layer to simulate this requirement respectively. The arg1 and arg2 parameters are operated at the bottom layer, and then the results are stored in the result. It is hoped that the result can be used in the Java layer. As for the return value of the method, it is the flag bit indicating whether the method execution is successful or not.

The requirements are implemented in JNI in two ways. Of course, these two are typical errors.

If you still remember the operation principle of JNI, it should be easy to understand that it only modifies the parameter value (in add1() and parameter address (in add2()) in the C thread. As for the parameters corresponding to the Java layer, there is no change, that is, when the basic types in JNI are used as parameters, they are only passed in as formal parameters, which has no impact on the upper layer. Of course, it is absolutely OK if it is used as a return value, but now we are talking about the method as a parameter value. So still this demand, how should we solve it?

Add two new methods in librarymanager.class

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>