【问题标题】:including classes with a wsc file包括带有 wsc 文件的类
【发布时间】:2015-01-20 02:19:58
【问题描述】:

好的,我在这里做错了什么? 我正在尝试以这种方式包含一个带有类的 vbscript:

SCRIPT.VBS:

set inc = createobject("script.runner")
inc.Include "class"
set x = new test
x.msg' here i get the error 'undefined class'!

注册的 .wsc 文件:

<?xml version="1.0"?>
<component>
<registration
description="wsc"
progid="script.runner"
version="1.00"
classid="{f65e154c-43b3-4f8f-aa3d-535af68f51d1}"
>
</registration>
<public>
<method name="Include">
<PARAMETER name="Script"/>
</method>
</public>
<script language="VBScript">
<![CDATA[
Sub Include(Script)
ExecuteGlobal(CreateObject("scripting.filesystemobject").OpenTextFile(Script & ".vbs", 1).Readall & VBNewLine)
End Sub
]]>
</script>
</component>

CLASS.VBS:

class test
public sub msg
msgbox "hi"
end sub
end class

我在想,如果我要使用类或其他东西,我可能需要在 wsc 文件中定义它吗? 我不知道。。

感谢您的帮助!

【问题讨论】:

    标签: vbscript com include createobject wsc


    【解决方案1】:

    VBscript 的 Execute(Global) 和 .COM 是非常不同的代码重用方式。你不应该混合它们。

    .wsc 允许您创建一个对象并使用它的方法和属性。这样的方法(工厂)可能会创建并返回另一个对象。所以如果你添加

    <method name="mkTest">
    </method>
    ...
    Function mkTest()
      Set mkTest = New test
    End Function
    

    到您的 .wsc 和

    set x = inc.mkTest
    x.msg
    

    对于您的 .vbs,整个繁琐的工作将“奏效”。

    您应该考虑一下您在现实世界中的任务,阅读good book about .COM,并提出一个不混合异构技术的简单策略(也许是勾勒出@的 Sub Include()/ExecuteGlobal 方法) 987654322@)。

    【讨论】:

      【解决方案2】:

      这样做了:

      脚本

      set inc = createobject("script.runner")
      inc.Include "C:\Users\GEEK\Desktop\small"
      set x = inc.AddClass("test")
      x.msg' here i get the error 'undefined class'!
      

      wsc 方法内部

      Function AddClass(ClassName)
      execute("Set AddClass = New " & ClassName)
      end Function
      

      和埃克哈德.霍纳, 你说得对。 我只是好奇如何解决问题,即使有更简单的方法 某事^^

      感谢大家的帮助!

      问候

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-10
        • 2015-11-24
        • 1970-01-01
        • 2011-10-22
        • 1970-01-01
        相关资源
        最近更新 更多