COM指南 (Step by Step COM Tutorial)-下(1)

翻译|其它|编辑:郝浩|2004-11-02 15:28:00.000|阅读 1733 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>


第七步 实现IClassFactory的方法


实现类CAddFactory的方法。创建一个新文件(AddObjFactory.cpp)。提供类IUnknown和IClassFactory的方法实现。AddRef,Release和QueryInterface方法实现和前面类CAddObj中这三个函数实现基本一样。在方法CreateInstance中,类CaddObj被实例化并且传回其接口指针。LockServer方法没有给出细节的实现。


HRESULT __stdcall CAddFactory::CreateInstance(IUnknown* pUnknownOuter,

const IID& iid,

void** ppv) 

{

//

//This method lets the client manufacture components en masse

//The class factory provides a mechanism to control the way

//the component is created. Within the class factory the 

//author of the component may decide to selectivey enable

//or disable creation as per license agreements 

//

//

// Cannot aggregate.

if (pUnknownOuter != NULL)

{

return CLASS_E_NOAGGREGATION ;



//

// Create an instance of the component.

//

CAddObj* pObject = new CAddObj ;

if (pObject == NULL)

{

return E_OUTOFMEMORY ;

}

//

// Get the requested interface.

//

return pObject->QueryInterface(iid, ppv) ;

}


HRESULT __stdcall CAddFactory::LockServer(BOOL bLock) 

{

return E_NOTIMPL;

}

/////////////////////////////////////////////////////////////////


第八步 实现DllGetClassObject的方法


一个进程内COM对象只是一个简单的WIN32DLL,他遵循既定的协议。每一个COM DLL必须有一个通过名字DllGetClassObject的出口函数。客户端将调用这个函数以得到类厂的接口(IUnknown or IClassFactory),之后就是调用CreateInstance方法。创建一个新文件(Exports.cpp),在其中实现DllGetClassObject。(代码如下)


STDAPI DllGetClassObject(const CLSID& clsid,

const IID& iid,

void** ppv)
{

//

//Check if the requested COM object is implemented in this DLL

//There can be more than 1 COM object implemented in a DLL

//

if (clsid == CLSID_AddObject)

{

//

//iid specifies the requested interface for the factory object

//The client can request for IUnknown, IClassFactory,

//IClassFactory2

//

CAddFactory *pAddFact = new CAddFactory;

if (pAddFact == NULL)

return E_OUTOFMEMORY;

else

{

return pAddFact->QueryInterface(iid , ppv);

}

}

//

//if control reaches here then that implies that the object

//specified by the user is not implemented in this DLL

//


return CLASS_E_CLASSNOTAVAILABLE;

}

/////////////////////////////////////////////////////////////////


第九步 实现DllCanUnloadNow


客户需要知道什么时候COM DLL可以被从内存中卸载。更进一步,一个进程内COM对象显示的通过调用API函数LoadLibrary装入内存中的客户程序的进程空间中。这个显示装载的DLL也可以使用FreeLibrary卸载。COM客户必须知道什么时候DLL可以被安全的卸载。一个客户必须确定没有任何来自于特别是DLL中的COM对象的实例仍在生命期中。要使得这个可以被简单的计算,在一个COM DLL中,我们在类CAddObj和CAddFactory的构造函数中自加一个全局变量(g_nComObjsInUse)。类似的,在析构函数中,自减这个全局变量。

我们输出另一个COM规定的函数:DllCanUnloadedNow,实现如下:


STDAPI DllCanUnloadNow()

{

//

//A DLL is no longer in use when it is not managing any existing objects

// (the reference count on all of its objects is 0). 

//We will examine the value of g_nComObjsInUse 

//


if (g_nComObjsInUse == 0)

{

return S_OK;

}

else

{

return S_FALSE; }

}


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP