pdi*_*ddy 5 java android swig function-pointers
我在C中有一些代码,其中一个方法有一个函数指针作为参数.我正在尝试在我的Android应用中使用C代码.
我决定使用SWIG来完成生成我需要的java文件的所有工作.一切都适用于常规函数(没有函数指针作为参数的函数).
但我不知道如何将我的JAVA方法作为回调函数传递给C函数.
这是一个例子:
这是我的multiply.h文件
typedef int (*callback_t) (int a, int b, int c);
int foo(callback_t callback);
Run Code Online (Sandbox Code Playgroud)
这是我的multiply.c文件
#include <stdio.h>
#include "multiply.h"
int foo(callback_t callback)
{
return callback(2, 4, 6);
}
Run Code Online (Sandbox Code Playgroud)
这是我的接口文件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"
Run Code Online (Sandbox Code Playgroud)
然后我运行以下swig命令来生成我需要的java文件
swig -java -package com.example.ndktest -o multiply-wrap.c mulitiply-swig.i
Run Code Online (Sandbox Code Playgroud)
然后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));
}
}
Run Code Online (Sandbox Code Playgroud)
exampleJNI.java
package com.example.ndktest;
public class exampleJNI {
public final static native int foo(long jarg1);
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
现在如何foo从我的java代码中调用此方法?参数类型为SWIGTYPE_p_f_int_int_int__int??? 我不明白如何将JAVA方法作为回调传递给C代码......我想我肯定在这里遗漏了一些东西....
任何帮助表示赞赏,谢谢
| 归档时间: |
|
| 查看次数: |
1562 次 |
| 最近记录: |