Help: writing ffi bindings to a C library

David Roundy droundy@abridgegame.org
Wed, 21 May 2003 11:41:02 -0400


On Wed, May 21, 2003 at 04:27:10PM +0100, Bayley, Alistair wrote:
> Can anybody point me to a tutorial, or some illustrative examples? At the
> moment I'm trying to avoid the various ffi tools, and just use the basic ffi
> capabilities in GHC, so I can get an idea of how it all works. When I'm more
> comfortable with ffi then I'll think about using the various tools to speed
> things up.

You could check out what I've done using ffi for darcs.  Browse to the
files Curl.hs and hscurl.c and hscurl.h at:

http://www.abridgegame.org/cgi-bin/darcs?darcs*

I've got some sort of scary code in there (threads, pipes and forks in C),
but the ffi stuff itself isn't bad at all.  ffi really isn't all that hard
to use, although I must admit I don't really understand all the options.
But what I do know how to do seems to be enough for me.

> How do I handle values of OCIEnv, OCIServer, etc? I think I'm supposed to
> use Ptr, ForeignPtr, and Storable to reference them, but I don't see how to
> combine these types/classes (ForeignPtr takes values of type Ptr a; how do I
> construct a Ptr a? What type should "a" be?).

Unless you actually need to ever access their contents, I'd just define the
pointers to be of type Ptr (), essentially like a void * pointer in C.  No
need to use Storable if they are only ever accessed through C functions
(which I would hope would be the case).
-- 
David Roundy
http://civet.berkeley.edu/droundy/