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 GUID or the file path and the file name, from which descriptions are imported. Interfaces can be imported with the Interface keyword or as classes 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.
Executing the example requires the MyDll.dll file of the type library located in the root directory of the C disk. The library implements a COM object with the name "Test" recorded in the Windows registry. The interface of the ITest COM object has the implemented TestFunction function accepting two integer parameters for input and returning a real value.
Comimport From "C:\MyDll.tlb"
Interface ITest;
Class Test: Object, ITest
End Class Test;
End Comimport;
Sub Main;
Var
a: Test;
i: Double;
Begin
a := New Test.Create;
Try
i := a.TestFunction(4, 4);
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.
If the library GUID is known, the Comimport statement can be written as follows:
Comimport From "{BC528177-3B14-4D47-9C2D-E69B6045CC62}"
Interface ITest;
Class Test: Object, ITest
End Class Test;
End Comimport;
See also: