不注册调用ActiveX Dll
不注册调用ActiveX Dll 不注册调用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