Creating and registering a COM component that can be used in NAV

We will implement a simple custom COM component that exposes an interface to multiply two decimal numbers.

How to do it...

  1. Start Visual Studio and create a new project. The initial steps repeat those from the Developing your own .NET class recipe from Chapter 4, .NET Interoperability in C/AL - you need to create a project based on a Class Library template. Name the project NAVAutomationServer and choose to create a new solution for the project.
  2. In the new project, remove all automatically generated using directives and insert one instead:
            using System.Runtime.InteropServices; 
    
  3. The following is code of the exported class:
            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; 
                } 
              } 
            } 
    
  4. Each COM class or interface is registered in the system registry and identified by its Globally Unique IDentifier (GUID). To generate a GUID for COM declaration, run a Visual Studio tool Create GUID from the Tools menu.
  5. The generated identifier can be presented in different forms for different purposes. For our COM interface, we need an attribute presentation [Guid(...)]. Choose the required form in the list and click Copy:

    How to do it...

  6. The new GUID is copied into the clipboard. Return to the code editor and insert it before the 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); 
            } 
    
  7. Click New GUID in form Create GUID to generate a new identifier for the Multiplication class, implementing the IMultiplication interface.
  8. Open project properties from the Project menu. In the Build tab, make sure that the Platform target is x64. If your build is targeting another platform, change it to x64.
  9. In the same tab, enable the Register for COM interop option.

    Note

    Visual Studio must be run with administrator credentials to register a COM component in the system registry.

  10. Close the properties and build the project. A library will be generated and automatically registered.
  11. In NAV C/SIDE, create a codeunit to test the new automation server.
  12. In the 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))); 
    

How it works...

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; 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.47.144