升级COM回调接口

Upgrading COM call-back interface

本文关键字:接口 回调 COM 升级      更新时间:2023-10-16

COM连接点回调接口有问题

在我们的样本中。如果我们有很少的回调接口ISomeEvents

interface ISomeEvents : IUnknown
{
         HRESULT Event1([in]int nData);
         HRESULT Event2([in]int nData);
         HRESULT Event3([in]int nData);
}
And in the CoClass we have the following statement 
coclass MyComp
{
    [default] interface IMyInterface;
    interface IMyInterFace2;
    [default, source] interface ISomeEvents;
};

现在每当我们添加新的接口作为增强的一部分,这不会破坏现有的客户端,但如果增强有对回调的任何修改最终都会更新接口ISomeEvents,这会破坏现有的客户端,我们被迫这样做,因为我认为我们可以只有一个[default,source]接口。

谁能告诉我这个问题的解决方法是什么?

的问候汤姆

如果您正在开发一个COM对象,其中客户端处于活动/生产状态,那么您真的不应该更改现有的接口,任何接口。这一直是COM开发的基本规则:"接口是不可变的";当然,在早期开发过程中也会有例外。

(这里引用一个例子:"COM接口是不可变的。你不能定义一个旧接口的新版本并给它相同的标识符。"(http://msdn.microsoft.com/en-us/library/windows/desktop/ms688484 (v = vs.85) . aspx)。在web上查找"interfaces are immutable"获取更多信息)

在这种情况下,您应该根据需要创建新的接口ISomeEvents2ISomeEvents3等,并让这些接口从以前的版本继承。将新接口作为新的默认源。

interface ISomeEvents1 : ISomeEvents
{
    /* new enhanced events here, for use by newly compiled clients */
}

最后,如果你需要更多的修改:

interface ISomeEvents2 : ISomeEvents1
{
    /* Even newer enhanced events here, for even newer clients */
}

等等