Java – JNI is passed by reference. Is it possible?

I have a java program that calls the C program to authenticate users I want the program to return true or false. If false, update the pointer to the error message variable, and then I can get the pointer from the Java program

Another exploration:

Native methods look like this:

public native String takeInfo(String nt_domain,String nt_id,String nt_idca,String nt_password,String& error);

I'll call it the method here:

boolean canLogin = takeInfo(domain,userID,“”,userPass,String& error)

Then in my C program, I will check whether the user is authenticated and store it in a Boolean value, get the error message and use it to update & the error The Boolean value is then returned to my java program, where I can display the error or let the user pass

Any ideas?

Initially I had it, so the program would return "true" or an error message as a jstring, but my boss wanted it to be as described above

Solution

A common technique is to simulate additional out parameters by using an object array in parameters

For example

public native String takeInfo(String nt_domain,String[] error);

boolean canLogin = takeInfo(domain,"",error);
if(!canLogin){
   String message = error[0];
}

There is another way to return the result object

class static TakeInfoResult{
   boolean canlogon;
   String error;
}
TakeInfoResult object = takeInfo(domain,userPass);

In the second case, you need to program more in the JNI layer

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
分享
二维码
< <上一篇
下一篇>>