Import, how to

Malcolm Wallace malcolm.wallace at
Sun Jul 18 18:11:39 EDT 2010

> I'm a beginner at haskell and ffi. I was trying to import and use is  
> haskell
> one C function, but I can't manage to do that.
> Can someone show me an example how to create an import and a use  
> example for
> such a C function:
> char functionName(char *, unsigned int, char *)

{-# LANGUAGE ForeignFunctionInterface #-}
module FOo where

import Foreign
import Foreign.C
import Data.Word

foreign import ccall "functionName" functionName_inC
         :: CString -> Word -> CString -> IO CChar

-- Assuming you really want to use Haskell Strings and Ints etc
-- and furthermore if the C function is really pure,
-- add the following wrapper.

functionName_h :: String -> Int -> String -> Char
functionName_h s0 i s1 =
     unsafePerformIO $
     withCString s0  $ \cs0->
     withCString s1  $ \cs1->
         do cchar <- functionName_inC cs0 (fromIntegral i) cs1
            return (castCCharToChar cchar)


More information about the FFI mailing list