Quantcast

Import, how to

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Import, how to

grzyb
Hi,

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 *)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Import, how to

Henning Thielemann

On Sun, 18 Jul 2010, grzyb wrote:

> 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 *)

Something like

foreign import functionName :: Ptr CChar -> CUInt -> Ptr CChar -> IO CChar
_______________________________________________
FFI mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/ffi
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Import, how to

Malcolm Wallace-2
In reply to this post by grzyb
> 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)


Regards,
     Malcolm

_______________________________________________
FFI mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/ffi
Loading...