Reputation: 1482
I want to know the actual usage of QueryInterface and IUnknown interface.
Upvotes: 2
Views: 1548
Reputation: 2365
QueryInterface checks whether the object that implements this interface supports the interface specified by IID. If so, QueryInterface
If the object does not support the interface, QueryInterface returns a nonzero error code, such as E_NoInterface.
IUnknown is the fundamental interface in COM-Lite, as in COM. All other COM-Lite interfaces must derive from it.
Used for Object lifetime management (when to free an object) and object self-description (how to determine object capabilities at runtime)
Upvotes: 1
Reputation: 170489
QueryInterface()
is a COM version of C# as
keyword - you call QueryInterface()
and supply an interface id and you either get success code (S_OK
) and a valid pointer to that interface of the object or error code E_NOINTERFACE
and null pointer which means the object doesn't implement such interface. IUnknown
is the interface containing QueryInterface()
and also the reference counting methods (AddRef()
and Release()
) which are used for COM object lifetime management. Every COM object must implement at least IUnknown
, otherwise you simply can't Release()
objects when you no longer need them and calling Release()
is the only way to tell you no longer need the object.
Upvotes: 2