Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Register for COM Interop" vs "Make assembly COM visible"

What is the real difference between these two options? What I know is:

Register for COM Interop
This options executes regasm on the assembly and registers the assembly as an COM component(or maybe not) in the registry with all COM like registry entries. Does this step generates a TLB file? What else is done?

Sometimes I see a tlb is generated when I compile the project but sometimes not, why is this?

Make assembly COM visible
What effect does this one has on an assembly? If I have the following type inside this assembly, do I still need to specify the "Make assembly COM Visible" even though my type is marked as ComVisible?

[GuidAttribute("02810C22-3FF2-4fc2-A7FD-5E103446DEB0"), ComVisible(true)] public interface IMyInterface { } 
like image 989
A9S6 Avatar asked Sep 13 '10 10:09

A9S6


People also ask

How do I make my assembly COM visible?

To make the assembly COM-visible in Visual Studio, select the “Make assembly COM-Visible” check box in the Assembly Information dialog box (Project Properties screen | Application page | Assembly Information button | Make assembly COM-Visible check box) as shown in Figure 3.

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.


2 Answers

"Make assembly COM visible" is a big hammer to make all public types in the assembly [ComVisible]. Rarely desirable, you will want to select the specific types that you want to be visible, like you did in your snippet.

After the assembly is built, it needs to be registered so that a COM client can find it back. Which only uses a number to identify the object he wants to create, the GUID, an extra lookup is necessary to find out what DLL implements it. Registering involves writing keys in the HKLM\Software\Classes\CLSID\{guid} portion of the registry. You can do it yourself by running Regasm.exe /codebase /tlb or you can leave it up to the build system to do it automatically after building the assembly.

Which is what "Register for COM interop" does. It is desirable since it ensures that the old copy of the DLL gets unregistered automatically before it is overwritten, that prevents registry pollution. VS needs to run elevated to have write access to those registry keys, one reason for making it optional. Or you just don't want to register it, common on build servers. I can't comment on why you'd sometimes not get the .tlb without more diagnostics.

like image 54
Hans Passant Avatar answered Oct 01 '22 12:10

Hans Passant


In addition to the accepted answer, the .tlb file is not created if the types or methods changed without first removing the corresponding record from the Windows's Registry (or closing the application or service that use the library), using the /U option of the RegAsm utility:

Regasm.exe yourlibrary.dll /codebase /tlb /u 

Also, it is important to mention that the registry for consumption of the library as COM, will only work in the development machine if it is compiled using the options Register for COM interop and Make assembly COM-Visible. It is necessary to register it manually in each destination computer using the indicated command line or, generating an script that is executed when the library is installed.

like image 21
César Qüeb Avatar answered Oct 01 '22 13:10

César Qüeb