Solved

COM MSScriptControl.ScriptControl To CodeDom

Posted on 2011-02-15
13
2,150 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

18 Experts available now in Live!

Get 1:1 Help Now