[plt-scheme] FFI: problem with define-cpointer-type

From: Sam Phillips (samdphillips at gmail.com)
Date: Thu Sep 25 14:22:24 EDT 2008

Hi,

I'm having problems using a type defined by define-cpointer-type as an
argument to a foreign function.  Whenever I call out I get:

  Scheme->C: expects argument of type <pointer>; given #<my-type>

Basically the code involved looks like:

;;; there is actually more in this struct...
(define-struct my-type (ptr))

(define-cpointer-type _my-type-ptr #f
  (lambda (v)
    (if (my-type? v)
      (my-type-ptr v)
      (error "cannot coerce to _my-type-ptr")))
  (lambda (ptr)
    (if ptr
      (make-my-type ptr)
      (error "got null"))))

(define my-type-maker
  (get-ffi-obj "my_type_maker" lib
    (_fun -> (ret : _my-type-ptr/null)
          -> (if ret ret (error "got null")))))

(define my-type-operation
  (get-ffi-obj "my_type_operation" lib
    (_fun _my-type-ptr -> _int)))

(define a (my-type-maker)) ; a is a proper my-type struct
(my-type-operation a)      ; throws above error

I can't seem to find any examples in Planet or core collects that use
define-cpointer-type in this way.  The closest thing I found was in the
examples magick.ss in defining the _PixelIterator type, and it uses a
ctype and not a cpointer.

Am I just doing this the totally incorrect way?

Thanks,
Sam


Posted on the users mailing list.