Click here to Skip to main content
Click here to Skip to main content

Get rid of COM Interop DLL by using the new C# 4 dynamic keyword

By , 7 Jan 2011
 

Up to now, to call a COM object from managed code, you had to either:

  • make a reference to the COM instance from your Visual Studio project (which means you had to have the COM object setup on your development machine)
  • or make a reference to a precompiled assembly containing your COM interop wrapper

With the new framework .NET and C# 4 comes the dynamic keyword that allows binding to an object's method at runtime using the underlying DLR.

So to make thing short, the only thing you've got to do to interop with a COM object is to instantiate it using an activator as a dynamic variable and call a runtime bound method on it, in two lines:

dynamic comInterop= Activator.CreateInstance(Type.GetTypeFromProgID("MyCOM.Object.Name"));
var result = comInterop.MethodCall(parameter);

Hope this helps! :-)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Philippe Bouteleux
Software Developer (Senior)
France France
Member
A happy .NET developper, fond of all the new bits of technologies M$ has to offer.
 
I code in C# using WPF, Linq, WCF and SQL Server all day long and I'm paied for that Smile | :)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionInconsistent crashes in C# application which uses COM interopmemberAmodVKulkarni14 Jun '12 - 2:51 
Hi Philippe,
 
I have an addin written in C# which runs inside Microsoft Project. This addin inturn uses COM API of another application (our own product which exposes COM API to communicate with its database). The target framework is .NET 4.0.
 
Most of the times the functionality of addin works fine. But sometimes (very unpredictable) we found that COM API does not get called when they are expected to get called. We confirmed that by putting log information inside API implementation. This log does not get generated when we face this issue. The still bad part is that addin gets crashed when this error situation occurs.
 
I have two points.
 
1) Is there any known issue with COM interoperability in .NET which can result into such inconsistent behavior?
 
2) Do you think your suggestion of using dynamic keyword and eleminating interop dll can improve the things. If yes, we might think of refactoring our code.
 
Thanks in advance for your reply.
AnswerRe: Inconsistent crashes in C# application which uses COM interopmemberPhilippe Bouteleux14 Jun '12 - 4:25 
Hi !
 
I'm sorry but I think I won't be of great help for you as I don't know how you are presently calling your COM object and whether there may have some known issues doing so.
 
However, I never had any issues related specifically to making calls from a .NET client to a COM server using wrappers within interop DLL.
 
That said, the best option is actually "give it a try" and see how it behaves going through dynamic instead.
 
As it is just two lines of code, I guess you only have to comment your actual code and voilà !
 
Cheers
QuestionObject Requiredmemberttflim21 Apr '12 - 14:15 
Tried your post but keep getting the error "Object Requried". Am I missing a step or 2?
AnswerRe: Object RequiredmemberPhilippe Bouteleux14 Jun '12 - 4:31 
Hi !
 
Sorry I've missed the notification of your request somewhere in the flow of my mailbox.
 
Just got another question and realized I missed yours. Whatever.
 
Not sure if it still relevant but I'd guess that it is the GetTypeByID() that returns null ?
 
Your COM identifier may not be correct or your COM server not correctly setup. Have you any other way to check that first?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 7 Jan 2011
Article Copyright 2011 by Philippe Bouteleux
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid