Building Our First Component
Earlier, we visited our first example of using an ASP
component—the BrickCalc component—and
we promised that we'd return to see how to build the component. The Brick
Calculator component is a Windows Script
Component (WSC), written using the Windows Script Component
Wizard and Notepad. WSCs are an ideal way to prototype components, allowing us
to write the components using VBScript or JScript,
which means that they can be written
in a matter
of minutes.
If this is the first time you've heard of WSCs, then you
might be asking yourself the same sorts of questions I asked myself when I
first heard about them. For example, we've already said that a COM object is a
binary file—so how does WSC create a COM object using VBSscript or JScript?
Moreover, how will we deal with registration and getting a CLSID? How do we get
an interface to work with?
A Similar Case
WSCs work in a very similar way to the OLE DB simple
providers in Visual Basic. An OLE DB simple provider is a two-tiered component
made up of your simple Visual Basic (or
C++ or J++ , for example) code and a DLL. The DLL processes the code
you've written, and exposes a regular OLE DB programming interface to its
consumers.
WSCs (previously known as Scriptlets) work in much the same
way. Whenever we register a script-based COM object, the registry entry points
to a COM-compliant executable called scrobj.dll. Furthermore, any script-based COM object has an extra key in
the registry, which points to a script file. This file contains metadata, which
defines both the executable code and the interfaces to be made externally
available.
Without further ado, let's build our first component for use
in ASP. In order to build the component, you'll need to download the WSC kit
from http://msdn.microsoft.com/scripting.
You'll need to download both the Windows Script Components (Windows Script Version 5) and the Windows Script Component Wizard—both of which
are available at this site.
Planning the Brick Calculator
Component
The Brick Calculator component has just one task to
perform—calculating the number of bricks needed to build a wall. So, the
component will implement one method called HowManyBricks(). Knowing that we
only need one method to perform the required task, we need think about what
information we should to supply the method, in the form of parameters, to
perform the calculation.
If we were approaching this methodically in the real world,
we could work out the size of the wall, check the size of the brick, and then
divide the size of the wall by the size of the brick to find out how many
bricks we need. To perform this calculation we'd need to know four things
q
The length of the wall
q
The height of the wall
q
The length of the bricks used
q
The height of the bricks used
We are defining one property of the component, BrickType, which will tell the
component the type of brick we are using—breezeblock or house brick. This
covers the length and the height of the bricks, which are hard coded into the
component. We are then left with the length and height of the wall, which we
will pass to our component as parameters to the method. Our method now looks
like this:
HowManyBricks(WallHeight, WallLength)
And that's all the information we need to create the shell
of the component using the Windows Script Component Wizard—to which we will
later add the functionality using script.
Using The Windows Script Component
Wizard
This Wizard is made available from the Programs | Microsoft Windows Script option
on the Start menu. When you start it up, you