Mohd Ahmed
Mohd Ahmed

Reputation: 1482

What is QueryInterface and IUnknown interface?

I want to know the actual usage of QueryInterface and IUnknown interface.

Upvotes: 2

Views: 1548

Answers (2)

Systematix Infotech
Systematix Infotech

Reputation: 2365

QueryInterface checks whether the object that implements this interface supports the interface specified by IID. If so, QueryInterface

  1. Increments the reference count.
  2. Sets the Obj parameter so that it points to an instance of the specified interface.
  3. Returns 0 to indicate success.

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

sharptooth
sharptooth

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

Related Questions