reflectioncominteroprcw

Fastest way to call a COM objects method without using a RCW


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?


Solution

  • 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.