4

I have a C++ application that needs to interface with a Java library. I'd like to receive notifications from the Java side so it needs to call back into the C++ app. 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.

Everything I've found so far is how to invoke Java callbacks from an external C++ function within a Java app. I need the opposite.

With straight C++ this, is of course, easy. 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?

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. The access API is a Java based API. My current plan is to build a Java wrapper that will handle all of the API calls. This wrapper will be invoked from my C++ app using JNI. When data is received, it will process it as much as possible and then notify my C++ app with the adjusted data.

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. There is no DLL to load for Java to use. Although I can make one if needed if everything can interface properly.

Function Path: C++ application -> Java class library -> C++ callback function

GKarRacer
  • 83
  • 8
  • Did you already manage to do the "C++ application -> Java" part? – André Puel Feb 06 '13 at 01:43
  • "There is no DLL to load for Java to use.".... you're going to have an awfully tough go of it writing JNI without a DLL. Get ready to make one (and start ***simple*** to get the interface down first.) – WhozCraig Feb 06 '13 at 02:43
  • I did some test functions to verify loading JNI and calling a Java class function. That was easy enough. It's the next step - Java back to C++ that I don't know yet. – GKarRacer Feb 06 '13 at 04:14
  • Starting simple: I completely agree and that is what I'm trying to nail down right now - the interface between Java -> C++. All examples I've found so far are calling C++ libraries from a Java app. But, that's not what I'm trying to do. – GKarRacer Feb 06 '13 at 04:16

1 Answers1

3

I found it. There is a function in the environment class called "RegisterNatives". With this I can register C++ callbacks for any Java class at runtime. I tried it and it works exactly as expected.

GKarRacer
  • 83
  • 8