Reputation: 1865
I'm loading a C DLL from a program written in Java. I want to be able to call one of the methods from the DLL with this declaration :
dll_function(const char* foo1, const char* foo2, const char* foo3, void** bar, size_t* bar2);
How do I call this method with arguments of correct type in Java ? I know (theorically) how to call it, but what I would like to know is how to pass the "void**" and "size_t*" from my Java program ? Basically, I want to know what the "equivalent type" for void and size_t*** is in Java...
I found the Pointer class but didn't manage to get it work ? Many thanks :)
Upvotes: 7
Views: 4174
Reputation: 11
It is not the same... a "new" is equivalent to an alloc and return a pointer to a memory zone. So here you just pass the pointer to the memory zone.
What you want is to pass the pointer to the pointer zone. I don't thing the equivalent exists in Java.
Upvotes: 0
Reputation: 1865
I tried your solutions but I guess I did not really understand correctly... But I solved my problem using this :
import com.sun.jna.ptr.IntByReference;
I called the C function like that in my Java Program :
IntByReference myVoidPointerPointer = new IntByReference();
myCLibrary.myCFunction(myVoidPointerPointer);
The C function "myCFunction" looks like this :
void myCFunction(void** parameter);
Is it OK to do that ? It's working, but I was wondering if it's a correct way to do it.
Upvotes: 0
Reputation: 12204
I worked on a Java/JNI/C project several years ago, and we had to maintain opaque C pointers within Java objects. We used long
values on the Java side to hold the C pointers, which were converted on the JNI side from jlong
to void*
or whatever pointer type we needed.
Since the Java long
type is 64 bits wide, and JNI/C pointers are typically either 32 bits or 64 bits wide, we did not have any problems converting between them.
Upvotes: 2
Reputation: 3417
The size_t
is an integer used for the size of a variable in memory; as such you should be safe using unsigned long
in Java.
The void*
is likely a pointer to an object of unknown type. This question, is quite interesting on the matter. In Java Object
is typically used in this case, but I do not know how you would convert between them, though this question might help there.
Upvotes: 1