[plt-scheme] passing a buffer to the FFI

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sun Mar 18 18:40:31 EDT 2007

At Wed, 14 Mar 2007 22:52:36 -0700, Pedro Pinto wrote:
> I am trying to wrap a MacOS api call which expects, among other  
> arguments, a pointer to a pre-allocated array of bytes. Upon return  
> the routine will fill the array with a utf-8 encoded string which I  
> would like to convert to a Scheme string:
> 
>     CFIndex CFStringGetBytes (
>       CFStringRef theString,
>       CFRange range,
>       CFStringEncoding encoding,
>       UInt8 lossByte,
>       Boolean isExternalRepresentation,
>       UInt8 *buffer,                                // <----- Pre- 
> allocated buffer
>       CFIndex maxBufLen,
>       CFIndex *usedBufLen
>     );
> 
> How do I do this with the FFI? The buffer size is not know until run- 
> time.

I'd call it a `_bytes' from the Scheme side and allocate the argument
with `make-bytes'. After the call returns, use `bytes->string/utf-8' to
decode .

Matthew



Posted on the users mailing list.