Microsoft® Windows® Script Component Creating a Script Component Type Library |
Script Component Tutorial Previous Next |
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.To create a script component type libraryNote For information about using a type library in the host application, refer to the application's documentation.
Generating Type Libraries Dynamically
- In Windows Explorer, right-click the script component .wsc file, and then choose Generate Type Library.
A .tlb file, Scriptlet.tlb, is generated for the script component, 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.
The script component run-time includes an Automation interface implemented by the Scriptlet.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
- In script inside the script component file, create an instance of the Scriptlet.TypeLib object by using syntax such as the following:
set oTL = CreateObject("Scriptlet.TypeLib") ' In VBScript. var oTL = new ActiveXObject("Scriptlet.TypeLib"); // In JScript.
Property/Method | Description |
AddURL | (Method) (Required) Adds the URL of the script component file from which to generate the type library. |
Path | (Property) (Required) The file name for the type library, which can optionally include a path. If the Path property is not set, a type library, Scriptlet.tlb, is created in the same directory as the component. |
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 Scriptlet.TypeLib object will create one, but then the type library will have a different GUID on each machine. |
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. |
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.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
Troubleshooting Type LibrariesIf 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,GenerateTypeLib options URLname
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
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,GenerateTypeLib -name:MyComponentTLib -file:d:\components\MyComponent.tlb -doc:\"Sample component typelib\" -guid:{a1e1e3e0-a252-11d1-9fa1-00a0c90fffc0} -major:1 -minor:0 d:\components\MyComponent.wsc
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.