BEGINNING COMPONENTS FOR ASP PART 6 – BUILDING OUR FIRST COMPONENT (Page 2)

The XML File Created by the Wizard

Let’s look at the XML file that our component has created (just navigate to the file in Windows Explorer and double-click). Don’t worry if you do not understand XML – this will actually show you just how easy the basics of XML are. As you will see, XML looks very similar to HTML.

We start off on the first line with the XML declaration, which any XML file uses. This is followed by a root tag, <component>, which surrounds the whole component. Then we have a processing instruction (in the tag that starts and ends with question marks) to say that we are supporting error handling and debugging (remember we checked tick boxes for these in the second screen of the Wizard):

<?xml version="1.0"?>
<component>

<?component error="true" debug="true"?>
...

Next we have the registration details, which include a description of the component, a ProgID, the version number and the CLSID. Note the simplicity of the XML tags that actually describe their contents:

...
<registration
  description="BrickCalc"
  progid="BrickCalc.WSC"
  version="1.00"
  classid="{f954a720-67c1-11d3-99b1-00104b4c84a4}"
>
</registration>
...

Next we have the properties and methods that the component will expose, nested inside the <public> tags, held in tags that say <property> and <method>:

...
<public>
  <property name="BrickType">
    <put/>
  </property>
  <method name="HowManyBricks">
    <PARAMETER name="WallLength"/>
    <PARAMETER name="WallHeight"/>
  </method>
</public>
...

Finally we have the block, which will contain the script code that adds the functionality of the component. (And a closing </component> tag.) At the moment it is like an interface without any implementation.

...
<script language="VBScript">
<![CDATA[

dim BrickType
BrickType = "BreezeBlock"

function put_BrickType(newValue)
  BrickType = newValue
end function

function HowManyBricks(WallLength, WallHeight)
  HowManyBricks = "Temporary Value"
end function

]]>
</script>

</component>

Now we have to add in the functionality, using a simple bit of VBScript (note that we have hard-coded values for the size of the bricks):

function HowManyBricks(WallLength, WallHeight)
  If BrickType = "BreezeBlock" Then
    TheResult = Int(WallLength / 0.75) + 1
    Result = TheResult * (Int(WallHeight / 0.33) + 1)
    HowManyBricks = Result
  Else
    TheResult = Int(WallLength / 0.35) + 1
    Result = TheResult * (Int(WallHeight / 0.15) + 1)
    HowManyBricks = Result
  End If
end function

Once you’ve done that, save the file – and now we are ready to register our component.

Registering the File

Windows Script Components are very easy to register, as the WSC kit adds options to Register, Unregister, and Generate TypeLib (type library) for a component when you right click on it in Windows Explorer. Don’t worry about the type library option for the moment; we’ll talk about type libraries later in the book.

unnamed-file-332 BEGINNING COMPONENTS FOR ASP PART 6 - BUILDING OUR FIRST COMPONENT
       (Page 2)

We should be notified that our component registered successfully. This uses the regsvr32.exe program to add the necessary references to the registry, including the extra key that points to scrobj.dll.

And that’s it – we have created our first COM component. To check that it works, try your own version with the ASP pages provided in the WroxBloxForm.asp/WroxBloxResult.asp example, which are available with all the rest of the code for the book from http://webdev.wrox.co.uk/books/2882/.

WSCs offer far greater functionality than is described here. They have evolved very quickly, and the best way to keep up with them is to check out http://msdn.microsoft.com/scripting/ where there are full tutorials and more examples.