The two main macros for interfacing LispWorks with a foreign language arefli:define-foreign-callable
which defines Lisp functions that can be called from the foreign language, andfli:define-foreign-function
which defines a short linking function that can call functions in a foreign language.
In Chapter 1, "Introduction to the FLI" we defined a foreign function for calling the Win32 functionSetCursorPos
. The code for this example is repeated here.
(fli:define-foreign-function (set-cursor-position "SetCursorPos") ((x :long) (y :long)) :result-type :boolean)
Figure 4.1 is an illustration ofset-cursor-position
, represented by a square, calling the C code which constitutesSetCursorPos
.
Figure 4.1 An FLI foreign function calling some C code.
The next diagram, Figure 4.2, illustrates a callable function. Whereas a foreign function consists of a Lisp function name calling some code in C, a callable function consists of Lisp code, represented by an oval in the diagram, which can be called from C.
Figure 4.2 C calling a callable function in Lisp.
Callable functions are defined usingfli:define-foreign-callable
, which takes as its arguments, amongst other things, the name of the C function that will call Lisp, the arguments for the callable function, and a body of code which makes up the callable function.
The following Lisp code defines a foreign callable function that takes the place of the Windows functionSetCursorPos
.
(fli:define-foreign-callable ("SetCursorPos" :result-type :boolean) ((x :long) (y :long)) (capi:display-message "The cursor position can no longer be set"))
Figure 4.3 illustrates what happens whenset-cursor-position
is called. The foreign functionset-cursor-position
(represented by the square) calls what it believes to be the Windows functionSetCursorPos
, but the callable function (represented by the oval), also calledSetCursorPos
, is called instead. It pops up a CAPI pane displaying the message "The cursor position can no longer be set".
Figure 4.3 An FLI foreign function calling a callable function.
For more information on calling foreign code and defining foreign callable functions see define-foreign-function
, page 50, and define-foreign-callable
, page 48.