JNI and storing C pointers in the Java Class variables

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.
 
G

Gordon Beaton

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.

Store the pointers as longs in the Java object. Pass them back as
longs to the native functions that need them, and do the necessary
casts in the native code.

If you can't use return values, then use SetLongField() to update the
fields in the java object.

/gordon
 
P

philippe.barthelemy_GOOGLE

Hi,

Long does not work, but I need to double-check my code.
One question :
will pointer-as-long be also OK on a 64bits machines ?
( I do not have an Itanium, but AMD does have fine 64bits CPU... )

--p
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top