QueryInterface
   HOME

TheInfoList



OR:

In the
computer programming Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
of applications on
Microsoft_Windows Windows is a Product lining, product line of Proprietary software, proprietary graphical user interface, graphical operating systems developed and marketed by Microsoft. It is grouped into families and subfamilies that cater to particular sec ...
through the
Windows_API The Windows API, informally WinAPI, is the foundational application programming interface (API) that allows a computer program to access the features of the Microsoft Windows operating system in which the program is running. Programs can acces ...
, the IUnknown interface is the fundamental interface
Component Object Model Component Object Model (COM) is a binary-interface technology for software components from Microsoft that enables using objects in a language-neutral way between different programming languages, programming contexts, processes and machines ...
(COM). The COM specification mandates that COM objects must implement this interface. Furthermore, every other COM interface must be derived from IUnknown. IUnknown exposes two essential features of all COM objects: object lifetime management through
reference counting In computer science, reference counting is a programming technique of storing the number of references, pointers, or handles to a resource, such as an object, a block of memory, disk space, and others. In garbage collection algorithms, refere ...
, and access to object functionality through other
interfaces Interface or interfacing may refer to: Academic journals * ''Interface'' (journal), by the Electrochemical Society * '' Interface, Journal of Applied Linguistics'', now merged with ''ITL International Journal of Applied Linguistics'' * '' Inter ...
. As well as being a foundational part of Microsoft's COM and DCOM models of objects, there are implementations of the same interface on other platforms -either because the other platforms implement some form of COM-compatibility, or because the design was considered effective. An IUnknown (or IUnknown-derived) interface generally consists of a pointer to a
virtual method table In computer programming, a virtual method table (VMT), virtual function table, virtual call table, dispatch table, vtable, or vftable is a mechanism used in a programming language to support dynamic dispatch (or run-time method binding). ...
that contains a list of pointers to the functions that implement the functions declared in the interface, in the order that they are declared in the interface. The in-process invocation call overhead is therefore identical to virtual method calls in C++..


Methods

The IUnknown interface exposes three methods: QueryInterface, AddRef, and Release:IUnknown definition
a
microsoft.com
; accessed 18-Jan-2008
* QueryInterface allows the caller to retrieve references to the interfaces that the component implements. It is similar to dynamic_cast<> in C++ or casts in
Java Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
and C#. Specifically, it is used to obtain a pointer to another interface, given a
GUID A Universally Unique Identifier (UUID) is a 128-bit label used to uniquely identify objects in computer systems. The term Globally Unique Identifier (GUID) is also used, mostly in Microsoft systems. When generated according to the standard methods ...
that uniquely identifies that interface (commonly known as an interface ID, or IID). If the COM object does not implement that interface, an E_NOINTERFACE error is returned instead. * AddRef is used to increment the reference count when a new client is acquiring the object. It returns the new reference count. * Release is used to decrement the reference count when clients have finished using the object. It returns the new reference count. The object will delete itself during release when the reference-count reaches zero, which means that the caller must never use an interface after calling Release. interface IUnknown ; The IUnknown interface ID is defined as a
GUID A Universally Unique Identifier (UUID) is a 128-bit label used to uniquely identify objects in computer systems. The term Globally Unique Identifier (GUID) is also used, mostly in Microsoft systems. When generated according to the standard methods ...
with the value of . A COM component's interfaces are required to exhibit the reflexive, symmetric, and transitive properties. The reflexive property refers to the ability for the QueryInterface call on a given interface with the interface's ID to return the same instance of the interface. The symmetric property requires that when interface B is retrieved from interface A via QueryInterface, interface A is retrievable from interface B as well. The transitive property requires that if interface B is obtainable from interface A and interface C is obtainable from interface B, then interface C should be retrievable from interface A.


Aggregation, Containment and Delegation

It is possible to combine multiple COM objects in a concept called ''aggregation''. With Aggregated Objects, the return value of an invocation of IUnknown.QueryInterface() can be a different implementation class than that of the object queried. This is a way to build COM objects which implement a large number of interfaces. In such aggregate objects, managing reference-based lifespans and maintiaining the equivalence relationship across the objects is exceedingly complicated. The alternative strategy, ''containment and delegation'' is more common as it is exactly the same code design style of compound objects in object-oriented software development -and implemented by such compound objects.


Implementations on other platforms

*IUnknown serves as the base for
Mac OS X macOS, previously OS X and originally Mac OS X, is a Unix, Unix-based operating system developed and marketed by Apple Inc., Apple since 2001. It is the current operating system for Apple's Mac (computer), Mac computers. With ...
's
Core Foundation Core Foundation (also called CF) is a C application programming interface (API) written by Apple Inc. for its operating systems, and is a mix of low-level routines and wrapper functions. Most Core Foundation routines follow a certain naming c ...
CFPlugIn framework.Plug-ins and Microsoft’s COM
a
apple.com
accessed 28-May-2025
*In Mozilla's
XPCOM Cross Platform Component Object Model (XPCOM) is a cross-platform component model from Mozilla. It is similar to Component Object Model (COM), Common Object Request Broker Architecture (CORBA) and system object model (SOM). It features multiple ...
component model, this interface is also known as nsISupportsnsISupports
accessed 28-May-2025 .


See also

* IOleObject - This is the base interface for
Object Linking and Embedding Object Linking and Embedding (OLE) is a proprietary technology developed by Microsoft that allows embedding and linking to documents and other objects. For developers, it brought OLE Control Extension (OCX), a way to develop and use custom user ...
(OLE) objects. * IDispatch - This interface provides name-based dynamic method dispatch for
OLE Automation In Microsoft Windows applications programming, OLE Automation (later renamed to simply Automation) is an inter-process communication mechanism created by Microsoft. It is based on a subset of Component Object Model (COM) that was intended for use ...
COM objects * IObjectWithSite - This COM interface allows a parent/child pair of objects to connect to each other to implement a
Browser Helper Object A Browser Helper Object (BHO) is a DLL module designed as a plugin for the Microsoft Internet Explorer web browser to provide added functionality. BHOs were introduced in October 1997 with the release of version 4 of Internet Explorer. Most BH ...
(BHO) * IInspectable - The COM-derived
Windows Runtime Windows Runtime (WinRT) is a platform-agnostic component and application architecture first introduced in Windows 8 and Windows Server 2012 in 2012. It is implemented in C++ and officially supports development in C++ (via C++/WinRT, C++/CX ...
(WinRT) uses this IUnknown-derived interface as its base interface


References

{{Reflist


External links


COM in plain C
Object-oriented programming Microsoft application programming interfaces