Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Java JNI
I have to return a array of structure values from c++ (jni) to java. I have the c++ struture as follows
 
MyCStructure
{
byte *Data;
int dataLength;
int width;
int height;
}mycstr;
 
And i have my java structure as
 
public class MyJavaStructure
{
public byte[] Data;
public int dataLength;
public int width;
public int height;
}
And My JavaMainClass.java is
 
  MyJavaStructure[] mystr =  MyJavaStructure[4]; 
public native int Process();
public LSImage getValues()
{
     return this.mystr ;
}
public void setValues(MyJavaStructure bDetails)
{
     this.mystr = bDetails; 
}
I tried to pass Object array (MyJavaStructure) directly from JNI using GetMethodId of setValues.
 
My JNI code is
 
JNIEXPORT jint JNICALL Java_TestJNI_Process(JNIEnv *jEnv, jobject jObj)
{
 
    MyCStructure* mycstr = new MyCStructure[4];
 
    iRet = Process(&mycstr);
 
    jclass jClass = jEnv->GetObjectClass(jObj);
    jmethodID constructor;
 

    jmethodID jObjectID = jEnv->GetMethodID (jLegendClass, "setValues", "(LMyCStructure;)V");
    if (jObjectID == NULL)
    {
        throwJavaException(jEnv, "Unable to find the method setValues");
        return NULL;
    }
    jEnv->CallObjectMethod(jObj, jObjectID, (jobject *) mycstr);
 
    printf ("After setting the output data value\n");
 
    jEnv->DeleteLocalRef(jClass);
 
    return iRet;
}
The value returned in mycstr contains 4 array of structure (MyCStructure) values and i tried to assign in a MyJavaStructure class object but it returned null.
 
Am i doing right or is there i have to change anything ?
Posted 11-Feb-13 23:48pm
Comments
Matthew Faithfull at 12-Feb-13 6:03am
   
No, you're not doing it right or it would be working. From a C++ point of view it looks OK and I don't have enough Java to give you a solution but I do have a question which might point you in the right direction. Your MyCStructure contains a pointer but I don't see any way in which JNI can work out how much data this points to. It can't know that you're storing that info elsewhere in the structure. Given that there's no way it can be generating code to copy the data so it would have to effectively be doing a cast to the Java structure. I don't know what byte[] produces in terms of runtime memory layout in Java but I doubt that it is a single 4 or 8 byte pointer, more likely some sort of array structure with a header similar to the whole structure you're using. I believe this is the root of your problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 120
4 DamithSL 95
0 OriginalGriff 6,045
1 DamithSL 4,611
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 12 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100