Difference between revisions of "Calling C Sharp via COM"

From wiki.visual-prolog.com

m (No top header)
(Commercial Edition only)
Line 1: Line 1:
This tutorial gives a description of one way of accessing C# from prolog.
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.
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 [http://www.visual-prolog.com/vip6/product/vipPE2CE.htm Visual Prolog Editions Comparison].


=== Creating a C# COM component in Visual Studio ===
=== Creating a C# COM component in Visual Studio ===

Revision as of 21:25, 15 January 2013

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)

[InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
GuidAttribute("0828DB14-1275-3926-813D-494F4FE771CF")]
public interface IMyInterface
{
    string MyFunction(string Input);
}

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

[ClassInterface(ClassInterfaceType.None),
GuidAttribute("95E62F78-55FF-4061-8238-6D3FCF88F5D0")]
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.

Further info about .Net COM integration

You can find more information here: [1]