Lisp HUG Maillist Archive

Passing address of Lisp foreign callable to C

Hello,

The FLI manual in section 4.3 shows how to define a
Lisp function callable from C. The only demonstration
of an actual call, though, is from a Lisp function
designed to call C functions. There is only Lisp
calling Lisp in that example. egrep found no
'define-foreign-callable' in the examples directories.
Is it possible to pass the address of the Lisp
function as a callback to the C world?

Can someone pass along a short example using a
fli:define-foreign-callable function as a Windows
callback? It would be especially nice to have a
RegisterClass / WndProc example but if it is not
possible to hook into the flow of messages, an example
of as much as is possible will be greatly appreciated.
Would that example change under delivery, when I don't
want CAPI but do need Lisp managing a windows message
loop?

I love CAPI so far but I have a conversion to do where
it seems much easier to port straight across, gaining
many Lisp-ish advantages while retaining
look-and-feel, without having to decide if CAPI can
reproduce everything exactly, or explain why it can't
be the same as before. Corman claims to allow straight
Win32 SDK apps but I'd rather stick with one Lisp
implementation rather than two. 

Thanks,

Jeff Caldwell

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com


Re: Passing address of Lisp foreign callable to C

On Tuesday 31 December 2002 10:43 am, Jeff Caldwell wrote:
> Is it possible to pass the address of the Lisp
> function as a callback to the C world?

I did something like this a while back.  Yes it is possible (LWW using C dll's 
that create Windows classes with callbacks handled by lisp).  I'll see if I 
can't find / create a coherent example of what I learned during this process 
and get back to you in a few days.

In the meantime, AHA's that come to mind are:

a) You can write a DLL using DevStudio, then use LWW as the .exe that calls 
the DLL by specifying lispworksXXX.exe in the DevStudio debug dialog (program 
to call the dll).

b) If your LWW/C-DLL program crashes, it is most likely caused by getting the 
calling convention wrong - check that first (LWW needs to use :stdcall to 
call the C DLL, but the C DLL code defaults to using :cdecl to call the lisp 
functions).

c) At some point, you need to pass the hInstance for the "parent" window to 
the C widget.  I found that a value of 0 (NULL) works (found this trick by 
perusing the source code for Tk).

In the end, the task was much less daunting that it looked (my main problem 
was my unfamiliarity with the Win32 API :-).

pt
tarvydas@attcanada.ca


Re: Passing address of Lisp foreign callable to C

> Can someone pass along a short example using a
> fli:define-foreign-callable function as a Windows
> callback? It would be especially nice to have a
> RegisterClass / WndProc example but if it is not
> possible to hook into the flow of messages, an example
> of as much as is possible will be greatly appreciated.
> Would that example change under delivery, when I don't
> want CAPI but do need Lisp managing a windows message
> loop?

(fli:define-foreign-callable ("lisp-wnd-func" :result-type (:one-of :long
(:boolean :long)))
        ((hWnd :hwnd) (Msg :ulong) (wParam :ulong) (lParam :long))
your function here)

;;you have to define your :hwnd, :ulong, etc. types

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

Then pass this pointer to windows.

This is old (1997) code so there may be easier ways to do this.

Marc


Re: Passing address of Lisp foreign callable to C

Unable to parse email body. Email id is 748

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