-1

I am writing a C++ class that consumes at least one COM interface from the Windows API. Obviously, when consuming these objects inside of the class I would need to properly call AddRef() and Release().

My question is...do I need to additionally implement IUnknown in the C++ class itself? My understanding is that I would only need to do that if the interface I was using was performing asynchronous operations or callbacks. In that case you would need to make sure that the C++ class itself was still "alive" when the asynchronous operation or callback completed. is this correct?

For clarification (and per Hans' comment) the specific interface I am interested in is IMFSourceReader(Windows Media Foundation). This interface defaults to synchronous mode. So again, if I understand this correctly, I would only need to implement IUnknown in my C++ class if I chose instead to use it in asynchronous mode. Otherwise calls to IMFSourceReader->AddRef() or IMFSourceReader->Release would be sufficient.

If, however, I chose to use IMFSourceReader in asynchronous mode, that requires IMFSourceReaderCallback interface, which in itself inherits IUnknown. In that case I'd have to implement it in the class.

Is my understanding correct? I'm still in the early stages of writing the implementation so I don't really have example code to share. At this point I'm just trying to get the very basic structure of my class nailed down.

  • Have you read [How IUnknown Works](https://msdn.microsoft.com/en-us/library/windows/desktop/dd388915(v=vs.85).aspx) already? – user0042 Dec 01 '17 at 17:49
  • 2
    If it is a COM interface implemented by Windows then clearly you do not need to ask such a vague question. Don't ask for the manual, show what you are trying to do. – Hans Passant Dec 01 '17 at 17:49
  • A tip: It's a good idea implementing a smart pointer like wrapper class to do the `AddRef()` and `Release()` calls properly (see [here](http://loki-lib.sourceforge.net/html/a00045.html) for reference). – user0042 Dec 01 '17 at 17:55
  • @user0042: Yes, smart pointers are useful. However, I'm trying to fully understand the underlying concepts and not just have the smart pointers hide all of the implementation for me – Michael Buckman Dec 01 '17 at 18:27
  • @MichaelBuckman You probably misunderstood. YOU have to implement the smart pointer then (unless using some Microsoft standard stuff dong that). The Loki policy is just a way how you could provide such implementation properly and in a safe way. – user0042 Dec 01 '17 at 18:31

1 Answers1

5

You only implement IUnknown if you are yourself implementing a COM object, merely consuming COM does not require that you implement IUnknown.

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
  • Thank you! This was exactly what I was looking for. I thought that was the case, but my study of COM is still in its infancy, so I wanted to be sure. – Michael Buckman Dec 01 '17 at 18:43
  • 1
    Just to add to that If the COM object you are consuming also exposes events (e.g. via IConnectionPointContainer) and you want to subscribe to those events with an event sink, you will end up being forced to implement a COM object (your event sink) in order to do so. – Joe Dec 01 '17 at 18:58