packages feed

cinvoke-0.1: Foreign/CInvoke.hs

{- |
This is the only module that normal users should need to import.

As an example, allocate 1GB of memory, zero it and free it again:

> 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]

Another example shows how to ask the X Window System for the resolution of the first screen.

> module Main where
> 
> import Foreign.CInvoke
> 
> main = do
>     cxt <- newContext
>     libx <- loadLibrary cxt "libX11.so"
>     xOpenDisplay   <- loadSymbol libx "XOpenDisplay"
>     xCloseDisplay  <- loadSymbol libx "XCloseDisplay"
>     xDisplayWidth  <- loadSymbol libx "XDisplayWidth"
>     xDisplayHeight <- loadSymbol libx "XDisplayHeight"
> 
>     display <- cinvoke xOpenDisplay (retPtr retVoid) [argString ":0"]
>     width   <- cinvoke xDisplayWidth retCInt [argPtr display, argCInt 0]
>     height  <- cinvoke xDisplayHeight retCInt [argPtr display, argCInt 0]
>     cinvoke xCloseDisplay retCInt [argPtr display]
>     putStrLn $ show width ++ "x" ++ show height

Contexts, libraries and symbols are all freed by the garbage collector when necessary.

The source distribution examples/ directory contains more complicated examples.

-}
module Foreign.CInvoke
    (FFIException(..)

    -- * Contexts
    ,Context
    ,newContext

    -- * Libraries
    ,Library
    ,loadLibrary

    -- * Symbols
    ,Symbol
    ,loadSymbol
    ,cinvoke
    ,withSymbol

    -- * Function arguments and return types
    ,Arg
    ,RetType
    ,withRetType
    ,module Foreign.CInvoke.Types
    ) where

import Foreign.CInvoke.Base
import Foreign.CInvoke.Types