Search code examples
vbscriptcomincludecreateobjectwsc

including classes with a wsc file


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!


Solution

  • 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).