28 Comments

  • Great info, i was wondering if this was possible in .Net but never got the time to look into it. BTW do you know if there exists a managed solution to add scripting capabilities to your .Net application.

  • Yes. I mention one in the article. Alintex Script.

  • Check the Microsoft.Vsa namespace. .Net provides its own classes for scripting C#, VB.Net and J#, but there are some "design leaks" as .Net was not designed to unload assemblies, you will find more info at msdn site.

  • Good article Roy :)

  • Juan: yes, I've looked at that namespace but decided that the MS Script was easier to use. I forgot to mention it the article but I've added it now. Thanks for the tip :)


  • Yo,



    Great article!! Keep in mind however that lots of the "cool" developers out there would rather cut off their right hand than go back to the VBScript environment after 2+ years of managed code development. That is one of my main pain points while working with InfoPath.



    I suggest going the long way and allowing for managed code.... a part II to this article perhaps? :)



  • Santo: you'd be right, only the scripting is for the clients\users, not the developers. And it's MUCH much easier doing stuff in VBscript that starting to import .Net namespaces. Easy wins for client usability in my eyes.


  • Just playing about with your example .. if my script is JScript and I call the following ...



    Form.Left += 200;

    Form.Left += 100;



    It bombs out .. if I change it to



    Form.Left += 200;

    2+2;

    Form.Left += 100;



    Moral of the story ... be careful..


  • As an addendum to my previous post, this does not seem to happen on non-gui elements. Doing AddObject() to bind our ORM objects allows us to call as frequently as we like :



    c = currentCase.GetCaseDetails();

    d = currentCase.Refresh();



    works fine.

  • Concept sounds nice but I failed to run it.



    Problems



    Step 1

    The script control didn't show up on the COM tab. Instead I had to browse for it

    c:\winnt\system32\mscript.ocx.

    Also is it the Microsoft Scripting, Scriplet or ScriptControl ?



    Step 2

    Put a VB line

    Dim myScript as new MSScriptControl.ScriptControlClass



    Step 3

    Didn't realize "script" was an object.

    The textbox line fail to compile because of

    Option Strict On disallows implicit converions from 'System.Object' to 'String'

    After fixing it compiles when.



    Running it causes an unhandled exception

    COM object with CLSID (...} is either not valid or not registered



    After typing on the command prompt Regsvr32 msscript.ocx it finally ran



    Entering 2+2 and execute



    Unhandled exception

    The operation could not be completed because the script engine has not been initialized to a valid language



    After putting in the form load

    myScript.Language = "VBScript"



    it finally worked.



    So put the complete code in it.



    But the concept is nice. Have you thought about making test scripts with it ?



    CE











  • great!But I had some problem on following code:

    --------------

    object [] paramArray = new object[1];

    paramArray[0] = "1";

    string strbody = "sub monitor(x) alert(x) end sub";

    scriptEngine.AddCode(strbody);

    scriptEngine.Run("monitor",ref paramArray);

    --------------

    when i run it ,it tells me :--

    System.Runtime.InteropServices.COMException (0x800A000D): type mismatch: 'alert'

    at MSScriptControl.ScriptControlClass.Run(String ProcedureName, Object[]& Parameters)

    at TongHua.WEBClient.Form1.button4_Click(Object sender, EventArgs e) in d:\WEBClient\form1.cs:line 679

  • Section Using the MSScript control in .Net help me a lot. Thanks

  • Yes.. it works fine.



    But in VB.NET, how can i trap EXCEPTION and show appropriate error info to user ?



    Thanx

  • I got it to work, except, im using VBScript, and im trying to add a textbox as an object, so I can go like

    txtbox2.write("Hey")



    but I dont know how to make a text box an object, ive tried



    Script.AddObject("textbox2",textBox2,True) - error, and you can use it to make a form an object, so anyone know how I make a text box an object?

  • I too have tried to add a textbox, but it doesn't work. I even tried doing Script.AddObject("TextBox2",Me.TextBox2,True) and it doesn't work.





    I've managed to create a debugger though:



    Try

    Script.ExecuteStatement(txtResult.Text)

    Catch ex As Exception

    TextBox1.Text = Script.Error.Description & " | Line of error: " & Script.Error.Line & " | Code error: " & Script.Error.Text

    End Try

  • I have just figured out with someone else how to add textboxes. It took as three days, but in the end we figured it out. Here is how you do it:



    Private Sub InitializeScript()

    script.Reset()

    script.Language = "VBScript"

    script.AddObject("Form1", Me, True)

    End Sub

    Dim blank As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    script.Language = "VBScript"

    blank = "Hey"

    End Sub

    Public Property BlankString()

    Get

    Return blank

    End Get

    Set(ByVal Value)

    blank = Value

    End Set

    End Property

    Public Property LabelText()

    Get

    Return Label1.Text

    End Get

    Set(ByVal Value)

    Label1.Text = Value

    End Set

    End Property



    Public Sub ShowScriptMessage(ByVal s As String)

    System.Windows.Forms.MessageBox.Show(s)

    End Sub



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    InitializeScript()

    script.ExecuteStatement("Form1.LabelText=""This is set via the script control""")

    script.ExecuteStatement("Form1.ShowScriptMessage(""This is shown from the script control"")")

    script.ExecuteStatement("MsgBox(""Hey"" & Form1.BlankString)")

    End Sub



    That shows how to add references and objects on your form. (roy, you should add that to your article.)

  • Couple of questions for anybody reading this...

    Is there any way to start execution of the script from a certain line number?

    Also is there any possibility of pausing the script while it is running?



    Thanks,

    John

  • Declaration:
    Public ScriptControl As MSScriptControl.ScriptControlClass

    in form_load
    ScriptControl = New MSScriptControl.ScriptControlClass
    ScriptControl.Language = "VBScript"

    in Sub
    ScriptControl.AddObject("form", Me, True)

    i've error "Specified cast is not valid".

    Also if i add a procedure
    ScriptControl.AddCode("Public sub Test(X) msgbox(X) end sub")
    Dim p(0 To 1) As Object
    p(1) = "test"
    ScriptControl.Run("Test", p)

    Exception from HRESULT: 0x800A01C2

    This code works good in vb6

  • Dim VBScript As New MSScriptControl.ScriptControlClass
    VBScript.Language = "vbscript"
    VBScript.AddObject("form", Me, True) 'Crashes here, "Invalid Cast Exception" -- But all my parameters are of the correct type. What gives?

    ...

    I can get it to do some things like: msgbox "Hello World" -- but msgbox "string1", "string2" crashes, also for loops seem to work, but I can't expose my objects to the scripts so this is pretty much useless to me.


  • I'm running .net 2005
    I have the same problem.
    Is this article as spam?

  • Jimmy,

    I tried the adding "Imports System.runtime.InteropServices" statement to my class file and yet, I get the same "Invalid Cast Exception" when using AddObjectin MSScriptControl. Could you please post some additional information in how you got MSScript Control to work with VS 2005.

    Thanks

  • Dana,

    If you're using Visual Studio 2005, make sure in your AssemblyInfo.cs file you have this:

    using System.Runtime.InteropServices;

    and, lower down, this:

    [assembly: ComVisible(true)]

    It's probably set to false - set it to true.

  • got the same error. tries with the 'using' stamement and the attribute definition but it simply doesn't work!

  • I got this to work.

    In C# I had created a simple class called ScriptableObject as a test. By default, a new class is created without an access modifier, making it internal.

    If you change it to public, you can then add it using the script.AddObject call.

    Good luck

  • If I create a text file with the script commands in it, how should I best load and execute the script in that file.

  • To make this work in VB2005, change your class definitions:

    Public Class ClassNameToAddToScriptControl

    to enable .AddObject for a class.

  • Great article - this is just what I was looking for. Was having an issue with the AddObject until I added this works fine now....

  • This is definately an interesting article. However, since the article was first published, other alternatives have been introduced. A good (and surprisingly easy) example is hosting IronPython within your application.

Comments have been disabled for this content.