Scripting your applications in .NET

14. August 2008 16:48 by Mrojas in General  //  Tags: , , , , ,   //   Comments (5)

In VB6 it was very simple to add scripting capabilities to your application.
Just by using the Microsoft Script Control Library
You can still use this library in .NET just as Roy Osherove' Bloc show in
http://weblogs.asp.net/rosherove/articles/dotnetscripting.aspx

However there are some minor details that must be taken care of:

* Objects must be exposed thru COM (Add the [ComVisible(true)] attribute to the class
* Add the ComVisible(true) attribute to the AssemblyInfo file
* Make these objects public
* Recommended (put your calls to Eval or ExecuteStatement inside try-catch blocks).

And here's an example:

using System;
using System.Windows.Forms;
 
namespace ScriptingDotNetTest
{
    [System.Runtime.InteropServices.ComVisible(true)]
    public partial class frmTestVBScript  : Form
    {
        public int MyBackColor
        {
            get { return System.Drawing.ColorTranslator.ToOle(this.BackColor); }
            set { this.BackColor = System.Drawing.ColorTranslator.FromOle(value); }
        }
 
        MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControl();
        private void RunScript(Object eventSender, EventArgs eventArgs)
        {
            try
            {
                sc.Language = "VbScript";
                sc.Reset();
                sc.AddObject("myform", this, true);
                sc.ExecuteStatement("myform.MyBackColor = vbRed");
            }
            catch 
            {
                MSScriptControl.IScriptControl iscriptControl = sc as MSScriptControl.IScriptControl;
                lblError.Text = "ERROR" + iscriptControl.Error.Description + " | Line of error: " + iscriptControl.Error.Line + " | Code error: " + iscriptControl.Error.Text;
            }
        }
 
        [STAThread]
        static void Main()
        {
            Application.Run(new frmTestVBScript());
        }
    }
}


TIP: If you don find the reference in the COM tab, just browse to c:\windows\system32\msscript.ocx

 

Comments (5) -

Anonymous
Anonymous
10/14/2008 2:57:05 AM #

Recently I received this message:


"Mauricio, hi,I read your blog with great interest.I have a question regarding your blog item "Scripting your applications in .NET”(blogs.artinsoft.net/.../...pplications-in-net.aspx">blogs.artinsoft.net/.../scripting-your-applications-in-net.aspx).


In your sample explicit conversion from “System.Drawing.Color” to “int” is used to enable VBScript to use OLE_COLOR.


However, when .NET component with COM Callable Wrapper (CCW) is used by a normal VB6 application the conversion from managed “System.Drawing.Color” to OLE_COLOR is provided implicitly. Can you please explain the difference?


Thanks.


Sure. And Thanks for the comment, it makes me happy to know that my blog is being helpful to some people.


COM Callable Wrapper (CCW) are generated by a tool called TLBIMP (Type Library to Assembly Converter). This is a great tool which code is also now available in codeplex (see my other posts about it).


This tool is in charge of generating the .NET code that performs the marshaling and other tasks that are needed to correctly invoke COM from .NET. When this tool analyses the typelib and encouters an OLE_COLOR it will generate the necessary transformations. But a the microsoft scripting engine is a COM component that knows nothing about .NET and that only comunicates with COM components. I am posting a follow up of this post that might give more insight about this matter.


Anonymous
Anonymous
10/14/2008 2:57:05 AM #

Recently I received this message:


"Mauricio, hi,I read your blog with great interest.I have a question regarding your blog item "Scripting your applications in .NET”(blogs.artinsoft.net/.../...pplications-in-net.aspx">blogs.artinsoft.net/.../scripting-your-applications-in-net.aspx).


In your sample explicit conversion from “System.Drawing.Color” to “int” is used to enable VBScript to use OLE_COLOR.


However, when .NET component with COM Callable Wrapper (CCW) is used by a normal VB6 application the conversion from managed “System.Drawing.Color” to OLE_COLOR is provided implicitly. Can you please explain the difference?


Thanks.


Sure. And Thanks for the comment, it makes me happy to know that my blog is being helpful to some people.


COM Callable Wrapper (CCW) are generated by a tool called TLBIMP (Type Library to Assembly Converter). This is a great tool which code is also now available in codeplex (see my other posts about it).


This tool is in charge of generating the .NET code that performs the marshaling and other tasks that are needed to correctly invoke COM from .NET. When this tool analyses the typelib and encouters an OLE_COLOR it will generate the necessary transformations. But a the microsoft scripting engine is a COM component that knows nothing about .NET and that only comunicates with COM components. I am posting a follow up of this post that might give more insight about this matter.


Anonymous
Anonymous
10/14/2008 2:57:05 AM #

Recently I received this message:


"Mauricio, hi,I read your blog with great interest.I have a question regarding your blog item "Scripting your applications in .NET”(blogs.artinsoft.net/.../...pplications-in-net.aspx">blogs.artinsoft.net/.../scripting-your-applications-in-net.aspx).


In your sample explicit conversion from “System.Drawing.Color” to “int” is used to enable VBScript to use OLE_COLOR.


However, when .NET component with COM Callable Wrapper (CCW) is used by a normal VB6 application the conversion from managed “System.Drawing.Color” to OLE_COLOR is provided implicitly. Can you please explain the difference?


Thanks.


Sure. And Thanks for the comment, it makes me happy to know that my blog is being helpful to some people.


COM Callable Wrapper (CCW) are generated by a tool called TLBIMP (Type Library to Assembly Converter). This is a great tool which code is also now available in codeplex (see my other posts about it).


This tool is in charge of generating the .NET code that performs the marshaling and other tasks that are needed to correctly invoke COM from .NET. When this tool analyses the typelib and encouters an OLE_COLOR it will generate the necessary transformations. But a the microsoft scripting engine is a COM component that knows nothing about .NET and that only comunicates with COM components. I am posting a follow up of this post that might give more insight about this matter.


Anonymous
Anonymous
10/14/2008 2:57:05 AM #

Recently I received this message:


"Mauricio, hi,I read your blog with great interest.I have a question regarding your blog item "Scripting your applications in .NET”(blogs.artinsoft.net/.../...pplications-in-net.aspx">blogs.artinsoft.net/.../scripting-your-applications-in-net.aspx).


In your sample explicit conversion from “System.Drawing.Color” to “int” is used to enable VBScript to use OLE_COLOR.


However, when .NET component with COM Callable Wrapper (CCW) is used by a normal VB6 application the conversion from managed “System.Drawing.Color” to OLE_COLOR is provided implicitly. Can you please explain the difference?


Thanks.


Sure. And Thanks for the comment, it makes me happy to know that my blog is being helpful to some people.


COM Callable Wrapper (CCW) are generated by a tool called TLBIMP (Type Library to Assembly Converter). This is a great tool which code is also now available in codeplex (see my other posts about it).


This tool is in charge of generating the .NET code that performs the marshaling and other tasks that are needed to correctly invoke COM from .NET. When this tool analyses the typelib and encouters an OLE_COLOR it will generate the necessary transformations. But a the microsoft scripting engine is a COM component that knows nothing about .NET and that only comunicates with COM components. I am posting a follow up of this post that might give more insight about this matter.


Anonymous
Anonymous
10/14/2008 2:57:05 AM #

Recently I received this message:


"Mauricio, hi,I read your blog with great interest.I have a question regarding your blog item "Scripting your applications in .NET”(blogs.artinsoft.net/.../...pplications-in-net.aspx">blogs.artinsoft.net/.../scripting-your-applications-in-net.aspx).


In your sample explicit conversion from “System.Drawing.Color” to “int” is used to enable VBScript to use OLE_COLOR.


However, when .NET component with COM Callable Wrapper (CCW) is used by a normal VB6 application the conversion from managed “System.Drawing.Color” to OLE_COLOR is provided implicitly. Can you please explain the difference?


Thanks.


Sure. And Thanks for the comment, it makes me happy to know that my blog is being helpful to some people.


COM Callable Wrapper (CCW) are generated by a tool called TLBIMP (Type Library to Assembly Converter). This is a great tool which code is also now available in codeplex (see my other posts about it).


This tool is in charge of generating the .NET code that performs the marshaling and other tasks that are needed to correctly invoke COM from .NET. When this tool analyses the typelib and encouters an OLE_COLOR it will generate the necessary transformations. But a the microsoft scripting engine is a COM component that knows nothing about .NET and that only comunicates with COM components. I am posting a follow up of this post that might give more insight about this matter.


Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading