We will implement a simple custom COM component that exposes an interface to multiply two decimal numbers.
NAVAutomationServer
and choose to create a new solution for the project.using System.Runtime.InteropServices;
namespace NavAutomationServer { [ComVisible(true)] public interface IMultiplication { int Multiply(int x, int y); } [ComVisible(true)] [ProgId("My.TestClass")] public class Multiplication : IMultiplication { public int Multiply(int x, int y) { return x * y; } } }
interface
keyword. The complete interface declaration should look as follows:[Guid("F74FD5F7-B5FA-4939-892C-A4F222DFBF1D")] [ComVisible(true)] public interface ITestInterface { int Multiply(int x, int y); }
Multiplication
class, implementing the IMultiplication
interface.OnRun
trigger, open C/AL locals and declare two variables exported from the COM type library:
Name |
DataType |
Subtype |
Multiplication |
Automation |
'NAVAutomationServer'. Multiplication |
IMultiplication |
Automation |
'NAVAutomationServer'.IMultiplication |
CREATE(Multiplication,FALSE,TRUE); IMultiplication := Multiplication; MESSAGE(FORMAT(IMultiplication.Multiply(2,8)));
The .NET implementation of COM interfaces hides technical details from the developer. The development of a COM component without the .NET framework requires the definition of interfaces in Interface Definition Language (IDL) and its implementation. With the .NET framework, it is sufficient to declare an interface and a class implementing this interface with the ComVisible
attribute.
Each COM interop library, class, and interface is identified with a GUID that must be registered in the system registry. Step 4 through Step 6 describe how to generate a GUID and assign it to COM declarations.
All that is required to register the library is to enable the Register for COM interop option in the project properties, as shown in Step 9.
After the library is registered, it can be used in C/AL as an Automation type variable - this is done in Step 12. Here, we create an instance of the Multiplication
class and perform an explicit type cast by assigning the class variable to an interface variable:
IMultiplication := Multiplication;
3.147.47.144