Click here to Skip to main content
15,916,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Microsoft.Office.Interop.Excel has the IRtdServer interface.

This interface is imported into a server app as
<ComImport(), Guid("EC0E6191-DB51-11D3-8F3E-00C04F3651B8"),
InterfaceType(ComInterfaceType.InterfaceIsDual)> _
Public Interface IRtdServer

    Function ServerStart(callback As IRTDUpdateEvent) As Integer

    Function ConnectData(topicId As Integer, <MarshalAs(UnmanagedType.SafeArray, SafeArraySubType:=VarEnum.VT_VARIANT)> ByRef strings As Array, ByRef newValues As Boolean) As Object

    Function RefreshData(ByRef topicCount As Integer) As <MarshalAs(UnmanagedType.SafeArray, SafeArraySubType:=VarEnum.VT_VARIANT)> Array

    Sub DisconnectData(topicId As Integer)

    Function Heartbeat() As Integer

    Sub ServerTerminate()

End Interface

The RTD server works fine with Microsoft Excel.

This interface is also imported into a client app that uses the server.

The code to create the server instance:
Dim serverType As Type = Type.GetTypeFromProgID(progID)
Dim obj As Object = Activator.CreateInstance(serverType, True)
Dim server As IRtdServer = CType(obj, IRtdServer)

This code raises the cast exception:
Unable to cast object of type '...' to type 'TestConsole.IRtdServer'.

It seems that the compiler do not use the COM interface declaration and use the client assembly type.

Excel works with the server correctly. So, the problem is in the client code...

Please help.

VS 2010, .NET 4.0.

1 solution

Probably it display an error that unable to cast due QueryInterface fails this mean that the object you trying to create does not support such interface. Internally it calls query interface with Guid specified in interface declaration, so check that guid too.
Also try to obtain the CLSID of the object and create the object by this CLSID instead of ProgID.
Another possible issue is in case if you create the COM object in one thread and access it in another with different threading model can cause such exceptions.
Also not sure that CType is necessary in your declaration, as I know it casting by default with QueryInterface.

Share this answer

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900