Reputation: 259
I'm trying to use jni to call native functions in c++. All throughout the code there are
const void*
void **
I researched online and the java equivalent would be just Object type. However I'm confused what to pass in as an parameter when calling these function in c++. Object type really can't store anything so I'm a bit confused.
Thanks!
Upvotes: 0
Views: 77
Reputation: 965
As pointer lengths are not fixed across different platforms, this question is not as trivial as it sounds.
Three ways to consider :
Use a Java long ( a jlong in JNI ) which is 64-bits. This assumes no one ever needs a pointer longer than 64-bits, which is a controversy I'm happy to avoid. :-) Not strictly portable but probably safe ( for a while ).
Use an array ( in Java ) to store the required pointer. So your JNI code would create a jbyteArray of length sizeof(void *) and your Java code would handle a byte[], and you'd copy the pointer value into that array. This is safe and portable as long as your Java code doesn't start mucking around with the array values ( which would be silly ). As an aside I would suggest taking a global ref of the jbyteArray as you typically need to maintain some record of that pointer value for later disposal by the C/C++ side of things, so you need to prevent Java from garbage collecting it ( by taking a reference ).
Lastly let your C/C++ code manage a hash table ( or similar ) mappings handles to maps. The handles can be e.g. Java int types ( 32 bits on both the C/C++ and Java sides ), and the length of the pointers is the JNI code's problem. Advantage is abstraction and portability. Disadvantage is complexity ( unless you've C/C++ hash table code lying around to use ). The persistence of that mapping data is also an issue.
Upvotes: 1