Visual InterDev

Packaging Script as Objects

      

Windows® Script Components provide you with an easy way to create powerful, reusable COM components using script. You create script components using any scripting language that supports the Microsoft® active scripting interfaces. Script languages that support these interfaces include JScript®, Visual Basic® Scripting Edition (VBScript), PerlScript, PScript, and Python.

Note   For more information about active scripting interfaces, see the Microsoft Scripting Web set at http://msdn.microsoft.com/scripting/.

Script components:

Using script components, you can create COM components for a variety of tasks, such as performing middle-tier business logic, accessing and manipulating data, adding transaction processing to applications, and adding interactive effects to a Web page using DHTML Behaviors.

You can use script components in any host application that supports COM. For example, you can create a script component and then use it as a COM component from an application written in Microsoft Visual Basic or in Microsoft Internet Explorer.

Certain types of script components are written to interact with specific environments. For example, you can create a Behavior script component that implements DHTML Behaviors for Internet Explorer 5.0. In that case, the host application must be Internet Explorer 5.0. Similarly, an ASP script component runs in a Microsoft Internet Information Services (IIS) server environment.

How Script Components Work

A script component acts like other COM components: it exposes properties and methods and can fire events. You instantiate a script component from a host application such as a Visual Basic program or a Web page. You can then get or set its properties, call its methods, and respond to events that it fires, just as you would for any COM component. Creating a COM component as a script component, however, is a little different from doing so in other environments.

Sample Script Component File

As the script component author, you create a script component file (.sct file) that contains XML elements. The file resembles an HTML file — some of the tag names are the same — but does not include elements to lay out text. In addition, because a script component is an XML file, it is parsed more carefully than an HTML file, and therefore has to conform more closely to XML conventions. For details, see Script Component Files and XML Conformance.

In Visual InterDev, you create script component files using the text editor.

Note   A Windows Script Component wizard is available on the Microsoft Scripting Web site at http://msdn.microsoft.com/scripting/. The wizard prompts you for basic information about your script component, and then writes a skeleton script component file for you.

A simple script component file might look like the example below, which calculates exponential values. The example exposes a property called number, a method called powerof, and an event called onnumberchange.

The property value is stored in a global variable called n. When the user sets the number property, the script component calls an internal function called setNumber, which checks that the value is valid, sets the property, and then fires the onnumberchange event. When the user gets the value of the number property, the script component calls the getNumber property, which simply returns the value stored in the global variable.

The powerof method takes a parameter called factor. After checking the validity of the factor value, the script component multiplies the value of the number property the appropriate number of times.

<?XML version="1.0"?>
<component>
<registration 
   progid="ScriptComponent.PowerOf"
   classid="{7871f442-5344-11d2-a42d-00c04f8ec80b}"/>

<public>
   <property name="number">
      <get internalname="getNumber"/>
      <put internalname="setNumber"/>
   </property>
   <method name="powerof">
      <parameter name="factor"/>
   </method>
   <event name="onnumberchange"/>
</public>
<script language="JScript">
<![CDATA[
var number;
function getNumber(){
   return number;
}
function setNumber(newNumber){
   if(isNaN(newNumber)){
      return;   // not a number, ignore
   }
   number = newNumber;
   fireEvent("onnumberchange");
}
function powerof(factor){
   if(isNaN(factor)){
      return;   // ignore non-numeric values
   }
   if(factor==0){
      number = 1;
      return;
   }
   if(factor==1){
      return;
   }
   factor = Math.floor(factor);   // integers only

   j = number;
   for(i=1; i<factor ; i++){
      number = number * j;
   }
}
]]>

</script>
</component>

You could call this script component from VBScript using syntax such as the following, which calculates 2 to the fourth power:

set n  = CreateObject("ScriptComponent.PowerOf")
n.number = 2
n.powerof(4)
msgbox("n = " & n.number)

Although this script component is simple, it illustrates the major elements of a script component:

Because script components are COM objects, they include a <registration> element that identifies the script component's prog ID and class ID.

Finally, because script components are XML files, script components also include:

The Script Component Run Time

After the script component file is created, you register it like any COM component. You are then ready to call it from a host application. For example, on a Web page, you can reference a script component by using an <OBJECT> tag, and then use its properties, methods, and events in script.

Script components require a script component run-time component. This is provided in the file called Scrobj.dll, which is installed with Visual InterDev. The script component run time serves as an entry point for the host application. The complexities of COM, including the implementation of such COM interfaces as IUnknown, are embedded in the various interface handlers. Your script component file, therefore, contains only the script required to implement the functionality of the COM component.

For More Information

Complete documentation for script components is provided in the Platform SDK section of the MSDN™ Library. See Windows Script Components for an introduction to the complete documentation set.