On .NET/COM Interoperability

I. Deploying a .NET Application for COM Access

  1. Create a .NET assembly containing a class that will be called by a COM client.
    Keep in mind a special requirement for the .NET code structure:

    • the .NET code should export a signed interface
      Guid("2D570F11-4BD8-40e7-BF14-38772063AAF0")]
      [InterfaceType(ComInterfaceType.InterfaceIsDual)]
      public interface INETCode
      {
      // exposed properties and methods
      }

    • In the .NET class, the INETCode interface should be the first interface implemented in the class declaration:

      [Guid("478176F4-5105-435c-8EBC-D4CB90B7B1C7")]
      [ClassInterface(ClassInterfaceType.None)]
      [ProgId("<Namespace>.NETCode")]
      public class NETCode : System.Windows.Forms.UserControl, INETCode
      {
      // implementation exposed methods for properties getter/setters.
      }

  2. Create a key pair and use it to sign the assembly to make the assembly globally available.
    To make it easier to work with the assembly one can add a title and description.
    To sign the assembly, one can run the sn utility and add the name of the key file by hand,
    or you can generate a strong name using the Visual Studio .NET user interface
    by edditing the <assembly-nameInfo>.vb file.

  3. Create a type library for the assembly (.tbl) with tblexp:
    tlbexp <assembly-name>.dll /out:<table-name>.tlb

  4. Register the type library (also creates the library itself).
    For it use the Assembly Registration Tool regasm from the .NET Framework SDK:
    regasm /tlb:<table-name>.tlb <assembly-name>.dll

  5. Install the .NET assembly into the Global Assembly Cache (GAC)
    so that it will be available as a shared assembly. To install an assembly into the GAC, use the gacutil tool:
    gacutil /i<assembly-name>.dll

II. Check the DLL generated

  • to see all functions that DLL exports, type in VS comand promt
    link /dump /exports <libname>.dll
    I'm sure there is a more elegant way to do it, but this one works as well

III. Calling a .NET assembly from within Visual C++ COM code