在学习DX一堆接口之前的知识补充,小的不能再小了。
一个COM对象实际上就是实现大量界面的C++类或者是一套C++类, 一个界面就是一套函数。
如图:一个COM对象,有IGRAPHICS、ISOUND和IINPUT三个界面。
每一个界面都有大量的函数可以调用,COM技术要求用户创建的所有的界面必须从一个指定的基本类界面IUnknown中导出,以具备功能:引用计数和查询接口。
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();
附源码: