每个新的 COM 类都必须重新实现 IUnknown 接口吗?
抱歉,如果这个问题对每个人来说都是显而易见的,但我对 COM 很陌生。从教程中我在这里看到 http://www.codeguru .com/cpp/com-tech/activex/tutorials/article.php/c5567,似乎每个用 C++ 创建的 COM 类都必须实现自己的 QueryInterface、AddRef 和 Release。由于这些方法对于任何新类都应该具有基本相同的实现,因此我不明白为什么没有一些抽象类或任何为开发人员实现它的类。我不明白为什么我应该重新实现很多人已经一次又一次实现的同样的东西(除非教程是错误的并且有什么东西)。
谢谢
Sorry if this question seems obvious for everyone, but I am very new to COM. From the tutorial I see here http://www.codeguru.com/cpp/com-tech/activex/tutorials/article.php/c5567, it seems like every COM class created in C++ must implement its own QueryInterface, AddRef and Release. Since these methods should have basically the same implementation for any new class, I don't understand why there isn't some abstract class or whatever that implements it for the developer. I don't understand why I should re-implement the same thing that so many people have already implemented again and again (unless the tutorial is wrong and there IS something).
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
自由贸易协定:
回答你的问题:是的,每个COM组件都必须实现IUnknown,它是COM构建的基础。但是,对于创建 COM 对象的“标准管道”,这就是 ATL 项目向导 用于。
FTA:
To answer your question: Yes, every COM component must implement IUnknown, it's the foundation COM is built on. However, as for the "standard pluming" for creating COM objects this is what the ATL Project Wizard is for.
如果您不想使用 ATL 或其他帮助程序库,您可以使用 QISearch 为您处理 QueryInterface 的辅助函数。 AddRef 和 Release 可以在您自己的基类中实现。
COM 也需要与普通 C 一起工作,因此 windows sdk 并没有真正超出类及其方法的定义。
If you don't want to use ATL or other helper libraries you can use the QISearch helper function that handles QueryInterface for you. AddRef and Release can be implemented in your own base class.
COM needs to work with plain C also so the windows sdk does not really go beyond the definition of the class and its methods.
是的,每个 COM 类都必须实现
IUnknown
,因为每个 COM 类都继承自IUnknown
- 这是基本的 COM 技术原理之一。这通常是通过使用 ATL 来完成的 - 它具有模板和宏,可以相当轻松地完成此操作,即使您不想使用 ATL,您也可以轻松地为大多数琐碎的情况(例如实现一个接口)编写一个模板并重用它。Yes, every COM class must implement
IUnknown
, because every COM class inherits fromIUnknown
- that's one of the basic COM technology principles. This is usually done by using ATL - it has templates and macros for doing that rather easily and even if you don't want to use ATL you can write a template for most trivial cases (like implementing one interface) pretty easily and reuse that.