To read the article online, visit http://www.4GuysFromRolla.com/webtech/050400-1.3.shtml

Building Windows Script Components, Part 3


  • Read Part 2
  • Read Part 1

  • In Part 1 and Part 2 we looked at how to create a Windows Script Component skeleton using the Wizard. In this part, we'll look at adding the source code to our "skeleton" component and registering the component. Finally, we'll look at how to use our newly created component in an ASP page!

    Take a moment to look at the source for the Temperature.wsc file we just created. Note the XML-format of the component. The part we are interested in is the actual source code, which is located between the <SCRIPT LANGUAGE="VBScript"> and </SCRIPT> tags. Note that our two functions contain just an empty body. It is up to us to write this code. Go ahead and enter the following code:

    function Fahrenheit2Celcius(fahrTemp)
       Fahrenheit2Celcius = (fahrTemp - 32) / 1.8
    end function
    
    function Celcius2Fahrenheit(celcTemp)
       Celcius2Fahrenheit = celcTemp * 1.8 + 32
    end function
    

    Now, we're ready to register our component! This is very easy to do, no more regsvr32! Simply right-click on the .wsc file and select Register. It's that easy!

    Register the Windows Script Component

    Now that the component is registered you can use it like any other COM component! From an ASP page, instantiate the object using Server.CreateObject("Temperature.Convert"). Here is some sample code using the new COM object:

    <%@ Language=VBScript %>
    <% Option Explicit %>
    <%
      Dim objTempConv
      Set objTempConv = Server.CreateObject("Temperature.Convert")
    %>
    
    <HTML>
    <BODY>
      49 degrees Fahrenheit is
      <%=objTempConv.Fahrenheit2Celcius(49)%> degrees.<BR>
    
      28.5 degrees Celcius is
      <%=objTempConv.Celcius2Fahrenheit(28.5)%> degrees.
    </BODY>
    </HTML>
    
    <%
      Set objTempConv = Nothing      'Clean up!
    %>
    

    The output of the above script is: 49 degrees Fahrenheit is 9.44444444444444 degrees. 28.5 degrees Celcius is 83.3 degrees.

    So there you have Windows Script Components! Note that if you wanted to alter this component at all, you would only need to edit the .wsc file! You wouldn't need to bother stopping/starting the Web server or reregistering the component! Personally I find Windows Script Components to be very cool, very powerful, and very useful.

    From Alert 4Guys Reader Matt J.
    Keep in mind that WSC components are Apartment threaded, so using them anywhere but at page scope is evil. (Of course, using object at Session or Application scope might also be evil, but they can at least perform well if they are Both threaded and aggregate the Free-Threaded Marshaler.)

    Happy Programming!


    Article Information
    Article Title: Building Windows Script Components, Part 3
    Article Author: Scott Mitchell
    Published Date: Thursday, May 04, 2000
    Article URL: http://www.4GuysFromRolla.com/webtech/050400-1.3.shtml


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers