Solved

COM MSScriptControl.ScriptControl To CodeDom

Posted on 2011-02-15
13
2,193 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 - Windows Protected Your PC 7 41
vb.net cycle through results of a sql query and capture variables 3 18
MailAddress in vb 4 29
randomize data from excell cell 15 45
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 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