{"id":1770,"date":"2019-11-10T14:56:38","date_gmt":"2019-11-10T09:26:38","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=1770"},"modified":"2019-11-10T15:01:34","modified_gmt":"2019-11-10T09:31:34","slug":"calling-c-methods-from-sicstus-prolog-through-com-interface","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2019\/11\/10\/calling-c-methods-from-sicstus-prolog-through-com-interface\/","title":{"rendered":"Calling C# Methods from Sicstus Prolog through COM Interface"},"content":{"rendered":"<p>Recently<span class=\"Apple-converted-space\">\u00a0<\/span>I needed to make use of a <em><strong>C#<\/strong><\/em> library (which I had implemented a few years ago) from <em><strong>Sicstus Prolog<\/strong><\/em>. Calling <em><strong>C\/C++<\/strong><\/em> functions from <em><strong>Sicstus Prolog<\/strong><\/em> is fairly well <a href=\"https:\/\/sicstus.sics.se\/sicstus\/docs\/latest4\/html\/sicstus.html\/Mixing-C-and-Prolog.html#Mixing-C-and-Prolog\" target=\"_blank\" rel=\"noopener\"><em>documented<\/em><\/a>. When it comes to <em><strong>C#\/.NET<\/strong><\/em>, the official documentation recommends the <a href=\"https:\/\/sicstus.sics.se\/sicstus\/docs\/latest4\/html\/sicstus.html\/PB-Intro.html#PB-Intro\" target=\"_blank\" rel=\"noopener\"><em><strong>PrologBeans<\/strong><\/em><\/a> library. After going through the documentation, I felt that it would not work for me for the following two reasons:<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">1) This library requires <em><strong>Prolog<\/strong><\/em> to be the server and <em><strong>C#\/.NET<\/strong><\/em> to be the client, and the primary communication is from the client to the server. But my requirement was the other way &#8211; calling <em><strong>C#<\/strong><\/em> from <em><strong>Prolog<\/strong><\/em>.<\/span><\/p>\n<p><span style=\"color: #0000ff;\">2) The server and client run as different processes. This can be useful when we need to build a scalable, heavy-weight client-server system, but it was &#8220;too heavy&#8221; for my requirements. I needed an in-process mechanism for the communication.<\/span><\/p><\/blockquote>\n<p>I looked around the documentation and found that the library <a href=\"https:\/\/sicstus.sics.se\/sicstus\/docs\/latest4\/html\/sicstus.html\/lib_002dcomclient.html#lib_002dcomclient\" target=\"_blank\" rel=\"noopener\"><em><strong>comclient<\/strong><\/em><\/a>\u00a0allows <em><strong>Sicstus Prolog<\/strong><\/em> to act as a <em><strong>COM<\/strong><\/em> client. This seemed like a workable solution; I have to provide a <em><strong>COM<\/strong><\/em> interface to my exisitng library and then it can be invoked from <em><strong>Sicstus Prolog<\/strong><\/em>. It is also an <em><strong>&#8220;in-process&#8221;<\/strong><\/em> interaction, and hence avoids inter-process communication overheads.<\/p>\n<p>And that is what I ended up doing. It worked pretty well.<\/p>\n<p>In this article, I wish to outline the process of implementing a simple <em><strong>COM<\/strong><\/em> object in <em><strong>C#<\/strong><\/em> and then show how to use it from <em><strong>Sicstus<\/strong><\/em>.<\/p>\n<p>I am using <em><strong>Visual Studio 2019 Ver 16.3.7<\/strong><\/em>, so there could be some differences between the screens I have shown here and yours (if you have a different version).<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<h3>Step 1:<\/h3>\n<p>Make sure you launch Visual Studio in <em><strong>&#8220;Administrator Mode&#8221;<\/strong><\/em> (this is required for <em><strong>Step 4<\/strong><\/em> to work). Create a new project of type <em><strong>&#8220;Class Library (.NET Framework)&#8221;<\/strong><\/em>. See the following image.<\/p>\n<figure id=\"attachment_1771\" aria-describedby=\"caption-attachment-1771\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/New-Project.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"1771\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2019\/11\/10\/calling-c-methods-from-sicstus-prolog-through-com-interface\/new-project\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/New-Project.jpg\" data-orig-size=\"1024,710\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1573284475&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Creating a New Project\" data-image-description=\"&lt;p&gt;Creating a New Project&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Creating a New Project&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/New-Project-1024x710.jpg\" class=\"wp-image-1771\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/New-Project.jpg?resize=650%2C451&#038;ssl=1\" alt=\"Creating a New Project\" width=\"650\" height=\"451\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/New-Project.jpg?w=1024&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/New-Project.jpg?resize=300%2C208&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/New-Project.jpg?resize=768%2C533&amp;ssl=1 768w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-1771\" class=\"wp-caption-text\"><strong>Creating a New Project<\/strong><\/figcaption><\/figure>\n<h3>Step 2:<\/h3>\n<p>Give a suitable name for the project and select an appropriate directory to save the solution.<\/p>\n<h3>Step 3:<\/h3>\n<p>In the project&#8217;s <em><strong>&#8220;Build Settings&#8221;<\/strong><\/em>, enable <em><strong>&#8220;Register for COM Interop&#8221;<\/strong><\/em>. See below.<\/p>\n<figure id=\"attachment_1772\" aria-describedby=\"caption-attachment-1772\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Build-Settings.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"1772\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2019\/11\/10\/calling-c-methods-from-sicstus-prolog-through-com-interface\/build-settings\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Build-Settings.jpg\" data-orig-size=\"701,740\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1573228198&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Enable COM Interop\" data-image-description=\"&lt;p&gt;Enable COM Interop&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Enable COM Interop&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Build-Settings.jpg\" class=\"wp-image-1772\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Build-Settings.jpg?resize=650%2C686&#038;ssl=1\" alt=\"Enable COM Interop\" width=\"650\" height=\"686\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Build-Settings.jpg?w=701&amp;ssl=1 701w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Build-Settings.jpg?resize=284%2C300&amp;ssl=1 284w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-1772\" class=\"wp-caption-text\"><strong>Enable COM Interop<\/strong><\/figcaption><\/figure>\n<h3>Step 4:<\/h3>\n<p>Another important step is to register the generated <em><strong>DLL<\/strong><\/em> for use as <em><strong>COM<\/strong><\/em> object through the <em><strong>&#8220;RegAsm&#8221;<\/strong><\/em> utility. Since this is done after the project is built, we can include this command in the <em><strong>&#8220;Post-build&#8221;<\/strong><\/em> step in the Project settings.<\/p>\n<figure id=\"attachment_1773\" aria-describedby=\"caption-attachment-1773\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Post-build.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"1773\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2019\/11\/10\/calling-c-methods-from-sicstus-prolog-through-com-interface\/post-build\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Post-build.jpg\" data-orig-size=\"701,740\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1573228255&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Registering the COM Object\" data-image-description=\"&lt;p&gt;Registering the COM Object&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Registering the COM Object&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Post-build.jpg\" class=\"wp-image-1773\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Post-build.jpg?resize=650%2C686&#038;ssl=1\" alt=\"Registering the COM Object\" width=\"650\" height=\"686\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Post-build.jpg?w=701&amp;ssl=1 701w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Post-build.jpg?resize=284%2C300&amp;ssl=1 284w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-1773\" class=\"wp-caption-text\"><strong>Registering the COM Object<\/strong><\/figcaption><\/figure>\n<p>This applies to <em><strong>64-bit DLL<\/strong><\/em> alone. The <em><strong>&#8220;\/tlb&#8221;<\/strong><\/em> option is not needed in our case, but would be required if you are, for example, implementing a <em><strong>COM<\/strong><\/em> client in <em><strong>C++<\/strong><\/em>.<\/p>\n<p>As I mentioned earlier, for this step to succeed, you must have started Visual Studio in <em><strong>&#8220;Administrator&#8221;<\/strong><\/em> mode.<\/p>\n<h3>Step 5:<\/h3>\n<p>Define the class that should act as the <em><strong>COM<\/strong><\/em> object (server). Make sure you include the <em><strong>&#8220;System.Runtime.InteropServices&#8221;<\/strong><\/em> Namespace. My example class is quite simple. It has just 4 methods as shown in the following image:<\/p>\n<figure id=\"attachment_1774\" aria-describedby=\"caption-attachment-1774\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Code.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1774\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2019\/11\/10\/calling-c-methods-from-sicstus-prolog-through-com-interface\/code-10\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Code.jpg\" data-orig-size=\"698,737\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1573228068&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"The COM Class\" data-image-description=\"&lt;p&gt;The COM Class&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;The COM Class&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Code.jpg\" class=\"wp-image-1774\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Code.jpg?resize=650%2C686&#038;ssl=1\" alt=\"The COM Class\" width=\"650\" height=\"686\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Code.jpg?w=698&amp;ssl=1 698w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Code.jpg?resize=284%2C300&amp;ssl=1 284w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-1774\" class=\"wp-caption-text\"><strong>The COM Class in C#<\/strong><\/figcaption><\/figure>\n<p>The first method <em><strong>&#8220;add&#8221;<\/strong><\/em> starts with a lowercase letter, which is not the recommended convention. I did this deliberately and will explain the reason later.<\/p>\n<p>The first two methods take two <em><strong>&#8220;int&#8221;<\/strong><\/em> arguments and return an <em><strong>&#8220;int&#8221;<\/strong><\/em>. The third method <em><strong>&#8220;Concat&#8221;<\/strong><\/em> takes two string args and returns a string.<\/p>\n<p>The last method <em><strong>&#8220;ProcID&#8221;<\/strong><\/em> returns the ID of the currently running process. I will be using this to show that both the <em><strong>Sicstus Prolog<\/strong><\/em> engine and <em><strong>C#<\/strong><\/em> run in the same process.<\/p>\n<p>In order to convert a class like this into a <em><strong>COM<\/strong><\/em> object, we have to decorate the class with additional attributes.<\/p>\n<p>The class requires a unique <em><strong>GUID<\/strong><\/em> that will be used from the client. Generating this is quite easy in Visual Studio.<\/p>\n<p>Select the <em><strong>&#8220;Create GUID&#8221;<\/strong><\/em> command from the <em><strong>&#8220;Tools&#8221;<\/strong><\/em> menu.<\/p>\n<figure id=\"attachment_1776\" aria-describedby=\"caption-attachment-1776\" style=\"width: 319px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/GUID-1.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1776\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2019\/11\/10\/calling-c-methods-from-sicstus-prolog-through-com-interface\/guid-1\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/GUID-1.jpg\" data-orig-size=\"319,416\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1573245095&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Generating GUID: Step 1\" data-image-description=\"&lt;p&gt;Generating GUID: Step 1&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Generating GUID: Step 1&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/GUID-1.jpg\" class=\"size-full wp-image-1776\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/GUID-1.jpg?resize=319%2C416&#038;ssl=1\" alt=\"Generating GUID: Step 1\" width=\"319\" height=\"416\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/GUID-1.jpg?w=319&amp;ssl=1 319w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/GUID-1.jpg?resize=230%2C300&amp;ssl=1 230w\" sizes=\"(max-width: 319px) 100vw, 319px\" \/><\/a><figcaption id=\"caption-attachment-1776\" class=\"wp-caption-text\"><strong>Creating GUID<\/strong><\/figcaption><\/figure>\n<p>This prompts you for the format of the <em><strong>GUID<\/strong><\/em>. Choose option <em><strong>(5)<\/strong><\/em>:<\/p>\n<figure id=\"attachment_1777\" aria-describedby=\"caption-attachment-1777\" style=\"width: 371px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/GUID-2.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1777\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2019\/11\/10\/calling-c-methods-from-sicstus-prolog-through-com-interface\/guid-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/GUID-2.jpg\" data-orig-size=\"371,391\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1573245127&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Selecting GUID Format\" data-image-description=\"&lt;p&gt;Selecting GUID Format&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Selecting GUID Format&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/GUID-2.jpg\" class=\"size-full wp-image-1777\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/GUID-2.jpg?resize=371%2C391&#038;ssl=1\" alt=\"Selecting GUID Format\" width=\"371\" height=\"391\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/GUID-2.jpg?w=371&amp;ssl=1 371w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/GUID-2.jpg?resize=285%2C300&amp;ssl=1 285w\" sizes=\"(max-width: 371px) 100vw, 371px\" \/><\/a><figcaption id=\"caption-attachment-1777\" class=\"wp-caption-text\"><strong>Selecting GUID Format<\/strong><\/figcaption><\/figure>\n<p>Copy and paste the generated <em><strong>GUID<\/strong><\/em> in the <em><strong>GUID<\/strong><\/em> attribute of the class. Although <em><strong>ProgID<\/strong><\/em>\u00a0attribute is not needed, I recommend it because this gives a <em><strong>readable<\/strong><\/em> name to the <em><strong>COM<\/strong><\/em> object. In the <em><strong>Prolog<\/strong><\/em> client, we can use either the <em><strong>GUID<\/strong><\/em> or the <em><strong>ProgID<\/strong><\/em> to connect to the <em><strong>COM<\/strong><\/em> object.<\/p>\n<p>My build target is <em><strong>&#8220;x64&#8221;<\/strong><\/em> as the target <em><strong>CPU<\/strong><\/em>. The reason is my <em><strong>Sicstus Prolog<\/strong><\/em> is a 64-bit application running on <em><strong>Windows 10<\/strong><\/em>.<\/p>\n<h3>Step 6:<\/h3>\n<p>Build the project. Here is the output of the Build process:<\/p>\n<figure id=\"attachment_1778\" aria-describedby=\"caption-attachment-1778\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Build-Output.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1778\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2019\/11\/10\/calling-c-methods-from-sicstus-prolog-through-com-interface\/build-output\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Build-Output.jpg\" data-orig-size=\"953,352\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1573228772&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Build Output\" data-image-description=\"&lt;p&gt;Build Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Build Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Build-Output.jpg\" class=\"wp-image-1778\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Build-Output.jpg?resize=650%2C240&#038;ssl=1\" alt=\"Build Output\" width=\"650\" height=\"240\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Build-Output.jpg?w=953&amp;ssl=1 953w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Build-Output.jpg?resize=300%2C111&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Build-Output.jpg?resize=768%2C284&amp;ssl=1 768w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-1778\" class=\"wp-caption-text\"><strong>Build Output<\/strong><\/figcaption><\/figure>\n<p>Files <em><strong>&#8220;Server.dll&#8221;<\/strong><\/em> and <em><strong>&#8220;Server.tlb&#8221;<\/strong><\/em> would have been created.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<h3><b>Using the COM Object from Prolog<\/b><\/h3>\n<p>I wrote a <em><strong>COM<\/strong><\/em> client in <em><strong>Prolog<\/strong><\/em>, adding convenient wrappers to call the <em><strong>C#<\/strong><\/em> functions. Here is my code:<\/p>\n<figure id=\"attachment_1779\" aria-describedby=\"caption-attachment-1779\" style=\"width: 616px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Prolog-Code.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1779\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2019\/11\/10\/calling-c-methods-from-sicstus-prolog-through-com-interface\/prolog-code\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Prolog-Code.jpg\" data-orig-size=\"616,914\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1573229077&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Prolog Client Code\" data-image-description=\"&lt;p&gt;Prolog Client Code&lt;\/p&gt;\n\" data-image-caption=\"\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Prolog-Code.jpg\" class=\"size-full wp-image-1779\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Prolog-Code.jpg?resize=616%2C914&#038;ssl=1\" alt=\"Prolog Client Code\" width=\"616\" height=\"914\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Prolog-Code.jpg?w=616&amp;ssl=1 616w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Prolog-Code.jpg?resize=202%2C300&amp;ssl=1 202w\" sizes=\"(max-width: 616px) 100vw, 616px\" \/><\/a><figcaption id=\"caption-attachment-1779\" class=\"wp-caption-text\"><strong>Prolog Client Code<\/strong><\/figcaption><\/figure>\n<p>The <em><strong>&#8220;open_com&#8221;<\/strong><\/em> predicate tries to connect to the <em><strong>COM<\/strong><\/em> object by passing its <em><strong>GUID<\/strong><\/em> or <em><strong>ProgID<\/strong><\/em>. When successful, it asserts a <em><strong>&#8220;fact&#8221;<\/strong><\/em> containing the object ID for subsequent use (removing any previously asserted IDs). Its counterpart <em><strong>&#8220;close_com&#8221;<\/strong><\/em> releases the <em><strong>COM<\/strong><\/em> object.<\/p>\n<p>The <em><strong>&#8220;call_fn&#8221;<\/strong><\/em> predicate facilitates the actual call. It uses the <em><strong>&#8220;comclient_invoke_method_fun&#8221;<\/strong><\/em> to invoke the <em><strong>C#<\/strong><\/em> function through the object handle and gets the corresponding result.<\/p>\n<p>If you look at the <em><strong>&#8220;add&#8221;<\/strong><\/em>, <em><strong>&#8220;mult&#8221;<\/strong><\/em>, <em><strong>&#8220;concat&#8221;<\/strong><\/em>, and <em><strong>&#8220;procid&#8221;<\/strong><\/em> predicates, you will notice that the names are in lowercase, even though the actual methods being called in <em><strong>C#<\/strong><\/em> are <em><strong>&#8220;add&#8221;<\/strong><\/em>, <em><strong>&#8220;Mult&#8221;<\/strong><\/em>, <em><strong>&#8220;Concat&#8221;<\/strong><\/em>, and <em><strong>&#8220;ProcID&#8221;<\/strong><\/em>. In <em><strong>Prolog<\/strong><\/em>, when you start a name in uppercase, it refers to a variable and not to the actual literal with that name. So we are forced to reference the <em><strong>C#<\/strong><\/em> functions in lowercase in <em><strong>Prolog<\/strong><\/em>, even though the actual function names might start in uppercase in <em><strong>C#<\/strong><\/em> (and that is the convention). It appears that the <em><strong>C#<\/strong><\/em> method names are lowercased by the &#8220;<em><strong>comclient<\/strong><\/em>&#8221; library in order to find the right match. I feel the\u00a0library could have handled this differently, but it is not a big issue.<\/p>\n<p>OK. Now let us look at the actual session in the <em><strong>Sicstus Prolog<\/strong><\/em> IDE.<\/p>\n<figure id=\"attachment_1780\" aria-describedby=\"caption-attachment-1780\" style=\"width: 327px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Session.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1780\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2019\/11\/10\/calling-c-methods-from-sicstus-prolog-through-com-interface\/session\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Session.jpg\" data-orig-size=\"327,249\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1573229352&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Prolog Session\" data-image-description=\"&lt;p&gt;Prolog Session&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Prolog Session&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Session.jpg\" class=\"size-full wp-image-1780\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Session.jpg?resize=327%2C249&#038;ssl=1\" alt=\"Prolog Session\" width=\"327\" height=\"249\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Session.jpg?w=327&amp;ssl=1 327w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/11\/Session.jpg?resize=300%2C228&amp;ssl=1 300w\" sizes=\"(max-width: 327px) 100vw, 327px\" \/><\/a><figcaption id=\"caption-attachment-1780\" class=\"wp-caption-text\"><strong>Prolog Session<\/strong><\/figcaption><\/figure>\n<p>You will notice that the <em><strong>Proc ID<\/strong><\/em>s from <em><strong>COM<\/strong><\/em> object as well as from the <em><strong>Prolog<\/strong><\/em> engine are the same. This confirms that the <em><strong>C#<\/strong> <\/em>code runs as part of the <em><strong>Prolog<\/strong><\/em> process.<\/p>\n<p>Everything works as expected. I am happy that <em><strong>COM<\/strong><\/em> solved my problem of connecting to <em><strong>C#<\/strong><\/em> from <em><strong>Prolog<\/strong><\/em>.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>You can download the <a href=\"http:\/\/www.rangakrish.com\/downloads\/MyServer.cs\" target=\"_blank\" rel=\"noopener\"><em><strong>C#<\/strong><\/em> code<\/a> and <a href=\"http:\/\/www.rangakrish.com\/downloads\/COMTester.pro\" target=\"_blank\" rel=\"noopener\"><em><strong>Prolog<\/strong><\/em> code<\/a> from here.<\/p>\n<p>Have a nice weekend!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently\u00a0I needed to make use of a C# library (which I had implemented a few years ago) from Sicstus Prolog. Calling C\/C++ functions from Sicstus Prolog is fairly well documented. When it comes to C#\/.NET, the official documentation recommends the PrologBeans library. After going through the documentation, I felt that it would not work for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[17,147],"tags":[219,67,218,151],"class_list":["post-1770","post","type-post","status-publish","format-standard","hentry","category-programming","category-prolog","tag-net","tag-c","tag-com-interface","tag-sicstus-prolog"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-sy","jetpack-related-posts":[{"id":1004,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/08\/05\/sicstus-prolog-building-a-windows-dll\/","url_meta":{"origin":1770,"position":0},"title":"Sicstus Prolog &#8211; Building a Windows DLL","author":"admin","date":"August 5, 2018","format":false,"excerpt":"Last week I upgraded to the latest version (4.4.1) of Sicstus Prolog\u00a0for Windows.\u00a0Since the Prolog engine can be embedded (royalty free) in other applications, it is useful to understand how to create a DLL (on Windows) for this purpose. In today's article, I would like to outline this process in\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Creating Prolog Program Image","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/08\/Saving-Prolog-Image.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/08\/Saving-Prolog-Image.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/08\/Saving-Prolog-Image.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1025,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/08\/19\/sicstus-prolog-building-a-windows-executable\/","url_meta":{"origin":1770,"position":1},"title":"Sicstus Prolog &#8211; Building a Windows Executable","author":"admin","date":"August 19, 2018","format":false,"excerpt":"In my previous post, I showed how to build a C-based Windows DLL to execute Prolog predicates in the Sicstus Prolog\u00a0engine. Today, I want to show how to build an executable (on Windows) from a C\/C++ program that uses Sicstus prolog engine. The process is quite simple. In order to\u2026","rel":"","context":"In &quot;Natural Language Processing&quot;","block_context":{"text":"Natural Language Processing","link":"https:\/\/www.rangakrish.com\/index.php\/category\/natural-language-processing\/"},"img":{"alt_text":"Creating WordNet Prolog Image","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/08\/Creating-image.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/08\/Creating-image.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/08\/Creating-image.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1677,"url":"https:\/\/www.rangakrish.com\/index.php\/2019\/08\/18\/using-berkeley-db-with-sicstus-prolog\/","url_meta":{"origin":1770,"position":2},"title":"Using Berkeley DB with Sicstus Prolog","author":"admin","date":"August 18, 2019","format":false,"excerpt":"One of the nice features of Sicstus Prolog\u00a0is the support for storing Terms\u00a0externally in a Berkeley DB\u00a0database. Since we can control how the terms are indexed in the database, it is possible to store and retrieve a large amount of Terms\u00a0(the limit is 2^32-1) fairly efficiently. This can be useful\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.rangakrish.com\/index.php\/category\/programming\/"},"img":{"alt_text":"Iterating Over the DB","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/08\/iterate-db.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/08\/iterate-db.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/08\/iterate-db.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2366,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/03\/28\/implementing-ilexicon-using-litedb\/","url_meta":{"origin":1770,"position":3},"title":"Implementing iLexicon using LiteDB","author":"admin","date":"March 28, 2021","format":false,"excerpt":"iLexicon is an \"intelligent\" dictionary that can be used to build Natural Language applications. I have two implementations, one in Lisp and another in Prolog. Both implementations are memory-based, in order to speed up performance. I have written several articles referencing it, for example see this. \u00a0 LiteDB is a\u2026","rel":"","context":"In &quot;Natural Language Processing&quot;","block_context":{"text":"Natural Language Processing","link":"https:\/\/www.rangakrish.com\/index.php\/category\/natural-language-processing\/"},"img":{"alt_text":"Sample Commands","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/03\/Session1.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2553,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/10\/03\/using-sicstus-prolog-with-lmdb\/","url_meta":{"origin":1770,"position":4},"title":"Using Sicstus Prolog with LMDB","author":"admin","date":"October 3, 2021","format":false,"excerpt":"In an earlier article, I showed how Sicstus Prolog allows us to use Berkeley DB for storage and retrieval of terms using the file system. Interestingly, the latest release of Sicstus Prolog, version 4.7.0, deprecates the Berekely DB library, while recommending an alternative implementation based on Lightning Memory-Mapped Database Manager\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.rangakrish.com\/index.php\/category\/programming\/"},"img":{"alt_text":"Facts About Homeopathic Remedies","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/10\/remedies-300x116.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3260,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/11\/26\/generating-polynomials-in-prolog\/","url_meta":{"origin":1770,"position":5},"title":"Generating Polynomials in Prolog","author":"admin","date":"November 26, 2023","format":false,"excerpt":"Polynomial is an important topic in High School maths curriculum. There are many online courses that explain the topic in great details with lots of examples and sample exercises. Wouldn\u2019t it be interesting if we can generate polynomials of given degree programmatically? It turns out that this is not such\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.rangakrish.com\/index.php\/category\/programming\/"},"img":{"alt_text":"Generating Polynomials - Main Clauses","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Code1-300x163.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Code1-300x163.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Code1-300x163.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/1770","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=1770"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/1770\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=1770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=1770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=1770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}