Windows Scripting Components (WSC) in ASP
 
Published: 12 Dec 2001
Unedited - Community Contributed
Abstract
This article describes Windows Scripting Components, how to create them and use them as components in your ASP applications.
by . .
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 32422/ 56

Introduction

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.

Script Registration

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

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.

Additional Resources

Other Stuff

You can find more information on Windows Scripting (and other scripting stuff) at http://msdn.microsoft.com/scripting/

If you're a lazy programmer then you can use a WSC wizard. It does all the Registration and Implements parts for you, which means that you just need to concentrate on the code. Download it at - http://msdn.microsoft.com/scripting/scriptlets/wz10en.exe. The code that it generates will look a whole lot different to the code that we've used today, but it is all the same really.

Summary

By now, creating and using WSC should be easy to you. And using the WSC wizard should make it a breeze. WSC are very versatile, can be taken to any server and run, they can provide nearly as much functionality as any other component and you can use them across your site.



User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-29 2:57:10 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search