Calling Lisp Functions from Xojo

Written by on April 17, 2022 in LISP, Programming, Xojo with 0 Comments

A few days ago, I purchased Xojo Pro commercial license. In case you haven’t heard about the product, Xojo is a popular cross-platform development tool targeting Desktop, Web and iOS (Android is expected in the near future). It supports an Object-Oriented Basic language. It has been around for over 25 years and has been steadily gaining wider adoption among developer community.

I am a long-term user of RAD Studio from Embarcadero, but I am always on the lookout for alternative tools, and Xojo satisfied my requirements. In fact, I have been playing with it for the past few days and I am quite pleased with what it offers.

One of the things that caught my attention was the ease with which functions defined in external libraries, for example DLLs in Windows, could be easily called from the Xojo Basic language. This is a fundamental requirement for any extensible platform. I wanted to test this feature by calling a couple of Lisp functions defined in a DLL. The rest of this article covers the details.

Here is the Lisp code that has been converted into a DLL:

The Lisp Code

The Lisp Code

The functions “randomInt” and “fibonacci” are exported from the DLL. Both the functions take an “Int64” argument and return an “Int64” value. I used Lispworks Enterprise Edition 8.0 (64 bit version for Windows 10) to build the DLL. The Lisp script to generate the DLL is shown below:

Generating the DLL

Generating the DLL

Now, moving on to Xojo, I created a simple Desktop App (Windows, 64 bit). Here is the design view:

Xojo App Design View

Xojo App Design View

Nothing fancy here. There are two edit fields to enter integers. When the “Randomize” button is pressed, the “randomInt” Lisp function wil be called and the result will be displayed in the text field below. Likewise, when th “Fibonacci” button is pressed, the corresponding Lisp function “fibonacci” will be called and the result shown. 

The overall Project structure is as shown below:

Project View

Project View

Let us now look at the actual code corresponding to the Button “Pressed” handlers. The first one is for the “Randomize” button:

Randomize Button Handler

Randomize Button Handler

Since our Lisp DLL will work only under Windows 64 bit, I am checking for the correct environment before executing the code. Calling the DLL function is made possible by the “Declare Function” statement. The rest of the code is pretty straightforward.

Here is the action handler for the “Fibonacci” button:

Fibonacci Button Handler

Fibonacci Button Handler

There is nothing else to do in the project. Let us “Build” the project now. Xojo takes a couple of seconds to generate the relevant files. 

The executable file is called “CallingLisp.exe”. We are not ready to run the program yet. Remember, we have our “foreign” code in a DLL. This DLL must be in the “Path” when we run our program. To keep things simple, I copied my DLL into the same directory as the EXE file:

Generated Files

Generated Files

The above shows all the files generated by Xojo compiler, along with the DLL I added.

Let us run our program by double-clicking “CallingLisp.exe”. I enter two integers in the input fields and click the respective buttons. The following shows the output:

Running the Program

Running the Program

The good news is that Xojo code has successfully called my Lisp functions to calculate the outputs displayed. 

I quit the App and run it again.

Another Run of the Program

Another Run of the Program

So, our program works perfectly! Of course, this is a “toy” application and hence there is nothing to brag about it.

What is nice is that Xojo makes it possible (and easy) to call “foreign” functions from existing libraries, thus making code reuse possible. And even for a newbie like me, this didn’t take much effort! Over the next few weeks, I will be spending more time on Xojo. Stay tuned.

Feel free to download the Project file and related resources.

Have a nice weekend!

Tags: ,

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *

Top