6

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.

TCS
  • 5,790
  • 5
  • 54
  • 86
  • I'm not sure that any old C++ application can run a COM .dll without registration; that seems to be the antithesis of COM itself. Perhaps that application is using the functionality provided by that DLL directly, without going through the COM server intermediary. – antiduh Jul 14 '14 at 22:19
  • @antiduh notice I ask only about compiling, not running. – TCS Jul 15 '14 at 01:09
  • From .NET, you can reference the TLB, why do you say it has to be registered? – Simon Mourier Jul 15 '14 at 08:14

1 Answers1

3

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • First of all, thanks a lot for the detailed answer, but if I understand correctly, it should be possible to generate interop assembly on a machine that the COM DLL is registered and then on the build server I should be able only to reference the interop and everything should work, did I get it right? – TCS Jul 15 '14 at 05:58
  • I tried it and the compile fails, I want to know if it fails because I am missing something else, or I got the whole thing completely wrong... – TCS Jul 15 '14 at 06:44
  • You asked an XY question. I answered Y, I have no idea what X looks like. Ask another question and explain your problem, document it well. – Hans Passant Jul 15 '14 at 09:02