Click here to Skip to main content
Click here to Skip to main content

C# Script Engine tutorial

, 21 Aug 2002
Rate this:
Please Sign up or sign in to vote.
Introduces how to use Microsoft.vsa to make your programs have script functions

Sample Image - CSScriptEngine.gif

Introduction

Many modern applications support script functions that provide the user a way to programmatically control the application itself. This technology lets the user have the ability to control the flow of the program as they wish. Before, Microsoft had provided IActiveScript interface to access the script engine. In .NET framework, Microsoft provides many classes to support script engine, please refer to ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfMicrosoftVsa.htm. I have written a sample program to show how to do it in C#. In this sample program, I created a winform control and put it in my sample program, later I will show how to use VB script to call method in this program, and handle the event from this control using the script. The user control is named UserControl1, it has one method and one event.

You must implement Microsoft.Vsa.IVsaSite, it provides a bridge between your program and script engine

public class TestVsaSite : Microsoft.Vsa.IVsaSite
{
    private System.Collections.Hashtable eventInstance;

    public TestVsaSite()
    {
        eventInstance = new Hashtable();
    }

    public void AddEvent(string name, string type, object instance, 
           bool events)
    {
        eventInstance.Add(name, instance);
    }

    public void GetCompiledState(out byte[] pe, out byte[] debugInfo)
    {
    }

    public object GetEventSourceInstance(string itemName, 
            string eventSourceName)
    {
        return eventInstance[eventSourceName];
    }

    public object GetGlobalInstance(string name)
    {
        return (object)null;
    }

    public void Notify(string notify, object info)
    {
    }

    public bool OnCompilerError(IVsaError error)
    {
        return false;
    }
}

Now create a script engine instance

m_vbEngine = new Microsoft.VisualBasic.Vsa.VsaEngine();
You can hook the event by calling
m_vbScript.AddEventSource(name, type);
<PRV lang=cs>m_vbTempRef = (IVsaReferenceItem)m_vbItems.CreateItem(name, 
    VsaItemType.Reference, VsaItemFlag.None);

Here is the VB script sample

imports myNotClass
imports System
Public Module Vespu

#Region "Automatically generated code, do not modify"
'Automatically generated code, do not modify
'Event Sources Begin
    <System.ContextStaticAttribute()> Public WithEvents ctrlInScript _
      As myNotClass.UserControl1
'Event Sources End
'End of automatically generated code
#End Region


Sub main
End Sub

Sub ctrlInScript_tick(count As Integer) Handles ctrlInScript.tick
dim disp as string
disp = "tick counter is: " & count.ToString()
ctrlInScript.SetText(disp)
End Sub
End Module

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

zhangzq71
Web Developer
China China
Have more than 10 programming experience. know SNA server, COM, CORBA well, and now I am moving to .net framework.

Comments and Discussions

 
GeneralSNA Communication PinmemberMario_F12-Aug-08 5:56 
GeneralVS 2005 Pinmemberlshyamal31-May-07 20:27 
GeneralRe: VS 2005 Pinmemberspinoza7-Nov-07 0:04 
General.NET 2 PinmemberMenny Even Danan2-Aug-05 23:32 
GeneralExecuting VBScripts from C# application PinmemberFor_IT26-Oct-04 0:25 
AnswerRe: Executing VBScripts from C# application PinmemberShmarya21-Oct-05 3:57 
GeneralCool! And one suggestion! Pinmemberxxxyyyzzz17-Oct-03 10:25 
AnswerRe: Cool! And one suggestion! Pinmemberpconsulting16-Sep-05 3:34 
GeneralRe: Cool! And one suggestion! PinmemberTeleStar16-Sep-05 12:36 
Generalproblem PinmemberJesus Oliva17-Dec-02 3:14 
GeneralRe: problem Pinmemberzhangzq7117-Dec-02 3:45 
GeneralC++ Scripting PinsussAlois Kraus22-Aug-02 20:36 
GeneralVery nice example. PinmemberGevik Babakhani22-Aug-02 3:26 
GeneralRe: Very nice example. Pinmemberzhangzq7122-Aug-02 3:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140821.2 | Last Updated 22 Aug 2002
Article Copyright 2002 by zhangzq71
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid