Originally posted by Ernest Friedman-Hill:
You could also return a pointer to a C++ object; you'd just cast it to an int or long and return that.
That doesn't sound very portable. JNI code ought to be portable, whenever possible. You never know when someone may ask you to make an implementation on a new platform.
A simple way to do this would be to call the C++ thing a "handle", not a "pointer", and declare its size as either int (4 bytes) or long (8 bytes). On platforms where a pointer would fit in that size, the C++ code could typedef the handle to be a pointer. On other platforms, the C++ code could implement some kind of mapping between handles and pointers. The important thing is that the interface between the C++ and Java code shouldn't talk about pointers.