轉載請聲明:原文轉自:http://www.cnblogs.com/xiezie/p/5930503.html html
JNIEXPORT void JNICALL Java_nativeMethod (JNIEnv *env, jobject thiz) { MyCPlusObj *obj = new MyCPlusObj(); jclass clazz = (jclass)(*env).GetObjectClass(thiz); jfieldID fid = (jfieldID)(*env).GetFieldID(clazz, "mObj", "I"); (*env).SetIntField(thiz, fid, (jint)obj); }
jclass objClazz = (jclass)env->GetObjectClass(obj);//obj爲對應的JAVA對象 jfieldID fid = env->GetFieldID(objClazz, "mObj", "I"); jlong p = (jlong)env->GetObjectField(obj, fid); MyCPlusObj *cPlusObj = (MyCPlusObj *)p; //cPlusObj 爲JAVA對象對應的C++對象
jobject gThiz = (jobject)env->NewGlobalRef(thiz);//thiz爲JAVA對象 (*obj).javaObj = (jint)gThiz;