[plt-scheme] C++ Bindings using FFI

From: Eli Barzilay (eli at barzilay.org)
Date: Tue May 9 17:37:43 EDT 2006

On May  9, Hans Oesterholt-Dijkema wrote:
> Ok, so how should we summarize this discussion?
> First of all, I see a lot of arguments for using the FFI to create
> scheme/C bindings.
> However, without CASE functionality that SWIG provides it
> still looks like a hell of a job to bind e.g. all 12.000 functions
> and #defines, enumeration types, of Gtk+ 2.x

Like I said -- the advantage of SWIG is in its ability to parse the
header files.

> From the typemaps I provided to SWIG at this moment almost
> 155.000 lines of C binding code are generated.

(This sounds like an awful lot of code.  I'm surprised.)

> I need only about 6.200 lines of C code to do other administration
> things.  That makes only 4% of the C code hand coded. With every gtk
> header file I add, this percentage is decreasing.

If you can get the type information in some readable format (readable
without a C-parser), then it should be easy to do the same with
Scheme, maybe easier.

My personal conclusion about this is that for some strange reason
people completely miss the use of the Scheme interface.  It's a
different mode of thinking, and you should just try it out to see the

One thing that is definitely easier using the Scheme interface is
trying quick things out.  I recently wanted to auto-type text on
Windows.  All I needed was to browse through MSDN and get to the
description of `keybd_event', and from that I was able to write

  (define key-press
    (get-ffi-obj "keybd_event" "user32.dll"
      (_fun (key : _byte)
            (_byte = 0) (flags : _int32 = 0) (extra : _pointer = #f)
            -> _void)))

and that's all.  That's very easy compared to a SWIG solution (which I
wouldn't even be able to use since I don't have a compiler on my

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!

Posted on the users mailing list.