Calling C function that has Void* pointer
Hi
I am trying to use some c functions in java using the JNI. This C function has 3 inputs, 2 integers and a void* pointer. I can import the 2 integers from java but dont know how to deal with the void* pointer.
I would be greatful if someone could help me with this problem.
Thanks
Derek
I forgot to say, the c function also returns a void* pointer, how would I get the pointer back into java.
As near as I can tell you are operating at the wrong level for java/C interaction. JNI doesn't support a void*, so you need to define more prcisely - in java or JNI terms - what it is you are trying to pass out and into java.
Put another way, think of the data you want to pass, and not pointers.
It depends, do you want to use something from the void* or do you just want to keep track of it for use in another JNI method.
If the former then you need to extract the info in JNI.
If the latter then just store the pointer in a java long and in your other JNI methods convert it back.
More precisely, you can store any pointer in a java long primitive. In order to do this you need to cast the pointer to the appropriate type when you need to use it.jlong jPtr = (jlong)cPtr;andvoid *cPtr = (void*)jPtr;
andyba at 2007-6-29 19:42:06 >

Hi,
i guess the void * points to some C thingy. If you got
the void * from C (via JNI), treat it as a long and cast it back in JNI
as andyba suggested. If it is more than this (e.g. a C++ object)
there is no general solution, but there are numerous special ones.
If you tell a little more concrete what your void * points to,
there might be more concrete suggestions.
To get an idea, study e.g. swig's java module. But be warned,
this is no simple topic.
http://swig.sourceforge.net
Have fun,
Klaus
To be clear, any C pointer may be stored in a java long (jlong) variable.C++ objects can be represented by java wrapper (peer) classes.Pointers to C++ objects can be treated as any other pointer (thats all they are anyway).
andyba at 2007-6-29 19:42:06 >

Hi
Thanks for all the replies. Some of you have been asking for more information about the data the the pointer points to.
The void* is a c pointer that can return one of 4 types of data - int, long, double or char.
What I have done so far is used method overloading for the functions that have a void* pointer as the input. But for the functions that have a return type of void* this can not be done.
If I understand what you are saying, it is that you have functions that can return any of four types of value: int, long, double, char?
Well, you can do that if you want - just return a java object that has all four types of data member.
But it sounds either like goofy design, or I just don't understand which functions are returning which kinds of data.
>But it sounds either like goofy design...Not that unusual in C using a 'union'. And like other things in C/C++ probably overused.