Passing Java function pointers to C

1,782 views
Skip to first unread message

SoothingMist

unread,
Oct 17, 2011, 11:17:42 AM10/17/11
to jna-...@googlegroups.com

Have searched this forum, spent a lot of time searching the Internet, and read numerous articles. Yet, I can not find an example that exposes a solution to my question. Am hoping the community can help.

There is a need to pass a Java function pointer to C and then have C call that function. The Java function has some number of arguments.

JNA's Function class (http://jna.java.net/javadoc/com/sun/jna/Function.html) convinces me that this should be possible without resorting to the Callback class.


The application resides on a Linux system.

Can someone point me to an example of the use of JNA's Function class or otherwise show how to pass a Java function pointer to C?

Many thanks for any insights you can offer.

SoothingMist

unread,
Oct 17, 2011, 1:41:16 PM10/17/11
to jna-...@googlegroups.com
JNA's function class description contains this phrase, "An abstraction for a native function pointer. An instance of Function represents a pointer to some native function." A colleague's description of "native" indicates that word refers to functions within the C code. So, it appears that the Function class does not help in this situation. Yet, the Callback approach does not appear to fit this situation. Is there a more direct approach to sending an n-argument Java function pointer to C for C to call?

Timothy Wall

unread,
Oct 17, 2011, 2:15:51 PM10/17/11
to jna-...@googlegroups.com
JNA's documentation contains this phrase:

JNA supports supplying Java callbacks to native code. You must define an interface that extends the Callback interface, and define a single callback method with a signature that matches the function pointer required by the native code.

(http://twall.github.com/jna/3.3.0/javadoc/overview-summary.html#callbacks)

SoothingMist

unread,
Oct 17, 2011, 2:21:03 PM10/17/11
to jna-...@googlegroups.com
Thanks for the response Timothy. The article at http://tutortutor.ca/cgi-bin/makepage.cgi?/tutorials/ct/jnmjnae does explain JNA's callback process and gives a good example. However, what the article describes sounds like such a round-about means of passing a pointer for C to call. It may be the only way though.

Timothy Wall

unread,
Oct 17, 2011, 3:18:27 PM10/17/11
to jna-...@googlegroups.com
You pass a java object that implements Callback and defines a method which implements its functionality. What's round-about about that?

Callback myCallback = new MyCallbackType() {
public void invoke(int someArgument) {
System.out.println("Callback called!");
}
};
myNativeFunction(myCallback);

SoothingMist

unread,
Oct 17, 2011, 3:22:44 PM10/17/11
to jna-...@googlegroups.com
Thanks Timothy. Maybe my brain has just not quite got it all yet. Let me whittle on it some more.

SoothingMist

unread,
Oct 20, 2011, 1:49:17 PM10/20/11
to jna-...@googlegroups.com
We have solved the issue at hand. Many thanks to Timothy.

The articles that proved most useful are:

http://tutortutor.ca/cgi-bin/makepage.cgi?/tutorials/ct/jnmjnae

http://www.newty.de/fpt/fpt.html
Reply all
Reply to author
Forward
0 new messages