为什么COM接口契约不可改变? [英] Why is the COM interface contract immutable?

查看:107
本文介绍了为什么COM接口契约不可改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经google了很多,发现很奇怪,没有人关心为什么COM接口是不可变的。我想,你无法从COM接口中删除任何方法的原因是因为客户端依赖于该接口,将遇到一个错误,这是不好的。但为什么要添加新的功能到界面改变任何?

I've googled quite a bit, and found it weird that no one cared to explain why COM interfaces are immutable. I suppose that the reason you're unable to remove any methods from a COM interface, is because a client relying on that interface, would encounter an error, which isn't good. But why would adding new features to the interface change any of that? Has this something to do with the underlying vtable?

推荐答案

如果添加一个方法,使用该方法的较新的客户端将失败当使用较旧版本的组件时。

If you add a method, a newer client that uses that method will fail when working with an older version of the component.

旧版本的组件不会有新的方法,除非您专门添加代码来实现它,重建组件,然后在所有使用组件。

Older versions of the component won't have the new method unless you specifically add code to implement it, rebuild the component, and then reinstall the component on all machines that use the component.

如果较新版本的客户端尝试在没有该方法的组件的旧版本上调用新方法,将会发生未定义的行为崩溃,但是静默数据损坏也是可能的)。新客户端将尝试通过vtable中的指针条目调用方法,当旧客户端构建时不存在该指针条目,因此旧客户端在此位置将具有一些不相关的值。

If a newer version of the client attempts to call the new method on an older version of the component that doesn't have the method, undefined behavior will occur (likely a crash but silent data corruption is also possible). The new client will be attempting to call a method through a pointer entry in the vtable that did not exist when the old client was built so the old client will have some unrelated value in this location.

当然,如果您控制客户端和组件并将它们部署在一起,但是COM设计用于更广泛的用例,这不会是一个问题比起那个来说。

Of course, this wouldn't be an issue for you if you control both client and component and deploy them together but COM is designed for a much broader range of use cases than that.

这篇关于为什么COM接口契约不可改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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