
On Wed, Feb 11, 2015 at 12:26 PM, Sylvain Henry
2015-02-11 16:18 GMT+01:00 Francesco Mazzoli
: Relatedly, if I have some function pointer known at runtime that addresses a C function that takes some arguments, I have no way to invoke it from Haskell, since all FFI imports must be declared at compile-time, and I don't know the address of the symbol I want to execute at compile time.
You can use FunPtr and wrapper imports to convert a FunPtr into a Haskell function. https://hackage.haskell.org/package/base-4.7.0.2/docs/Foreign-Ptr.html#g:2
You may be interested in my dynamic-linker-template package [1] to avoid having to write boilerplate wrappers.
Code using http://hackage.haskell.org/package/libffi looks pretty similar, though the specification needs terms (such as argCInt) instead of a mandatory type signature. GHC itself includes libffi so maybe the foreign import "dynamic" calls end up doing exactly the same thing as the hackage libffi does.