I'm trying to return an array of long values to
Java to process. I believe the problem is in the C++ code. Here is the function definition:
JNIEXPORT jlongArray JNICALL Java_papers_pi_PiAbsConn_pipt_1location
(JNIEnv *env, jobject obj, jlong pt) {
long arLocs[5];
jlongArray buf = env->NewLongArray(5);
if(buf == NULL) {
return NULL; //Out of Memory
}
long ret = pipt_location(pt, arLocs);
if(ret == 0){
for(int i=0;i<5;i++) {
printf("Location %d is %ld.\n", i, arLocs[i]);
env->SetLongArrayRegion(buf, i, 1, (const jlong*)&arLocs[i]);
}
}
return buf;
}
The printf statement verifies that I got the correct data from the pipt_location call. The code compiles. But when I get back to Java the array appears to have all 0s in it. Here is the java method:
public abstract class PiAbsConn {
// Native function calls
...
public native long[] pipt_location(long pt);
...
}
public class PiConn extends PiAbsConn {
...
public long GetLocations(long pt, long arLocs[]) {
long ret = 0;
try {
arLocs = pipt_location(pt);
} catch(Exception e) {
ret = e.hashCode();
}
return ret;
}
...
}
public class PiTag extends PiConn {
private long[] mlngLocations;
...
private void FindAttributes(long pipt) {
long arLocs[] = new long[5];
// Locations
try {
if(GetLocations(pipt, arLocs) == 0) {
System.out.println("GetLocations(long, long[]) suceeded.");
for(int i=0;i<5;i++) {
System.out.println("arLocs[" + i + "] = " + arLocs[i]);
mlngLocations[i] = arLocs[i];
}
}
} catch (NullPointerException e) {
mlngStatus = e.hashCode();
System.out.println("No valid values returned. Error " + mlngStatus);
}
}
...
}
I've ellipsed out the code that isn't relavent to the question. Here is the output:
Called SetTagName.
Looking for pipt for
Test.
Returned 76820 as pipoint.
Called pipt_location for point 76820.
Location 0 is 22.
Location 1 is 235.
Location 2 is 0.
Location 3 is 1.
Location 4 is 0.
GetLocations(long, long[]) suceeded.
arLocs[0] = 0
arLocs[1] = 0
arLocs[2] = 0
arLocs[3] = 0
arLocs[4] = 0
Location1 of tag Test is 0
I've tried a couple of variations on the line setting the array values to be passed back to Java.
env->SetLongArrayRegion(buf, i, 1, arLocs[i]); (won't compile)
env->SetLongArrayRegion(buf, i, 1, &arLocs[i]); (won't compile)
and a few other wierd ones.
Any suggestions would be appreciated.
Thanks,
Dave