不注册直接使用COM组件的方法

来源:互联网 发布:好玩的桌面软件 编辑:程序博客网 时间:2024/05/17 06:26

           不注册直接使用COM组件的方法

 

      1. 定义导出函数指针

     

[cpp] view plaincopy
  1. //定义函数指针  
  2. typedef   HRESULT    (_stdcall *Func) (REFCLSID , REFIID , LPVOID*);  
  3. Func   g_DllGetClassObject=NULL;  

      2.导出COM组件这个DLL 的 DllGetClassObject函数

     

[cpp] view plaincopy
  1. if (g_DllGetClassObject==NULL)  
  2.     {  
  3.         CString strParam =  theApp.strRootDirectory + TEXT("//MMGifCtrl.dll");  
  4.         HMODULE  module=LoadLibrary(strParam.GetBuffer(0));  
  5.   
  6.         if (module!=NULL)  
  7.         {  
  8.             g_DllGetClassObject=  (Func) GetProcAddress(module,"DllGetClassObject");  
  9.         }  
  10.     }  

     3.通过 DllGetClassObject 获得COM的类厂  ,然后通过类厂获得所要的接口

    

[cpp] view plaincopy
  1. IMMGifCtrl *    pShowGif     = NULL;  
  2. CComQIPtr<IClassFactory>  pGifFactory;  
  3. HRESULT         hr;  
  4.   
  5.   
  6.   
  7. //如果 g_DllGetClassObject不为空  
  8. if (g_DllGetClassObject)  
  9. {  
  10. hr=   g_DllGetClassObject(__uuidof(CMMGifCtrl), IID_IClassFactory,(LPVOID*)&pGifFactory);  
  11.   
  12.    if (hr  ==S_OK)  
  13.    {  
  14.     pGifFactory->CreateInstance(NULL,__uuidof(IMMGifCtrl),(LPVOID*)&pShowGif);  
  15.   
  16.     if (pShowGif)  
  17.     {  
  18.           //成功获得COM接口  
  19.         }  
  20.    }  
  21.   
  22. }  

0 0