P
Ph. Barthelemy
Hi,
I am trying to write a piece a JNI code, and I have questions about
pointers read back and forth from the Java-side to the C-side.
Let say the C-side has two functions to access a device.
'connect' which opens the device and return a handle, eg. a pointer to
struct
'write' which writes bytes to the device, and needs the handle as a
parameters
So the Java-side calls the native 'connect' and retrieving the handle.
and later on, the java-side calls 'write'.
I'd like to stroe the handle as a member variable of the Java class.
So a need the translate a C pointer to a Java type.
Something like does not works : the 'handle' field is rubbish and a
can not reuse it in the 'write' function.
// partial code...
JNIEXPORT jint Java_package_class_connect( ... )
{
handle *h; // handle a struct...
h = openDevice();
jclass cls = (*env)->GetObjectClass(env, obj);
jfieldID fid = (*env)->GetFieldID(env, cls, "handle",
"Ljava/lang/Object;");
(*env)->SetObjectField(env, obj, fid, h);
}
My question is :
how am I suppose the cast the handle to pass it to the java-side ?
As an 'jobject' ?
(if so than my code maybe broken somewhere else, which is possible...
)
( I can not use return values, as the actual code uses 2 handles... )
something like
TIA
--Philippe
PS :
you want more details ?
I am trying the JNIze a bit of libusb-win32 to get access to USB
devices.
so my handles are actually a pointer to a usb_dev_handle and a pointer
to a usb_device.
I am trying to write a piece a JNI code, and I have questions about
pointers read back and forth from the Java-side to the C-side.
Let say the C-side has two functions to access a device.
'connect' which opens the device and return a handle, eg. a pointer to
struct
'write' which writes bytes to the device, and needs the handle as a
parameters
So the Java-side calls the native 'connect' and retrieving the handle.
and later on, the java-side calls 'write'.
I'd like to stroe the handle as a member variable of the Java class.
So a need the translate a C pointer to a Java type.
Something like does not works : the 'handle' field is rubbish and a
can not reuse it in the 'write' function.
// partial code...
JNIEXPORT jint Java_package_class_connect( ... )
{
handle *h; // handle a struct...
h = openDevice();
jclass cls = (*env)->GetObjectClass(env, obj);
jfieldID fid = (*env)->GetFieldID(env, cls, "handle",
"Ljava/lang/Object;");
(*env)->SetObjectField(env, obj, fid, h);
}
My question is :
how am I suppose the cast the handle to pass it to the java-side ?
As an 'jobject' ?
(if so than my code maybe broken somewhere else, which is possible...
)
( I can not use return values, as the actual code uses 2 handles... )
something like
TIA
--Philippe
PS :
you want more details ?
I am trying the JNIze a bit of libusb-win32 to get access to USB
devices.
so my handles are actually a pointer to a usb_dev_handle and a pointer
to a usb_device.