Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C# needs to register COM DLL in order to reference it?

To use COM .dll in C++, all I need in compile time is to #import the TLB (or DLL which extracts the TLB), and I'm ready to go. Why does C# require the DLL to be registered (reg-free COM is the same as registered) to compile? Why does the TLB not enough?

Notice my question regards the compilation of the .dll using the COM object. I understand why the object must be registered during runtime.

like image 638
TCS Avatar asked Jul 14 '14 18:07

TCS


1 Answers

First off, unlike a C++ compiler or the many other compilers that can read type libraries, neither the CLR nor .NET compilers actually read a type library. They both depend on an interop library, a .NET assembly that's generated from a type library. It only contains declarations, decompiled from the tlb, in a format that both the CLR and the compilers can understand.

The primary tool that does this is Tlbimp.exe, the type library importer.

Running Tlbimp.exe is not a hard requirement, although you'd consider doing this on a build server. The IDE also support browsing registered type libraries from the Add Reference dialog. The item that gets added to the project contains the registry key, not the type library name. The <ResolveComReference> MSBuild task generates the interop assembly from the registry info.

The major advantage of doing this is that it is now easy to auto-generate the manifest entries so the COM server can be used without being registered. Aka "reg-free COM". The registry info is required to provide the manifest entries. Turned on simply by setting the Isolated property to True for the reference. Very desirable.

like image 157
Hans Passant Avatar answered Sep 24 '22 19:09

Hans Passant