使用接口在 JNI 中实现回调函数

编程入门 行业动态 更新时间:2024-10-28 07:20:48
本文介绍了使用接口在 JNI 中实现回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用接口"在 Java 中实现回调函数.我已将应用程序部分编写为 MyJavaFunction(int size, m_GetSizeInterface);

I need to implement callback function in Java using "interface". I have wrote the application part as MyJavaFunction(int size, m_GetSizeInterface);

m_GetSizeInterface 是一个包含回调函数 GetSize 的接口.此 GetSize 方法在应用程序中被覆盖.在 JNI 中,我需要调用具有原型的 CPP 函数 int MyCPPFunction(int size, int (*callback)(int* ID));

m_GetSizeInterface is an Interface which contains the callback function GetSize. This GetSize method is override in the application. In JNI I need to call a CPP function having prototype int MyCPPFunction(int size, int (*callback)(int* ID));

如何将此 GetSize 作为参数传递给 JNI 中的 MyCPPFunction?请帮忙

How can I pass this GetSize as parameter to MyCPPFunction in JNI? Please help

public int GetSize (m_SizeClass arg0)
{
    g_size = arg0.size;
        return 0;
}

推荐答案

这里的复杂之处在于您想要调用本机 C++ 代码,而您又想要调用 Java 方法.这实际上有点棘手.

The complication here is that you want to invoke native C++ code which you, in turn, want to invoke a java method. This is actually a bit tricky.

需要创建一个JNI C++函数供java调用,并匹配一个C++函数MyCPPFunction 回调签名.后者将充当调用 java 方法的包装器.

You need to create a JNI C++ function for java to call, and a C++ function matching the MyCPPFunction callback signature. The latter will act as a wrapper to call the java method.

因为包装器需要有关 JNI 环境的信息,而这些信息不能由参数提供(以免破坏签名),所以您创建了一些全局变量来保存它:

Because the wrapper will need information about the JNI environment, which cannot be provided by parameters (lest we ruin the signature) you create a few global variables to hold it:

jobject g_getSizeIface;
jmethodID g_method;
JNIEnv *g_env;

java 将调用的 C++ 函数如下:

The C++ function which java will call is the following:

JNIEXPORT void JNICALL Java_ClassName_MyCPPFunction
     (JNIEnv *env, jint size, jobject getSizeInterface)
{
      jclass objclass = env->GetObjectClass(getSizeInterface);
      jmethodID method = env->GetMethodID(objclass, "GetSize", "(m_SizeClass)I");
      if(methodID == 0){
          std::cout << "could not get method id!" << std::endl;
          return;
      }
      g_method = method;
      g_getSizeIface = getSizeInterface;
      g_env = env
      MyCPPFunction(size, WrapperFunc);
}

包装函数是这样的:

int WrapperFunc(int *id)
{
      jint retval;
      //marshalling an int* to a m_SizeClass boogy-woogy.
      ...
      g_env->ExceptionClear();
      retval = g_env->CallIntMethod(g_getSizeIface, g_method, 
                                    /*marshalled m_SizeClass*/);
      if(g_env->ExceptionOccurred()){
          //panic! Light fires! The British are coming!!!
          ...
          g_env->ExceptionClear();
      }     
      return rvalue;
}

这篇关于使用接口在 JNI 中实现回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-28 19:14:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/738856.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:回调   函数   接口   JNI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!