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

Carl
  • 1,224
  • 2
  • 19
  • 35

3 Answers3

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.

Brandon Moretz
  • 7,512
  • 3
  • 33
  • 43
  • 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. – Carl Jun 28 '11 at 13:50
  • But anyway you answered the question, I forgot I had read that at the beginning of the tutorial sorry for the stupid question. – Carl Jun 28 '11 at 13:51
  • @Carl Not a stupid question, everyone on earth has "duh" moments (these come exponentially when working with COM I've found). Good luck your project. – Brandon Moretz Jun 28 '11 at 13:54
  • @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. – sharptooth Jun 28 '11 at 13:59
  • @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 -_-... – Carl Jun 28 '11 at 14:16
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.

Anders
  • 97,548
  • 12
  • 110
  • 164
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.

sharptooth
  • 167,383
  • 100
  • 513
  • 979