Make your .Net application support scripting - a practical approach

My blog has moved.
You can view this post at the following address:
http://www.osherove.com/blog/2004/2/17/make-your-net-application-support-scripting-a-practical-appr.html
Published Tuesday, February 17, 2004 5:42 AM by RoyOsherove
Filed under:

Comments

Tuesday, February 17, 2004 1:54 AM by Sijin Joseph

# re: Make your .Net application support scripting - the practical way

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.
Tuesday, February 17, 2004 2:11 AM by Roy Osherove

# re: Make your .Net application support scripting - the practical way

Yes. I mention one in the article. Alintex Script.
Tuesday, February 17, 2004 3:41 AM by Juan M. Servera

# re: Make your .Net application support scripting - the practical way

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.
Tuesday, February 17, 2004 3:50 AM by Stefano Demiliani

# re: Make your .Net application support scripting - the practical way

Good article Roy :)
Tuesday, February 17, 2004 4:07 AM by Roy Osherove

# re: Make your .Net application support scripting - the practical way

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 :)
Tuesday, February 17, 2004 9:05 AM by Addy Santo

# re: Make your .Net application support scripting - the practical way


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? :)

Tuesday, February 17, 2004 3:44 PM by Roy Osherove

# re: Make your .Net application support scripting - a practical approach

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.
Wednesday, February 18, 2004 8:51 AM by R

# re: Make your .Net application support scripting - a practical approach


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..
Wednesday, February 18, 2004 1:27 PM by R

# re: Make your .Net application support scripting - a practical approach


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.
Thursday, February 19, 2004 3:24 AM by c enders

# re: Make your .Net application support scripting - a practical approach

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





Sunday, June 6, 2004 1:21 PM by tom

# re: Make your .Net application support scripting - a practical approach

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
Friday, July 2, 2004 6:34 AM by Shailesh Bapat

# re: Make your .Net application support scripting - a practical approach

Section Using the MSScript control in .Net help me a lot. Thanks
Wednesday, July 7, 2004 9:33 AM by Denis P Gohel

# re: Make your .Net application support scripting - a practical approach

Yes.. it works fine.

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

Thanx
Saturday, July 17, 2004 12:49 PM by dcahrakos

# re: Make your .Net application support scripting - a practical approach

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?
Sunday, July 18, 2004 4:53 PM by plasmatic

# re: Make your .Net application support scripting - a practical approach

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
Monday, July 19, 2004 1:21 PM by plasmatic

# Adding Textboxes and References

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.)
Thursday, July 22, 2004 1:26 PM by John

# re: Make your .Net application support scripting - a practical approach

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
Thursday, July 13, 2006 5:01 PM by Daniele

# Specified cast is not valid

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
Thursday, August 31, 2006 5:15 PM by Mikey

# It doesn't work.

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.
Tuesday, September 5, 2006 6:35 AM by Jimmy

# re: Make your .Net application support scripting - a practical approach

I'm running .net 2005 I have the same problem. Is this article as spam?
Wednesday, September 6, 2006 9:38 PM by Jimmy

# re: Make your .Net application support scripting - a practical approach

I found the answer! use: Imports System.runtime.InteropServices ... Public Class ClassNameToAddToScriptControl Then it goes well with AddObject of ScriptControl
Thursday, September 7, 2006 12:45 PM by Dana

# re: Make your .Net application support scripting - a practical approach

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
Monday, September 11, 2006 4:19 PM by Dan

# re: Make your .Net application support scripting - a practical approach

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.
Saturday, October 21, 2006 2:49 AM by kli

# re: Make your .Net application support scripting - a practical approach

got the same error. tries with the 'using' stamement and the attribute definition but it simply doesn't work!
Thursday, October 26, 2006 10:18 PM by Morgan

# re: Make your .Net application support scripting - a practical approach

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
Friday, January 5, 2007 4:46 PM by Zerox Millienium

# re: Make your .Net application support scripting - a practical approach

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

Wednesday, February 28, 2007 6:01 AM by Davman

# re: Make your .Net application support scripting - a practical approach

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

<ComVisible(True)> Public Class ClassNameToAddToScriptControl

to enable .AddObject for a class.

Wednesday, March 7, 2007 6:31 PM by Tom Henrich

# re: Make your .Net application support scripting - a practical approach

Great article - this is just what I was looking for. Was having an issue with the AddObject until I added this <ComVisible(True)> works fine now....

Monday, March 12, 2007 7:11 AM by Alistair

# re: Make your .Net application support scripting - a practical approach

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.