什么是“jobject thiz"?在 JNI 中,它的用途是什么? [英] What is "jobject thiz" in JNI and what is it used for?

查看:62
本文介绍了什么是“jobject thiz"?在 JNI 中,它的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难找到答案.但是,JNI 函数调用中使用的jboject thiz"是什么?例如:

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_awmon_Test( JNIEnv* env, jobject thiz ) {

我经常使用 env 来分配对象,但我从来没有使用过 thiz,我不确定它是做什么用的.仅用于知识目的.

I use env to allocate objects often, but I've never used thiz and I'm not sure what it is for. Just for knowledge purposes.

推荐答案

下面是一个JNI封装函数,它有两个参数,返回一个原始对象数组:

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. 用Java_启动函数

  1. Start the function with Java_

附加以 _ 分隔的包名称(取消划线),即 com_company_awesomeapp.至此函数名组成:Java_com_company_awesomeapp

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

在已定义本地方法的地方附加 Java 类名,后跟实际的函数名称.所以此时我们应该有如下函数名:Java_com_company_awesomeapp_MainActivity_Test

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

第一个参数是一个指向存储所有JNI函数指针的结构的指针,即所有#include <jni.h> 之后可用的预定义函数.

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>.

第二个参数是对已在其中声明本机方法的 Java 对象的引用. 您可以使用它从当前JNI函数调用Java对象的其他方法,即从C编写的JNI代码调用Java实例方法或 C++.

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++.

例如,如果您在 MainActivity.java 文件中有以下 Java 类:

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();
}

那么,JNI 函数的 jobject thiz 参数将是对 MainActivity 类型对象的引用.

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

这篇关于什么是“jobject thiz"?在 JNI 中,它的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆