Must the IUnknown interface be re-implemented by every new COM class?

≡放荡痞女 提交于 2019-12-23 10:20:46

问题


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


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/6507372/must-the-iunknown-interface-be-re-implemented-by-every-new-com-class

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