WSC in ASP
Published 12/12/01
Introduction
Windows Script Components (WSC)
have been around for a while, but I've never really seen them being used that
widely. A WSC is just a file with some script in it (written in your favorite
scripting language, eg. VBScipt, JScript, JavaScript etc.) and can be accessed
in an ASP page like a normal component. WSCs are very versatile, they can be
easily created, used, and distributed. In this article I'll be showing you how
to do these things with WSC and look at some of the uses that they have.
The Creation
Creating a WSC is easy, just open
up Notepad (or your favorite editor). The following code is the standard
beginning of a WSC.
<Scriptlet>
<Registration ProgID="TestScript.wsc" DESCRIPTION="Just a test"
VERSION="1" CLASSID="{f14923b9-8821-4083-8c28-f689a89333f6}">
</Registration></Scriptlet> |
Everything is enclosed in the <scriptlet> tag,
it holds everything. The next tag is the <registration> tag, this holds
information about what the program is and what it does.
It starts by stating the Program ID (Required) which is a unique name to
identify the program by (usually the file name), then the description
(optional) and the version (optional), then comes the ClassID (optional), you
can create this by running uuidgen if you have Visual Studio installed.
Next comes the part that tells ASP how its
going to communicate to the scriptlet (through properties and methods) in the
<implements> tag -
<Scriptlet>
<Registration ProgID="TestScript.wsc" DESCRIPTION="Just a test"
VERSION="1" CLASSID="{f14923b9-8821-4083-8c28-f689a89333f6}">
</Registration>
<Implements ID=Automation TYPE=Automation>
<Property name="message" />
<Property name="name" InternalName="number" />
<Method Name="Print" />
<Method Name="CalcTax" InternalName="tax" />
</implements>
</Scriptlet> |
The Implements tag holds information about the properties and
method. Both, have the same properties (Name and Internal Name) and
basically do the same thing. The Name property tells ASP what to call the
property/method and the InternalName tells the WSC what to call it inside the
script (it's the same if the InternalName is not defined). So - Inside the WSC,
we call CalcTax and outside it is still CalcTax. You'll see this in operation
soon. Also, anything that you don't want to be accessed outside, you just
leave out.
<Scriptlet>
<Registration ProgID="TestScript.wsc" DESCRIPTION="Just a test"
VERSION="1" CLASSID="{f14923b9-8821-4083-8c28-f689a89333f6}">
</Registration>
<Implements ID=Automation TYPE=Automation>
<Property name="message" />
<Property name="name" InternalName="number" />
<Method Name="Print" />
<Method Name="CalcTax" InternalName="tax" />
</implements>
<Script language="VBSCRIPT">
Dim message
Dim number
Dim privated
privated = " Printed by WSC <br>"
Function Print(message)
Print = message & privated
End Function
Function tax(number)
tax = number * 1.125
End Function
</Script>
</Scriptlet> |
You should all recognize this as VBScript and I
shouldn't need to go over it. You'll notice that the variable 'privated' isn't
in the <implements> so you can't access it externally.