Integrating Lisp with the World


Sometimes you can't fight the world and its conventions, and make other people use Lisp. But it doesn't matter, because you can write your part in Lisp and enjoy all its benefits while providing a completely standard interface to external programs.

LispWorks

Using a .dll File Inside a Lisp Program

You can find the files below in this zip file.
  1. Create a dll file from the following C files: boo.h and boo.cpp. Note that the exported functions the_foo and the_bar use the C calling convention (they are wrapped with: extern "C"). This is necessary for Lisp to be able to use the dll correctly.
  2. Copy the resulting boo.dll file as well as import.lisp to a new directly, open, compile, and load import.lisp in the LispWorks environment, and run the test program: (test).

Converting a Lisp Program to a .dll File

You can find the files below in this zip file.

Creating a dll from a Lisp program uses the Lisp exe from the command line. It is convenient to do the following step once (not once per program):

We will create a .dll file from a lisp program that contains two functions: my-foo gets two integers and outputs a string, and my-bar gets two strings and outputs an integer. Put the following files in a lisp code directory, such as C:\src\lisp\ :
  1. defsys.lisp, dll-export-tools.lisp, export.lisp -- these files include the Lisp functions my-foo and my-bar, as well as convenient wrappers that convert the C-style input into Lisp input and gives it to my-foo and my-bar, as well as convert the Lisp results to C-style output.
  2. deliver.lisp and deliver.bat -- these are files that create the dll. Edit the directory inside deliver.lisp to the directory where you put these files. There is a flag in deliver.lisp: *deliver-optimum*. If it is nil, no attempt at reducing the size of the dll is made (the dll can be ~17K because it contains all of Lisp). deliver.lisp has an example of an optimization that works for our case, and reduces the dll size to ~3K. To read more on optimization, look at the Delivery User Guide.
  3. Run deliver.bat. This will create the file foo_prog.dll in the same directory as deliver.bat. Copy this dll to wherever your C development environment can find it, such as C:\src\c\ .
One way of testing this dll is to write a Lisp program that loads and uses it, just as we did in the previous section. But ultimately, you want to write a C program that uses this dll. Put the following files in a new directory:
  1. foo.h, foo.cpp -- provide wrappers to the functions MY_FOO and MY_BAR that are exported by foo_prog.dll.
  2. test.cpp -- a test program that uses foo_prog.dll.
  3. Compile, link, and run the code. The test program loads foo_prog.dll and calls it in various ways.
Try the following:
  1. Make sure *deliver-optimum* is set to nil in deliver.lisp, and create the dll. When you run the test program, the errors that occur in the dll (after safe mode is turned off) cause the lisp interpreter to be invoked, and debug options are available. In particular, if you choose to test the error in MY_BAR, as the error is a continuable error, you can choose to continue by writing :c 1, and the execution will terminate as usual.
  2. Change deliver.lisp so that *deliver-optimum* is set to t, and create the dll again. Now when errors occur in the dll, the Lisp interpreter is not invoked. Instead, an error window popps up.

Allegro

Using a .dll File Inside a Lisp Program

I will soon write some explanations about it. In the meantime, look at this page.

Converting a Lisp Program to a .dll File

I will soon write some explanations about it. In the meantime, look at this page.

Back to the Smart Programming Page