Skip to content

Latest commit

 

History

History
49 lines (45 loc) · 2.3 KB

File metadata and controls

49 lines (45 loc) · 2.3 KB
title QueryInterface | Microsoft Docs
ms.custom
ms.date 11/04/2016
ms.reviewer
ms.suite
ms.technology
cpp-windows
ms.tgt_pltfrm
ms.topic article
f1_keywords
QueryInterface
dev_langs
C++
helpviewer_keywords
interfaces, pointers
interfaces, availability
QueryInterface method
ms.assetid 62fce95e-aafa-4187-b50b-e6611b74c3b3
caps.latest.revision 10
author mikeblome
ms.author mblome
manager ghogen
translation.priority.ht
cs-cz
de-de
es-es
fr-fr
it-it
ja-jp
ko-kr
pl-pl
pt-br
ru-ru
tr-tr
zh-cn
zh-tw

QueryInterface

Although there are mechanisms by which an object can express the functionality it provides statically (before it is instantiated), the fundamental COM mechanism is to use the IUnknown method called QueryInterface.

Every interface is derived from IUnknown, so every interface has an implementation of QueryInterface. Regardless of implementation, this method queries an object using the IID of the interface to which the caller wants a pointer. If the object supports that interface, QueryInterface retrieves a pointer to the interface, while also calling AddRef. Otherwise, it returns the E_NOINTERFACE error code.

Note that you must obey Reference Counting rules at all times. If you call Release on an interface pointer to decrement the reference count to zero, you should not use that pointer again. Occasionally you may need to obtain a weak reference to an object (that is, you may wish to obtain a pointer to one of its interfaces without incrementing the reference count), but it is not acceptable to do this by calling QueryInterface followed by Release. The pointer obtained in such a manner is invalid and should not be used. This more readily becomes apparent when _ATL_DEBUG_INTERFACES is defined, so defining this macro is a useful way of finding reference counting bugs.

See Also

Introduction to COM
QueryInterface: Navigating in an Object