To ensure the standardization of Components in basic functions, COM defines a basic interface, iunknown. At the same time, it is stipulated that the iuknown or its derived class must be used to derive many other interfaces of the component. This ensures that all interfaces of the component have a unified basic function.
The following describes the basic iuknown interfaces in detail:
Class
{
Public:
Virtual hresult _ stdcall QueryInterface (const IID & IID, void ** GMM) = 0;
Virtual ulong _ stdcall addref () = 0;
Virtual ulong _ stdcall release () = 0;
};
1. There are actually three virtual functions;
2. hresult: Return Value of the function. If this function is returned after execution, it will contain actual data. If it is returned immediately, it will contain status information-whether the sending is successful or not, which does not explain how the function is executed.
3. Virtual hresult _ stdcall QueryInterface (const IID & IID, void ** GMM) = 0; the first parameter IID indicates the IID of the interface to be queried. If the query is successful, the second parameter is the interface pointer obtained after the first parameter is queried. The Pointer Points to the virtual function table of this interface, in this virtual function table, the pointer of the function in this interface is stored. Obviously, only after obtaining the interface pointer can the customer obtain the service that the interface can provide through the interface pointer.
4. ulong: The description in msdn is as follows:
Ulong |
UnsignedLong. The range is 0 through 4294967295 decimal. This type is declared in windef. h as follows: Typedef unsigned long ulong; |
That is, the unsigned long integer.
5. The addref and release functions are used to control the lifetime of component objects. Before using a component, the customer program needs to apply to create a component object. If the customer no longer needs this component object, the customer should release it in time. When a customer program references a component object, the customer calls the function addref to add the counter value to 1. When the customer program references the component object, the customer calls the release function to reduce the counter value by 1. In this way, the component object can always know how many customer programs are referencing itself.