VBS中如何能GetRef一个VB6类模块创建的ActiveX DLL中的事件???
VB6 ActiveX DLL 代码,(工程名为:“QTEST”,类模块名为:“MyPlugin”):
Public Event TestEvent()Public Function MyTest(ByVal Number1 As Variant, ByVal Number2 As Variant) As Long MyTest = Number1 + Number2 RaiseEvent TestEventEnd Function
Dim EvObj Set EvObj = CreateObject("Qtest.MyPlugin") EvObj.TestEvent = GetRef("EvObj_TestEvent") Sub EvObj_TestEvent() msgbox "EvObj_TestEvent" End Sub
Option ExplicitPrivate ScriptListener As ObjectPublic Event TestEvent()Public Function AddHandler(Caller) As Boolean On Error GoTo AddHandlerError Set ScriptListener = Caller AddHandler = True Exit Function AddHandlerError: AddHandler = False Exit FunctionEnd FunctionPublic Function RemoveHandler(Caller) As Boolean On Error GoTo RemoveHandlerError If ScriptListener Is Caller Then Set ScriptListener = Nothing RemoveHandler = True Else RemoveHandler = False End IfExit FunctionRemoveHandlerError: RemoveHandler = False Exit FunctionEnd FunctionPublic Function MyTest(ByVal Number1 As Variant, ByVal Number2 As Variant) As Long MyTest = Number1 + Number2 RaiseEvent TestEvent If Not ScriptListener Is Nothing Then ScriptListener.FromEvent MyTestEnd Function
[解决办法]
不能用 Class 那能不能用 GetObject 呢
我只在 WSH 环境中测试了一下 WSC
把下面代码保存成 C:\Listener.WSC
<?xml version="1.0" encoding="UTF-8"?><?component error="1" debug="1" ?><component> <comment>Alvin(ialvin.cn)</comment> <registration description="Connection" progid="Alvin.Listener" version="1.00"></registration> <public> <property name="Action"><put/></property> <method name="Fire"></method> </public><script language="VBScript"><![CDATA[Dim theActionFunction put_Action(ByRef value) Set theAction = valueEnd FunctionFunction Fire() Call theAction()End Function]]></script></component>