JNI - Passing and returning complex values

A

Aditya Rajgarhia

Did you see my suggestion about using SWIG to wrap the structs and passing
the wrapped structs to java, providing write-through access and so
avoiding any copying? The more i think about it, the more i think this is
a better way to do it than copying. And there's no GC problem.

Ok, I read the Java part of the manual and ran SWIG on my C code. It
generated the wrapper classes in Java. However, the getters and
setters are created in Java, not in C. For my problem it seems that
they should be in C, like in Tom's original solution. I want to set
the struct in the C code before passing to Java, and then retrieve the
returned (updated) values in C.

Also, SWIG generated the JNI code for calling C code from the Java
code, which is the opposite of what I need.

So if I use SWIG, am I supposed to write the C getters and setter
myself? In that case, there would be no point in using SWIG, if all it
did was to type out the data fields of the classes. The majority of
the work in my case is writing the C getters and setters and the C JNI
code for calling the equivalent Java methods.

Unless I am missing something completely, and the generated code is
indeed what I need. Please point out if so :)

Thanks
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Aditya Rajgarhia schreef:
|> Did you see my suggestion about using SWIG to wrap the structs and
passing
|> the wrapped structs to java, providing write-through access and so
|> avoiding any copying? The more i think about it, the more i think this is
|> a better way to do it than copying. And there's no GC problem.
|
| Ok, I read the Java part of the manual and ran SWIG on my C code. It
| generated the wrapper classes in Java. However, the getters and
| setters are created in Java, not in C. For my problem it seems that
| they should be in C, like in Tom's original solution. I want to set
| the struct in the C code before passing to Java, and then retrieve the
| returned (updated) values in C.
|
| Also, SWIG generated the JNI code for calling C code from the Java
| code, which is the opposite of what I need.
|
| So if I use SWIG, am I supposed to write the C getters and setter
| myself? In that case, there would be no point in using SWIG, if all it
| did was to type out the data fields of the classes. The majority of
| the work in my case is writing the C getters and setters and the C JNI
| code for calling the equivalent Java methods.
|
| Unless I am missing something completely, and the generated code is
| indeed what I need. Please point out if so :)

Well, SWIG means ‘Simplified Wrapper Interface Generator’ and it is
designed to wrap C/C++ code in other languages, of which Java is one.
It seems like this is not what you need, so then this is not the right
tool for you. I don’t know about a tool which does the inverse, but you
might ask in the SWIG mailing list.

But are you sure it is not what you need? You can do all the getting
and setting stuff from Java, so why wouldn’t you?

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iEYEARECAAYFAkh1uZoACgkQe+7xMGD3itRihwCbBBfZeyToIahiKSxxgES5mr2b
sMAAnj6nHlkjXwH65BApjnJS0LHrr3qo
=3Fex
-----END PGP SIGNATURE-----
 
T

Tom Anderson

Ok, I read the Java part of the manual and ran SWIG on my C code. It
generated the wrapper classes in Java. However, the getters and setters
are created in Java, not in C. For my problem it seems that they should
be in C, like in Tom's original solution. I want to set the struct in
the C code before passing to Java, and then retrieve the returned
(updated) values in C.

You don't need a wrapper on the C side - you just use the original struct.
The wrapper uses the same actual struct, it doesn't copy it - it's a way
for the java code to see the C struct.
Also, SWIG generated the JNI code for calling C code from the Java code,
which is the opposite of what I need.

Right. Sadly, for callbacks, you will need to write the JNI stuff
yourself. Attach the thread, get a JNIEnv pointer, sort out your object
reference and method ID, and make the call. Don't forget to detach the
thread again before returning.

tom
 
A

Aditya Rajgarhia

Ok, so I wrote all the wrappers and the JNI code myself and finally
got a couple of functions to work. It is rather messy (and error-
prone) but anyway, now that I have a better idea of things I was
reading through this thread again and wondering if indeed I could use
SWIG to make the code cleaner than writing it from scratch.
You don't need a wrapper on the C side - you just use the original struct.
The wrapper uses the same actual struct, it doesn't copy it - it's a way
for the java code to see the C struct.

Well, it seems that this would work if I first create a wrapper object
in Java, which would hold a pointer to a C struct. I can populate the
struct in Java using the setter functions, which would write the
struct on the C side. Then, the Java code could call the native
function and pass the wrapper object as a parameter. The native
function would get the pointer to the struct from the wrapper object
and operate directly on it. Right?

Certainly this would be very handy, but I'm wondering if it would be
of much use in my situation where I first wish to create and populate
the struct in C (as I describe in the first post in the thread). The
Java method will then operate on this struct using the wrapper object.
To me it does seem as if I'll need the getter/setters on the C side,
because I cannot pass this struct to Java directly or through a
pointer (since Java can't access the C pointer unlike the other way
around).
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top