首先,COM(组件对象模型)技术是一种规范,它解释了如何建立可动态互变组件的规范,这个规范提供了为了保证能够互操作,客户和组件应当遵循的一些二进制和网络标准。(摘自百度百科)
说白了,COM的精髓在于:组件,它让大规模软件开发更容易模块化了。模块化的好处,更容易开发,更容易维护。
对于COM技术,要整体学习的话是一段很长的路,本身包含的东西很多,又难理解。
有关COM的一些讨论见这个贴子:COM技术讨论 里面有很多强大的东西。
这里给出一个COM技术的微型实现 AddRef,Release,QueryInterface这三个组成COM的东西
程序来自《windows游戏编程大师技巧〉
// DEMO5_1.CPP - A ultra minimal working COM example
// NOTE: not fully COM compliant
// INCLUDES //
#include <stdio.h>
#include <malloc.h>
#include <iostream>
#include <objbase.h> // note: you must include this header it contains important constants
// you must use in COM programs
using namespace std;
// GUIDS /
// these were all generated with GUIDGEN.EXE
// {B9B8ACE1-CE14-11d0-AE58-444553540000}
const IID IID_IX =
{ 0xb9b8ace1, 0xce14, 0x11d0, { 0xae, 0x58, 0x44, 0x45, 0x53, 0x54, 0x0, 0x0 } };
// {B9B8ACE2-CE14-11d0-AE58-444553540000}
const IID IID_IY =
{ 0xb9b8ace2, 0xce14, 0x11d0, { 0xae, 0x58, 0x44, 0x45, 0x53, 0x54, 0x0, 0x0 } };
// {B9B8ACE3-CE14-11d0-AE58-444553540000}
const IID IID_IZ =
{ 0xb9b8ace3, 0xce14, 0x11d0, { 0xae, 0x58, 0x44, 0x45, 0x53, 0x54, 0x0, 0x0 } };
// INTERFACES
// 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;
};
// CLASSES AND COMPONENTS ///
// define the COM object
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;
};
// CLASS METHODS
HRESULT __stdcall CCOM_OBJECT::QueryInterface(const IID &iid, void **iface)
{
// this function basically casts the this pointer or the Iunknown
// pointer into the interface requested, notice the comparison with
// the GUIDs generated and defined in the begining of the program
// 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
///
IUnknown *CoCreateInstance(void)
{
// this is a very basic implementation of CoCreateInstance()
// it creates an instance of the COM object, in this case
// I decided to start with a pointer to IX -- IY would have
// done just as well
IUnknown *comm_obj = (IX *)new(CCOM_OBJECT);
cout << "Creating Comm object" << endl;
// update reference count
comm_obj->AddRef();
return(comm_obj);
} // end CoCreateInstance
///
void main(void)
{
// 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();
} // end main
有注释比较容易懂。
COM技术现在并不是特别火热,但是MS从操作系统到各种软件无不是基于COM的,对于windows平台的软件开发,学习COM是比要的路
还有这句话很经典:一个技术可能过时,但一种理念不大会过时,只不过换了马甲罢了
思想才是最重要的。
文章最重要的部分是那个贴子的链接。