Personal tools

Library/cinvoke

From HaskellWiki

< Library(Difference between revisions)
Jump to: navigation, search
(add some initial docs)
 
(Add hackage + haddock links)
 
Line 20: Line 20:
 
cinvoke free (retPtr retVoid) [argPtr p]
 
cinvoke free (retPtr retVoid) [argPtr p]
 
</haskell>
 
</haskell>
  +
  +
It can be found at hackage: http://hackage.haskell.org/package/cinvoke
  +
  +
The Haddock documentation is (for now) at: http://student.science.uva.nl/~rturk/cinvoke-0.1-docs/

Latest revision as of 21:53, 7 March 2011

cinvoke is a library allowing C functions of types only known at runtime to be called from Haskell.

As an example is worth a thousand words:

 module Main where
 
 import Foreign.Ptr
 import Foreign.CInvoke
 
 main = do
     cxt <- newContext
     libc <- loadLibrary cxt "libc.so.6"
     malloc <- loadSymbol libc "malloc"
     memset <- loadSymbol libc "memset"
     free   <- loadSymbol libc "free"
     let sz = 2^30
     p <- cinvoke malloc (retPtr retVoid) [argCSize sz]
     cinvoke memset (retPtr retVoid) [argPtr p, argCInt 0, argCSize sz]
     cinvoke free (retPtr retVoid) [argPtr p]

It can be found at hackage: http://hackage.haskell.org/package/cinvoke

The Haddock documentation is (for now) at: http://student.science.uva.nl/~rturk/cinvoke-0.1-docs/