Calling C# Methods from LispWorks Lisp through COM Interface

Written by on February 28, 2021 in LISP, Programming with 0 Comments

In an earlier article, I showed how to invoke C# methods from Sicstus Prolog. Today, I would like to detail the steps for doing the same from LispWorks Lisp.

LispWorks Enterprise Edition comes budled with support for COM and Automation. This is what we will be using. What this means is that we have to implement the C# module as a COM server, register it locally, and then invoke those methods through the Lisp COM layer. This is not the only way to access functionality implemented in C#/.NET, but that is what we will explore today.

Since I have already explained the steps to build the C#-based COM server (Steps 1 through 6) in the earlier article, I will skip that part now (the COM server class is slightly different this time, but the steps are the same).  Here is the C# Code that acts as COM Server:

The COM Server in C#

The COM Server in C#

Using the Automation Package

According to LispWorks documentation, there are two ways to access the Automation methods:

1) If the type library (“.tlb”) is available, it is possible to generate the corresponding FLI definitions from COM definitions.

2) Invoke the Automation methods dynamically without depending on the type library.

For today’s example, I am going to follow the second approach because it is simpler, although slightly less efficient than (1).

We will be using the functions “invoke-dispatch-method” and “invoke-dispatch-get-property” for accessing the COM server functionality. However, to simplify access, we will define suitable wrapper functions:

Wrapper Functions

Wrapper Functions

Here is a simple function to test the logic:

Testing the Functionality

Testing the Functionality

And here is the sample session:

Sample Session

Sample Session

That is it. I think the approach is quite intuitive.

I used LispWorks 64 bit Enterprise Edition 7.1.2 for this example. Many thanks to Martin Simmons of LispWorks for his prompt response whenever I reached out to him with questions.

You can download the Lisp source code here, and the C# source code here.

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.

Top