views:

429

answers:

3

I'm trying to find the cleanest and fastest way for calling a COM objects methods.

I was using a RCW for the object but every time a new version of the third party COM object comes out its GUID changes which then renders the RCW useless, so I had to change and start using

Type mytype = Type.GetTypeFromProgID("MyCOMApp.Application");

so that every time a new version of the COM object comes out I don't have to recomplie and redeploy my app.

At the moment I am using refelection like mytype.InvokeMemeber but I feel it is so slow compared to just calling the RCW.

How does everyone else tackle the problem of changing 3rd party COM object versions, but still maintaining the speed of a RCW?

+3  A: 

If you want to make the calls in reflection easier, you could use VB.NET, and make late calls on a variable typed as Object. VB.NET will help with the calls to reflection. You can also set a reference to Microsoft.VisualBasic.dll and make the call to CallByName as well to help with the reflection calls.

However, is it the IID (the interface GUID) or the class GUID that changes? If it is the class GUID that changes, then you can define the interface once, and then get the Type through a call to GetTypeFromProgID. Once you have that, you can pass the type to the CreateInstance method on the Activator class and then cast to the interface, which won't change.

If the IID does change, however, you will have to use reflection every time.

casperOne
Is using VB late binding slower then using reflection in C#?
Nathan W
@Nathan W: No, it's not. The mechanism at its core is the same (Reflection).
casperOne
I found out that it is only the class GUID that changes so the cast to the interface method works great. Thanks :)
Nathan W
A: 

I am not a pro on this subject.

Having said that, you can create a VB6 based class library which wraps the COM component you want to use & let VB6 do the work of creating a new instance for you using late binding.
e.g. CreateObject("Excel.Application")

You might also let the actual methods be called by VB6 class rather than asking .NET.
Not sure, if that helps at all.

shahkalpesh
A: 

Dear

Create RCW file with COM object then use RCW file using reflection. Use ILDASM.exe utility to find methods in RCW.

Muhammad Aqeel Qureshi

aqeel