Calling C Sharp via COM


Revision as of 12:02, 4 March 2016 by Thomas Linder Puls (talk | contribs) (category)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This tutorial gives a description of one way of accessing C# from Visual Prolog.

We make the C# library COM visible and then we import the COM component in Visual Prolog.

Notice that COM is only supported by the Commercial Edition of Visual Prolog: see Visual Prolog Editions Comparison.

Creating a C# COM component in Visual Studio

  1. Create a C# project. I just created one that was a class library.
  2. Open the project properties. Open the assembly information. Select ‘Make Assembly Com-Visible’.
  3. Make an interface that expose the functions you want to call and a class that implements that interface.

For the interface set the attributes InterfaceType ot InterfaceIsIUnknown and a GuidAttribute to some GUID (generated by tools\Create GUID in VS)

public interface IMyInterface
    string MyFunction(string Input);

The ClassInterface should be None and here should also be a GuidAttribute.

public class MyClass : IMyInterface

Remember to add:

using System.Runtime.InteropServices;

Compile the program.

Importing the COM component in Visual Prolog

Copy assembly dll to your prolog projects exe folder.

If it has to be placed somewhere else there is some work with the registration which have to use /codebase, currently we expect it to be placed in the exe folder

Run tlbexp.exe on the dll to get a xxx.tbl file tlpexp.exe comes together with the windows SDK.

Register the dll using regasm.exe (this has to be done every time the program have to run on a new machine).

Import the COM component into the Visual Prolog project. Select 'New in new package' in the project tree. Give the package a name and select COM package load the type library (that is the tlb file).

Now the program should be able to run.

See also