COM Interop registration

大憨熊 提交于 2019-12-05 07:31:49

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.

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.

Here are a few other things to true

  • Make sure the types you want to register are marked as public
  • Add the ComVisible(true) attribute to the type directly in addition to the assembly
  • Add the Guid attribute

Not sure if the last 2 are strictly necessary but I would try them out.

I think you need to have a strong-named assembly. Do you sign your assembly with a key file?

Further, also try specifying : [Guid("{newly generated GUID}")] [ClassInterface(ClassInterfaceType.AutoDual)]

Tory

I had the same problem on a project where I checked the "Register For COM interop" option for my C# project. There is an easy solution:

While in the Solution Explorer, go to the Properties folder. Open the AssemblyInfo.cs file. Scroll down to the line that says: [assembly: ComVisible(false)]

Change this to: [assembly: ComVisible(true)]

This will remove the warning messages, and allows the .tlb file to be created, which then enables the .NET code for COM visibility.
If you don't want the entire assembly to be COM-visible then follow one of the other tips above.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!