[Haskell-cafe] how to call a callback in a haskell dll

Donn Cave donn at avvanta.com
Fri Nov 15 18:05:16 UTC 2013

Quoth "Kees Bleijenberg" <k.bleijenberg at lijbrandt.nl>,
> My program creates a Windows dll. The calling program calls a function with
> strings as parameters. Works fine. 
> I want to add a extra parameter. This extra parameter is a callback:  a
> function Int -> Int: stdcall. In Haskell I want to call this function.  So I
> have to convert the pointer to a (formal) Haskell function.
> In the examples for the FFI, Haskell provides the callback. I need an
> example or documentation.

It's unclear to me whether the Haskell program initially chooses
the function and creates a pointer to it, or that happens in C/C++.
In the former case, I think Foreign.StablePtr serves that purpose.

In the latter case, Haskell will receive the function as something
like FunPtr (CInt -> CInt), and you need a "dynamic" foreign function
to convert the pointer, like
  foreign import ccall "dynamic" iiPtrStub
     :: FunPtr (CInt -> CInt) -> (CInt -> CInt)

That's lightly documented in the library docs under Foreign.Ptr.


More information about the Haskell-Cafe mailing list