Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call a function pointer #31

Open
pron opened this issue Jan 27, 2015 · 9 comments
Open

Call a function pointer #31

pron opened this issue Jan 27, 2015 · 9 comments
Milestone

Comments

@pron
Copy link

pron commented Jan 27, 2015

Is there a way to invoke a function pointer received from C code?

@headius
Copy link
Member

headius commented Apr 23, 2015

Directly? Not really. But I think you have at least two options:

  • Bind syscall directly. I have no idea the correct way to call it from there because I've never used it.
  • Use jffi to invoke the function. jffi contains the low-level native bits that jnr-ffi uses for function calls. See Foreign, which has "invoke" endpoints, and look at code that uses them.

Do you think this is a feature we should lift up to jnr-ffi?

@pron
Copy link
Author

pron commented Apr 23, 2015

Do you think this is a feature we should lift up to jnr-ffi?

Oh, absolutely! This is the scenario I'm facing: I'm trying to implement an SPI for a filesystem in Java. The Java method receives a C callback -- a visitor -- which it should then invoke for each file in a directory.

AFAIK, JNA has this feature, too.

@SerCeMan
Copy link

SerCeMan commented May 9, 2015

+1, very important feature. Also, function pointer and structures as closure parameters would be very useful.

@wks
Copy link

wks commented Dec 2, 2015

+1. I am developing a virtual machine which exposes a JNI-like interface, where the C native program receives a struct (like JNIEnv) and each API function is a function pointer in that struct. If a Java programmer wants to control a native component that exposes such an interface (imagine controlling another JVM from one JVM), it will be very common to call many functions via their function pointers.

The desired JNR interface should be similar to Closure, albeit it is Java calling C rather than C calling back to Java. The callee is a stateless C function rather than an object-bound Java method, so it should be easier to implement.

@headius
Copy link
Member

headius commented Sep 26, 2016

I don't think we've made any progress on this, but it's probably still useful. Anyone want to bring this issue back to life and try to come up with a good API for this?

@headius headius added this to the 2.2.0 milestone Sep 28, 2016
@gembin
Copy link

gembin commented Oct 20, 2016

I think it's useful, How to implement this kind of callback?
What' the equivalent of com.sun.jna.Callback ?

https://curl.haxx.se/libcurl/c/curl_easy_setopt.html

CURLOPT_HEADERFUNCTION

Callback for writing received headers. See CURLOPT_HEADERFUNCTION
 setopt(CurlOption.CURLOPT_HEADERFUNCTION, headerHandler);
 setopt(CurlOption.CURLOPT_WRITEFUNCTION, contentHandler);

@headius
Copy link
Member

headius commented Nov 8, 2016

It seems unlikely that you can't already do this in jnr-ffi via some combination of APIs. Are we simply missing a nice endpoint that says "give me a callable wrapper around this C function pointer?

@pron
Copy link
Author

pron commented Nov 13, 2016

I think someone has made such an API here.

@atoulme
Copy link

atoulme commented Jun 2, 2019

I'm interested by this functionality as well. Is there a better way to accomplish this now? If not, I'll look at Foreign and jffi as @headius mentioned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants