首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

COM 嵌套种实现COM

2012-12-28 
COM 嵌套类实现COM#include ia.h#include ib.h#include atlbase.hclass CO:public IUnknown{public:

COM 嵌套类实现COM

#include "ia.h"#include "ib.h"#include <atlbase.h>class CO:public IUnknown{public:CO();HRESULT _stdcall QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject);ULONG _stdcall AddRef();ULONG _stdcall Release();class CA:public IA{public:CO* m_parent;HRESULT _stdcall QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject);ULONG _stdcall AddRef();ULONG _stdcall Release();int _stdcall Add(int a,int b);} ca;class CB:public IB{public:CO* m_parent;HRESULT _stdcall QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject);ULONG _stdcall AddRef();ULONG _stdcall Release();HRESULT _stdcall Pass(int a,int b,int* c);} cb;private:long m_ref;};CO::CO(){m_ref=0;ca.m_parent=this;cb.m_parent=this;}HRESULT _stdcall CO::QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject){if(riid==IID_IA){*ppvObject=(IA*)(&ca);}else if(riid==IID_IB){*ppvObject=(IB*)(&cb);}else if(riid==IID_IUnknown){*ppvObject=(IUnknown*)(this);}else{*ppvObject=NULL;return E_NOINTERFACE;}reinterpret_cast<IUnknown*>(*ppvObject)->AddRef();return S_OK;}ULONG _stdcall CO::AddRef(){++m_ref;return m_ref;}ULONG _stdcall CO::Release(){--m_ref;if(m_ref==0){delete this;return 0;}return m_ref;}HRESULT _stdcall CO::CA::QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject){return m_parent->QueryInterface(riid,ppvObject);}ULONG _stdcall CO::CA::AddRef(){return m_parent->AddRef();}ULONG _stdcall CO::CA::Release(){return m_parent->Release();}int _stdcall CO::CA::Add(int a,int b){int x=a+b;return x;}HRESULT _stdcall CO::CB::QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject){return m_parent->QueryInterface(riid,ppvObject);}ULONG _stdcall CO::CB::AddRef(){return m_parent->AddRef();}ULONG _stdcall CO::CB::Release(){return m_parent->Release();}HRESULT _stdcall CO::CB::Pass(int a,int b,int* c){int x=a-b;c=&x;return S_OK;}IUnknown* CreateInterface(){IUnknown* pc=static_cast<IUnknown*>(new CO);return pc;}

?

热点排行