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比较好,这样的话简捷的多.
那本书(Advanced Visual Basic)中让vb能够函数指针的方法不错,但是要添加类型库,还要自己创建轻量com对象显得颇为麻烦.我想,不如直接利用vb自己建对象算了.
代码如下:
''建一class,如下
''---------------------------------------------------------------------------------------
'' Module : cFucPtr
'' DateTime : 2006-2-7 17:36
'' Author : Lingll
'' Email : lingll_xl@163.com
'' HomePage : http://lingll.yeah.net/
'' Purpose :
''---------------------------------------------