每个新的 COM 类都必须重新实现 IUnknown 接口吗?

发布于 2024-11-17 18:46:11 字数 407 浏览 2 评论 0原文

抱歉,如果这个问题对每个人来说都是显而易见的,但我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

全部不再 2024-11-24 18:46:11

自由贸易协定:

“我相信每一位希望了解 COM 背后基本原理的程序员都必须使用纯 C++ 编写至少一个简单的 COM 对象,即无需借助 MFC/ATL 附带的模板和宏。”

回答你的问题:是的,每个COM组件都必须实现IUnknown,它是COM构建的基础。但是,对于创建 COM 对象的“标准管道”,这就是 ATL 项目向导 用于。

FTA:

"I believe that every programmer who wishes to understand the basic principles behind COM, must write atleast one simple COM object using plain C++ , i.e. without the aid of templates and macros that comes along with MFC/ATL."

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.

酒解孤独 2024-11-24 18:46:11

如果您不想使用 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.

故事未完 2024-11-24 18:46:11

是的,每个 COM 类都必须实现 IUnknown,因为每个 COM 类都继承自 IUnknown - 这是基本的 COM 技术原理之一。这通常是通过使用 ATL 来完成的 - 它具有模板和宏,可以相当轻松地完成此操作,即使您不想使用 ATL,您也可以轻松地为大多数琐碎的情况(例如实现一个接口)编写一个模板并重用它。

Yes, every COM class must implement IUnknown, because every COM class inherits from IUnknown - 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文