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

3

6

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

Adoree answered 28/6, 2011 at 13:40 Comment(0)
P
3

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.

Pentameter answered 28/6, 2011 at 13:45 Comment(5)
This wizard is quite useless. When I try it, it tells me it is going to add ATL support to my project, and then I get a fatal error RC1015: cannot open include file "MyResource.h. My project compiles fine, and the MyResource.h file exists and it is the include path.Adoree
But anyway you answered the question, I forgot I had read that at the beginning of the tutorial sorry for the stupid question.Adoree
@Adoree Not a stupid question, everyone on earth has "duh" moments (these come exponentially when working with COM I've found). Good luck your project.Pentameter
@Carl: You resolve that RC1015 by editing the resource compiler settings - they are in Configuration->Resources->General and you have to alter the include path.Liberalize
@sharptooth: Yeah I had already done that, my project wouldn't compile if not. But when I run the wizard it gives me RC1015, then I check the project settings and my resource include paths are removed. So I re-write them, but every time I run the wizard they are removed, don't know why -_-...Adoree
D
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.

Dummy answered 28/6, 2011 at 13:52 Comment(0)
L
1

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.

Liberalize answered 28/6, 2011 at 13:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.