Passing an interface to MethodInfo.Invoke()... where is QueryInterface?

This seems like it would work, but it doesn't:

Given this class

namespace FXSpreader

{

[ProgId("FXSpreader.RTDServer")]

[ComVisible(true)]

[Guid("60A6C959-649B-49f4-BD3A-1DAACFD35CF8")]

class RTDServer : IRTDUpdateEvent

{

...

}

Given this constructor:

public RTDServer(string _dataSource)

{

// Get the COM type

Type myType = Type.GetTypeFromProgID(_dataSource, false);

Guid guidRTD = myType.GUID;

if (myType != null)

{

// Create an instance of that type

Object tmpObject = Activator.CreateInstance(myType);

Excel.IRTDUpdateEvent tmpEventHandler = (Excel.IRTDUpdateEvent)this;

Object[] parms = { (Excel.IRTDUpdateEvent)tmpEventHandler };

MethodInfo mi = tmpObject.GetType().GetMethod("ServerStart");

mi.Invoke(tmpObject, parms);

}

}

"_dataSource" is a string that contains "FXRTD.RTDServer"

FXRTD.RTDServer is a class (in a different assembly) that implements Excel.IRtdServer and has a valid "ServerStart" method:

public int ServerStart(IRTDUpdateEvent CallbackObject)

When I step through, everything looks great up to the last line (mi.Invoke...).

At that point, parms is an array with a single element, but that element is not of type Excel.IRTDUpdateEvent. Instead, it's the type of "this". The Invoke method throws an exception and states that it cannot convert from the type of this to Excel.IRTDUpdateEvent.

The purpose of my application is to emulate Excel to use an Excel.IRtdServer for a datasource. The FXRTD.RTDServer works correctly in Excel.

If I could somehow get QueryInterface to return an object that isn't of type "FXSpreader.RTDServer" and instead is of type Excel.IRTDUpdateEvent then I'd be happy, but I've tried everything I can think of.

Any thoughts



Answer this question

Passing an interface to MethodInfo.Invoke()... where is QueryInterface?

  • R.Working

    And this solution is... (drumroll)

    Go back to ATL/COM. After spending a couple of weeks on this problem and unable to find a solution, I'm fairly confident that there simply isn't a solution.

    Even writing a Managed C++ class, the problem still exists. You simply cannot pass a pointer to an interface as a parameter to a COM method without switching to unmanaged code. None of the COM Interoperability helpers actually do what's necessary.

    I'm still hoping someone will prove me wrong, but for now I'm assuming this is the correct answer.


  • Passing an interface to MethodInfo.Invoke()... where is QueryInterface?