Results 1 to 16 of 16

Thread: cActiveScript (vbRichClient5) vs ScriptControl (msscript.ocx)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2017
    Posts
    19

    cActiveScript (vbRichClient5) vs ScriptControl (msscript.ocx)

    Hi all

    I tried to search in google and in these forums a little more information about cActiveScript class in vbRichClient5. I don't even know if I am allowed to ask about this, if it is not the case I apologize for that

    I am developing an application that lets the user write some code in vbScript, JScript or python and accessing the application objects using msscript.ocx

    I am using now Olaf's vbRichClient5 and I have seen this cActiveScript class. I just wanted to know if any of you have used it or have a little more info about its advantages vs the usual msscript.ocx

    I am missing little features as the last error Object (with more info then the LastErrString) or the Procedures collection

    Thank you!

  2. #2
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: cActiveScript (vbRichClient5) vs ScriptControl (msscript.ocx)

    Quote Originally Posted by nicopeis View Post
    I don't even know if I am allowed to ask about this, if it is not the case I apologize for that
    Don't let the opinions of a few Forum-Users intimidate you...

    There's tons of questions about e.g. Crystal-Reports in this Forum (which is PayWare) -
    whereas vbRichClient5 is a free obtainable COM-lib, and the only unicode-aware Class-Framework we have in the Community,
    which is "as complete" and comes with such stable and well-tested Class-interfaces (so that code-examples will work for everyone in the same way).

    Quote Originally Posted by nicopeis View Post
    I am using now Olaf's vbRichClient5 and I have seen this cActiveScript class.
    I just wanted to know if any of you have used it or have a little more info about its advantages vs the usual msscript.ocx
    Well, it surely does offer a few advantages over the msscript.ocx as e.g.:
    - full coverage of all VB6-Enums and Constants within VBScript-Code
    - full coverage of all vbRichClient5 Enums and Constants within VBScript-Code
    - global "visibility" of the RC5 Main-entry-Objects (New_c and Cairo, from which you can instantiate all other RC5-Classes) (that's available also in JScript for example)
    - a Set of AddOn-Functions for more VB6-compatibility (as e.g. IIf, VarPtr, StrPtr, ObjPtr, Val, Str, StrConv, Format, Environ, Beep, DoEvents_)
    - Event-Support inside VBScript- defined Classes
    - Event-Support inside normal VBScript-Code
    - Ability to call Win32-API-functions from within ScriptCode (due to New_c being available inside the Script)
    - Support for the (quite a lot faster) JScript9-engine (on machines which have an IE-version > 9 installed)

    I wrote it mainly, to be able to implement DB-stored "scripted RC5-Forms" (which change often on the customer-side, to be able to change them without recompiling the App-Binaries)

    Quote Originally Posted by nicopeis View Post
    I am missing little features as the last error Object (with more info then the LastErrString) or the Procedures collection
    A few more Error-Infos can be obtained, when you declare the cActiveScript-Variable WithEvents
    (the Error-Event will hand out also the Line- and Col-Numbers then).

    Though the "Modules" and "Procedures"-Collections you will have to implement yourself (which shouldn't be a larger problem IMO).

    Below comes an (only *.bas-Module Sub Main() based) example,
    how to use such Scripts in conjunction with the "globally visible" New_c and Cairo-EntryPoints (which one already knows from "normal RC5+VB6-examples"):

    Code:
    Option Explicit
    
    Sub Main()
      Dim SC As cActiveScript, SB As cStringBuilder
      Set SC = New_c.ActiveScript
      Set SB = New_c.StringBuilder
      
      'first we add Code for a VBScript-Class (a simple Cairo-Widget)
        SB.AppendNL "Class cwMyWidget"
        SB.AppendNL "  Dim W"
      
        SB.AppendNL "  Sub Class_Initialize()"
        SB.AppendNL "     Set W = Cairo.WidgetBase"
        SB.AppendNL "         W.Moveable = True"
        SB.AppendNL "     AddEventSinkOn Me, 'W_Paint', 6" '<- the ParamCount of the Event-Handler needs to be passed
        SB.AppendNL "  End Sub"
        
        SB.AppendNL "  Property Get Widget():  Set Widget = W:          End Property"
        SB.AppendNL "  Property Get Widgets(): Set Widgets = W.Widgets: End Property"
      
        SB.AppendNL "  Sub W_Paint(ByVal CC, ByVal x, ByVal y, ByVal dx, ByVal dy, ByVal UserObj)"
        SB.AppendNL "      CC.Paint 1, Cairo.CreateSolidPatternLng(vbMagenta, 0.3)"
        SB.AppendNL "      S = 'Hello World' & vbCrLf & 'from ' & W.Key & vbCrLf & '(move me around)'"
        SB.AppendNL "      CC.DrawText 0, 0, dx, dy, CStr(S), False, vbCenter, 4, True"
        SB.AppendNL "  End Sub"
        SB.AppendNL "End Class"
     
      'finally we add the Main-Code (using a Cairo-Form, two Widget-Class-Instances from the code above, and a RC5-cTimer)
        SB.AppendNL "Set Form = Cairo.WidgetForms.Create(vbSizable, 'Form-Caption', True, 480, 320)"
        SB.AppendNL "    Form.Widgets.Add New cwMyWidget, 'MyWidget1', 10, 10, 150, 150"
        SB.AppendNL "    Form.Widgets.Add New cwMyWidget, 'MyWidget2', 60, 60, 150, 150"
        SB.AppendNL "Set Timer1 = New_c.Timer(600, True): AddEventSupportFor 'Timer1'"
      
        SB.AppendNL "Sub Timer1_Timer()"
        SB.AppendNL "    Form.Caption= 'Form-Caption: ' & Now"
        SB.AppendNL "End Sub"
        
        SB.AppendNL "Form.Show vbModal"
          
      'the main-code (since it is not wrapped in a Sub-Routine) is now immediately executed "On Add"
      SC.AddCode Replace(SB.ToString, "'", """") '<- replace the single-quotes we've used in above code-defs with the needed double-quotes
      
      'we reach this point only, after the (modal) CairoForm we created in the Script above was closed...
      Set SB = Nothing
      Set SC = Nothing
      New_c.CleanupRichClientDll
    End Sub
    The above ScriptCode produces the following:



    HTH

    Olaf

  3. #3
    Fanatic Member
    Join Date
    Sep 2010
    Location
    Italy
    Posts
    678

    Re: cActiveScript (vbRichClient5) vs ScriptControl (msscript.ocx)

    I'm unfamiliar with vbscript, and I need help
    Since I use RC5 I prefer solutions in this area.
    What I would like to do:
    Run a script that has an array of Singles for Input and an array of Singles for output.
    - How to start?
    - is there any other example to learn from regarding cActiveScript (vbRichClient5)?

  4. #4
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: cActiveScript (vbRichClient5) vs ScriptControl (msscript.ocx)

    do you have qq,let's study together,our qq group (vb7)id:1032313876
    down qq app from App Store。
    https://im.qq.com/immobile/index.html

  5. #5
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: cActiveScript (vbRichClient5) vs ScriptControl (msscript.ocx)

    Quote Originally Posted by reexre View Post
    I'm unfamiliar with vbscript, and I need help
    Since I use RC5 I prefer solutions in this area.
    What I would like to do:
    Run a script that has an array of Singles for Input and an array of Singles for output.
    - How to start?
    - is there any other example to learn from regarding cActiveScript (vbRichClient5)?
    cActiveScript is quite compatible to the MS-ScriptControl -
    for simple "Plugin-like" routines, you will only need .AddCode - and .Run.

    And VBScript is basically like "VB6-Code, reduced to Variants only"
    (so you can develop your Test-Scripts in the VB6-IDE and then just "copy the routine as String-Block into the ScriptControl")

    The (mainly speed-related) problem with your requirement is,
    that VBScript does *really* only support Variants (not Singles directly, nor "Arrays of Singles").

    You can work around that, by either using a (redimed and filled at the VB6-side) Variant-(2D)-Array wich takes up your Singles ,
    or by using the cArrayList (of SubType vbSingle), to save a bit more space with the Array-allocation (then passing the ObjInstance via Variant to the Script).

    Here is an example for a ScriptRoutine, which does "GreyScale-conversion" on a cArrayList(of type Single).
    Code:
    '*** VBScript-ImageProcessing-Routine, passing Floats in a cArrayList (only Variants are allowed)
    '*** This will also work unchanged as a VB6-Routine in the IDE (where I've tested it first)
    Sub ProcessPixels(FloatsPerPxl, ImgW, ImgH, Floats)
      Dim i, Grey
      For i = 0 To Floats.Count - 1 Step 4
          Grey = Floats(i) * 0.114 + Floats(i + 1) * 0.587 + Floats(i + 2) * 0.299
          Floats(i + 0) = Grey
          Floats(i + 1) = Grey
          Floats(i + 2) = Grey
         'Floats(i + 3) = ... we leave the Alpha-Channel unchanged in this Routine
      Next
    End Sub
    Ok, here the complete example:
    Code:
    Private Sub Form_Load()
      'we need a (filled) Cairo-Surface and a cArrayList-instance of type vbSingle
      Dim Srf As cCairoSurface, Floats As cArrayList
      Set Srf = Cairo.ImageList.AddIconFromResourceFile("", "shell32", 167)
      Set Floats = New_c.ArrayList(vbSingle)
     
      Dim Pxl() As Byte, y As Long, x As Long, i As Long
     
      'convert all BGRA-bytes into Singles (stuffing them into our Floats cArrayList)
      Srf.BindToArray Pxl
      For y = 0 To UBound(Pxl, 2): For x = 0 To UBound(Pxl, 1)
          Floats.Add CSng(Pxl(x, y) / 255!)
      Next x, y
         
      Dim SB As cStringBuilder 'now the Script-String-construction
      Set SB = New_c.StringBuilder
          SB.AppendNL "Sub ProcessPixels(FloatsPerPxl, ImgW, ImgH, Floats)"
          SB.AppendNL "  Dim i, Grey"
          SB.AppendNL "  For i = 0 To Floats.Count - 1 Step 4"
          SB.AppendNL "    Grey = Floats(i) * 0.114 + Floats(i + 1) * 0.587 + Floats(i + 2) * 0.299"
          SB.AppendNL "    Floats(i + 0) = Grey"
          SB.AppendNL "    Floats(i + 1) = Grey"
          SB.AppendNL "    Floats(i + 2) = Grey"
          SB.AppendNL "    'Floats(i + 3) = ... we leave the Alpha-Channel unchanged in this Routine"
          SB.AppendNL "  Next"
          SB.AppendNL "End Sub"
      
      With New_c.ActiveScript 'and finally "hanging it in" and executing it
           .AddCode SB.ToString
           .CodeObject.ProcessPixels 4, Srf.Width, Srf.Height, Floats
      End With
      
      'play the converted Pixels back from cArrayList-Singles to their BGRA-Slots
      For y = 0 To UBound(Pxl, 2): For x = 0 To UBound(Pxl, 1)
          Pxl(x, y) = Floats(i) * 255: i = i + 1
      Next x, y
      Srf.ReleaseArray Pxl 'now we can release the virtual (2D)-ByteArray
        
      Set Me.Picture = Srf.Picture 'and show the result
    End Sub
    The speed would be quite bad though (on larger Images) -
    mainly due to accessing every single ColorValue of a Pixel via a Method-Call.

    A bit faster perhaps would be a passed (2D) VariantArray I think, but then each transported Color-Value
    would not only take up 4Bytes (as in a cArrayList of type Single), but 16Bytes (as a Float, wrapped in a Variant).

    In case this is for "Power-Users of your Tool, who want to play around with a new experimental algo in a Preview",
    you should perhaps make sure, to give them only Images in a somewhat reduced size as Input...

    More speed would come from "true C-Scripting" (e.g. via the tinyCC-library) -
    but then you would not get a simple (catchable) COM-Error back from a "messed up Script",
    but risk "crashing the whole Host-App" in case "some Pointer doesn't sit right in the C-Script".


    HTH

    Olaf

  6. #6
    Fanatic Member
    Join Date
    Sep 2010
    Location
    Italy
    Posts
    678

    Re: cActiveScript (vbRichClient5) vs ScriptControl (msscript.ocx)

    thanks Olaf, nice example.
    Yes, I wanted to add the possibility of running a script directly in the image processing.

    You can work around that, by either using a (redimed and filled at the VB6-side) Variant- (2D) -Array wich takes up your Singles,
    Exactly
    Although the variants consumes a lot more memory I prefer this solution.
    2D array of Variants which are previously filled
    Because otherwise the operation is done in single pixel. (per-pixel) Or, to put it better, adjacent pixels cannot also be (easly) used as inputs. (Y + 1 for example) (and that's what I want)

    [ The "Per pixel" mode is Already implemented (in a different manner) in my Program in modules: Formula1, Formula2, Formula3 and FormulaRGB. Yes they are quite slow, to see how they work you can select one of them clicking "Add FX-Module" and then Check "Filter by ..." below Project-List. ]

    Here's how I proceeded, but without success.

    -Use (for the moment) only one channel.
    -Use 2D array of Varaints

    I already have a 2D array of Singles available which contains the input with range 0-1
    With 2 loops I assign it to a 2D Variant

    Code:
        Dim SC        As cActiveScript
        Dim SB As cStringBuilder, CO As Object
        Dim InputVariant() As Variant
        Dim OutputVariant() As Variant
        Set SB = New_c.StringBuilder   
    
        W = UBound(V1, 1)
        H = UBound(V1, 2)
        ReDim InputVariant(W, H)
        ReDim OutputVariant(W, H)
    
         For X = 0 To W: For Y = 0 To H
                 InputVariant (X, Y) = V1 (X, Y)
         Next: Next
    I run the script

    Code:
        Set SC = New_c.ActiveScript
    
        SB.AppendNL "Sub Process(W, H, INP(),ByRef OUT())"
        SB.AppendNL "Dim X, Y"
        SB.AppendNL "For X = 0 to W"
        SB.AppendNL " For Y = 0 to H"
        SB.AppendNL "  OUT(X, Y) = INP(X, Y)"   ' Just for test
        SB.AppendNL " Next"
        SB.AppendNL "Next"
        SB.AppendNL "End Sub"
    
        With SC
            .AddCode SB.ToString
            If Len(.LastErrString) Then
                MsgBox .LastErrString, vbExclamation
            Else
                .CodeObject.Process W, H, InputVariant, OutputVariant
            End If
        End With
    And I convert the variant output back to singles
    Code:
        For X = 0 To W: For Y = 0 To H
                OV1(X, Y) = OutputVariant(X, Y)
        Next: Next
    but unfortunately OutputVariant (X, Y) is empty

    What am I doing wrong ?

    EDIT:
    this also doesn't work
    Code:
      SB.AppendNL "Sub Process(W, H, INP, OUT)"
    I get NO error

  7. #7
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: cActiveScript (vbRichClient5) vs ScriptControl (msscript.ocx)

    Quote Originally Posted by reexre View Post
    ...unfortunately OutputVariant (X, Y) is empty

    What am I doing wrong ?
    You will have to pass "plain Variant-Variables" for ByRef-transport to work (and without Array-decorators).

    Also your For-loops are not right (they need to go from 0 to W - 1, 0 to H -1 respectively)...

    Here again the Grey-Conversion (now factor 10 faster, based on "separated Channels",
    each in its own 2D-VariantArray).

    Code:
    Option Explicit
    
    Private Sub Form_Click()
      'we need a (filled) Cairo-Surface and a cArrayList-instance of type vbSingle
      Dim Srf As cCairoSurface, Floats As cArrayList
      Set Srf = Cairo.ImageList.AddIconFromResourceFile("", "shell32", 167)
    
      'convert all BGRA-bytes into separate 2D-VariantArrays (one per channel)
      Dim B: GetColorChannel Srf, 0, B
      Dim G: GetColorChannel Srf, 1, G
      Dim R: GetColorChannel Srf, 2, R
    
      Dim SB As cStringBuilder 'now the Script-String-construction
      Set SB = New_c.StringBuilder
          SB.AppendNL "Sub Process(W, H, B, G, R)"
          SB.AppendNL "  Dim y, x, Grey"
          SB.AppendNL "  For y = 0 To H - 1: For x = 0 To W - 1"
          SB.AppendNL "    Grey = B(x, y) * 0.114 + G(x, y) * 0.587 + R(x, y) * 0.299"
          SB.AppendNL "    B(x, y) = Grey"
          SB.AppendNL "    G(x, y) = Grey"
          SB.AppendNL "    R(x, y) = Grey"
          SB.AppendNL "  Next: Next"
          SB.AppendNL "End Sub"
    
      With New_c.ActiveScript 'and finally "hanging it in" and executing it
           New_c.Timing True
          .AddCode SB.ToString
          .CodeObject.Process Srf.Width, Srf.Height, B, G, R
          
    '      Process Srf.Width, Srf.Height, B, G, R
      End With
      
      'play the converted Pixels back from the Channel-Arrays to their BGRA-Slots in the Srf
      SetColorChannel Srf, 0, B
      SetColorChannel Srf, 1, G
      SetColorChannel Srf, 2, R
      
      Caption = New_c.Timing
      Set Me.Picture = Srf.Picture 'and show the result
    End Sub
    
    Sub GetColorChannel(Srf As cCairoSurface, ByVal ChnZeroBased As Long, V2D)
      Dim B() As Byte, y As Long, x As Long
      If Srf.BindToArray(B) Then ReDim V2D(0 To Srf.Width - 1, 0 To Srf.Height - 1) Else Exit Sub
         For y = 0 To UBound(B, 2): For x = ChnZeroBased To UBound(B) Step 4
          V2D(x \ 4, y) = B(x, y) / 255
         Next x, y
      Srf.ReleaseArray B
    End Sub
    Sub SetColorChannel(Srf As cCairoSurface, ByVal ChnZeroBased As Long, V2D)
      Dim B() As Byte, y As Long, x As Long
      If UBound(V2D, 1) <> Srf.Width - 1 Or UBound(V2D, 2) <> Srf.Height - 1 Then Err.Raise vbObjectError, , "Dimensions don't match"
      If Not Srf.BindToArray(B) Then Exit Sub
         For y = 0 To UBound(B, 2): For x = ChnZeroBased To UBound(B) Step 4
            B(x, y) = V2D(x \ 4, y) * 255
         Next x, y
      Srf.ReleaseArray B
    End Sub
    
    '*** VBScript-ImageProcessing-Routine (only Variants are allowed)
    '*** This will also work unchanged as a VB6-Routine in the IDE (where I've tested it first)
    Sub Process(W, H, B, G, R)
      Dim y, x, Grey
      For y = 0 To H - 1: For x = 0 To W - 1
          Grey = B(x, y) * 0.114 + G(x, y) * 0.587 + R(x, y) * 0.299
          B(x, y) = Grey
          G(x, y) = Grey
          R(x, y) = Grey
      Next: Next
    End Sub
    As for "full error-support" in cActiveScript:
    - one should declare (and instantiate) it at Class-Level for that -> WithEvents
    - many Errors will be detected already in the "parsing-phase" (when .AddCode is called)
    - and then thrown at you (including Line- and Char-Number) in the appropriate Event

    HTH

    Olaf
    Last edited by Schmidt; Feb 21st, 2020 at 04:11 AM.

  8. #8
    Fanatic Member
    Join Date
    Sep 2010
    Location
    Italy
    Posts
    678

    Re: cActiveScript (vbRichClient5) vs ScriptControl (msscript.ocx)

    Thank you Olaf !
    For now resolved
    My only error was
    Code:
        Dim InputVariant() As Variant
        Dim OutputVariant() As Variant
    That Corrected is
    Code:
        Dim InputVariant As Variant
        Dim OutputVariant As Variant
    Maybe in future I'll ask more details about "full error-support"

  9. #9
    Fanatic Member
    Join Date
    Sep 2010
    Location
    Italy
    Posts
    678

    Re: cActiveScript (vbRichClient5) vs ScriptControl (msscript.ocx)

    If someone is interested to see it working I implemented it in PhotoModularFX ( see my signature to download )
    It is in 2 new modules : Script11 and Script33.
    To Test, you can select one of them clicking "Add FX-Module" , Check "Filter by ..." (below the Project-List) and "Load and Run" one of the Projects.

  10. #10
    New Member
    Join Date
    Feb 2020
    Posts
    13

    Re: cActiveScript (vbRichClient5) vs ScriptControl (msscript.ocx)

    Hi Olaf.
    Would it be possible to use the JSON object in cActiveScript?

    According to MSDN documentation:

    Starting with JScript 5.8, by default, the JScript scripting engine supports the language feature set as it existed in version 5.7. This is to maintain compatibility with the earlier versions of the engine. To use the complete language feature set of version 5.8, the Windows Script interface host has to invoke IActiveScriptProperty::SetProperty.

    Thanks!

  11. #11
    New Member
    Join Date
    Feb 2020
    Posts
    13

    Re: cActiveScript (vbRichClient5) vs ScriptControl (msscript.ocx)

    Hi Olaf.
    Would it be possible to use the JSON object in cActiveScript?

    According to MSDN documentation:

    Starting with JScript 5.8, by default, the JScript scripting engine supports the language feature set as it existed in version 5.7. This is to maintain compatibility with the earlier versions of the engine. To use the complete language feature set of version 5.8, the Windows Script interface host has to invoke IActiveScriptProperty::SetProperty.

    Thanks!

  12. #12
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: cActiveScript (vbRichClient5) vs ScriptControl (msscript.ocx)

    Quote Originally Posted by paliadoyo View Post
    Would it be possible to use the JSON object in cActiveScript?

    According to MSDN documentation:

    Starting with JScript 5.8, by default, the JScript scripting engine supports the language feature set as it existed in version 5.7. This is to maintain compatibility with the earlier versions of the engine. To use the complete language feature set of version 5.8, the Windows Script interface host has to invoke IActiveScriptProperty::SetProperty.
    Ok, implemented this version-switch now (for the "JScript9"-mode of the engine) -
    this new RC5-version 5.0.76 is available in the BaseDlls-package at the usual place...

    So the following code should (after registering of RC5-version 5.0.76), properly report a JSON-serialized String:
    Code:
    Option Explicit
    
    Private WithEvents SC As cActiveScript
     
    Private Sub Form_Load()
      Set SC = New_c.ActiveScript("JScript9", False, False)
          SC.AddCode "function Test(){var o={}; o.SomeProp=123; return JSON.stringify(o)}"
      MsgBox SC.Run("Test")
    End Sub
     
    Private Sub SC_error(Description As String, ByVal LineNr As Long, ByVal CharPos As Long)
      Debug.Print Description, LineNr, CharPos
    End Sub
    Olaf

  13. #13
    New Member
    Join Date
    Feb 2020
    Posts
    13

    Re: cActiveScript (vbRichClient5) vs ScriptControl (msscript.ocx)

    No words...
    Fantastic support!

    Many thanks

  14. #14
    PowerPoster
    Join Date
    Sep 2012
    Posts
    2,083

    Re: cActiveScript (vbRichClient5) vs ScriptControl (msscript.ocx)

    Quote Originally Posted by Schmidt View Post
    Ok, implemented this version-switch now (for the "JScript9"-mode of the engine) -
    this new RC5-version 5.0.76 is available in the BaseDlls-package at the usual place...

    So the following code should (after registering of RC5-version 5.0.76), properly report a JSON-serialized String:
    Code:
    Option Explicit
    
    Private WithEvents SC As cActiveScript
     
    Private Sub Form_Load()
      Set SC = New_c.ActiveScript("JScript9", False, False)
          SC.AddCode "function Test(){var o={}; o.SomeProp=123; return JSON.stringify(o)}"
      MsgBox SC.Run("Test")
    End Sub
     
    Private Sub SC_error(Description As String, ByVal LineNr As Long, ByVal CharPos As Long)
      Debug.Print Description, LineNr, CharPos
    End Sub
    Olaf
    Hi Olaf, Could RC5.ActiveScript return a string array directly from JScript.String.split, or a collection of Match objects directly from JScript.RegExp.exec? Thanks.

  15. #15
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: cActiveScript (vbRichClient5) vs ScriptControl (msscript.ocx)

    Quote Originally Posted by dreammanor View Post
    Hi Olaf, Could RC5.ActiveScript return a string array directly from JScript.String.split, or a collection of Match objects directly from JScript.RegExp.exec? Thanks.
    It could (if you set the return-value to a VB-Variable, typed As Object).

    But these LateBound-ObjVars (which have a TypeName of JScriptTypeInfo)
    are "clumsy" (and slow) to deal with...

    If it's StringArrays you want to return, then it'd be much faster to return a "plain value-type" (a String) from the JScript-side -
    (for complex - and nested Objects, or Arrays of Objects, such a returned String could be a JSON.stringify-result).

    Code:
    Option Explicit
    
    Private WithEvents SC As cActiveScript
     
    Private Sub Form_Load()
      Set SC = New_c.ActiveScript("JScript9", False, False)
          'SC.AddCode "function Test(){var o={}; o.SomeProp=123; return JSON.stringify(o)}"
          SC.AddCode "function GetMatches1(sInput, sRegExp){ return sInput.match(new RegExp(sRegExp,'g')) }"
          SC.AddCode "function GetMatches2(sInput, sRegExp){ return sInput.match(new RegExp(sRegExp,'g')).join('<|>') }"
    End Sub
    
    Private Sub Form_Click()
      Cls
      Dim sArr1() As String, sArr2() As String, i As Long
      
      New_c.Timing True
          sArr1 = GetMatches1("fox, mox, box, sox, rox, tox", "\w(ox)")
      Print New_c.Timing,
      
      New_c.Timing True
          sArr2 = GetMatches2("fox, mox, box, sox, rox, tox", "\w(ox)")
      Print ","; New_c.Timing
      
      For i = 0 To UBound(sArr1)
        Print "sArr1(" & i & ")="; sArr1(i), ", sArr2(" & i & ")="; sArr2(i)
      Next
    End Sub
    
    Function GetMatches1(sInput As String, sRegExp As String) As String()
      Dim ResArr As Object, i As Long
      Set ResArr = SC.CodeObject.GetMatches1(sInput, sRegExp) 'retrieve an Arr-Obj of type: JScriptTypeInfo
      ReDim sArr(0 To ResArr.length - 1) As String 'allocate a VB-String-Array in a matching size
      For i = 0 To UBound(sArr)
        sArr(i) = CallByName(ResArr, i, VbGet) 'and copy the single strings over by a Latebound-MethodCall
      Next
      GetMatches1 = sArr
    End Function
    
    Function GetMatches2(sInput As String, sRegExp As String) As String()
      GetMatches2 = Split(SC.CodeObject.GetMatches2(sInput, sRegExp), "<|>") 'much simpler (and faster)
    End Function
    
    Private Sub SC_error(Description As String, ByVal LineNr As Long, ByVal CharPos As Long)
      Debug.Print Description, LineNr, CharPos
    End Sub
    The above code shows, that GetMatches2() is much faster than GetMatches1()...

    HTH

    Olaf

  16. #16
    PowerPoster
    Join Date
    Sep 2012
    Posts
    2,083

    Re: cActiveScript (vbRichClient5) vs ScriptControl (msscript.ocx)

    Wonderful. Thank you very much, Olaf.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width