JNI Pass By Reference, is this possible?

I have a Java program that calls C ++, a program for authenticating users. I would like the program to return either true or false, and if false, update the pointer to the variable of the error message that I can then extract from the Java program.

Another explanation:

The native method will look something like this:

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

I would call this method here:

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

Then in my C ++ program I would check if the user will be authenticated and save it in a logical state, and then, if false, get an error message and update the error with it. Then return this boolean to my Java program, where I can display the error or allow the user to execute.

Any ideas?

Initially, I had this, so the program would return either "true" or an error message like jstring, but my boss would like, as described above.

+3
source share
2 answers

There is a general method for modeling an additional parameter out using an array of objects in the parameter.

For example.

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

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

There is another way to do this by returning a result object

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

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

+6
source

, , NULL "" ( , ) , - ?

+1

Source: https://habr.com/ru/post/1719530/


All Articles