Blueimp 論壇首頁

列印 2024/4/17 上午 07:27:36 在同一個網頁中顯示這個話題的所有文章
文章作者 jieh2011/8/24 下午 02:13:33
smile   Registry a java callback method to any C++ task-thread
這篇是我們討論要讓 C++ 呼叫 Java 執行結果的可能性時,C 同事提供的資料,尚未 study 且 J 同事還沒實作出來並與 K 同事的 C++ 語言整合。

http://mqjing.blogspot.com/2008/01/java-c-callback-java-method.html

Registry a java callback method to any C++ task-thread ...

Sometimes, in java application, we will launch a C++ working thread to do a high computing consuming job. When the job done, send a notification to the java application.

You can use a Message to finish the requirement.Also, you can use JNI to invoke any method of a java object that are referenced from the parameter jobject.

If you invoke the Java method in other thread in C++, you will face a problem! The JNI interface pointer (JNIEnv) is valid only in the current thread.If you want to access java object at other thread, you should use AttachCurrentThread to attach current thread to the java virtual machine.



How to use AttachCurrentThread in your code? An example is listed as following.


JNIEXPORT jint JNICALL Java_xxx(JNIEnv *env, jobject jobj, ...){

...

Data->jobj = env->NewGlobalRef(jobj);

// 建立 c++ thread, 執行高耗時程序, 完成時, call back 到 java method

}

=============== Other C++ thread method ==============

// Step 1: 目前的 JVM 取得 env 的範例

JNIEnv* env;
int bOk=curJavaVM->AttachCurrentThread((void**)&env, (void*) 0);


// Step 2: 取得 jclass 和 method Id

// 注意:

// 如果你的java callback 是 static method, 可以使用 FindClass

// 例如: jclass cls=env->FindClass("xxx/myclass");

// 但是, 如果你呼叫的 java callback 是一般 method , 則

// 請使用 GetObjectClass, 範例如下: 其中 jobj 為傳進來 jobject

jclass cls=env->GetObjectClass(Data->jobj);


jmethodID mid=env->GetMethodID(cls,"myfunction", "(I)V");


// Step 3: 利用 env 呼叫 放在 java 物件中的 method

env->CallVoidMethod(Data->jobj, mid, 123);
if(env->ExceptionOccurred()) {
printf("error occured on render2_callback method");
env->ExceptionDescribe();
env->ExceptionClear();
}

// Step 4: 因為 jobj 使用 NewGlobalRef , 所以記得移除
env->DeleteGlobalRef(Data->jobj);


其他程式


JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
{
curJavaVM=vm; // 取得 目前vm
return JNI_VERSION_1_2;
}
JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved)
{
}


----------------------------------------
支持小惡魔
BTC : 19tn3RnCuwZVukXAwyhDWZD4uBgUZoGJPx
LTC : LTFa17pSvvoe3aU5jbmfcmEpo1xuGa9XeA
知識跟八卦一樣,越多人知道越有價值;知識最好的備份方法,散播!
藍色小惡魔(林永傑): 臉書