Lisp HUG Maillist Archive

Foreign Callbacks

Windows often uses callbacks. How can I call back into a Lisp function? I
see define-foreign-callable, but the assumption seems to be that you're
defining functions to be called from by name from a DLL...? To call back
from Windows I'd need to pass some pointer-to-a-function, where the
function declaration was defined by Windows itself. So ultimately I assume
that I need to get a pointer to the function defined by
define-foreign-callable...?

Also, just to check: I'm going to pass a Lisp object out which then gets
sent back in from the callback. I don't want the object to be garbage
collected while it's out in the wild. I can do that by making sure some
Lisp thing points to it while it's held externally--but is there another
way? Or should I be worried about something else...?


Re: Foreign Callbacks


> Windows often uses callbacks. How can I call back into a Lisp function? I
> see define-foreign-callable, but the assumption seems to be that you're
> defining functions to be called from by name from a DLL...? To call back
> from Windows I'd need to pass some pointer-to-a-function, where the
> function declaration was defined by Windows itself. So ultimately I assume
> that I need to get a pointer to the function defined by
> define-foreign-callable...?

(fli:define-foreign-callable ("lisp-wnd-func" :result-type :long)
        ((hWnd :hwnd) (Msg :ulong) (wParam :ulong) (lParam :long))
;your stuff here
)

(defvar *lisp-wnd-func* (fli:make-pointer :symbol-name "lisp-wnd-func"
:functionp t))

This is very old code, may be it's simpler now.

> Also, just to check: I'm going to pass a Lisp object out which then gets
> sent back in from the callback. I don't want the object to be garbage
> collected while it's out in the wild. I can do that by making sure some
> Lisp thing points to it while it's held externally--but is there another
> way? Or should I be worried about something else...?

You don't pass a lisp object, you have to allocate a foreign object that you
can pass to windows.

Marc




Updated at: 2020-12-10 09:01 UTC