一个COM例子


来自:http://hi.baidu.com/starloooooove/item/df8a17ad8f7893aa29ce9dc5


学习DX一堆接口之前的知识补充,小的不能再小了。

一个COM对象实际上就是实现大量界面的C++类或者是一套C++类, 一个界面就是一套函数。

如图:一个COM对象,有IGRAPHICS、ISOUND和IINPUT三个界面。

<span style="font-size:14px;">每一个界面都有大量的函数可以调用,COM技术要求用户创建的所有的界面必须从一个指定的基本类界面IUnknown中导出,以具备功能:引用计数和查询接口。
</span>
<span style="font-size:14px;">interface   IUnknown  
{  
          virtual   HRESULT   _stdcall   QueryInterface(const IID   & iid,   (void**) ip)=0;  
          virtual   ULONG   _stdcall   AddRef(void)=0;  
          virtual   ULONG   _stdcall   Release(void)=0;  
}   


IUnknown是所有接口的基础,他负责两项工作:   
          IUnknown::QueryInterface负责得到该组件的其他接口的指针。
         IUnknown::AddRef/Release负责管理该组件的生存期,但有人使用该组件时,保证该组件不会被意外删除;再没人使用该组件时,保证该组件被自动删除。

例子:

1、定义IX、IY两个接口

// define the IX interface
interface IX: IUnknown
{

virtual void __stdcall fx(void)=0;

};

// define the IY interface
interface IY: IUnknown
{

virtual void __stdcall fy(void)=0;

};

2、定义COM对象

class CCOM_OBJECT : public IX,
                    public IY
{
public:

CCOM_OBJECT() : ref_count(0) {}
~CCOM_OBJECT() {}

private:

virtual HRESULT __stdcall QueryInterface(const IID &iid, void **iface);
virtual ULONG __stdcall AddRef();
virtual ULONG __stdcall Release();

virtual void __stdcall fx(void) {cout << "Function fx has been called." << endl; }
virtual void __stdcall fy(void) {cout << "Function fy has been called." << endl; }

int ref_count;

};

3、具体实现QueryInterface、AddRef和Release

HRESULT __stdcall CCOM_OBJECT::QueryInterface(const IID &iid, void **iface)
{// requesting the IUnknown base interface
if (iid==IID_IUnknown)
{
cout << "Requesting IUnknown interface" << endl;
*iface = (IX*)this;

} // end if

// maybe IX?
if (iid==IID_IX)
{
cout << "Requesting IX interface" << endl;
*iface = (IX*)this;

} // end if
else // maybe IY
if (iid==IID_IY)
{
cout << "Requesting IY interface" << endl;
*iface = (IY*)this;

} // end if
else
{ // cant find it!
cout << "Requesting unknown interaface!" << endl;
*iface = NULL;
return(E_NOINTERFACE);
} // end else

// if everything went well cast pointer to IUnknown and call addref()
((IUnknown *)(*iface))->AddRef();

return(S_OK);

} // end QueryInterface



ULONG __stdcall CCOM_OBJECT::AddRef()
{
// increments reference count
cout << "Adding a reference" << endl;
return(++ref_count);

} // end AddRef

///

ULONG __stdcall CCOM_OBJECT::Release()
{
// decrements reference count
cout << "Deleting a reference" << endl;
if (--ref_count==0)
{
delete this;
return(0);
} // end if
else
return(ref_count);

} // end Release

4、创建实例

IUnknown *CoCreateInstance(void)
{IUnknown *comm_obj = (IX *)new(CCOM_OBJECT);

cout << "Creating Comm object" << endl;
comm_obj->AddRef();

return(comm_obj);

}

5、Main调用

// create the main COM object
IUnknown *punknown = CoCreateInstance() ;

// create two NULL pointers the the IX and IY interfaces
IX *pix=NULL;
IY *piy=NULL;

// from the original COM object query for interface IX
punknown->QueryInterface(IID_IX, (void **)&pix);

// try some of the methods of IX
pix->fx();

// release the interface
pix->Release();


// now query for the IY interface
punknown->QueryInterface(IID_IY, (void **)&piy);

// try some of the methods
piy->fy();

// release the interface
piy->Release();

// release the COM object itself
punknown->Release();</span>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值