863
社区成员
发帖
与我相关
我的任务
分享
类工厂。动态创建接口,实现接口,并可以调用。详细看例子。
'比如 有一个接口ICaller,有两个成员定义如下
Sub CallSub(ByRef a As Long, ByRef b As Long)
Function CallFunc(ByRef a As Long, ByRef b As Long) As Long
'那么我们可以这么定义一个FunctionDelegator
Dim oCaller As ICaller
Set oCaller = COMHelper.FunctionDelegator.NewDelegator(2)
'//生成一个Delegator,它有2个成员
'同时,我们在一个标准模块里定义了两个要指向执行的函数
Private Sub pTestCallSub(ByRef a As Long, ByRef b As Long)
Private Function pTestCallFunc(ByRef a As Long, ByRef b As Long) As Long
'//那么
Call COMHelper.FunctionDelegator.Redirect(oCaller, AddressOf pTestCallSub)
'//将该Delegator的函数执行指向 pTestCallSub方法
Call oCaller.CallSub(1,2)
'//执行这一句,将自动转向pTestCallSub这个方法。注意 pTestCallSub的定义一定要跟接口ICaller.CallSub的定义是一致的。
Private Sub pTestRegisterServer()
Call COMHelper.UnregisterActiveX("C:\WINDOWS\system32\scrrun.dll")
Debug.Print TypeName(CreateObject("Scripting.Dictionary"))
'//将报错,因为已经被注销
Call COMHelper.RegisterActiveX("C:\WINDOWS\system32\scrrun.dll")
Debug.Print TypeName(CreateObject("Scripting.Dictionary"))
'//可以执行,因为已经被重新注册
End Sub
Private Function pTestClassLoader()
Dim o As Object ' Dictionary
Dim oLoader As DyComHelperType.COMLoader
Dim sClsID As String
Set oLoader = COMHelper.NewCOMLoader("C:\WINDOWS\system32\scrrun.dll")
Set o = oLoader.CreateObject("Dictionary")
'//初始化 Dictionary类
Debug.Print TypeName(o)
o.Add "Key1", 1
o.Add "Key2", 2
Debug.Print o.Count
Debug.Print oLoader.CanUnloadNow
'//看看能不能卸载该Dll。返回值为False,因为该Dll有实例未释放
Set o = Nothing
Debug.Print oLoader.CanUnloadNow
'//这次可以释放了
sClsID = oLoader.GetCLSIDFromName("Dictionary")
'//根据名称加载是很慢的,尝试根据ClsID 加载,如果多次重复加载某一对象,这么处理将会有极大效率提升
Set o = oLoader.CreateObjectFromCLSID(sClsID)
'//初始化 Dictionary类
Debug.Print TypeName(o)
o.Add "Key1", 1
o.Add "Key2", 2
Set o = Nothing
Debug.Print oLoader.Unload
Set oLoader = Nothing
'//释放Dll
End Function