在现有的COM接口上添加新功能会破坏其二进制兼容性吗? [英] Would adding a new function to the existing COM interface break its binary compatibility?

查看:58
本文介绍了在现有的COM接口上添加新功能会破坏其二进制兼容性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在COM接口上添加一个新方法,而所有现有方法都保持不变,这会在更新之前破坏对消费者的兼容性吗?

I want to add a new method to a COM interface with all the existing one unchanged, will this break its compatibility for the consumer before this update?

推荐答案

这取决于:如果这是一个内部未发布的接口,则您可以随意更改它,只要您控制与该接口交互的所有代码即可.

It depends: if this is an internal unpublished interface, you are free to change it at will so long as you control all the code that interacts with that interface.

但是,一旦发布,规则就是严格的:每个接口都有自己的IID.您可以通过修改,添加或删除方法的任何方式来更改该接口,它是一个全新的接口,并且需要一个新的IID.

Once published, however, the rules are strict: every interface has its own IID. You change that interface in any way - by modifying, adding or removing methods - it's a whole new interface, and requires a new IID.

但是:COM并不关心新接口的实现方式:因此,您可以让您的类将其实现为仅添加新方法的旧接口的派生,并让您的实现类实现该派生,因此只要QI在询问旧接口或新接口时返回合适的接口即可.

However: COM does doesn't care how that new interface is implemented: so you can have your class implement it as a derivation of the old interface that just adds a new method, and have your implementation class implement the derivation, so long as QI returns a suitable interface when asked for either the old or new interface.

例如:

class IInterfaceOriginal: public IUnknown
{
public:
    ...
    // lots of methods
    ...
};

class IInterfaceUpdated: public IInterfaceOriginal
{
public:
    // Add just one additional method
    STDMETHOD(AdditionalMethod)(...) = 0;
};


class CImplementation: IInterfaceNew // this was IInterfaceOld
{
    // Also add implemention of AdditionalMethod somewhere here...

    HRESULT STDMETHODCALLETYPE QueryInterface( REFIID riid, void **ppvObject )
    {
        *ppvObject = NULL;
        if(riid == __uuidof(IUnknown)
        || riid == __uuidof(IInterfaceOriginal)
        || riid == __uuidof(IInterfaceUpdated)) // This is added
        {
            // Return a IInterfaceUpdated in response to a QI for either of IUnknown,
            // or the old or new interface. This works because in C++, any IInterfaceUpdaed
            // is also both of those two other interfaces.
            *ppvObject = static_cast<IInterfaceUpdated*>(this);
        }
        else
            return E_UNKNOWN;
        return ((IUnknown*)*ppvObject)->AddRef();
    }

    ...
}

因此,从技术上讲,添加另一个接口"时,实际上是在这里添加了一些代码:仅定义一个从旧接口派生的新接口,将您的类实现的接口更改为新接口(并添加新方法的实现),最后更新QI以支持新方法和旧方法-为两者(以及IUnknown)返回相同的接口.

So, while you are technically "adding another interface", you're actually adding little code here: just defining a new interface that derived from the old one, changing the interface your class implements to the new one (and adding the implementation for the new method), and finally updating QI to support both the old and new methods - returning the same interface for both (and for IUnknown too).

这篇关于在现有的COM接口上添加新功能会破坏其二进制兼容性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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