Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

COM MSScriptControl.ScriptControl To CodeDom

Posted on 2011-02-15
13
Medium Priority
?
2,384 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
13 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34905678
0
 
LVL 83

Expert Comment

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

Author Comment

by:sayeth
ID: 34905861
im using VB.NET 2008.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34905882
>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
ID: 34906089
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
ID: 34906456
Does the speed depend on the code? Did you try it with different code?
0
 
LVL 1

Author Comment

by:sayeth
ID: 34906820
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
ID: 34907279
So it seems that the problem is with script itself. Can you show us the script?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 34907881
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
ID: 34910354
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 2000 total points
ID: 34910443
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
ID: 34910914

Thanks a lot guys.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34911954
I thought you were working with VBScript!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

618 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