Posted by: leppie | January 25, 2009

Some more FFI goodness

🙂

> (import (ironscheme ffi))
> (define dlload (pinvoke-call kernel32 LoadLibrary intptr (string)))
> (define dlsym (pinvoke-call kernel32 GetProcAddress intptr (intptr string)))
> (define ffitestlib (dlload "ffitest"))
> (define cb (dlsym ffitestlib "fnfficallback"))
> (define cb-sig (ffi-callout int32 (intptr)))
> (define fnfficallback (cb-sig cb))
> (define fxplus ((ffi-callback int32 (int32 int32))
.   (lambda (x y)
.     (printf "I got ~a and ~a\n" x y)
.     (fx+ x y))))
>
> (fnfficallback fxplus)
I got 10 and 30
40
> (define fxplus ((make-ffi-callback 'int32 '(int32 int32)) ; procedural interface
.    (lambda (x y)
.      (printf "I got ~a and ~a\n" x y)
.      (fx+ x y))))
> (fnfficallback fxplus)
I got 10 and 30
40
>
>

The C code is:

typedef int fxplus(int,int);
int fnfficallback(fxplus p)
{
  return p(10,30);
}
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: