ComImport IRTDServer接口的转换错误 [英] Cast error for ComImport IRTDServer interface

查看:96
本文介绍了ComImport IRTDServer接口的转换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Microsoft.Office.Interop.Excel具有IRtdServer接口。



此界面导入服务器应用程序

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

    <DispId(10)>
    Function ServerStart(callback As IRTDUpdateEvent) As Integer

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

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

    <DispId(13)>
    Sub DisconnectData(topicId As Integer)

    <DispId(14)>
    Function Heartbeat() As Integer

    <DispId(15)>
    Sub ServerTerminate()

End Interface



RTD服务器可以正常使用Microsoft Excel。



此界面也导入到使用服务器的客户端应用程序中。



创建服务器实例的代码:


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



似乎编译器不使用COM接口声明并使用客户端程序集类型。



Excel可以正常使用服务器。所以,问题在于客户代码...



请帮助。



VS 2010, .NET 4.0。


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.

推荐答案

你好,

可能它会显示一个错误,由于QueryInterface失败而无法转换,这意味着你尝试的对象创建不支持这样的界面。在内部,它使用接口声明中指定的Guid调用查询接口,因此也要检查guid。

还尝试获取对象的CLSID,并通过此CLSID而不是ProgID创建对象。

另一个可能的问题是,如果你在一个线程中创建COM对象并使用不同的线程模型在另一个线程中访问它会导致这样的异常。

也不确定你的CType是否必要声明,因为我知道它默认使用QueryInterface进行投射。



问候,

Maxim。
Hello,
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.

Regards,
Maxim.


这篇关于ComImport IRTDServer接口的转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆