// Complete com example
# Include <unknwn. h>
# Include <iostream. h>
# Include <objbase. h>
Void trace (const char * MSG)
{
Cout <MSG <Endl;
}
/* Note that virtual functions are used to implement the multi-state interfaces */
/* Use _ stdcall to support DLL calling */
// Interface
Interface IX: iunknown
{
Virtual void _ stdcall FX () = 0;
};
Interface Iy: iunknown
{
Virtual void _ stdcall Fy () = 0;
};
Interface iz: iunknown
{
Virtual void _ stdcall FZ () = 0;
};
// Forward references for guids
Extern const IID idd_ix;
Extern const IID idd_iy;
Extern const IID idd_iz;
// Component
Class CA: Public IX,
Public Iy
{
/* All functions inherited from the interface should be virtual functions. Because the base class has already declared the virtual keyword, this can be omitted */
Public:
// Iunknown
Hresult _ stdcall QueryInterface (const IID & IID, void ** GMM );
Ulong _ stdcall addref (void)
{
Return 0;
}
Ulong _ stdcall release (void)
{
Return 0;
}
// IX
Void _ stdcall FX ()
{
Cout <"FX" <Endl;
}
// Iy
Void _ stdcall Fy ()
{
Cout <"fy" <Endl;
}
};
// Guid
// {6134635d-4669-4c4b-beb3-9223591ae918}
Static const guid iid_ix =
{0x6134635d, 0x4669, 0x4c4b, {0xbe, 0xb3, 0x92, 0x23, 0x59, 0x1a, 0xe9, 0x18 }};
// {D90FD541-D995-4303-9A07-1CC54C6BA576}
Static const guid iid_iy =
{0xd90fd541, 0xd995, 0x4303, {0x9a, 0x7, 0x1c, 0xc5, 0x4c, 0x6b, 0xa5, 0x76 }};
// {F881374D-98D7-48a2-AA72-373A988AF3D9}
Static const guid iid_iz =
{0xf881374d, 0x98d7, 0x48a2, {0xaa, 0x72, 0x37, 0x3a, 0x98, 0x8a, 0xf3, 0xd9 }};
Hresult _ stdcall CA: QueryInterface (const IID & IID, void ** GMM)
{
If (iid_iunknown = IID)
{
Trace ("QueryInterface: Return pointer to iunknow .");
// * GMM = static_cast <iunknown *> (this); iunknown is a virtual class that cannot generate instances
* GMM = static_cast <IX *> (this );
}
Else if (iid_ix = IID)
{
Trace ("QueryInterface: Return pointer to IX .");
* GMM = static_cast <IX *> (this );
}
Else if (iid_iy = IID)
{
Trace ("QueryInterface: Return pointer to Iy .");
* GMM = static_cast <Iy *> (this );
}
Else
{
Trace ("QueryInterface: no interface .");
* GMM = NULL;
Return e_nointerface;
}
// The reinterpret_cast operator modifies the operand type, but only re-interprets the BIT model of the given object without binary conversion.
Reinterpret_cast <iunknown *> (* GMM)-> addref (); // doubt
Return s_ OK;
}
// Creation Function
Iunknown * createinstance ()
{
Iunknown * Pi = static_cast <IX *> (New CA );
Pi-> addref ();
Return PI;
}
Int main ()
{
Hresult hr;
// Method 1
Trace ("client: Get An iunknow Pointer ");
Iunknown * piunknown = createinstance ();
Trace ("client: Get interface IX ");
// Method 2
// Obtain IX
IX * pix = NULL;
HR = piunknown-> QueryInterface (iid_ix, (void **) & pix );
If (succeeded (HR ))
{
Trace ("client: succeeded getting IX ");
PIX-> FX (); // use interface IX
}
Else
{
Trace ("client: cocould not get interface IX ");
}
// Obtain Iy
Iy * piy = NULL;
HR = piunknown-> QueryInterface (iid_iy, (void **) & piy );
If (succeeded (HR ))
{
Trace ("client: succeeded getting Iy ");
Piy-> Fy (); // use interface Iy
}
Else
{
Trace ("client: cocould not get interface Iy ");
}
Trace ("client: ask for an unsupported interface ");
Iz * Piz = NULL;
HR = piunknown-> QueryInterface (iid_iz, (void **) & Piz );
If (succeeded (HR ))
{
Trace ("client: succeeded getting iz ");
PIX-> FX (); // use interface IX
}
Else
{
Trace ("client: cocould not get interface iz ");
}
Trace ("client: Get interface Iy from interface IX ");
Iy * piyfromix = NULL;
HR = pix-> QueryInterface (iid_iy, (void **) & piyfromix );
If (succeeded (HR ))
{
Trace ("client: succeeded getting Iy ");
Piyfromix-> Fy ();
}
Iunknown * piunknownfromiy = NULL;
HR = piy-> QueryInterface (iid_iunknown, (void **) & piunknownfromiy );
If (succeeded (HR ))
{
Cout <"are the iunknown pointers equal? "<Endl;
If (piunknownfromiy = piunknown)
{
Cout <"Yes, piunknownfromiy = piunknown" <Endl;
}
Else
{
Cout <"No, piunknownfromiy! = Piunknown "<Endl;
}
}
Delete piunknown;
}