Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
545 views
in Technique[技术] by (71.8m points)

java - JNI Set String Field

I try to work with structure between C and Java. For this I created a Java class:

public class DriveInfo {
    public String val1;
    public String val2;
    public String val3;
}

This I used inside a Java function:

    private void getDriveInfos() {
        DriveInfo newInfo = new DriveInfo();
        newInfo.val1 = "Test";
        getDriveInfo(newInfo);
        logModel.add(logModel.getSize(), newInfo.val1);
    }

The getDriveInfo is declared in JNI Interface with:

private native int getDriveInfo(DriveInfo mClass);

JNIEXPORT jint JNICALL Java_JNIViie_getDriveInfo
  (JNIEnv *env, jobject job, jobject jStruct);

JNIEXPORT jint JNICALL Java_JNIViie_getDriveInfo
  (JNIEnv *env, jobject jobj, jobject jStruct){

    jclass clazz;
    jfieldID fid;
    jstring buffer = env->NewStringUTF("myname");

    clazz = env->GetObjectClass(jStruct);
    fid = env->GetFieldID(clazz,"val1","Ljava/lang/String;");
    if(clazz == 0){
        return 0;
    }
    env->SetObjectField(clazz,fid,buffer);


    return 0;

}

I expected that the getDriveInfo function will print out "myname" not "test". My question is now, what I have done wrong so that the val1 do not contain the "myname" string?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...