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 tag, it holds everything. The next tag is the 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 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 so you can't access it externally.
Script Registration
Now that you have your script, you need to use it. but first - registration.
Registration
To register the script, first save it somewhere on your hard-drive then find it in Explorer -
After finding it, right-click on it -
And choose - 'Register'. Then you should see something like this -
Which means that somewhere in your registry, a key has been created (usually the classid or the progid) that tells windows where on the hard-drive the script is.
Using the Script
[ Back To Top ]
Using the Script
Ok, now we can use the script in ASP. Your ASP file should look something like this -
<%
Set objWSC = CreateObject("testscript.wsc")
Response.Write(objWSC.Print("Hello"))
Response.Write(objWSC.CalcTax(5.20) & "<br>")
'Response.Write(objWSC.privated & "<br>")
%>
This code should be familiar to you. It creates the object like any other ASP component and then we proceed to print various things to the screen -
Hello Printed by WSC
5.85
I commented out the last line because you will get an error (because, if you remember, it's a private property). Also, we used CalcTax instead of Tax on the second line.
You can also use the Script in your page if its on another server i.e.
<%
Set objWSC = CreateObject("testscript.wsc", "box2")
%>
Uses the testscript.wsc on the computer called box2.