QueryInterface函数

QueryInterface函数《COM技术内幕》§3——QueryInterface函数§3——IUnknown.CPP代码清单:////IUnkown.cpp//// #include#include voidtrace(constchar*msg){cout //InterfaceinterfaceIX:IUnknown

大家好,又见面了,我是你们的朋友全栈君。

COM技术内幕》 §3 —— QueryInterface函数

§3 —— IUnknown.CPP代码清单:

//

//IUnkown.cpp

//

//

 

#include <iostream.h>

#include <objbase.h>

 

void trace(const char * msg) {cout << msg << endl ;}

 

//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 IID_IX;

extern const IID IID_IY;

extern const IID IID_IZ;

 

//

//Component

//

class CA : public IX, public IY

{

         //IUnknown implementation

         virtual HRESULT __stdcall QueryInterface(const IID& iid, void** ppv);

         virtual ULONG  __stdcall AddRef() {
return 0;}

         virtual ULONG  __stdcall Release() {
return 0;}

 

         //Interface IX implementation

         virtual void       __stdcall Fx() {cout << “Fx” << endl ;}

         //Interface IY implementation

         virtual void       __stdcall Fy() {cout << “Fy” << endl ;}

};

 

HRESULT __stdcall CA::QueryInterface(const IID& iid, void** ppv)

{

         if(iid == IID_IUnknown)

         {

                   trace(“QueryInterface:Return pointer to IUnknown.”);

                   *ppv = static_cast< IX* >(this);

         }

         else if(iid == IID_IX)

         {

                   trace(“QueryInterface:Return pointer to IX.”);

                   *ppv = static_cast< IX* >(this);

         }

         else if(iid == IID_IY)

         {

                   trace(“QueryInterface:Return pointer to IY.”);

                   *ppv = static_cast< IY* >(this);

         }

         else

         {

                   trace(“QueryInterface:Interface not supported.”);

                   *ppv = NULL;

                   return E_NOINTERFACE;

         }

         reinterpret_cast< IUnknown* >(*ppv)->AddRef();

         return S_OK;

}

 

//

//Creation function

//

IUnknown* CreateInstance()

{

         IUnknown* pI = static_cast< IX* >(new CA);

         pI->AddRef();

         return pI;

}

 

//

//IIDs

//

//{32bb8320-b41b-11cf-a6bb-0080c7b2d682}

static const IID IID_IX =

         {0x32bb8320, 0xb41b, 0x11cf,

         {0xa6, 0xbb, 0x0, 0x80, 0xc7, 0xb2, 0xd6, 0x82}};

//{32bb8321-b41b-11cf-a6bb-0080c7b2d682}

static const IID IID_IY =

         {0x32bb8321, 0xb41b, 0x11cf,

         {0xa6, 0xbb, 0x0, 0x80, 0xc7, 0xb2, 0xd6, 0x82}};

//{32bb8322-b41b-11cf-a6bb-0080c7b2d682}

static const IID IID_IZ =

         {0x32bb8322, 0xb41b, 0x11cf,

         {0xa6, 0xbb, 0x0, 0x80, 0xc7, 0xb2, 0xd6, 0x82}};

 

//

//Client

//

int main()

{

         HRESULT hr;

         trace(“Client:Get an IUnknown pointer.”);

         IUnknown* pIUnknown = CreateInstance();

 

         trace(“Client:Get Interface IX.”);

         IX* pIX = NULL;

         hr = pIUnknown->QueryInterface(IID_IX,(void**)&pIX);

         if(SUCCEEDED(hr))

         {

                   trace(“Client:Succeeded getting IX.”);

                   pIX->Fx();       //Use interface IX

         }

        

         trace(“Client:Get Interface IY.”);

         IY* pIY = NULL;

         hr = pIUnknown->QueryInterface(IID_IY,(void**)&pIY);

         if(SUCCEEDED(hr))

         {

                   trace(“Client:Succeeded getting IY.”);

                   pIY->Fy();       //Use interface IY

         }

                  

         trace(“Client:Get Interface IZ.”);

         IZ* pIZ = NULL;

         hr = pIUnknown->QueryInterface(IID_IZ,(void**)&pIZ);

         if(SUCCEEDED(hr))

         {

                   trace(“Client:Succeeded getting IZ.”);

                   pIZ->Fz();

         }

         else

         {

                   trace(“Client:Could 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();

         }

         trace(“Client:Get interface IUnknown from IY.”);

         IUnknown* pIUknownFromIY = NULL;

         hr = pIY->QueryInterface(IID_IUnknown, (void**)&pIUknownFromIY);

         if(SUCCEEDED(hr))

         {

                   cout << “equal?” ;

                   if(pIUknownFromIY == pIUnknown)

                   {

                            cout << “Yes.” << endl;

                   }

                   else

                   {

                            cout << “No.” << endl;

                   }

         }

 

         //Delete the component

         delete pIUnknown;

         return 0;

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/147640.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号