The following code illustrates the use of the
Run method.
' ########################################################################################
' Microsoft Script Control example.
' 2008 José Roca - Use at your own risk.
' ########################################################################################
' SED_PBWIN ' Use the PBWIN compiler
#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
' ========================================================================================