What is “jobject thiz” in JNI and what is it used for?

后端 未结 3 1868
傲寒
傲寒 2020-12-31 06:25

I am having a hard time finding an answer to this. But, what is \"jboject thiz\" used for in JNI function calls? For example:

jobjectArray Java_com_gnychis         


        
相关标签:
3条回答
  • 2020-12-31 06:43

    I found this link that should help clarify the question.

    https://library.vuforia.com/articles/Solution/How-To-Communicate-Between-Java-and-C-using-the-JNI

    Here is an example in it that uses the "jobject".

    JNIEXPORT void JNICALL
    Java_com_qualcomm_QCARSamples_ImageTargets_ImageTargets_initApplicationNative(
                                JNIEnv* env, jobject obj, jint width, jint height)
    {
        ...
        jclass activityClass = env->GetObjectClass(obj);
        jmethodID getTextureCountMethodID = env->GetMethodID(activityClass,
                                                        "getTextureCount", "()I");
        if (getTextureCountMethodID == 0)
        {
            LOG("Function getTextureCount() not found.");
            return;
        }
        textureCount = env->CallIntMethod(obj, getTextureCountMethodID);
        ...
    }
    
    0 讨论(0)
  • 2020-12-31 06:46

    jobject thiz means the this in java class.

    Sometimes if you create a static native method like this.

    void Java_MyClass_method1 (JNIEnv *, jclass);
    

    jclass means the class itself.

    0 讨论(0)
  • 2020-12-31 07:01

    The following is a JNI wrapper function which has two parameters, and returns a primitive array of objects:

    jobjectArray Java_com_gnychis_awmon_Test( JNIEnv* env, jobject thiz );
    

    From the function name you have given I don't think it is complete, that is, you haven't respected the obligatory function name convention which is:

    1. Start the function with Java_

    2. Append the package name separated by _ (undescores) i.e. com_company_awesomeapp. So far the function name is composed of: Java_com_company_awesomeapp

    3. Append the Java class name where the native method has been defined, followed by the actual function name. So at this point we should have the following function name: Java_com_company_awesomeapp_MainActivity_Test

    The first parameter is a pointer to a structure storing all JNI function pointers, i.e. all the predefined functions you have available after you #include <jni.h>.

    The second parameter is a reference to the Java object inside which this native method has been declared in. You can use it to call the other methods of the Java object from the current JNI function, i.e. Call Java instance methods from JNI code written in C or C++.

    If for example you have the following Java class inside the MainActivity.java file:

    public class MainActivity extends Activity
    {
        static
        {
            try
            {
                System.loadLibrary("mynativelib");
            }
            catch (UnsatisfiedLinkError ule)
            {
                Log.e(TAG, "WARNING: Could not load native library: " + ule.getMessage());
            }
        }
    
        public static native Object[] Test();
    }
    

    Then, the jobject thiz parameter of the JNI function would be a reference to an object of type MainActivity.

    0 讨论(0)
提交回复
热议问题