Difference between revisions of "Calling C Sharp via COM"

From wiki.visual-prolog.com

m (Further info about .Net COM integration: See also)
(category)
 
Line 61: Line 61:
 
* [http://msdn.microsoft.com/en-us/library/zsfww439.aspx Exposing .NET Framework Components to COM]
 
* [http://msdn.microsoft.com/en-us/library/zsfww439.aspx Exposing .NET Framework Components to COM]
  
[[Category:Tutorials]]
+
[[Category:Interfacing other programming languages]]

Latest revision as of 11:02, 4 March 2016

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.

See also