Foreign C with pointers

Don Stewart dons at galois.com
Tue Dec 18 13:58:53 EST 2007


zhen.sydow:
>    Hi,
> 
>    I am creating a binding to an existing library (lib*.a) from Haskell.
> 
>    How can i bind a function that get a pointer?
> 
>    e.g: I have in the c library.
>    int GetData( Data * d );
> 
>    The steps that i need are:
>    1 - create the Data in Haskell
>    2 - create the foreign import sentence in a lib*.hs
>    3 - use the Data after the call
> 
>    Where can i get a good tutorial about FFI? I try
>    [1]http://www.haskell.org/hdirect/ffi.html but it's too general (need i
>    pair of examples, i think)

Say we have:

     #include <string.h>

     void *
     memmove(void *dst, const void *src, size_t len);

We can bind to that as:

    foreign import unsafe ccall "string.h memmove"
        c_memmove :: Ptr Word8 -> Ptr Word8 -> CSize -> IO (Ptr Word8)

You can use Foreign.Ptr.* and Foreign.ForeignPtr (or even, say,
Data.ByteString) to get at Ptr on the Haskell side.


More information about the Glasgow-haskell-users mailing list