Solved

COM to .NET and .NET To COM

Posted on 2013-05-16
5
499 Views
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
OR
   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?
0
Comment
Question by:metro156
  • 3
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 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.
0
 

Author Closing Comment

by:metro156
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.
0
 

Author Comment

by:metro156
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.
0
 
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.
0
 

Author Comment

by:metro156
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.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Iterate two lists and remove inactive 11 49
DataGridView Events ? 3 37
VB.net/WinForms: adjust Screen Resolution 6 41
What .NET URL re-routing tool did I use? 2 37
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now