Partager via


Referencing Other Components

 

Your Windows Script Component can include references to external components that you need to create the script component, such as:

  • Additional COM objects

  • Type libraries

  • Resources, such as numbers and text, that you do not want to hard-code into your script component's scripts.

Referencing Additional COM Components

In your script component, it might be necessary to create instances of other COM components as needed. You can do so in two ways:

  • In script Create instances of other objects directly in your script. For example, you can use the CreateObject function in Microsoft Visual Basic Scripting Edition (VBScript) or the new ActiveXObject Object in JScript.

  • Using an OBJECT element Use an <object> element similar to the <OBJECT> tag you use in HTML pages. Using an <object> element makes objects globally available to any script and allows scripting tools to provide statement completion. It also provides a convenient way to summarize and document the objects you use in your script component.

    Note

    Although <object> elements in script components are similar to <OBJECT> tags in HTML pages, the list of attributes for an <object> element in a script component is shorter, because script components do not present a user interface.

To create an OBJECT element

  1. Create an <object> element. This element should be inside the <component> element, but outside of any other element such as a <script> element.

The following example shows an object definition in a script component.

<object id="cnn" progid="ADODB.Connection"/>

Referencing an External Type Library

Many components that you might work with support type libraries, which provide a complete listing of the component's classes and their members. By referencing the component's type libraries, you can use constants defined in the type library.

To include a reference to a type library

  1. Include a <reference> element in your script component that specifies the location and name of the type library to include. For example:

    <reference object="ADODB.Connection.2.0"/>
    

Referencing Resources

Resource elements can include information that might change between versions, strings that might be translated, and other values.

To reference resources

  1. In the script component, but outside of the <public> and <script> elements (and <implements> element, if any), create one <resource> element for each resource you want to define, giving each element a unique ID. The following example shows two <resource> elements:

    Note

    A CDATA section is required to make the contents of the <resource> element opaque to the parser. For details, see Script Component Files and XML Conformance.

    <component id="MyScriptlet">
    <public>
       <method name="random" internalName="getRandomNumber"/>
    </public>
    <resource id="errNonNumeric"><![CDATA[Non-numeric value passed]]>
    </resource>
    <resource id="errOutOfRange"><![CDATA[Passed value is out of range ]]>
    </resource> 
    
  2. In your script, include the resource text or number by calling the getResource function, as shown in the following example.

    Note

    A CDATA section is required to make the script in the <script> element opaque to the parser. For details, see Script Component Files and XML Conformance.

    <script language="VBScript">
    <![CDATA[
    Function getRandomNumber(upperBound)
       If IsNumeric(upperBound) Then
          getRandomNumber = Cint(upperBound * Rnd + 1)
       Else
          getRandomNumber=getResource("errNonNumeric")
       End If
    End Function
    ]]>
    </script>
    

See Also

Script Component File Contents
Referencing Another Script Component in the Same Package