Would adding a new function to the existing COM interface break its binary compatibility?

旧街凉风 提交于 2019-12-07 08:45:57

问题


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?


回答1:


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.

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.

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.

For example:

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();
    }

    ...
}

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




回答2:


It would certainly break derived interfaces, so it should not be done even if it appears to work.

Instead, derive a new interface containing the additional method(s), and have clients that require the additional functionality QI for the new IID.




回答3:


As long as new method is appended, COM interface should be backward compatible. If client application wants to use new COM interface, they have to update header file (C++) or re-add reference(.NET) to the new COM interface. Also, you may need to restart the OS if COM interface is changed without updating IID. In conclusion, it would work without changing IID, but it may be more "correct" to change IID.



来源:https://stackoverflow.com/questions/9258154/would-adding-a-new-function-to-the-existing-com-interface-break-its-binary-compa

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!