views:

509

answers:

2

Hey,

I have a Java program that calls a C++ program to authenticate users. I would like the program to return either true or false, and if false, update a pointer to an error message variable that i then can grab from the Java program.

Another explination:

The nataive method would 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 that method here:

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

then in my C++ program i would check if the user is authenticated and store it in a boolean, then if false, get the error message and update &error with it. Then return that boolean to my Java program where i could display the error or let the user through.

Any ideas?

Originally i had it so the program would return either "true" or the error message, as a jstring, but my boss would like it as described above.

A: 

If your scenario is as simple as you describe, why not just return NULL for the "true" case (even though this is typically backwards of what you might expect) and return a pointer to the error struct in the case something goes wrong?

Goyuix
because the boss man says so. thanks for the idea though
Petey B
+1  A: 

There is a common technique to simulate additional out parameter by using object array in 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 a another way to do by returning a result object

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

In the 2nd case you will need to program more in the JNI layer.

Dennis Cheung
Thanks, what would i need to do in the Native (C++) code to update "error" with the error message though?
Petey B
prepare your String object, and assign it into the array
Dennis Cheung