If you have to create a dll with C# (or VB.net) and use it in old .NET framework, you have to create a COMvisible dll. How? Let’s start!

  1. First of all, you have to create the inteface for the method you want to make COMvisible. So, for example:
    namespace Myns
    {
        [ComVisible(true)]
        public interface Myinterface
        {
            [ComVisible(true)]
            string Foo(string param1, string param2, out string output, out int outputLen);
             .....
        }
    }
    

    To use correctly the ComVisible attribute, include the dll System.Runtime.InteropServices.

  2. Your class has to implement your intercace. Further, before you class, write this:
    namespace Mysn
    {
    ....... //see previous snippet
    [ClassInterface(ClassInterfaceType.None)]
    [ComVisible(true)]
    public class Myclass : Myinterface
    {
    ......
    }
    
  3. Before the class to make COMvisible,write:
    [ComVisible(true)]
    public
    <pre>string Foo(string param1, string param2, out string output, out int outputLen)
    {...}</pre>
    
  4. Now, in the project properties, check the “Make assembly COM visible” checkbox in the assembly info pop up.
  5. Finally, check the “Register for COM interop box in the project’s build option  if you want Visual Studio to register the assembly on the build machine (to make this work, open Visual Studio as administrator).

NOTE: the framework of the project must be equal or lower the framework 3.5

ANOTHER NOTE: it’s better if you sign the project, by using the Signing tab in a project’s Properties dialog box in Visual Studio. This is the easiest and most convenient way to sign an assembly with a strong name.

Advertisements