參考:COM技術內幕
DllGetClassObject
https://msdn.microsoft.com/en-us/library/windows/desktop/ms680760.aspx
If a call to the CoGetClassObject function finds the class object that is to be loaded in a DLL,
CoGetClassObject uses the DLL's exported DllGetClassObject function.
IClassFactory
https://msdn.microsoft.com/en-us/library/windows/desktop/ms694364.aspx
After calling the CoGetClassObject function to get an IClassFactory interface pointer to the class object,
call the CreateInstance method of this interface to create an object.
CoCreateInstance
https://msdn.microsoft.com/en-us/library/windows/desktop/ms686615.aspx
CoCreateInstance等價
{
CoGetClassObject(rclsid, dwClsContext, NULL, IID_IClassFactory, &pCF);
hresult = pCF->CreateInstance(pUnkOuter, riid, ppvObj
pCF->Release();
}
CoGetClassObject: 將在注冊表中查找指定的組件。找到之後,它將裝載實現此組件的DLL。
裝載成功之後,它將調用在DLL服務器中實現的DllGetClassObject。
DllGetClassObject: 此函數的作用是創建響應的類場。
查詢IClassFactory接口,並將其返回給CoCreateInstance。
CoCreateInstance將使用此接口來調用IClassFactory::CreateInstance函數
IClassFactory::CreateInstance調用了new操作符來創建指定的組件,並查詢組件的IX接口。
//客戶調用COM流程:
CoCreateInstace(rclsid,NULL,dwClsContext,IID_IX,(void**)&pIX); //IX* pIX
|--> CoGetClassObject(rclsid, dwClsContext, NULL, IID_IClassFactory, &pCF) //IClassFactory* pCF
|--> DllGetClassObject(rclsid,IID_IClassFactory,&pCF)
|--> CFactory* pFactory = new CFactory();
|--> pFactory->QueryInterface(IID_IClassFactory,&pCF); //返回類場指針IClassFactory* pCF
|--> pCF->CreateInstance(pUnkOuter, IID_IX, &pIX); //IX* pIX 組件接口指針pIX
pIX->Fx();
注意:.def模塊定義文件
LIBRARY "ComComponent"
EXPORTS
DllCanUnloadNow PRIVATE
DllGetClassObject PRIVATE
DllRegisterServer PRIVATE
DllUnregisterServer PRIVATE