COM初学者看看,有助于理解

 
//采用多重继承方式,模拟实现COM接口和COM对象的例子
#define IID_IUnknown 1
#define IID_IVideo 2
#define IID_IAudio 3

struct IUnknown 
{
	virtual BOOL QueryInterface(int nIid, void  **ppvObj)=0;
	virtual DWORD Release()=0;
	virtual DWORD AddRef()=0;
};

class IVideo:virtual public IUnknown //接口类定义
{
public:
	virtual BOOL OpenVideoChannel()=0;
	virtual BOOL CloseVideoChannel()=0;
	virtual DWORD GetVideoChannelState()=0;
};

class IAudio:virtual public IUnknown //接口类定义
{
	virtual BOOL OpenAudioChannel()=0;
	virtual BOOL CloseAudioChannel()=0;
	virtual void TransferAudio()=0;
	virtual DWORD GetAudioChannelState()=0;
};

class CVisiblePhone:public IAudio,public IVideo //COM对象类定义
{
public:
		CVisiblePhone();
		~CVisiblePhone();
public:
	//IUnknown memeber function
	HRESULT QueryInterface(int nIid, void  **ppvObj);
	DWORD Release();
	DWORD AddRef();

	//IVudio memeber function
	BOOL OpenVideoChannel();
	BOOL CloseVideoChannel();
	DWORD GetVideoChannelState();

	//IAideo memeber function
	BOOL OpenAudioChannel();
	BOOL CloseAudioChannel();
	void TransferAudio();
	DWORD GetAudioChannelState();
private:
	DWORD m_Ref;
};

//实现接口查询函数QueryInterface()
HRESULT CVisiblePhone::QueryInterface(int iid,void **ppvObj)
{
	if (iid==IID_IUnknown)
	{
		*ppvObj=(IUnknown *)((IVideo *)this);
		((IUnknown *)*ppvObj)->AddRef();
	}
	else if (iid==IID_IVideo)
	{
		*ppvObj=(IVideo *)this;
		((IVideo *)*ppvObj)->AddRef();
	}
	else if (iid==IID_IAudio)
	{
		*ppvObj=(IAudio *)this;
		((IAudio *)*ppvObj)->AddRef();
	}
	else
	{
		*ppvObj=NULL;
		return E_NOINTERFACE;
	}
	return S_OK;
}


 

 

 

//采用内嵌类的方式在一个COM对象中实现多个接口
#define IID_IUnknown 1
#define IID_IVideo 2
#define IID_IAudio 3

class CVisiblePhone//COM对象类定义
{
public:
	CVisiblePhone();
	~CVisiblePhone();
public:
	//IUnknown memeber function
	HRESULT QueryInterface(int nIid, void  **ppvObj);
	DWORD Release();
	DWORD AddRef();
	
	//embeded IAudio
	class CAudioObj:public IAudio
	{
		//IUnknown memeber function
		HRESULT QueryInterface(int nIid, void  **ppvObj);
		DWORD Release();
		DWORD AddRef();
		
		//IAideo memeber function
		BOOL OpenAudioChannel();
		BOOL CloseAudioChannel();
		void TransferAudio();
		DWORD GetAudioChannelState();
		
	}m_AudioObj;
	
	//embeded IVudio
	class CVideoObj:public IVideo
	{
		//IUnknown memeber function
		HRESULT QueryInterface(int nIid, void  **ppvObj);
		DWORD Release();
		DWORD AddRef();
		
		//IVudio memeber function
		BOOL OpenVideoChannel();
		BOOL CloseVideoChannel();
		DWORD GetVideoChannelState();
	}m_VideoObj;
private:
	DWORD m_Ref;
};

//实现接口查询函数QueryInterface()
HRESULT CVisiblePhone::QueryInterface(int iid,void **ppvObj)
{
	if (iid==IID_IUnknown)
	{
		*ppvObj=(IUnknown *)&m_VideoObj;
		AddRef();
	}
	else if (iid==IID_IVideo)
	{
		*ppvObj=&m_VideoObj;
		AddRef();
	}
	else if (iid==IID_IAudio)
	{
		*ppvObj=&m_AudioObj;
		AddRef();
	}
	else
	{
		*ppvObj=NULL;
		return E_NOINTERFACE;
	}
	return S_OK;
}


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值