代码改变世界

客户调用COM流程

2016-06-15 08:59  唯爱2011  阅读(439)  评论(0编辑  收藏  举报

参考: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