including classes with a wsc file

◇◆丶佛笑我妖孽 提交于 2019-12-11 19:19:58

问题


Ok, what am i doing wrong here? i'm trying to include a vbscript with a class inside this way:

SCRIPT.VBS:

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

REGISTERED .wsc file:

<?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

I was thinking maybe i need to define it in the wsc file if i'm going to use classes or something? i don't know..

Thanks for any help!


回答1:


VBscript's Execute(Global) and .COM are very different ways of re-using code. You shouldn't mix them.

A .wsc lets you create one object and use its methods and properties. Such a method (factory) may create and return another object. So if you add

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

to your .wsc and

set x = inc.mkTest
x.msg

to your .vbs, the whole rigmarole will 'work'.

You should think about your real world task, read a good book about .COM, and come up with a simple strategy that does not mix heterogeneous technologies (maybe the Sub Include()/ExecuteGlobal approach sketched here).




回答2:


did this:

script

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'!

inside wsc method

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

and Ekkehard.Horner, You're right. I'm just to curious about how to solve a problem even when there are easier ways to do something ^^

Thanks for all the help!

regards



来源:https://stackoverflow.com/questions/27069802/including-classes-with-a-wsc-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!