Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


COM to .NET and .NET To COM

Posted on 2013-05-16
Medium Priority
Last Modified: 2013-05-17
When we talk about creating communication between a .NET assembly and a COM assembly:
Scenario 1 : .NET client  to COM.dll  
   Create a interop assembly by
      1) tlbimp.exe  COM.dll   ( which contains a type library)
      2) The tlbimp.exe command creates an interop assembly based upon the type library  in COM.dll. Let's call that Interop assembly Interop.dll
      3) Then we add a reference to .NET Client project -selecting Interop.dll as our reference.

OR 1) more simply We can directly create a Interop reference simply by using Visual Studio - Add Reference to the COM.dll ( which will automatically extract the type lib and creates the interop assembly)
Scenario concerning COM client   to .NET
 Using  the .NET dll let's call it myNet.dll:
   1) execute Regasm.exe  myNET.dll to register the myNET.dll public classes
   2) Now a COM client can communicate with the  myNET.dll
   1)Simply Register myNET.dll using Visual Studio's - Selecting Build Tab- Register For COM Interop check box) and build the project myNET. This should automatically register the public classes of the .NET module myNET.dll

In both scenarios: Are we essentially creating a code stub ( inter-op assembly) for both scenarios to communicate with the original COM.dll and myNET.dll respectively. Meaning after all that setup the original dlls still need to be present to communicate to?

Results Diagram:
.NET Client --> Interop.dll -> COM.dll
COM Client --> Interop.dll -> myNET.dll

Why do I ask this question? Well in my MCTS 70-536 exam book, it talks about in the case of COM to .NET. In that case it says the  command Regasm.exe myNET.dll registers the public classes contained in myNET.dll so a COM client can talk to it. Isn't it really registering the interop.dll so COM client will communicate with myNET.dll? The Interop.dll being the communication stub between them. And the same for .NET to COM?
Question by:metro156
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 40

Accepted Solution

Jacques Bourgeois (James Burger) earned 2000 total points
ID: 39173888
The interop is necessary only when .NET communicates with COM. It is not only a stub, it also performs a few functions such as converting the format of the dates which is different between .NET and COM.

By the way, with framework 4.0, there is also the option of embedding the Interop types into the .NET application so that one can get rid of the interop file. See Embed Interop Types in the Properties window when you are in the Reference tab.

When Microsoft conceived .NET, they were aware that we would need that feature and they provided the interop as a way for .NET applications to communicate with COM applications.

The other way around, since COM was designed before .NET, it has no awareness of what .NET is. So they designed the thing another way. My understanding is that when you activate the Make assembly COM-Visible option, the compiler creates into the application classes that do the reverse, such as translating dates from the .NET format to the COM format. These are the classes that are registered and exposed to COM applications. So, in some way, the interop is then built into the .NET application.

Author Closing Comment

ID: 39174575
Great answer again. I think what threw me is what I read in my MCTS -Exam book; the statement registering classes versus assembly.  What came to mind is where are those classes resident after the Regasm.exe is performed?

Thank you.

Author Comment

ID: 39174703
Just a comment about calling a COM.dll from a .NET client. My MCTS Exam book mentions that you can call a COM.dll by creating prototype methods using the DllImport attribute beside what we just discussed.  However, the book shows an example only using the DllImport attribute when you .NET client is talking to a Flat dll.  Sometimes authors can confuse you. Is that true that a DllImport attribute in your .NET client can call a COM.dll?
I believe the DllImport is called P/Invoke.
LVL 40
ID: 39175069
I have never heard of using COM through DllImport. But I never heard that it is not possible either. So I cannot say.

According to the documentation, "You apply this attribute directly to C# and C++ method definitions". You can create COM dlls with other languages than C and C++

And if it is possible, But it would be a very limited way of working.

Also according to the documentation, "You cannot use methods that require an instance of a class". Only static/shared methods. Since most uses of COM is through objects, it would not be very useful.

And marshalling is not supported so it would not make the corrections such as the date formats I talked about sooner.

And it requires more workd from the programmer.

So, if it is possible, I do not see the point.

Author Comment

ID: 39175308
Maybe while writing the book, their proofreaders somehow overlooked the statement. Anyway, I definitely agree with what you have said about DllImport attribute.
Thank you for the great effort you have made in clarifying COM to NET and NET To COM.

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question