Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

com

iunknown

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

like image 594
Carl Avatar asked Jun 28 '11 13:06

Carl


2 Answers

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.

like image 51
Brandon Moretz Avatar answered Sep 29 '22 06:09

Brandon Moretz


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.

like image 43
Anders Avatar answered Sep 29 '22 05:09

Anders