Freigeben über


Creating a Script Component Type Library

 

You can generate a type library for your Windows Script Component containing information about its interfaces and members. In some host applications (such as Visual Basic), type libraries are necessary to enable events for the script component, while in other host applications, type libraries are optional. However, even if a type library is not required, generating one can make working with a script component easier and less error-prone in the host application.

For example, if you are using Visual Basic as your host application, use the References dialog box to select a script component's type library. This allows you to bind events to the script component and make them visible in Visual Basic. In addition, when you are writing Visual Basic code that references the script component, Visual Basic uses the type library information in statement completion and in the Object Browser so you can easily see and use properties, methods, and events that the script component exposes.

Note

For information about using a type library in the host application, refer to the application's documentation.

Procedures

To create a script component type library

  • In Windows Explorer, right-click the script component .wsc file, and then choose Generate Type Library.

    A .tlb file is generated for the script component with the same name as the script component file, written to the folder where the .wsc file is, and registered in the Windows registry.

For more precise control over generating type libraries, you can generate the type library dynamically from script inside the script component file, or you can use a command-line interface.

Generating Type Libraries Dynamically

The script component run-time includes an Automation interface implemented by the Component.TypeLib object. You can use this object in script to generate a type library from within the script component file. This is particularly useful for creating a type library automatically when the script component is registered.

To create a script component type library dynamically

  1. In script inside the script component file, create an instance of the Component.TypeLib object by using syntax such as the following:

    set oTL = CreateObject("Scriptlet.TypeLib")
    
  2. Set the following properties of the Component.TypeLib object:

    Property/Method

    Description

    AddURL

    (Method) Adds the URL of the script component file from which to generate the type library. You can call this method property multiple times to include multiple script components in the type library.

    Path

    (Property) The path and file where the type library will be written. The default path is the current directory and the default name is the name of the script component file with a .tlb extension. If the object is unable to create the specified library, it defaults to creating a type library called component.tlb.

    Doc

    (Property) A string containing any information that is stored in the registry with the type library information.

    GUID

    (Property) A GUID for the type library. (This is not the GUID for the script component.) If you do not provide one, the TypeLib object will create one, but then the type library will have a different GUID on each machine. The GUID must be exactly in this format, where x represents a hex value:

    {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}

    Name

    (Property) The internal name for the type library. This name is displayed in some applications, such as the Visual Basic Object Browser.

    MajorVersion

    (Property) An integer value you assign.

    MinorVersion

    (Property) An integer value you assign.

  3. Call the type library object's Write method to create the .tlb file, then register it.

  4. If you want to create an additional type library, call the TypeLib object's Reset method to clear the list of script component files in the AddURL property, reset the URLs and any other properties you want, and then call the Write method again.

For example, the following script in a <registration> element creates a type library dynamically.

Note

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

<registration
   description="My Test Component"
   progid="Component.TestScript"
   version="1"
   classid="{2154c700-9253-11d1-a3ac-0aa0044eb5f}">
   <script language="VBScript">
   <![CDATA[
   Function Register()
      Set oTL = CreateObject("Scriptlet.TypeLib")
         oTL.AddURL "d:\components\MyComponent.wsc"   ' Script component URL.
         oTL.AddURL "d:\components\YourComponent.wsc"
         oTL.Path = "d:\components\MyComponent.tlb"   ' .tlb path.
         oTL.Doc = "Sample component typelib"   ' Documentation string.
         oTL.GUID = "{a1e1e3e0-a252-11d1-9fa1-00a0c90fffc0}"
         oTL.Name = "MyComponentTLib" ' Internal name for tlb.
         oTL.MajorVersion = 1
         oTL.MinorVersion = 0
         oTL.Write ' Write tlib to disk.
         oTL.Reset ' Clear list of URLs in AddURL/.
      End Function
   ]]>
   </script>
</registration>

Command-line Interface

If you are comfortable using the Command Prompt window, you can call the Rundll32.exe program to create type libraries.

To create a type library from the command prompt

  • Call Rundll32.exe using this syntax:

    rundll32.exe path\scrobj.dll,TypeLib options
    

    Where:

    • path The path where Scrobj.dll is located on your computer.

    • options A set of flags and values you can use to specify type library information in the form -flag:value. You can specify the options in any order. The following flags are supported, with values as described under the previous section, To create a script component type library dynamically.

      -name:Name

      -file:Path

      -doc:\"Doc\"

      -guid:GUID

      -major:MajorVersion

      -minor:MinorVersion

      -URL:AddURL

For example, the following command creates a type library called MyComponent.tlb from the script component MyComponent.wsc (the command is wrapped for clarity):

rundll32.exe c:\winnt\system32\scrobj.dll,TypeLib 
   -name:MyComponentTLib -file:d:\components\MyComponent.tlb 
   -doc:\"Sample component typelib\" 
   -guid:{a1e1e3e0-a252-11d1-9fa1-00a0c90fffc0} -major:1 -minor:0 
   -URL:d:\components\MyComponent.wsc

Troubleshooting Type Libraries

The process of generating a type library can fail for various reasons. The error you see might not be clear enough in all cases to make it obvious where the problem lies. If you are unable to generate a type library, review the following list of likely causes for the failure.

  • If a property is defined by functions, the get and put functions must have the same number of arguments with the same names. For details, see Exposing Properties.

    Note

    It is possible to define a script component in which the get and put property functions have different numbers of arguments, but you cannot create a type library for that script component.

  • If you are exposing events, you cannot use the same dispatch identifiers (dispid) more than once in a script component. Additionally, you cannot use a negative value for the dispid unless it is within a specified range. For details, see Exposing Methods.

  • The ID attributes of elements in the script component must be unique. If you are generating a type library from more than one script component, then the IDs must be unique in the type library as a whole.

See Also

Script Component File Contents
Creating Registration Information
Checking For Errors in Script Component Files
Script Component Files and XML Conformance