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

.NetVisual StudioCom InteropCompiler Options

.Net Problem Overview


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
{
}

.Net Solutions


Solution 1 - .Net

"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.

Solution 2 - .Net

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionA9S6View Question on Stackoverflow
Solution 1 - .NetHans PassantView Answer on Stackoverflow
Solution 2 - .NetCésar QüebView Answer on Stackoverflow