Previous Page | Next Page

Selecting a Windows Programming Language

Using VBScript


Overview of IOM Interfaces in VBScript

The preceding sections provide many examples of using the IOM interfaces in a full Visual Basic environment (or in a VBA environment like Microsoft Word and Excel).

You can also use the IOM interfaces from a VBScript environment. VBScript is a commonly used scripting language that is available in Active Server Pages (ASP), Dynamic HTML (DHTML), Microsoft Outlook 97 and later, and Windows Scripting Host.

Scripto is an ActiveX DLL that has been developed by SAS in order to provide workarounds for the following common VBScript limitations:

Note:   Scripto is provided with SAS Integration Technologies so that developers who choose to use VBScript can effectively use the SAS automation interfaces. However, Scripto is not specific to SAS and can be used with other automation interfaces.  [cautionend]

The performance of VBScript is often slower than a Visual Basic application for several reasons. First, VBScript provides only late binding. It uses IDispatch instead of v-table calls. Also, VBScript is interpreted, not compiled. In addition, the way that VBScript invokes methods (InvokeMethod) causes additional overhead because all parameters must be copied twice to get them in the proper format.

The slower performance of VBScript is especially evident in the case of safe arrays in which SAS expects to contain the actual type of element in the array (such as string or an enumeration value). VBScript expects it to contain only VARIANTS, which in turn contain the appropriate type of element. InvokeMethod takes care of this conversion. However, it produces an additional copy of the array.

Scripto does not address these performance issues. We recommend that if performance is an issue, consider something other than a scripting language for your implementation. We also recommend that you only use Scripto when calling methods whose signatures require it.

Two components are implemented by the Scripto DLL: Scripto and StreamHelper.


The Scripto Interface: IScripto

IScripto is the single interface to the Scripto component. It provides two methods:

SetInterface IUnknown

The SetInterface method is used to specify the IOM interface that contains the method that you want to invoke. The interface must be set before using InvokeMethod. The interface specified must support IDispatch, and that IDispatch implementation must support type information. If either of these is not true, SetInterface returns E_INVALIDARG(&H80070057). An instance of the Scripto component can handle only a single interface at a time. Although you can create multiple instances of Scripto that handle with a different interface, you typically need only a single instance of Scripto. Switching between interfaces does not consume a significant amount of system resources. SetInterface performs an AddRef function on the interface that is specified. You can release this reference when you are finished with the interface using the following statement:

SetInterface Nothing
Also, when you release IScripto, it releases any interface that Scripto still references. After you set an interface, you can still make calls on it without using Scripto.
Return-Parameter InvokeMethod( methodName, params)

The InvokeMethod method invokes the desired IOM interface method through the Scripto DLL. This method can be invoked only after the interface has been set using SetInterface. The methodName parameter is a string that contains the name of the method to invoke on the interface that has been set. The params parameter is an array whose elements contain the parameters for the method that is to be invoked. The order of the parameters in the array must be in reverse order of how they are specified in the method signature. For example, the first parameter in the method signiture must be the final parameter in the array. The array must have exactly the same number of elements as there are parameters to the method. Otherwise, InvokeMethod returns DISP_E_BADPARAMCOUNT(&H8002000E).

Note:   If the method has a parameter that uses the [retval] modifier, then this parameter is returned as the return value to InvokeMethod and you do not need an element in the params array for it.   [cautionend]

If the method that you invoke returns a value, then InvokeMethod returns the value in the Return-Parameter.

Note:   If the method returns a reference to an object, then be sure to use the Set keyword. For example:

Set obServer = obScripto.InvokeMethod(_
"CreateObjectByServer", ServerDefParms)
  [cautionend]

The StreamHelper Interface

The StreamHelper interface contains three methods that are related to working with the SAS BinaryStream (available through the SAS FileService). These methods are useful only when working with SAS; these methods make calls on IBinaryStream to read and write binary data.

WriteBinaryFile IBinaryStream, fileName

copies the entire contents of the given IBinaryStream into the specified file. This method can be useful for copying a SAS ResultPackage from the SAS server to the machine where the VBScript is running. It can also be used to copy GIF images. The IBinaryStream parameter that is passed in must have already been opened with permission to read.

ReadBinaryFile fileName, IBinaryStream

reads the entire contents of the file into the SAS BinaryStream. This method does the reverse of what WriteBinaryFile does. The IBinaryStream parameter that is passed in must be open for permission to write. Sending binary data to SAS can be useful if you want to include a binary file in a SAS package.

arrayOfBytes ReadBinaryArray (IBinaryStream, numBytes)

reads the specified number of bytes from the IBinaryStream parameter and returns them in the arrayOfBytes array. If the value of the numBytes parameter is 0, then the entire contents of the BinaryStream is returned in the array. The array is a variant containing a safe array of bytes. This array can be passed directly to the Response.BinaryWrite method of Microsoft ASP.


Programming Examples

Previous Page | Next Page | Top of Page