Calling C# Methods from LispWorks Lisp – Part 2

Written by on March 14, 2021 in LISP, Programming with 0 Comments

In the last article, I showed how we can invoke C# methods from LispWorks Enterprise Edition, through the COM/Automation interface. That approach relied on invoking the Automation methods dynamically, without depending on the Type library (*.tlb). 

In this article, I will discuss the other approach, which uses the Type library. According to LispWorks documentation, this approach can result in more efficient method calling. So, depending on whether the Type library for the server object is available or not, one can choose either of the two approaches.

Declare A Public Interface for the Server Class

In order to use this approach, the C# code has to be slightly modified (compared to what we defined in the “dynamic” approach). We have to define a public interface that exposes the Server functionality. The Server class itself must then implement this interface.

C# Code

C# Code

Another minor change is the addition of “ClassInterface” attribute for the Server class.

Once this is done, the rest of the steps in building the DLL and TLB are the same as outlined in this article.

Generate FLI Definitions from COM Definitions

This is done by defining a system definition and then compiling and loading it to the current environment.

Compiling System Definition

Compiling System Definition

Invoking the Automation Methods

In order to invoke the Automation methods through the generated FLI definitions, we use the “call-dispatch-XXXX” set of macros. Here is a function that does that.

Calling Automation Methods Through Interface

Calling Automation Methods Through Interface

You will notice that we call the Server methods through the “interface” that we defined in C#. Take note of the name mapping: “MyServerInterface” in C# is mapped to “my-server-interface” in Lisp.

It is possible to call the Automation methods directly through the Server class reference, without going through the interface (although the interface is generally preferred). Here is how.

Calling Automation Methods Through Server Class Reference

Calling Automation Methods Through Server Class Reference

For comparison, how will the code change if we decide to use the dynamic layer instead of the generated FLI definitions? We discussed this in the last article.

Calling Through Dynamic Layer

Calling Through Dynamic Layer

In this case we use the “invoke-dispatch-XXXX” set of macros.

Here is the trace of the test session:

Session Trace

Session Trace

I hope you are able to follow the above discussion.

I used LispWorks Enterprise Edition 7.1.2 (Windows) for this example. You can download the Lisp source here. The C# code is available here.

I wish to thank Martin Simmons of LispWorks for his generous assistance when I was working on this article. 

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