The Comimport Statement

The ComImport statement is used to import descriptions of classes and interfaces from libraries (dll) and types libraries (tlb). The From keyword is followed by a string with library GUID or the file path and the file name of the types library, from which descriptions are imported. Interfaces can be imported with the Interface keyword or classes can be imported with the Class keyword. All methods and properties with no Restricted or Hidden attributes are imported for each interface.

Use the New statement to create objects for imported classes. An instance of a COM object is created. Calling object methods and methods of interfaces implemented by COM objects results in calling corresponding methods of these COM objects.

Example

Consider the example of implementing a COM object in a project in the C# language and its further use in the Fore language.

Create a new console application project in the Microsoft Visual Studio development environment and name it, for example, TestCom. Determine the following settings for the project:

  1. Select the Class Library type ion the Application tab in the Output type drop-down list.

  2. Select the Register for COM interop checkbox on the Build tab.

  3. Add the commands on the Build Events tab in the Post-build event command line box:

Replace the generated default code in the Program.cs unit with the following C# code:

using System;
using System.Runtime.InteropServices;
namespace TestCom
{
    [Guid("33BE5927-0438-4505-A81F-76E09AFE7669")]
    [ComVisible(true)]
    public interface ITest
    {
        int TestSumm(int a, int b);
    }
    [Guid("BC528177-3B14-4D47-9C2D-E69B6045CC62")]
    [ClassInterface(ClassInterfaceType.None), ComSourceInterfaces(typeof(ITest))]
    [ComVisible(true)]
    public class Test : ITest
    {
        public int TestSumm(int a, int b)
        {
            return a + b;
        }
    }
}

GUIDs for interface and class can be replaced with new ones. After the project is compiled, the created library will be registered in the registry, and the TestCom.dll and TestCom.tlb files will be available in the subdirectory \TestCom\TestCom\bin\Debug\.

To work with a COM object in Fore, copy the obtained dll file to the folder with installed Foresight Analytics Platform and in the Fore code write the path to the tlb file, for example:

Comimport From "d:\Work\TestCom\TestCom\bin\Debug\TestCom.tlb"
    Interface ITest;
    Class Test: Object, ITest
    End Class Test;
End Comimport;

Sub Main;
Var
    a: Test;
    i: Integer;
Begin
    a := New Test.Create;
    Try
        i := a.TestSumm(19);
        Debug.WriteLine(i.ToString);
    Except
        Debug.WriteLine("Error executing the TestFunction function");
    End Try;
End Sub Main;

After executing the example the TestFunction function is executed. If the function returns a valid value, this value is displayed in the development environment console, otherwise an error message is returned.

Besides, one can rewrite the Comimport statement using the GUID of the created library, which is available in the AssemblyInfo.cs file of the C# project:

Comimport From "{ef6d37f5-ee50-4029-929f-e31e92e9164d}"
    Interface ITest;
    Class Test: Object, ITest
    End Class Test;
End Comimport;

See also:

Fore Language Guide