Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

COM Interop registration

Tags:

c#

.net

com

I have a .NET assembly which I am exposing to COM. The assembly has two public interfaces and one public class. When I build the assembly I get this warning:

(assemblyName.dll) does not contain any types that can be registered for COM Interop.

My assembly information includes the following line.

[assembly: ComVisible(true)]

Most people having this problem on the web, that I have found, fixed it with the above line in their assembly information. This has not helped for me.

I also tried adding [ComVisible(true)] to the class and interface definitions, and it also did not help.

like image 468
trampster Avatar asked Nov 06 '09 03:11

trampster


People also ask

What does register for COM Interop do?

This option will cause Visual Studio to automatically register your assembly as a COM component in the windows registry when the project is compiled. Registering for COM Interop requires administrator permissions.

How do I register .NET COM DLL?

You can run a command-line tool called the Assembly Registration Tool (Regasm.exe) to register or unregister an assembly for use with COM. Regasm.exe adds information about the class to the system registry so COM clients can use the . NET Framework class transparently.


2 Answers

ComVisible classes generally need to have a public default constructor. Its members should typically also reference only ComVisible types.

You don't need to specify ComVisible(true) on the class if you have specified it at the assembly level.

However, the usual way to generate an assembly with ComVisible classes is:

  • Specify ComVisible(false) at assembly-level. Thus only classes that are explicitly marked with ComVisible(true) are exposed to COM.

  • Define an explicit ComVisible interface :

e.g.

[
ComVisible(true),
GuidAttribute("..."),
Description("...")
]
public interface IMyComVisibleType
{
        // members...
     }
  • Your ComVisible class should specify ClassInterfaceType.None, and should implement the ComVisible interface:

e.g.

     [
     ComVisible(true),
     GuidAttribute("..."),
     ClassInterface(ClassInterfaceType.None)
     ]
     public sealed class MyComVisibleType : IMyComVisibleType
     {
        // implementation ...
     }

Note that the Guid and Description attributes are not required, but useful to give you more control of the COM generation.

If the above doesn't help, try posting some sample code and I'm sure someone will be able to help.

like image 183
Joe Avatar answered Oct 16 '22 16:10

Joe


I ran into the default constructor problem. What fooled me was that the type library file will contain the class GUID reference even though that class is not being registered. A quick way to see what will be registered is to create a registry file ('assembly.reg') like this:

regasm assembly.dll /regfile:assembly.reg /codebase

There's a good discussion of exposing interfaces in COM Interop: Base class properties not exposed to COM. Some example code is here: Exposing .NET Components to COM.

like image 37
Bob Nadler Avatar answered Oct 16 '22 16:10

Bob Nadler