Windows Script Components, Part Deux
The wizard is great, but you'll have to dig deeper to reap all of WSC's rewards.
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.
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"
Of course, you can save even more space by putting
the XML terminating slash "/" on the
Go ahead and delete the corresponding "get_xxx"
and "put_xxx" methods from the <script>
section, as these properties will be accessed
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
You also need to change the Dim statement inside the <script> section.
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
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>.
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.
= iNum1 + iNum2
Your Windows Script Component should now look like this. (Your GUID,
of course, will be different!)
= iNum1 + iNum2
Is Any of This Registering
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.
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.
Run the script, and the number "3" is echoed
to the screen. Not overly impressive, but not
bad for our first component!
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.