Wiki

Clone wiki

ActiveScriptEngine / Home

ActiveScriptEngine

The ActiveScriptEngine library allows you to easily host and interact with ActiveX scripts in a .NET application.

Examples

Host some VBScript code and call it from C#

var engine = new ActiveScriptEngine("VBScript");

engine.AddCode("Public Function Add(a, b) " +
               "   Add = a + b " +
               "End Function");

engine.Start();

dynamic script = engine.GetScriptHandle();
int result = script.Add(1, 3);

Identical example using JScript

var engine = new ActiveScriptEngine("JScript");

engine.AddCode("function Add(a, b) {" +
               "   return a + b; " +
               "}");

engine.Start();

dynamic script = engine.GetScriptHandle();
int result = script.Add(1, 3);

Add C# objects to the script engine and use them from script.

[ComVisible(true)]
public class HostFunctions
{
    public void Echo(string text)
    {
        Trace.WriteLine(text);
    }
}
...

var engine = new ActiveScriptEngine(VBScript.ProgID);
engine.AddGlobalMemberObject("Host", new HostFunctions());

engine.AddCode("Echo \"Hello from VBScript\"");
engine.Start();      

Complex Example

[ComVisible(true)]
public class HostFunctions
{
    public void Echo(string text)
    {
        Trace.WriteLine(text);
    }
}

[ComVisible(true)]
public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public string Name
    {
        get
        {
            return FirstName + " " + LastName;
        }
    }

    public void SayHello()
    {
        Trace.WriteLine("My name is : " + Name);
    }
}


using (var engine = new ActiveScriptEngine(VBScript.ProgID))
{
    // Add HostFunctions instance whose public members will be accessible
    // as global functions inside the script.
    engine.AddGlobalMemberObject("Host", new HostFunctions());

    engine.AddCode("Echo \"Inside script\"");

    engine.AddCode("Private Sub PrivateTask() " + Environment.NewLine +
                    "   'Perform some task " + Environment.NewLine +
                    "End Sub" +

                    Environment.NewLine +

                    "Public Sub PublicTask() " + Environment.NewLine +
                    "   ' Perform some task " + Environment.NewLine +
                    "End Sub",

                    "Tasker", "Tasker"); // Add this code in the Tasker namespace

    engine.AddCode("Public Sub RunTask() " + Environment.NewLine +
                    "   Tasker.PublicTask " + Environment.NewLine +
                    "End Sub");

    engine.Start();

    dynamic script = engine.GetScriptHandle(); // Access to root script handle.

    try
    {
        script.RunTask(); // Can invoke public methods in global scope.

        script.Tasker.PublicTask(); // Using script.Tasker can invoke public methods in Tasker namespace.

        dynamic tasker = engine.GetScriptHandle("Tasker"); // Access to Tasker script handle.

        tasker.PublicTask(); // using tasker handle can access public methods in tasker namespace.

        engine.AddObject("joe", new Person()
            {
                FirstName = "Joe",
                LastName = "Bloggs"
            });

        script.joe.SayHello(); // Access joe directly from dynamic.

        engine.AddCode("Echo \"Hello \" & joe.Name & \" from VBScript\""); // Access joe from inside the script.
    }
    catch (COMException)
    {
        var error = engine.LastError;
        Trace.WriteLine("An error occurred: " + error.Description);
        Trace.WriteLine("Line number: " + error.LineNumber);
        Trace.WriteLine("Error source text: " + error.LineText);
    }
}

Updated