Author Topic: IScriptControl.AddCode Method  (Read 3908 times)

0 Members and 1 Guest are viewing this topic.

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • User-Rate: +209/-0
IScriptControl.AddCode Method
« on: July 15, 2008, 12:40:15 AM »

The following code illustrates the use of the AddCode method.

Code: [Select]
' ########################################################################################
' 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 = "Sub Multiply(vPrm1, vPrm2)" & $CRLF & _
                  "   Dim result" & $CRLF & _
                  "   result = vPrm1 * vPrm2" & $CRLF & _
                  "   MsgBox result" & $CRLF & _
                  "End Sub"
      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)
   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
' ========================================================================================
« Last Edit: August 07, 2011, 08:02:55 AM by José Roca »