不註冊調用ActiveX Dll

來源:互聯網
上載者:User

不註冊調用ActiveX Dll
(by lingll 完成於2006-2-7 13:45)

每個ActiveX Dll都應該有個DllGetClassObject函數,利用該函數就可以直接建立所需的com對象,而不需要通過註冊表(或者註冊),

STDAPI DllGetClassObject(
  REFCLSID rclsid,  //CLSID for the class object
  REFIID riid,      //Reference to the identifier of the interface
                    // that communicates with the class object
  LPVOID * ppv      //Address of output variable that receives the
                    // interface pointer requested in riid
);

這裡必須知道兩樣東西,一個rclsid,就是需要建立的com對象的CLSID,另一個是 riid,該對象的一個介面的 id.
然而,調用DllGetClassObject,並不能直接建立所需要的對象,但可以得到對應的 IClassFactory,再由 IClassFactory.CreateInstance得到所需的對象.
vb實現代碼大概如下:
需要用到一個庫,http://www.mvps.org/emorcillo/download/vb6/tl_ole.zip
(引用頁,http://www.mvps.org/emorcillo/en/code/vb6/wbframe.shtml)
另外,也將那個ActiveX Dll引用進工程,這裡,並不是需要註冊它,而是為了方便使用它的方法,因為並沒有使用new來建立對象,
程式編譯後即使不註冊那個Dll檔案都能夠正常使用.

Option Explicit

'假設ActiveX Dll 的檔案名稱為dllDemo.dll,並且處於工程同一目錄
Private Declare Function DllGetClassObject Lib "dllDemo.dll" ( _
    rclsid As UUID, riid As UUID, ByRef ppv As Any) As Long

'class id
Private Const ClsStr_Obj As String = "{C1A334BA-D1A4-48D0-98D5-47FE934961DF}"
'介面id
Private Const IidStr_Ins As String = "{231114D5-E046-4DAE-B192-0AB49D493A85}"

'IClassFactory id
Private Const strIID_IClassFactory As String = "{00000001-0000-0000-C000-000000000046}"

Private ClsId_Obj As UUID
Private Iid_Ins As UUID
Private iid_iunknow As UUID
Private iid_iclassfactory As UUID


Private Sub Command1_Click()
Dim tobj As olelib.IUnknown
Dim tobj2 As dllDemo.IDemo
Dim tFac As olelib.IClassFactory

Call DllGetClassObject(ClsId_Obj, iid_iclassfactory, tFac)

tFac.CreateInstance Nothing, iid_iunknow, tobj
Set tFac = Nothing
Set tobj2 = tobj

'調用IDemo.Test測試所建立的對象
tobj2.Test
End Sub

Private Sub Form_Load()
'將string轉換為 UUID
CLSIDFromString ClsStr_Obj, ClsId_Obj
CLSIDFromString IidStr_Ins, Iid_Ins
CLSIDFromString IIDSTR_IUnknown, iid_iunknow
CLSIDFromString strIID_IClassFactory, iid_iclassfactory
End Sub

 

至此,問題似乎已經解決了,只要為不同的ActiveX Dll編寫對應的DllGetClassObject函數就可以了,只是當檔案名稱未定時就比較難辦了,例如編寫外掛程式時.
解決辦法是用LoadLibrary動態調用各個dll上的DllGetClassObject.可惜的是vb不支援函數指標.我的辦法是藉助vc來解決.用vc寫dll供vb調用,主要代碼如下:

// CrCom.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include <unknwn.h>
#include <objbase.h>

typedef int (CALLBACK *MYPROC)(REFCLSID,REFIID,LPVOID *);

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
      )
{
    return TRUE;
}


// if(riid==NULL)riid=&IID_IUnknown
int _stdcall CrComObj(
      LPCSTR lpDll,
      CLSID *rclsid,
      IID *riid,
      LPVOID * ppv)
{
 HINSTANCE hinstLib;
    MYPROC ProcAdd;  

 BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
 int rtn=0;
 // Get a handle to the DLL module.
 
 
    hinstLib = LoadLibrary(lpDll);
 
    // If the handle is valid, try to get the function address.
 
    if (hinstLib != NULL)
    {   
        ProcAdd =(MYPROC)GetProcAddress(hinstLib, "DllGetClassObject");
 
        // If the function address is valid, call the function.
  
        if (fRunTimeLinkSuccess = (ProcAdd != NULL))
  {  
   if(rclsid==NULL)
   {
    FreeLibrary(hinstLib);
    return 0;
   }
   
   if(riid==NULL)
    riid=(IID *)&IID_IUnknown;

   IClassFactory *pIf;
   pIf=NULL;
            if(ProcAdd(*rclsid,IID_IClassFactory,(void **)&pIf)==S_OK && pIf!=NULL)
   {
    if(pIf->CreateInstance(NULL,*riid,ppv)==S_OK)
     rtn=(int)hinstLib;    
    pIf->Release();
    pIf=NULL;
   }
  }
        // Free the DLL module.
 
        if(!rtn)fFreeResult = FreeLibrary(hinstLib);
    }
 return rtn;
}


// if strriid==NULL, use IID_IUnknown;
int _stdcall CrComObj2(
      LPCSTR lpDll,
      LPCSTR  strrclsid,
      LPCSTR  strriid,
      LPVOID * ppv )
{
 HINSTANCE hinstLib;
    MYPROC ProcAdd;  

 BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
 
 int rtn=0;
 // Get a handle to the DLL module.
 
 
    hinstLib = LoadLibrary(lpDll);
 
    // If the handle is valid, try to get the function address.
 
    if (hinstLib != NULL)
    {   
        ProcAdd =(MYPROC)GetProcAddress(hinstLib, "DllGetClassObject");
 
        // If the function address is valid, call the function.
 
        if (fRunTimeLinkSuccess = (ProcAdd != NULL))
  {  
   CLSID rclsid;
   IID riid;
   
   if(strrclsid==NULL)
   {
    FreeLibrary(hinstLib);
    return 0;
   }
   CLSIDFromString((LPOLESTR )strrclsid,&rclsid);

   if(strriid!=NULL)
    CLSIDFromString((LPOLESTR )strriid,&riid);
   else
    riid=IID_IUnknown;

   IClassFactory *pIf=NULL;

            if(ProcAdd(rclsid,IID_IClassFactory,(void **)&pIf)==S_OK && pIf!=NULL)
   {
    if(pIf->CreateInstance(NULL,riid,ppv)==S_OK)
     rtn=(int)hinstLib;    
    pIf->Release();
    pIf=NULL;
   }
  }
        // Free the DLL module.
  
        if(!rtn)fFreeResult = FreeLibrary(hinstLib);
    }
 return rtn;
}


在vb中的使用方法,CrComObj傳遞的是UUID,CrComObj2傳遞的是String,

'函式宣告
Private Declare Function CrComObj Lib "CrCom.dll" ( _
    ByVal lpDll As String, ByVal rclsid As Long, ByVal riid As Long, ByRef ppv As Any) As Long
Private Declare Function CrComObj2 Lib "CrCom.dll" ( _
    ByVal lpDll As String, ByVal strrclsid As Long, ByVal strriid As Long, ByRef ppv As Any) As Long


Dim tobj As olelib.IUnknown
Dim tobj2 As dllDemo.IDemo

hlib = CrComObj(App.Path & "/dllDemo.dll", VarPtr(ClsId_Obj), 0, tobj)
Set tobj2 = tobj
tobj2.Test

'或者

hlib=CrComObj2(App.Path & "/dllDemo.dll", StrPtr(ClsStr_Obj), 0, tobj)
Set tobj2 = tobj
tobj2.Test


CrComObj與CrComObj2返回的是LoadLibrary的傳回值,必要的時候需要用FreeLibrary釋放.


後記:
我的多頁面瀏覽器LE中,也實現了不註冊調用ActiveX Dll,我是直接使用了一本書(Advanced Visual Basic)的代碼,代碼頗長,似乎也挺複雜,原先使用的時候也不明所以然,後來終於搞清楚了,其原理是一樣的,但是因為vb不支援函數指標,於是它花了很大力氣去處理這個問題.相比而言,我覺得還是借用一下vc比較好,這樣的話簡捷的多.


lingll
lingll_xl@163.com
2006-2-7

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.