简体   繁体   English

通过JNI从Java回调回本机应用程序

[英]Calling back into a native application from Java, via JNI

So I built this C library. 所以我建立了这个C库。 It calls Java methods via the JNI, everything works fine. 它通过JNI调用Java方法,一切正常。 But how can I call back from Java into my C application? 但是,如何从Java回调到C应用程序中呢?

I imagine the following: I do have a function, I take its address, I have a function pointer. 我想象以下情况:我确实有一个函数,我取得了它的地址,我有一个函数指针。 I pass the pointer to Java (for example as a callback, an event handler, or anything). 我将指针传递给Java(例如,作为回调,事件处理程序或任何东西)。 How can I call that function from within Java? 如何在Java中调用该函数?

Is there maybe a method to dynamically and explicitly bind a native void method(); 是否有一种方法可以动态地显式绑定native void method(); to a function pointer or something? 指向函数指针或其他东西?

Is there maybe a method to dynamically and explicitly bind a native void method(); 是否有一种方法可以动态地显式绑定本机的void method(); to a function pointer or something? 指向函数指针或其他东西?

Yes there is! 就在这里! You can use RegisterNatives to dynamically bind Java methods with C/C++ functions. 您可以使用RegisterNatives动态地将Java方法与C / C ++函数绑定。

To change implementation you can first call UnregisterNatives followed by a new call to RegisterNatives. 要更改实现,您可以先调用UnregisterNatives,然后再调用RegisterNatives。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM