[racket] FFI

From: Jay Kominek (kominek at gmail.com)
Date: Fri May 10 14:44:52 EDT 2013

On Fri, May 10, 2013 at 3:08 AM, Kubilay Kaan <heraklea at gmx.de> wrote:
> (require ffi/com
>          ffi/unsafe
>          ffi/unsafe/define
>          ffi/winapi
>          racket/string
>          racket/format)
> (define _BOOL       (make-ctype _int (lambda (v) (if v 1 0)) (lambda (v) (not (zero? v)))))

_bool is already available as a type.

> (define-ffi-definer define-testdll (ffi-lib "test.dll"))
> (define-testdll GetTestObject(_fun
>                                    (pObj : (_ptr io _intptr))
>                                    -> (r : _BOOL)
>                                    -> (values r   pObj)))
> (define p (malloc 'atomic _int32))
> (define i (cast p _pointer _intptr))
> (define-values (res pObj) (GetTestObjecti))

Using "(_ptr io _pointer)" instead of "(_ptr io _intptr)" should do
the same thing, and let you skip the cast you perform later.

So... GetTestObject's type in C is:

BOOL GetTestObject(int32_t **x)

and you pass it a pointer to an int32, but might get back a different
pointer? That seems a little odd.

> I run the script: res is #t  and pObj is something like "3d14b008".
> How can I access the members and Functions of the Object with the given pObj
> adress?

We don't really know anything about pObj. How would you access these
things in C?

> Can I map the  Object in the address to a real Racket Object, on which I can
> work?

If you can get to these things via the FFI (which remains to be seen),
then you should be able to wrap the whole thing as a Racket class.
(Mapping fields might be tricky?)

Jay Kominek

Posted on the users mailing list.