Mr. Script
Windows Script Components, Part Deux
The wizard is great, but you'll have to dig deeper to reap all of WSC's rewards.
- By Chris Brooke
- 10/01/2001
This month, I'll continue my series on Windows Script Components. Last
month, we used the Windows Script Component Wizard to create our very
first Windows Script Component (WSC). The wizard handled all the plumbing,
leaving us the simple task of defining our Properties and Methods. The
next step is to add the code to the WSC.
We're Off to See the Wizard
(And Fix His Mistakes!)
No doubt about it—the Script Component Wizard
is a wonderful tool. Unfortunately, the skeleton
component it creates is far from ready. The first
thing you'll notice is that all the Properties
have been set up as Procedural Properties (remember
those?), each with corresponding Get and Put methods.
<public>
<property
name="MyProp1">
<get/>
<put/>
</property>
<property
name="MyProp2">
<get/>
<put/>
Because both of these properties are Read/Write
and because I don't need to apply any logic in
order to determine the values of these properties,
I'll go ahead and change them to "normal"
Public Properties.
<public>
<property
name="MyProp1">
</property>
<property
name="MyProp2">
</property>
Of course, you can save even more space by putting
the XML terminating slash "/" on the
same line.
<public>
<property
name="MyProp1">
<property
name="MyProp2">
Go ahead and delete the corresponding "get_xxx"
and "put_xxx" methods from the <script>
section, as these properties will be accessed
directly.
Getting Hungarian?
You may have noticed that the Properties are referenced internally using
their Public names. If you wish to use Hungarian Notation within your
component (and you should), you need to add a reference to an internal
name.
<public>
<property
name="MyProp1" internalname="iNum1">
<property
name="MyProp2" internalname="iNum2">
You also need to change the Dim statement inside the <script> section.
<script
language="VBScript">
<![CDATA[
dim
iNum1
dim iNum2
This is pretty cool! Remember that, in classes,
the only way to use Hungarian Notation was to
create Procedural Properties. WSCs allow you to
avoid that added code and still use any internal
name you wish. Ahh—the joys of XML! I'll
also get rid of the default value of "True"
for MyProp1 (AKA: iNum1), as I'm dealing with
numbers now.
Now Let's Make It Do Something
The wizard created two Functions for my two methods. I'll fill in some
code to do some simple math. Method 1 will change the values of MyProp1
and MyProp2. Because it won't need to return a value, I'll change it from
a Function to a Sub. I'll also change the parameters to integers instead
of strings. (Actually, all I've really changed is the Hungarian Notation.
WSCs, just like VBScript, treat every variable as the type VARIANT.) Don't
forget to also change this in the PARAMETER value of the Method's declaration
line under <public>.
sub
MyMethod1(iValue1, iValue2)
iNum1=iValue1
iNum2=iValue2
end sub
Method 2 will add the values of the two Properties. Because it'll be
accessing the properties directly, I don't need to provide arguments.
However, as it's returning a value (the sum of the two numbers), I need
to leave it as a Function.
function
MyMethod2()
MyMethod2
= iNum1 + iNum2
end
function
Your Windows Script Component should now look like this. (Your GUID,
of course, will be different!)
<?xml version="1.0"?>
<component>
<registration
description="MyScriptComponent"
progid="MyScriptComponent.WSC"
version="1.00"
classid="{7428c732-9472-422e-9878-bdca8433db0e}"
>
</registration>
<public>
<property
name="MyProp1" internalname="iNum1">
<property
name="MyProp2" internalname="iNum2">
<method
name="MyMethod1">
<PARAMETER name="iValue1">
<PARAMETER name="iValue2">
</method>
<method
name="MyMethod2">
</method>
</public>
<script
language="VBScript">
<![CDATA[
dim
iNum1
dim
iNum2
sub
MyMethod1(iValue1, iValue2)
iNum1=iValue1
iNum2=iValue2
end sub
function
MyMethod2()
MyMethod2
= iNum1 + iNum2
end function
]]>
</script>
</component>
Is Any of This Registering
With You?
The final step before using the new component
is to register it. The easiest way to do this
is to simply right-click the file in Explorer
and select "Register" (Figure 1).
|
Figure 1. Windows makes
registering WSCs extremely simple. (Click
image to view larger version.) |
Windows knows that WSC files are components,
so it automatically gives you the option of registering
it. For those who like doing things the manual
way, you can still use Regsvr32.exe to do it.
C:\Regsvr32 path\component_name.wsc
Looking back at Figure 1, you can see that there's
another option—Generate Type Library. This
allows you to create a type library (.TLB file)
so you can look at the component through an object
browser like XRay.exe. This comes in handy once
you've built up a repository of WSCs and need
to look at a component to determine its interfaces.
You create these type libraries one at a time,
as the .TLB file created is always called "Scriptlet.tlb."
(You can manually generate your .TLB file from
a script and give it the appropriate name, but
automatically generating it and then renaming
it is easier!) Rename it to match the WSC's name
and keep it in the same folder. Figure 2 shows
the new component viewed inside XRay.
|
Figure 2. The generated
type library lists the methods and properties,
just like a normal component. (Click image
to view larger version.) |
Putting it to Work
Now that the component is registered, all that
remains is writing a script to use it. The following
script uses Method1 to set the values of the Properties
and Method2 to display the sum.
' MathTest.vbs
Dim objWSC
Set
objWSC=CreateObject("MyScriptComponent.WSC")
objWSC.MyMethod1 1,2
WScript.Echo objWSC.MyMethod2
Set
objWSC=Nothing
WScript.Quit
Run the script, and the number "3" is echoed
to the screen. Not overly impressive, but not
bad for our first component!
Homework
To really demonstrate the flexibility of
WSCs, your homework for this month is to take
the "Customer" class I created in the July issue
and turn it into a Windows Script Component. You
may be surprised just how easy it is.
About the Author
Chris Brooke, MCSE, is a contributing editor for Redmond magazine and director of enterprise technology for ComponentSource. He specializes in development, integration services and network/Internet administration. Send questions or your favorite scripts to [email protected].