When To Implement IUnknown in a class

1k views Asked by At

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.

1

There are 1 answers

2
SoronelHaetir On BEST ANSWER

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