使用嵌套类来实现接口
例子:
class CDictionaryObj : public CCmdTarget
{
DECLARE_DYNCREATE(CDictionaryObj)
CDictionaryObj();
public:
class XDictionary : public IDictionary
{
public:
virtual ULONG __stdcall AddRef();
virtual ULONG __stdcall Release();
virtual HRESULT __stdcall QueryInterface(REFIID iid, LPVOID* ppvObj);
virtual BOOL __stdcall Initialize();
......
} m_xDictionary;
friend class XDictionary;
protected:
virtual ~CDictionaryObj();
DECLARE_MESSAGE_MAP()
DECLARE_INTERFACE_MAP()
DECLARE_OLECREATE(CDictionaryObj)
};
效果和class CDictionaryObj : public IDictionary 一样。因为对外部而言,使用QueryInterface来获取接口指针,获取IDictionary指针,相当于返回m_xDictionary的地址。然后就可以通过此指针来操作IDictionary提供的接口了。
上面的例子能够看出CDictionaryObj 并没有实现QueryInterface, AddRef, Release函数。这是因为他继承于CCmdTarget,CCmdTarget已经实现了。而且它的实现还可以调用到内部类的三个接口函数。
CCmdTarget提供的消息机制中,使用了静态映射表(消息ID和处理函数在WinMain执行之前,就已经建立好对应关系了)。同样,COM机制中使用接口映射表。
宏 | 代码 |
DECLARE_INTERFACE_MAP() | private: static const AFX_INTERFACEMAP_ENTRY _interfaceEntries[]; protected: static AFX_DATA const AFX_INTERFACEMAP interfaceMap; static const AFX_INTERFACEMAP* PASCAL _GetBaseInterfaceMap(); virtual const AFX_INTERFACEMAP* GetInterfaceMap() const; |
struct AFX_INTERFACEMAP_ENTRY | struct AFX_INTERFACEMAP_ENTRY { const void* piid; // the interface id (IID) (NULL for aggregate) size_t nOffset; // offset of the interface vtable from m_unknown }; |
struct AFX_INTERFACEMAP | struct AFX_INTERFACEMAP { // NULL is root class const AFX_INTERFACEMAP* (PASCAL* pfnGetBaseMap)(); // map for this class const AFX_INTERFACEMAP_ENTRY* pEntry; }; |
BEGIN_INTERFACE_MAP(theClass, theBase) | const AFX_INTERFACEMAP* PASCAL theClass::_GetBaseInterfaceMap() { return &theBase::interfaceMap; } const AFX_INTERFACEMAP* theClass::GetInterfaceMap() const { return &theClass::interfaceMap; } AFX_COMDAT const AFX_DATADEF AFX_INTERFACEMAP theClass::interfaceMap = { &theClass::_GetBaseInterfaceMap, &theClass::_interfaceEntries[0], }; AFX_COMDAT const AFX_DATADEF AFX_INTERFACEMAP_ENTRY theClass::_interfaceEntries[] = { |
INTERFACE_PART(theClass, iid, localClass) | { &iid, offsetof(theClass, m_x##localClass) }, |
END_INTERFACE_MAP() | { NULL, (size_t)-1 } }; |
DECLARE_OLECREATE(class_name) | public: static AFX_DATA COleObjectFactory factory; static AFX_DATA const GUID guid; |
IMPLEMENT_OLECREATE(class_name, external_name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) | AFX_DATADEF COleObjectFactory class_name::factory(class_name::guid, RUNTIME_CLASS(class_name), FALSE, _T(external_name)); AFX_COMDAT const AFX_DATADEF GUID class_name::guid = { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }; |
说明: _interfaceEntries[] 保存了AFX_INTERFACEMAP_ENTRY 数组,这个数组也就是接口ID和接口vtable与父类指针的偏移量的映射表。interfaceMap 其中一个指针指向父类,另外一个指向_interfaceEntries[]。_GetBaseInterfaceMap函数返回interfaceMap的地址。这张映射表的实现是通过BEGIN_INTERFACE_MAP(theClass, theBase), INTERFACE_PART(theClass, iid, localClass), END_INTERFACE_MAP() 三个宏实现的。
CCmdTarget 实现了俩个版本的IUnkown,非委托、委托IUnkown。
从CCmdTarget继承下来的,为了实现聚合,调用的应该是委托IUnkown接口(External)。
目前为止,只有接口的实现,没有出现类厂,类厂已经被MFC自动生成了。
在DLL 的AppWizard向导中选中:Automation,向导会生成如下代码:
STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return AfxDllGetClassObject(rclsid, riid, ppv);
}
STDAPI DllCanUnloadNow(void)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return AfxDllCanUnloadNow();
}
// by exporting DllRegisterServer, you can use regsvr.exe
STDAPI DllRegisterServer(void)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
COleObjectFactory::UpdateRegistryAll();
return S_OK;
}
我们知道,类厂应该是DllGetClassObject里面创建的,这个函数调用了AfxDllGetClassObject。秘密就在其后。
SCODE AFXAPI AfxDllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
*ppv = NULL;
DWORD lData1 = rclsid.Data1;
// search factories defined in the application
AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
AfxLockGlobals(CRIT_OBJECTFACTORYLIST);
for (COleObjectFactory* pFactory = pModuleState->m_factoryList;
pFactory != NULL; pFactory = pFactory->m_pNextFactory)
{
if (pFactory->m_bRegistered != 0 &&
lData1 == pFactory->m_clsid.Data1 &&
((DWORD*)&rclsid)[1] == ((DWORD*)&pFactory->m_clsid)[1] &&
((DWORD*)&rclsid)[2] == ((DWORD*)&pFactory->m_clsid)[2] &&
((DWORD*)&rclsid)[3] == ((DWORD*)&pFactory->m_clsid)[3])
{
// found suitable class factory -- query for correct interface
SCODE sc = pFactory->InternalQueryInterface(&riid, ppv);
AfxUnlockGlobals(CRIT_OBJECTFACTORYLIST);
return sc;
}
}
AfxUnlockGlobals(CRIT_OBJECTFACTORYLIST);
#ifdef _AFXDLL
AfxLockGlobals(CRIT_DYNLINKLIST);
// search factories defined in extension DLLs
for (CDynLinkLibrary* pDLL = pModuleState->m_libraryList; pDLL != NULL;
pDLL = pDLL->m_pNextDLL)
{
for (pFactory = pDLL->m_factoryList;
pFactory != NULL; pFactory = pFactory->m_pNextFactory)
{
if (pFactory->m_bRegistered != 0 &&
lData1 == pFactory->m_clsid.Data1 &&
((DWORD*)&rclsid)[1] == ((DWORD*)&pFactory->m_clsid)[1] &&
((DWORD*)&rclsid)[2] == ((DWORD*)&pFactory->m_clsid)[2] &&
((DWORD*)&rclsid)[3] == ((DWORD*)&pFactory->m_clsid)[3])
{
// found suitable class factory -- query for correct interface
SCODE sc = pFactory->InternalQueryInterface(&riid, ppv);
AfxUnlockGlobals(CRIT_DYNLINKLIST);
return sc;
}
}
}
AfxUnlockGlobals(CRIT_DYNLINKLIST);
#endif
// factory not registered -- return error
return CLASS_E_CLASSNOTAVAILABLE;
}
MFC使用的就是COleObjectFactory 这个通用的类厂(Ole是历史原因导致的,其实CComObjectFactory更加合适)。
COleObjectFactory 从 CCmdTarget派生,实现了IClassFactory2接口。此类厂使用MFC动态创建机制(DECLARE_DYNCREATE),创建对象。那么如何将此类厂和某个类关联?使用DECLARE_OLECREATE,IMPLEMENT_OLECREATE。他们将类的CLSID和类的RUNTIME信息传递给CComObjectFactory类厂。这样类厂就能使用CLSID和动态信息进行创建对象了。那么类厂是如何被Afx找到的呢。从代码中看出,类厂指针的是从模块信息里面的类厂链来获取的。而类厂构造函数中调用了pModuleState->m_factoryList.AddHead(this); 将自己的信息加入模块信息里面,模块信息是全局的变量。这样就跑通了。流程如下:
AfxDllGetClassObject ->
Module State (factoryList), look up Factory’s clsid. ->
Get IClassFactory2 reference ->
CreateInstance ->
Dynamic create object using Dynamic information. ->
Finish.
What should we do?
VC++ 6.0 提供了MFC和ATL两套库,用来开发COM。
这里使用MFC:
1. 使用AppWizard生产MFC exe/dll, 选择Automation
2. 定义接口
3. 继承CCmdTarget,实现接口,(嵌入式)BEGIN_INTERFACE_PART, INIT_INTERFACE_PART, STDMETHOD, STDMETHOD_, END_INTERFACE_PART.
4. 定义各个接口函数体:Class::XlocalClass::AddRef/Release/QueryInterface(), 实现接口的各个接口
5. 定义类的静态信息,DECLARE_INTERFACE_MAP,实现使用:BEGIN_INTERFACE_MAP, INTERFACE_PART, END_INTERFACE_MAP
6. 使用MFC类厂,类里面使用:DECLARE_OLECREATE, 实现使用 IMPLEMENT_OLECREATE