The two main macros for interfacing LispWorks with a foreign language are define-foreign-callable which defines Lisp functions that can be called from the foreign language, and define-foreign-function which defines a short linking function that can call functions in a foreign language.
In Introduction to the FLI we defined a foreign function for calling the Win32 function
. The code for this example is repeated here.
(fli:define-foreign-function (set-cursor-position "SetCursorPos")
A FLI foreign function calling some C code. is an illustration of
, represented by a square, calling the C code which constitutes
Figure 4.1 A FLI foreign function calling some C code.
The next diagram, C calling a callable function in Lisp., 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 using
, 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.
To call a Lisp function from C or C++ you need to define it using
. Then call
argument and pass the result to C or C++ as a function pointer.
For the purpose of creating a self-contained illustration in Lisp, the following Lisp code defines a foreign callable function that takes the place of the Windows function
((x :long) (y :long))
"The cursor position can no longer be set"))
Supposing you had the above foreign callable defined in a real application, you would use
(make-pointer :symbol-name "SetCursorPos")
to create a foreign pointer which you pass to foreign code so that it can call the Lisp definition of
A FLI foreign function calling a callable function. illustrates what happens when
is called. The foreign function
(represented by the square) calls what it believes to be the Windows function
, but the callable function (represented by the oval), also called
, is called instead. It pops up a CAPI pane displaying the message "The cursor position can no longer be set".
Figure 4.3 A FLI foreign function calling a callable function.
For more information on calling foreign code and defining foreign callable functions see Strings and foreign callables, define-foreign-function, and define-foreign-callable.