Difference between revisions of "Calling C Sharp via COM"
(New page: == Howto call C# == This tutorial gives a description of one way of accessing C# from prolog. We make the C# library COM visible and then we import the COM component in visual prolog. =...) |
|||
Line 60: | Line 60: | ||
You can find more information here: [http://msdn.microsoft.com/en-us/library/zsfww439.aspx] | You can find more information here: [http://msdn.microsoft.com/en-us/library/zsfww439.aspx] | ||
[[Category:Tutorials]] |
Revision as of 11:55, 20 February 2009
Howto call C#
This tutorial gives a description of one way of accessing C# from prolog.
We make the C# library COM visible and then we import the COM component in visual prolog.
Creating a C# COM component in Visual Studio
- Create a C# project. I just created one that was a class library.
- Open the project properties. Open the assembly information. Select ‘Make Assembly Com-Visible’.
- 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]