Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1501030
  • 博文数量: 218
  • 博客积分: 6394
  • 博客等级: 准将
  • 技术积分: 2563
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-08 15:33
个人简介

持之以恒

文章分类

全部博文(218)

文章存档

2013年(8)

2012年(2)

2011年(21)

2010年(55)

2009年(116)

2008年(16)

分类:

2010-04-05 21:15:42

COM组件的调用过程

    HRESULT hr = E_FAIL;
    //COM 组件初始化
    CoInitializeEx(NULL,COINIT_MULTITHREADED);
    //显示调用LoadLibrary,为了加载注册DLL,一般这个工作是由SETUP.exe做的。
    typedef HRESULT (_stdcall * PFREG)();
    HINSTANCE hInst = LoadLibrary("Test.dll");
    //调用DLL中的DllRegisterServer(),将DLL注册进相应的注册表文件;
    PFREG pRegister = (PFREG)::GetProcAddress(hInst,("DllRegisterServer"));
    hr=(*pRegister)();
    //获得相应的COM对象工厂的接口(IClassFactory)
    //This function(CoGetClassObject) provides a pointer to an interface on a class
    //object associated with a specified class identifier. Call CoGetClassObject directly
    //when you want to create multiple objects through a class object whose class
    //identifier is already in the system registry. Most class objects implement the
    //IClassFactory interface. You would then call IClassFactory::CreateInstance to
    //create an uninitialized object.
    IClassFactory * pIFactory = NULL;
    hr = CoGetClassObject(CLSID_TEST,
        CLSCTX_INPROC_SERVER,
        NULL,
        IID_IClassFactory,
        (void**)&pIFactory);
    //Find in 注册表==>LoadLibrary(加载DLL资源)==>(调用DLL的函数)DllGetClassObject,注意它不会调用函数DllRegisterServer
    //进行DLL的注册
    //注意通常的情况是COM对象都会继承IUnknown,也可能会继承其他的接口(ILook...)
    //通过工厂获得COM对象通用接口(IUnknown)
    IUnknown * pIUnknown=NULL;
    hr = pIFactory->CreateInstance(NULL,IID_IUnknown,(void**)&pIUnknown);
    hr = pIFactory->Release();
    //通过工厂获得COM对象一个接口(ILook)
    ILook * pILook = NULL;
    //Returns a pointer to a specified interface on an object to which a client
    //currently holds an interface pointer.
    hr =->QueryInterface(IID_IWindowx,(void**)&pILook);
    //调用COM对象接口方法
    HWND hWnd = pILook->CreateWindowx(NULL,0,0);
    CoUninitialize() ;


阅读(5362) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~