swig c java gemt,SWIG C函数指针和JAVA

博客讲述了如何在Android应用中使用C代码时,通过SWIG处理函数指针作为参数的问题。作者面临的问题是如何将JAVA方法作为回调函数传递给C函数,SWIG生成了包含SWIGTYPE_p_f_int_int_int__int类型的参数,不清楚如何在JAVA代码中正确调用。寻求帮助以理解如何将JAVA方法与C函数接口匹配并进行回调。
摘要由CSDN通过智能技术生成

我在C中有一些代码,其中一个方法有一个函数指针作为参数.我正在尝试在我的

Android应用中使用C代码.

我决定使用SWIG来完成生成我需要的java文件的所有工作.一切都适用于常规函数(没有函数指针作为参数的函数).

但我不知道如何将我的JAVA方法作为回调函数传递给C函数.

这是一个例子:

这是我的multiply.h文件

typedef int (*callback_t) (int a, int b, int c);

int foo(callback_t callback);

这是我的multiply.c文件

#include

#include "multiply.h"

int foo(callback_t callback)

{

return callback(2, 4, 6);

}

这是我的接口文件multiply-swig.i

%module example

%{

/* Includes the header in the wrapper code */

#include "multiply.h"

%}

/* Parse the header file to generate wrappers */

%include "multiply.h"

然后我运行以下swig命令来生成我需要的java文件

swig -java -package com.example.ndktest -o multiply-wrap.c mulitiply-swig.i

然后swig生成以下文件:

example.java

package com.example.ndktest;

public class example {

public static int foo(SWIGTYPE_p_f_int_int_int__int callback) {

return exampleJNI.foo(SWIGTYPE_p_f_int_int_int__int.getCPtr(callback));

}

}

exampleJNI.java

package com.example.ndktest;

public class exampleJNI {

public final static native int foo(long jarg1);

}

SWIGTYPE_p_f_int_int_int__int.java

package com.example.ndktest;

public class SWIGTYPE_p_f_int_int_int__int {

private long swigCPtr;

protected SWIGTYPE_p_f_int_int_int__int(long cPtr, boolean futureUse) {

swigCPtr = cPtr;

}

protected SWIGTYPE_p_f_int_int_int__int() {

swigCPtr = 0;

}

protected static long getCPtr(SWIGTYPE_p_f_int_int_int__int obj) {

return (obj == null) ? 0 : obj.swigCPtr;

}

}

现在如何从我的java代码中调用这个foo方法?参数的类型为SWIGTYPE_p_f_int_int_int__int ???我不明白如何将JAVA方法作为回调传递给C代码……我想我肯定在这里遗漏了一些东西….

任何帮助表示赞赏,谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值