从C ++回调到Java

时间:2011-06-17 08:43:16

标签: java c++ java-native-interface

我在C ++中用数字积分编写了以下代码:

// integrate.h:
#ifdef BUILDING_DLL
#define  DLL_MACRO __declspec(dllexport)
#else
#define  DLL_MACRO __declspec(dllimport)
#endif

extern "C" {
  typedef double (*Function1VariablePtr)(double x);
  double DLL_MACRO integrate(Function1VariablePtr function, double min, double max);
}

// integrate.cpp: 
#include "integrate.h"
double integrate(Function1VariablePtr function, double min, double max) {
  const int n = 1001;
  double dx  = (max - min)/(1.0*(n - 1));
  double sum = 0.0;
  for(int i = 0; i < n; i++) {
    double xmid = min + (i + 0.5)*dx;
    sum += function(xmid)*dx;
  }
  return sum;
}

现在我想从Java调用这个函数。 我发现如何直接在JNI“桥接”代码中实现集成:

// C++ "bridge" code to from/to Java:  
JNIEXPORT jdouble JNICALL 
Java_IntegrateJApp_JIntegrate(JNIEnv *jnienv, jclass jc,    
                              jdouble xmin, jdouble xmax) {
  jmethodID mid = jnienv->GetStaticMethodID(jc, "Function1D","(D)D");
  if (mid == 0)
    return - 1.0;
  const int n = 1001;
  double dx  = (xmax - xmin)/(1.0*(n - 1));
  double sum = 0.0;
  for(int i = 0; i < n; i++) {
    double xmid = xmin + (i + 0.5)*dx;
    double f = jnienv->CallStaticDoubleMethod(jc, mid, xmid);
    sum += f*dx;
  }
  return sum;
}

// Java code calling "bridge":
class IntegrateJApp { 
  public static void main(String[] args) { 
      System.loadLibrary("JIntegrate");
      double I = JIntegrate(0.0, 2*Math.PI);
      System.out.println( Double.toString(I) ); 
  } 
  public static double Function1D(double x) {
      return Math.sin(x);
  }
  public static native double JIntegrate(double xmin, double xmax);
} 

但是我不想直接在C ++桥代码中实现数值集成,而是在integrate.cpp中调用代码。

我该怎么做? integrate.cpp中的integrate()函数需要一个我没有的函数指针。 有没有办法使用JNI获取Java内部函数的指针?

提前感谢您的任何答案!

2 个答案:

答案 0 :(得分:3)

您必须使用C ++代码创建DLL并从JNI

调用它

加载DLL:

System.loadLibrary("PATH\\yourdllname.dll");

创建功能链接

public static native integrate(parameters);

答案 1 :(得分:2)

可以做的一种方法是使用指向成员函数的指针并更改集成函数的签名。

见下面的一般想法:

<强> functionwrapper.h

声明一个函数包装类。

class FunctionWrapper
{
public:
    typedef double (FunctionWrapper::*Function1VariablePtr)(double x);

    FunctionWrapper(JNIEnv*, jclass);
    double compute(double x);
};

<强> integrate.h

消除前一个指向函数typedef的指针,并更改方法签名以包含包装器对象和指向其成员函数的指针。

#include "functionwrapper.h"
extern "C" {
    double DLL_MACRO integrate(FunctionWrapper*, FunctionWrapper::Function1VariablePtr, double min, double max);
}

<强> integrate.cpp

将函数调用更改为成员函数调用。

#include "integrate.h"
double integrate(FunctionWrapper* wrapper, FunctionWrapper::Function1VariablePtr function, double min, double max)
{
    // ...
    sum += (wrapper->*function)(xmid)*dx;
    // ...
}

归还总和; }

JNI“桥梁”代码:

定义包装器代码并定义执行实际调用的函数。直接从JNI函数调用integrate函数:

#include "functionwrapper.h"

FunctionWrapper::FunctionWrapper(JNIEnv *jnienv, jclass jc) : m_jnienv(jnienv), m_jc(jc) {
    m_method= jnienv->GetStaticMethodID(jc, "Function1D","(D)D");
}

double FunctionWrapper:compute(double x) {
    return m_jnienv->CallStaticDoubleMethod(m_jc, m_method, x);;
}

// C++ "bridge" code to from/to Java:
JNIEXPORT jdouble JNICALL
Java_IntegrateJApp_JIntegrate(JNIEnv *jnienv, jclass jc,
                              jdouble xmin, jdouble xmax) {
    FunctionWrapper wrapper(jnienv, jc);
    return integrate(&wrapper, &FunctionWrapper::compute, 2, 3);
}