Solved

COM MSScriptControl.ScriptControl To CodeDom

Posted on 2011-02-15
13
2,123 Views
Last Modified: 2012-05-11
' I create a COM MSScriptControl.ScriptControl code that work ok, but it's too slow.
        'im trying to convert it to CodeDom Compiler to see if it is faster; but i dont know anything about CodeDom
        'can any one help me convert this code to CodeDom. Thanks
        Dim Code As New System.Text.StringBuilder
        Dim codeToString As String = RichTextBitch.Text
        gvSC = New MSScriptControl.ScriptControl
        With gvSC
            .Language = "VBScript"
            .SitehWnd = Me.Handle.ToInt32
            .AllowUI = True
        End With

        Code.Append(RichTextBitch.Text).AppendLine()
        Code.Append(myfunctionsWrite).AppendLine()

        gvSC.AddObject("Pimp1", class1)
        gvSC.AddObject("MySelf", class2)
        gvSC.AddObject("Pimp3", class3, True)
        gvSC.AddCode(Code.ToString)
        If gvSC.Error.Number <> 0 Then
            'MsgBox(gvSC.Error.Description, MsgBoxStyle.Information, "Processing Script")
        End If
        'My Result are in the classes
0
Comment
Question by:sayeth
  • 6
  • 5
  • 2
13 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
And by the way, in my knowledge, CodeDom only supports VB.NET and C# code not VBScript.
0
 
LVL 1

Author Comment

by:sayeth
Comment Utility
im using VB.NET 2008.
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
>im using VB.NET 2008.
Is that in response to my comment http:#34905682? If so, I was referring to this

With gvSC
            .Language = "VBScript"
0
 
LVL 1

Author Comment

by:sayeth
Comment Utility
oh ok. is there anything i can do to make the above code go faster, for example System.Text.StringBuilder, etc.
since, that ScriptControl is the fasted approach

thanks for the help
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
Does the speed depend on the code? Did you try it with different code?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Author Comment

by:sayeth
Comment Utility
yes n no; It  depend on the array of information it searching through while the  Script is running.
for ex. dim infoDepth(2000) as string is fast
             dim infoDepth(3000) as string is ok
              dim infoDepth(4000) as string is slow
infoDepth is a class that i add to the Script =  gvSC.AddObject("MySelf", class2)
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
So it seems that the problem is with script itself. Can you show us the script?
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Here is a class that I use to evaluate expressions, like the script control does.  It uses CodeDOM, and is inspired by a CodeProject article.


' Source: 
' http://www.codeproject.com/csharp/runtime_eval.asp 
' 
' VB.NET 2005 specific version converted from C# code. 

Imports Microsoft.VisualBasic
Imports System
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports System.Collections.Generic
Imports System.Reflection
Imports System.Text

Public Class ExpressionEvaluator

  ' Instance of the compile evaluator engine class. 
  Private _CompiledClass As Object = Nothing

  ' Variable declarations, referenced assembly, using statements, 
  ' and all evaluation expressions should be added before calling the 
  ' CompileEvaluator method. 
  Private _VariableDeclarations As New List(Of String)()
  Private _ReferencedAssemblies As New List(Of String)()
  Private _Imports As New List(Of String)()
  Private _Expressions As New Dictionary(Of String, String)()

  ' Return a list of errors. 
  Private _Errors As New List(Of String)()

  Public Sub New()
    ' Add reference to System.dll 
    _ReferencedAssemblies.Add("System.dll")

    ' Add Imports System
    _Imports.Add("System")
  End Sub

  ''' <summary> 
  ''' Use the variable declarations, referenced assemblies (i.e. System.dll, System.Xml.dll), 
  ''' using statements (i.e. using System;), and expressions to be evaluated, and compile 
  ''' them all into a single class with multiple function methods that will return an evaluation 
  ''' for each expression. 
  ''' </summary> 
  ''' <returns></returns> 
  Public Function CompileEvaluator() As Boolean
    Dim provider As CodeDomProvider = New VBCodeProvider()

    Dim parameters As New CompilerParameters()

    parameters.GenerateExecutable = False
    parameters.GenerateInMemory = True

    ' Add references system as System.Xml.dll. 
    For Each assemblyName As String In Me.ReferencedAssemblies
      parameters.ReferencedAssemblies.Add(assemblyName)
    Next

    Dim code As New StringBuilder()

    ' Add using statements to the top of the module. 
    For Each importItem As String In Me.[Imports]
      code.AppendFormat("Imports {0} " & vbLf & "", importItem.Replace("Imports", "").Trim())
    Next importItem

    ' Create the class definition. 
    code.Append(" Public Class EvaluatorEngine" & vbLf)

    ' Add private variable declarations. 
    For Each declaration As String In Me.VariableDeclarations
      code.AppendFormat(" Private {0}" & vbLf, declaration)
    Next declaration

    ' Build an evaluator method for each expression. 
    For Each key As String In Me.Expressions.Keys
      Dim expression As String = Me.Expressions(key)

      code.AppendFormat(" Public Function Evaluate{0}() As Object" & vbLf, key)
      code.AppendFormat(" Return {0}" & vbLf, expression)
      code.Append(" End Function" & vbLf)
    Next key

    code.Append(" End Class" & vbLf)

    ' Compile the evaluator engine class. 
    Dim results As CompilerResults = provider.CompileAssemblyFromSource(parameters, code.ToString())

    If results.Errors.HasErrors Then
      For Each err As CompilerError In results.Errors
        _Errors.Add(err.ErrorText)
        ' Add each compiler error to a list. 
      Next
    Else
      ' Create an instance of the evaluator engine class. 
      Dim assy As Assembly = results.CompiledAssembly
      _CompiledClass = assy.CreateInstance("EvaluatorEngine")
    End If

    ' Return the compiler success status. 
    Return Not results.Errors.HasErrors
  End Function

  Public ReadOnly Property VariableDeclarations() As List(Of String)
    Get
      Return _VariableDeclarations
    End Get
  End Property

  Public ReadOnly Property ReferencedAssemblies() As List(Of String)
    Get
      Return _ReferencedAssemblies
    End Get
  End Property

  Public ReadOnly Property [Imports]() As List(Of String)
    Get
      Return _Imports
    End Get
  End Property

  Public ReadOnly Property Errors() As List(Of String)
    Get
      Return _Errors
    End Get
  End Property

  Public ReadOnly Property Expressions() As Dictionary(Of String, String)
    Get
      Return _Expressions
    End Get
  End Property

  ''' <summary> 
  ''' Use a key to find an evaluation expression method in the evaluator engine class, 
  ''' and invoke the method to return a result. 
  ''' </summary> 
  ''' <example> 
  ''' Dim eval As New ExpressionEvaluator()
  ''' 
  ''' eval.VariableDeclarations.Add("a As Integer = 5") 
  ''' eval.VariableDeclarations.Add("b As Integer = 10")
  ''' 
  ''' eval.Expressions.Add("Sum", "a + b")
  ''' 
  ''' If eval.CompileEvaluator() Then
  '''   Dim result As Integer = Val(eval.Evaluate("Sum"))
  ''' End If
  ''' </example> 
  ''' <param name="expressionKey">The key to the expression to be evaluated</param> 
  ''' <returns>The evaluation result</returns> 
  Public Function Evaluate(ByVal expressionKey As String) As Object
    If _CompiledClass Is Nothing Then
      Throw New ApplicationException("Evaluator not compiled.  Please use CompileEvaluator first.")
    End If
    If Me.Expressions.ContainsKey(expressionKey) Then
      Dim method As MethodInfo = _CompiledClass.GetType().GetMethod("Evaluate" + expressionKey)
      Return method.Invoke(_CompiledClass, Nothing)
    End If
    Return Nothing
  End Function

End Class

Open in new window

0
 
LVL 1

Author Comment

by:sayeth
Comment Utility
Thanks again guys.

To CodeCruiser: the script depend on the custom. but i have lots of pre build functions the custom might use. for example.

A 'variable
B 'variable
Do While GetData    'getdata is a prebuild function that get each data in  infoDepth(2000)  

  A = Null
  B = Null
  Do
    If G = Null Then Exit Do 'G is a preBuild variable
    V= Result(A,B,G)  ' V is a preBuild variable n Result is a prebuild function
    Exit Do
  Loop
Loop


To TheLearnedOne: Nice;
Is it possible to add object in CodeDom like in COM MSScriptControl.ScriptControl
for Ex.

dim class1 as new classSumThing
gvSC = New MSScriptControl.ScriptControl
gvSC.AddObject("MySelf", class1)
'gvSC.AddObject("Pimp3", class3, True)


Loop
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
Comment Utility
With CodeDom, you are creating text that will be compiled, so you can include text from another class into the code.

The text is collected with a StringBuilder:

     
     Dim code As New StringBuilder()
     code.Append(" Public Class EvaluatorEngine" & vbLf)

Open in new window


You could add constructs for collecting classes to include.  There are 3 collectors defined in that code for variables, references, and import statements:

  Private _VariableDeclarations As New List(Of String)()
  Private _ReferencedAssemblies As New List(Of String)()
  Private _Imports As New List(Of String)()

Open in new window

0
 
LVL 1

Author Comment

by:sayeth
Comment Utility

Thanks a lot guys.
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
I thought you were working with VBScript!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now