JNI Calls different in C vs C++?

前端 未结 4 1008
情深已故
情深已故 2020-12-09 02:07

So i have the following code in C that utilizes Java Native Interface however i would like to convert this to C++ but am not sure how.

 #include 

        
4条回答
  •  失恋的感觉
    2020-12-09 02:46

    I used to have the book Essential JNI. And while it is kinda dated, much of it still works today.

    If I recall correctly, in C, Java constructs are simply pointers. Thus, in your code, "(*env)->" is dereferencing pointers to give you access to the underlying methods.

    For C++, "env" is actually an object - a different entity than a C pointer. (And JNI can actually provide real objects for your C++ code to manipulate, since C++ actually supports objects.) So "env->" has a different meaning in C++, it means "call the method that is contained in the object pointed to by "env".

    The other difference, I believe, is that many of the C-JNI functions require that one of your parameters be the "JNIEnv *env". So in C you might have to say (*env)->foo(env, bar). With c++, the second reference to "env" is not necessary, so you can instead say "env->foo(bar)"

    Unfortunately, I don't have the above book in front of me, so I can't quite confirm this! But I think investigating those two things (specifically looking for them on google or in other JNI code) will get you pretty far.

提交回复
热议问题