Storage of C pointers in java
I have a native method that returns a handle to some c++ object that I later pass as a parameter to another native method. I store the pointer as a java int through the return code. I noticed in some other posts (eg. http://forum.java.sun.com/thread.jsp?forum=52&thread=165499 ) that a long type is suggested for this function.
I'm programming on Windows NT, isn't this c++ pointer a 32-bit value? Why use a long for storage of these pointers? I'd appreciate any feedback.
[500 byte] By [
RTDog19] at [2007-9-26 5:55:36]

I believe that when 64-bit Windows is released you will need a 64-bit pointer. Using the Java "long" instead of an "int" won't hurt and it will be one less thing you have to mess with in the future.
Hi,with a little effort, JNI can be source code compatibleon different machines. Why should you loose that makingpointers int ?Have fun,Klaus
>Why use a long for storage of these pointers?
Unlike the other posters I am a bit more pragmatic in my choice of long over int.
A win32 pointer just fits inside a int. However, that means it also might end up setting the sign flag on the bit. Since java doesn't believe in unsigned values, using that extra bit might cause complications.
So I figure I would rather be safe than sorry.
I've been using Java longs to store my C pointers for a while now--it makes no difference. Remember, the abstractions a higher level language places on data are just that, abstractions. At the machine level, bits are bits and it could care less. That's what type casting and bit-wise masking are for.
I am using JNI for Java to C++ interface.
I need to call from a Java function to a C++ function with a buffer (doesn't matter which type - String, ByteArray, CharArray...) without having to copy the Java buffer to a C++ buffer.
It means that somehow I need to work with pointers.
Do you know to do that ?
Also, can you tell me how to create a forun using the above question.
Thanks.
In Java, your declaration of a native function that copies bytes from a C array
into a java byte array would look something like this:
package mypackage;
public class myclass {
private native void GetCBuffer(byte[] javabuffer);
.
.
.
}
In your C code, the JNI call would look something like this:
char cbuffer[500]; // The c buffer to copy from
/*
* Class:myclass
* Method:GetCBuffer
* Signature: ([B)V
*/
JNIEXPORT void JNICALL mypackage_myclass _GetCBuffer
(JNIEnv *inEnv, jobject inObj, jbyteArray theData)
{
// Use JNI calls to copy the bytes from our C array into a Java array
jsize size = inEnv->GetArrayLength(theData);
inEnv->SetByteArrayRegion(theData, 0, size, (jbyte *)(&(cbuffer[0])));
}
I would strongly suggest you get a copy of "The Java Native Interface" by Liang.
It's the best book out there on the JNI.
I hope this helps.
>without having to copy the Java buffer to a C++ buffer.Nope. Not in the current version of the JNI interface at any rate.I think the next version is supposed to support a very limited version of that.