This section describes some rules that must be followed by all implementations of QueryInterface, this allows the customer to gain sufficient knowledge about components and implement some control and other useful processing. Without these rules, it is impossible to compile components, because in this case, the behavior of QueryInterface will be uncertain. Specifically, these rules are:
The same iunknown pointer is always returned by QueryInterface.
If the customer has obtained an interface, it will always obtain this interface.
The customer can obtain the existing interfaces again.
The customer can return to the starting interface.
If you can obtain a specific interface from an interface, you can obtain it from any interface.
// The component has only one iunknown interface.
Bool samecomponents (IX * pix, Iy * piy)
{
Iunknown * PI1 = NULL;
Iunknown * Pi2 = NULL;
// Obtain the iunknown interface from the PIX
PIX-> QueryInterface (iid_iunknown, (void **) & PI1 );
// Obtain the iunknown interface from piy
Piy-> QueryInterface (iid_iunknown, (void **) & Pi2 );
// Are these two pointers equal?
Return PI1 = Pi2;
};
// Obtain an existing interface again
Void getinteragain (IX * pix)
{
IX * pix2 = NULL;
// Query the IX interface from IX
Hresult hR = pix-> QueryInterface (iid_ix, (void **) & pix2 );
Assert (succeeded (HR ));
};