• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

Microsoft Script Control Overview

Started by José Roca, July 15, 2008, 12:19:26 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca

Microsoft Script Control Overview

Microsoft Script Control Overview

 

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.

 

Select a Scripting Language

 

The first step is to configure the Script Control for the correct scripting language.

 

pIScriptControl.Language = UCODE$("VBScript")

 

Other scripting languages can be used by the Script Control.

 

Add Code to a Procedure

 

Before you can run the MyTest function, use the AddCode method to add the complete function 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 = "Function MyTest" & $CRLF & _

            "  MyTest = ""Hello this is a test""" & $CRLF & _

            "End Function"

pIScriptControl.AddCode UCODE$(StrScript)

 

You can add arguments to a procedure or function.

 

strScript = "Sub Multiply(vPrm1, vPrm2)" & $CRLF & _

            "   Dim result" & $CRLF & _

            "   result = vPrm1 * vPrm2" & $CRLF & _

            "   MsgBox result" & $CRLF & _

            "End Sub"

pIScriptControl.AddCode UCODE$(StrScript)

 

To pass the parameters, you need to use a safe array.

 

DIM rgsabound AS SAFEARRAYBOUND

DIM psa AS DWORD

DIM vPrm AS VARIANT

DIM ix AS LONG

 

' Create a SafeArray with two elements

rgsabound.lLBound = 0

rgsabound.cElements = 2

psa = SafeArrayCreate(%VT_VARIANT, 1, rgsabound)

ix = 0 : vPrm = 20

SafeArrayPutElement(psa, ix, vPrm))

ix = 1 : vPrm = 6

SafeArrayPutElement(psa, ix, vPrm))

 

Once code has been added using the AddCode method, you can use the Run method to run it.

 

' Run the script

vRes = pIScriptControl.Run(UCODE$("Multiply"), psa)

 

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.

pIScriptControl.ExecuteStatement UCODE$("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:

 

pIScriptControl.ExecuteStatement UCODE$("x = 100")

vRes = pIScriptControl.Eval(UCODE$("x = 100"))

MSGBOX STR$(CINT(VARIANT#(vRes)))  ' -1 = True

vRes = pIScriptControl.Eval(UCODE$("x = 100/2"))

MSGBOX STR$(CINT(VARIANT#(vRes)))  ' 0 = False

 

Creating an Instance of the Script Control

 

pIScriptControl = NEWCOM "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.

 

pIScriptControl.AllowUI = %VARIANT_TRUE

 

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.

 

The file MSSCRIPT.INC includes a wrapper function called MSScriptControl_GetErrorInfo that calls the Error object and returns an string containing rich error information. You can call it as follows:

 

IF OBJRESULT THEN

  MSGBOX MSScriptControl_GetErrorInfo(pIScriptControl, OBJRESULT)

END IF

 

You can use this function as a template to build your own localized function.



José Roca

 
The following example creates an instance of the Script Control, selects VBScript as the scripting language, adds an script and runs it.


#COMPILE EXE
#DIM ALL
#INCLUDE "MSSCRIPT.INC"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pSc AS IScriptControl
   LOCAL strScript AS STRING
   LOCAL vRes AS VARIANT
   LOCAL rgsabound AS SAFEARRAYBOUND
   LOCAL psa AS DWORD
   LOCAL vPrm AS VARIANT
   LOCAL ix AS LONG

   ' Creates an instance of the Microsoft Script Control
   pSc = NEWCOM "MSScriptControl.ScriptControl"
   IF ISNOTHING(pSc) THEN
      MSGBOX "Error creating an instance of the Microsoft Script Control"
      EXIT FUNCTION
   END IF

   TRY
      ' Set the language. It can be "VBScript" or "JScript"
      pSc.Language = UCODE$("VBScript")
      ' Make an script.
      strScript = "Function Multiply(vPrm1, vPrm2)" & $CRLF & _
                  "   Dim result" & $CRLF & _
                  "   result = vPrm1 * vPrm2" & $CRLF & _
                  "   Multiply = result" & $CRLF & _
                  "End Function"
      pSc.AddCode UCODE$(StrScript)
      ' Create a SafeArray with two elements
      rgsabound.lLBound = 0
      rgsabound.cElements = 2
      psa = SafeArrayCreate(%VT_VARIANT, 1, rgsabound)
      ix = 0 : vPrm = 20
      SafeArrayPutElement(psa, ix, vPrm)
      ix = 1 : vPrm = 6
      SafeArrayPutElement(psa, ix, vPrm)
      ' Allow to display user-interface elements
      pSc.AllowUI = %VARIANT_TRUE
      ' Run the script
      vRes = pSc.Run(UCODE$("Multiply"), psa)
      MSGBOX "Result: " & STR$(VARIANT#(vRes))
   CATCH
      MSGBOX MSScriptControl_GetErrorInfo(pSc, OBJRESULT)
   FINALLY
      ' Destroy the safearray
      IF psa THEN SafeArrayDestroy psa
      psa = %NULL
   END TRY

   ' Releases the interface
   pSc = NOTHING

END FUNCTION
' ========================================================================================