Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is Interop.xxxxx.dll generated?

I recently took over a project and have little VS COM experience so please forgive me if I am not asking the right questions..

I have a C++ project which generate a COM dll, lets name it abc.dll.

I have another C# project which references the COM dll, however under the references, it is pointing to Interop.abc.dll. I deleted all abc.dll within the directory and interrop.abc.dll to see how the project would react, upon starting the project, interop.abc.dll is automatically generated. This boggles my mind because I dont know how interop.abc.dll is generated.

So here are my questions:

  1. How does the C# project reference the interop.abc.dll initially if this is generated?

  2. How is the interop.abc.dll generated if there is no abc.dll to begin with (I havnt built it)

  3. I played around with the project and then interop.abc.dll stopped generating and was causing errors, why is it?

like image 955
user2584960 Avatar asked Mar 07 '14 23:03

user2584960


People also ask

How do I create an Interop DLL?

To generate an interop assembly from a type libraryAdding the /out: switch produces an interop assembly with an altered name, such as LOANLib. dll. Altering the interop assembly name can help distinguish it from the original COM DLL and prevent problems that can occur from having duplicate names.

What is an Interop DLL?

The interop DLL wraps the calls to the VB6 component and makes them transparent. When registering the DLLs on the machine you'll be executing the application on, you still have to register the VB6 DLL. The interop DLL will sit your app's bin folder and Marshal the calls out.

What is Tlbimp?

The output of Tlbimp.exe is a binary file (an assembly) that contains runtime metadata for the types defined within the original type library. You can examine this file with tools such as Ildasm.exe. This tool is automatically installed with Visual Studio.


1 Answers

COM declarations are stored in a type library. Which is very similar to .NET metadata, COM was the grand-father of .NET, metadata that tells a compiler what types are stored in an assembly. Type libraries however use a fairly awkward binary format, it is very different from .NET metadata. Different enough to make the conversion from a type library to .NET metadata non-trivial. Some constructs have no conversion at all, some are troublesome enough that you ought to know about it.

So the .NET team decided that the conversion should be a separate step, one that could display warnings, or errors, when the type library content doesn't match .NET metadata closely enough. That conversion tool is Tlbimp.exe, the type library import tool. They did add this feature to the Project + Add Reference dialog. As long as the conversion doesn't generate any warnings that this works just fine. It very often does.

So the job of Tlbimp.exe, and the IDE in the case of Add Reference, is to mechanically translate the type library content to .NET metadata content. By convention, a type library named "Foo" is converted to "Interop.Foo.dll". It doesn't have to be, just the default name. You'll end up with a .NET assembly that doesn't contain any code, just metadata. .NET types that any .NET compiler can directly consume and otherwise match the type library declarations. The [ComImport] attribute is a very important one that tells the CLR that the metadata is actually for COM types.

So this all explains bullet 1. Bullet 2 is a no-go, you really do need the type library. It is very often embedded as a resource in the unmanaged DLL, Tlbimp.exe knows how to find it. Nobody can see the errors in 3 so of course it is unguessable what the problem might be. Just keep in mind that the conversion is not always without trouble, the point of making the conversion step explicit.

like image 199
Hans Passant Avatar answered Oct 19 '22 12:10

Hans Passant