Each interface defined by COM must be inherited from iunknown, mainly because the iunknown interface provides two very important features: Lifetime Control and interface query. The client program can only communicate with the COM object through interfaces. Although the client program can control the existence of the object regardless of the Implementation Details within the object. If the customer wants to continue the operation on the object, it must ensure that the object can always exist in the memory. If the customer has completed the operation on the object, the object will not be needed in the future, it must release the object in time to improve resource utilization. Iunknown introduces the reference counting method to effectively control the object lifecycle.
On the other hand, if a COM Object implements multiple interfaces, at the initial time, the client program is unlikely to obtain all the interface pointers of the object, and it will only have one interface pointer. If the client program needs other pointers, how does it obtain another interface pointer through this interface pointer? Iunknown uses the interface query ("QueryInterface") method to complete the bar between interfaces.
The following is the definition of iunknown:
Class iunknown {public: Virtual hresult _ stdcall QueryInterface (const IID & IID, void ** bp) = 0; virtual ulong _ stdcall addref () = 0; vittual ulong _ stdcall release () = 0 ;};
Among them, QueryInterface is used to query other interfaces of the COM object, and addref and release are used to operate the reference count.