[plt-scheme] Problem with FFI functions returning pointers to 'C' functions

From: Tim Brown (tim.brown at cityc.co.uk)
Date: Wed Sep 12 05:10:05 EDT 2007

I have a scheme program that registers a scheme callback with a
C (FFI) function. The C function returns a pointer to the currently
set callback function. Thus I can chain callbacks together.

(define set-callback (ffi-get-obj "set_callback" my-lib
   (_fun (_fun _int -> _bool) -> (_fun _int -> _bool))))

(define old-callback
  (set-callback (lambda (i)
                 ; do stuff with i
                 (if old-callback (old-callback i)))))

A more concrete example is setting a signal(3C) call:

(require (lib "foreign.ss"))
(unsafe!)
(define c-signal
   (get-ffi-obj "signal" #f (_fun _int (_fun _int -> _void) ->
                                (_fun _int -> _void))))
(c-signal 15 (lambda (f) #f))

However, when I run this on: MzScheme v370 [3m]
SunOS 5.10 x86 or even Linux

I get the following error:
ffi-call: expects type <non-null-cpointer> as 1st argument, given: #f;
other arguments were: (#<ctype>) #<ctype>

What am I doing wrong here? If the object is declared as
(_fun _int (_fun _int -> _void) -> _void)
everything is tickity-boo with the FFI.

(Of course, I can't then use the returned value)

Regards,

Tim

-- 
Tim Brown <tim.brown at cityc.co.uk>  | City Computing Limited            |
T: +44 20 8770 2110                | City House, Sutton Park Road      |
F: +44 20 8770 2130                | Sutton, Surrey, SM1 2AE, GB       |
-----------------------------------------------------------------------|
BEAUTY:  What's in your eye when you have a bee in your hand           |
-----------------------------------------------------------------------'
City Computing Limited registered in London No. 1767817.
Registered Office: City House, Sutton Park Road, Sutton, Surrey, SM1 2AE
VAT number 372 8290 34.



Posted on the users mailing list.