In the computer programming of applications on Microsoft Windows through the Windows API, the IUnknown interface is the fundamental interface of Component Object Model (COM).[1][2] The COM specification[3] mandates that COM objects must implement this interface. Furthermore, every other COM interface must be derived from IUnknown. IUnknown resides in the global namespace. IUnknown exposes two essential features of all COM objects: object lifetime management through reference counting, and access to object functionality through other interfaces. 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 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++.[4]

Methods

edit

The IUnknown interface exposes three methods: QueryInterface(), AddRef(), and Release():[5]

  • QueryInterface() allows the caller to retrieve references to the interfaces that the component implements. It is similar to dynamic_cast<T> in C++ or casts in Java and C#. Specifically, it is used to obtain a pointer to another interface, given a GUID 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 {
    virtual HRESULT QueryInterface(REFIID riid, void** ppvObject) = 0;
    virtual ULONG AddRef() = 0;
    virtual ULONG Release() = 0;
};

The IUnknown interface ID is defined as a GUID with the value of {00000000-0000-0000-C000-000000000046}.

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

edit

It is possible to combine multiple COM objects in a concept called aggregation.[6] 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. [7]

Implementations on other platforms

edit

See also

edit

References

edit
  1. ^ Box, Don (1998). Essential COM. Addison-Wesley Professional. p. 50. ISBN 978-0-201-63446-4.
  2. ^ Troelsen, Andrew W. (2000-06-30). Developer's Workshop to COM and ATL 3.0. Jones & Bartlett Learning. p. 339. ISBN 978-1-4496-3147-5.
  3. ^ The Component Object Model Specification, archived from the original on 2004-02-15
  4. ^ "The Component Object Model". microsoft.com. Microsoft. 30 May 2018. Retrieved 12 February 2019.
  5. ^ IUnknown definition at microsoft.com Archived 2013-07-11 at the Wayback Machine; accessed 18-Jan-2008
  6. ^ "Aggregation". microsoft.com. Microsoft.
  7. ^ "Containment and Delegation". microsoft.com. Microsoft.
  8. ^ Plug-ins and Microsoft’s COM at apple.com; accessed 28-May-2025
  9. ^ nsISupports; accessed 28-May-2025
edit

📚 Artikel Terkait di Wikipedia

Component Object Model

type HRESULT – method result codes such as S_OK, E_FAIL, E_OUTOFMEMORY IUnknown – base type for object references; abstract virtual functions to support

IDispatch

IDispatch is the interface that exposes the OLE Automation protocol. Extending IUnknown, it is one of the standard interfaces that can be exposed by COM objects

ActiveX

Object (BHO) Google Native Client – an alternative development from Google IUnknown interface JavaBeans Netscape Plugin Application Programming Interface (NPAPI)

Object Linking and Embedding

library with the .ocx extension. In 1996 all interfaces for controls (except IUnknown) were made optional to keep the file size of controls down, so they would

Steve Yegge

Retrieved 2022-01-04. "Steve Yegge on Interviewing | John Lam on Software". Iunknown.com. 2008-04-25. Archived from the original on 2010-10-14. Retrieved 2010-12-07

Variant type (COM)

VT_VARIANT pvarVal Variant VARIANT 13 0x0d VT_UNKNOWN punkVal Nothing4 00000000 IUnknown * 14 0x0e VT_DECIMAL decVal DECIMAL 16 0x10 VT_I1 cVal Byte CHAR 17 0x11

User-Mode Driver Framework

it only uses COM as a programming pattern, for example exploiting COM's IUnknown interface. At startup, UMDF calls DllGetClassObject to get a pointer to

Object composition

responsible for assuring that methods of those interfaces inherited from IUnknown actually invoke the corresponding methods of the owner. This is to guarantee