我有一个 java 回调函数,我想将其传递给 C++ 包装器,后者又将其传递给 C/C++ 函数。
我该怎么做?
我想包装器必须创建某种类型的 C++ lambda 函数或类似的东西来解析参数。
编辑:
这是 C 中的回调签名。我将在 Java 中实现它们。 (至少其中一些)
typedef struct ASIOCallbacks
{
void (*bufferSwitch) (long doubleBufferIndex, ASIOBool
void (*sampleRateDidChange) (ASIOSampleRate sRate);
long (*asioMessage) (long selector, long value, void* message, double* opt);
ASIOTime* (*bufferSwitchTimeInfo) (ASIOTime* params, long doubleBufferIndex, ASIOBool directProcess);
} ASIOCallbacks;
顺便说一句:永远不会使用 ASIOTime *params。 我在 C 函数中设置回调。这是路径...
Java 将回调传递给 C++ 包装器。 C++ 包装器将它们传递给 C 函数以设置回调。
最佳答案
我想您已经在 C++ 代码中的某处调用了 JVM 和 JNI 环境来托管您的 Java 函数:
JavaVM *jvm; // Pointer to the JVM (Java Virtual Machine)
JNIEnv *env; // Pointer to native interface
... // initialisation of these
让我们看第一个例子:
您的 java 函数将类似于:
public class Callback {
public static void bufferSwitch (long index, ASIOBool b);
}
我猜你已经用 Java 实现了类似 ASIOBool 类型的东西。如果没有,你将不得不这样做。
现在您可以编写一个C++ 包装器:
class Callback {
public:
static void bufferSwitch (long index, ASIOBool b);
...
};
void Callback::bufferSwitch (long index, ASIOBool b) {
jclass cls = env->FindClass("Callback"); // loads the Java class
if (cls==nullptr)
throw exception("Java class not found");
jmethodID mid = env->GetStaticMethodID(cls, "bufferSwitch","(JLASIOBool;)V");
if (mid==nullptr)
throw exception("Java method with appropriate signature not found");
// primitive types can be passed by value
// but for the other we must build an object
jclass cls2 = env->FindClass("ASIOBool"); // loads the Java class
if (cls2==nullptr)
throw exception("Java class not found");
jmethodID ctor = env->GetMethodID(cls2, "<init>", "()V");
if (ctor==nullptr)
throw exception("Java class constructor not found");
jobject jb = env->NewObject(cls2, ctor);
// ... to do: you have to initialize the Java ASIOBool object with the C++ ASIOBool components
// so if it's only a bool, better use the fundamental java type Boolean
// to avoid building a java object from C++
env->CallStaticVoidMethod(cls2, mid, (jlong)index, jb); // FINALLY WE CAN CALL !
env->DeleteLocalRef(jb); // tell the java garbage collector that we no longer use the tmp object
}
请注意,对于用作参数的每个 Java 对象(也不是基本类型),您必须提供签名。 fundamental types are coded作为一封信。其他类型/类以 L 开头,后跟完整的类名和分号。对于出现在包中的类,您必须指定全名(例如:“Ljava/lang/String;”用于 java 字符串)。
要了解更多信息:C++ JNI tutorial和 C JNI tutorial .
关于java - 传递给 C 的 JNI 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30278677/