|
















|
Microsoft® Windows® Script Components provide you with an easy way to
create COM components using scripting languages such as Microsoft® Visual
Basic® Scripting Edition (VBScript) and Microsoft® JScript®. Use script
components as COM components in applications such as Microsoft® Internet
Information Services (IIS), Microsoft® Windows® Script Host, and any other
application that can support COM components.
TB_SCR56.DLL is a collection of wrapper functions that allow
you to use these script components in procedural form with the PowerBASIC
compilers. It wrappes SCRRUN.DLL (Microsoft Scripting Runtime),
VBSCRIPT.DLL (Microsoft VBScript Regular Expressions), WSHCON.DLL
(WSHController Library), WSHEXT.DLL (Script Signer), WSHOM.OCX (Windows
Script Host Object Model), several scriptable objects contained in
SHELL32.DLL, and MSSCRIPT.OCX (Microsoft Script Control).
Microsoft Script Control
Overview
The Microsoft Script Control allows the user to write and run scripts for
any scripting engine, such as VBScript or JScript, both of which are
included with the Script Control. You can add the run-time functionality
of any Automation object that exposes methods and properties to the Script
Control. By combining the run-time functionality of an application with a
scripting engine, you can create a scripting application that runs macros
to control the application.
The help file, called MSSCRIPT.HLP, is located in the C:\WINDOWS\SYSTEM32
folder.
Select a Scripting Language
The first step is to configure the Script Control for the correct
scripting language.
| |
IScriptControl_SetLanguage lpSc, "VBScript" |
Other scripting languages can be used by the Script Control.
Add Code to a Procedure
Before you can run the DivideByZero procedure, use the AddCode method to
add the complete procedure to the Script Control. If you try to add an
incomplete procedure (one with no End Sub or End Function), an error will
occur. The following example adds procedure code to the Script Control:
| |
strScript = "Sub DivideByZero(vPrms)" & $CRLF & _
" Dim prime" & $CRLF & _
" prime = 3" & $CRLF & _
" MsgBox prime/0" & $CRLF & _
"End Sub"
IScriptControl_AddCode lpSc, StrScript
|
You can add arguments to a procedure or function.
| |
strScript =
"Sub Multiply(vPrms)" & $CRLF & _
" Dim result" & $CRLF & _
" result = vPrms(0) * vPrms(1)"
& $CRLF & _
" MsgBox result" & $CRLF & _
"End Sub"
IScriptControl_AddCode lpSc, StrScript
Dim vParameters As Variant
Dim vParams(1) As Variant
vParams(0) = 20 As Long
vParams(1) = 6 As Long
vParameters = vParams()
|
Once code has been added using the AddCode method, you can then use the
Run method to run the procedure.
| |
' Run the script
IScriptControl_Run lpSc, "Multiply",
vParameters, vRes |
Executing Scripting Statements
To execute scripting code, the Script Control features the
ExecuteStatement method. For example, the following code executes a MsgBox
statement:
| |
' Create a message box with the word Hello in it.
IScriptControl_ExecuteStatement lpSc, "MsgBox
""Hello"""
|
Evaluating Scripting Statements
You can also evaluate lines of scripting code using the Eval method. The
Eval method simply tests a line of scripting code, as shown in the
following example:
| |
IScriptControl_ExecuteStatement lpSc, "x = 100"
IScriptControl_Eval lpSc, "x = 100", vRes
MsgBox Str$(CInt(Variant#(vRes))) ' -1 = True
IScriptControl_Eval lpSc, "x = 100/2", vRes
MsgBox Str$(CInt(Variant#(vRes))) ' 0 = False
|
Creating an Instance of the Script Control
| |
lpSc = ScriptingCreateObject ("MSScriptControl.ScriptControl")
|
It returns the address of a pointer to the ScriptControl object that will
be passed as the first parameter of all the methods and procedures of this
object.
Using the AllowUI Property
The AllowUI property determines if the scripting engine is permitted to
display user interface elements. This can apply to the Script Control
itself, such as when it displays timeout messages. It can also apply to
scripting engines that use ActiveX scripting interfaces.
| |
IScriptControl_SetAllowUI lpSc, -1 |
Using the Error Property
With the Script Control, errors may come from two sources: the Script
Control itself, or the script the Control is attempting to run. In the
latter case, to debug scripting code, the Script Control features the
Error property, which returns a reference to the Error object. Using the
Error object, the Script Control can return the number and description of
the error, and also the the line number where the error occurs in the
script.
TB_SCR56 provides a wrapper function that calls the Error object and
returns an string containing rich error information. Call it as follows:
| |
If ScriptingError Then
MsgBox MSScriptControl_GetErrorInfo (lpSc, ScriptingResult)
End If |
You can
take this function as a template to build your own localized function.
|
|