当前位置: 代码迷 >> vbScript >> VBS中怎么能GetRef一个VB6类模块创建的ActiveX DLL中的事件?
  详细解决方案

VBS中怎么能GetRef一个VB6类模块创建的ActiveX DLL中的事件?

热度:2696   发布时间:2013-02-26 00:00:00.0
VBS中如何能GetRef一个VB6类模块创建的ActiveX DLL中的事件???
VB6 ActiveX DLL 代码,(工程名为:“QTEST”,类模块名为:“MyPlugin”):
VB code
Public Event TestEvent()Public Function MyTest(ByVal Number1 As Variant, ByVal Number2 As Variant) As Long    MyTest = Number1 + Number2    RaiseEvent TestEventEnd Function


在VBS中准备调用的代码:
VBScript code
    Dim EvObj    Set EvObj = CreateObject("Qtest.MyPlugin")     EvObj.TestEvent = GetRef("EvObj_TestEvent")    Sub EvObj_TestEvent()    msgbox "EvObj_TestEvent"    End Sub


但结果却并不好用。。。请问需要怎么做才可以调用这个ActiveX DLL 的TestEvent事件呢??
谢谢!!!

------解决方案--------------------------------------------------------
我查阅了大量资料,始终找不到用标准VB事件处理调用接口实现Script响应Win32程序事件的方法。最后得出的结论是:

VBScript本来就是设计用来与Web文档等活动文档一起工作的,在VBScript中像WithEvents等许多特性都不被支持。所以,要让VBScript来响应Win32程序事件,必须提供额外的接口,基本上就是用Callback方法。

以下为我用Callback方法实现的VBScript事件处理程序:

VB6 ActiveX DLL(工程名:“QTEST”,类模块名:“MyPlugin”): 
VB code
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 code
<?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>
  相关解决方案