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

查看:380
本文介绍了什么是"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函数指针的结构的指针,即所有

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对象的引用.

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天全站免登陆