[plt-scheme] Using FFI with complex structures and input/output parameters?

From: Elena Garrulo (egarrulo at gmail.com)
Date: Thu Jul 2 08:31:23 EDT 2009

Well, I finally nailed it down.  It would look like this (code
shrinked down from major example, not tested!) :

;; void fill_buffer (BYTE* buffer, DWORD* buffer_size);
(define fill_buffer
  (get-ffi-obj "fill_buffer"
                (buffer : _pointer)
                (buffer_size : ((_ptr io _DWORD)))
                -> _void
                -> (list buffer_size)))) ;; <- OUTPUT?

Which you would call like this:

(define buffer_size 1000) ;; Pick your buffer size.
;; Buffer allocation.
(define buffer (malloc _byte (* buffer_size (ctype-sizeof  _byte))))

;; call "fill_buffer" and retrieve size of result.
(set! buffer_size (car (fill_buffer buffer buffer_size)))

;; resize buffer accordingly
(set! buffer (cblock->vector buffer _byte buffer_size))

;; remember to (free buffer) when you're done.

Thanks to Eli for his suggestions.

Posted on the users mailing list.