[英]Calling a c++ callback from Java invoked from C++ application via JNI
I have a C++ application that needs to interface with a Java library. 我有一个需要与Java库接口的C ++应用程序。 I'd like to receive notifications from the Java side so it needs to call back into the C++ app.
我想从Java端收到通知,因此需要回调到C ++应用程序。 I've found numerous examples on invoking Java functions from C++ (this is the easy part) and calling C/C++ functions from Java that are exported from a DLL.
我发现了很多关于从C ++调用Java函数的例子(这是一个简单的部分),并从Java调用从Java导出的C / C ++函数。
Everything I've found so far is how to invoke Java callbacks from an external C++ function within a Java app. 到目前为止,我发现的一切都是如何从Java应用程序中的外部C ++函数调用Java回调。 I need the opposite.
我需要相反的。
With straight C++ this, is of course, easy. 使用直接C ++,这当然很简单。 I'd simply pass a function pointer to the class which would in turn call as a callback when needed.
我只是将一个函数指针传递给该类,该类将在需要时调用为回调函数。 How can I do this when I'm instead invoking a Java function?
当我改为调用Java函数时,我该怎么做?
In case there's a better way altogether than what I'm asking for here is the overall application: 如果有一个更好的方式,那么我要求的是整个应用程序:
My C++ application needs to access an external server which will stream data back to my app. 我的C ++应用程序需要访问外部服务器,该服务器将数据流回我的应用程序。 The access API is a Java based API.
访问API是基于Java的API。 My current plan is to build a Java wrapper that will handle all of the API calls.
我目前的计划是构建一个Java包装器,它将处理所有API调用。 This wrapper will be invoked from my C++ app using JNI.
这个包装器将使用JNI从我的C ++应用程序调用。 When data is received, it will process it as much as possible and then notify my C++ app with the adjusted data.
收到数据后,它会尽可能地处理数据,然后通过调整后的数据通知我的C ++应用程序。
To sum up how can I call a C++ function callback from a Java class which was in turn invoked via JNI from a C++ application. 总结如何从Java类调用C ++函数回调,而Java类又是通过JNI从C ++应用程序调用的。 There is no DLL to load for Java to use.
没有DLL加载以供Java使用。 Although I can make one if needed if everything can interface properly.
如果一切都能正常接口,我可以根据需要制作一个。
Function Path: C++ application -> Java class library -> C++ callback function 功能路径:C ++应用程序 - > Java类库 - > C ++回调函数
I found it. 我找到了。 There is a function in the environment class called " RegisterNatives ".
环境类中有一个名为“ RegisterNatives ”的函数。 With this I can register C++ callbacks for any Java class at runtime.
有了这个,我可以在运行时为任何Java类注册C ++回调。 I tried it and it works exactly as expected.
我尝试了它,它的工作方式与预期完全一致。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.