Excel RTD COM服务器 – 无法将UpdateEvent(类)转换为IRTDUpdateEvent(接口)

此问题与Kenny Kerr在“Excel RTD服务器:C#接口”中的博客文章相关,可在此处find该博客文章,该文章允许您构buildExcel RTD服务器,而不包含对任何特定Exceltypes库的引用; 不得不包括一个引用,使您的RTD服务器Excel版本特定(转发兼容,但不是向后兼容,我相信)。 对Exceltypes库不具有任何依赖性可简化RTD到具有不同版本的Excel(XP,2003,2007和2010)的计算机的部署。

现在,没有RTD引用特定的Exceltypes库来获取接口IRtdServer和IRTDUpdateEvent是非常好的。 但是我有一个时间使得Kenny的build议工作的魔鬼。

这是我所做的:

1) Added IRtdServer.cs and IRTDUpdateEvent.cs to my RTD project and put the interface definitions from your blog into those files (not changing the GUIDs). 2) Removed any reference to an Excel type library. 3) Build and regasm OK. 

我在VBA和VBScript中有很小的testing工具,通过模拟Excel对RTD的调用来testing我的MyRTD.dll RTD服务器。 以下是相关的代码片段:

第一个VBA:

 Const RTDProgID As String = "MyRTD.RTD" Const UpdateEventProgID As String = "MyRTD.UpdateEvent" ' Create the RTD server object. Dim rtd As Object Set rtd = CreateObject(RTDProgID) ' Start the RTD server, passing in a callback object. Dim callback As Object Set callback = CreateObject(UpdateEventProgID) Dim status As Long status = rtd.ServerStart(callback) <---- Fails here. 

此代码在最后一行上失败,消息沿着“无法将MyRTD.UpdateEvent强制转换为MyRTD.IRTDUpdateEvent”消息。 尽pipeUpdateEvent类实现了IRTDUpdateEvent接口。

第二个VBScript:

 ' ProgIDs for COM components. Const RTDProgID = "MyRTD.RTD" Const UpdateEventProgID = "MyRTD.UpdateEvent" ' Real-time data (RTD) object Dim rtd Set rtd = CreateObject(rtdID) ' Callback object. This is how ' the RTD would notify Excel of ' new data updates. Dim callback Set callback = CreateObject(UpdateEventProgID) ' Start the RTD server, passing in ' the callback object. Dim status status = rtd.ServerStart(callback) <---- Fails here. 

这段代码在最后一行失败,消息沿着“无效的过程调用或参数”(我假设callback的结果是错误的types/接口)。

任何帮助,将不胜感激。

  Best regards, Andrew Sheppard 

在做了一些这方面的工作之后,与Kenny Kerr交换了一些电子邮件,很明显,问题就出现了,因为具有相同GUID和使用ComImport的接口如果在不同的程序集中定义,则不会被视为相同的东西,即使定义相同。

我有一个进程内(DLL)和进程外(EXE)实时数据(RTD)服务器共享完全相同的代码库; 也就是相同的RTD,不同的执行模式。 我已经采取了IRTDUpdateEvent并把它放在它自己的程序集。 IRTDUpdateEvent当然是由Excel对象库实现的; 但我自己定义它,所以我不必让我的RTD依赖于特定版本的Excel(2002,2003,2007,2010),使部署更简单。

如果我使用C#4.0,这将不会是一个问题,因为“types等同”的新function。 您可以使具有相同GUID的类/接口具有相同的行为(这更有意义),而不pipe它们在何处定义。 但是我的目标平台是4.0之前的版本。

解决的办法是将IRTDUpdateEvent从它自己的程序集移回DLL和EXE程序集。 这样做,DLL和EXE RTD服务器与Excel和VBA以及VBScript和C#客户端一起工作。