Click here to Skip to main content
13,351,156 members (24,815 online)
Click here to Skip to main content
Add your own
alternative version


54 bookmarked
Posted 21 Aug 2002

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


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()
End Sub
End Module


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


About the Author

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

You may also be interested in...


Comments and Discussions

GeneralSNA Communication Pin
Mario_F12-Aug-08 6:56
memberMario_F12-Aug-08 6:56 
GeneralVS 2005 Pin
lshyamal31-May-07 21:27
memberlshyamal31-May-07 21:27 
GeneralRe: VS 2005 Pin
spinoza7-Nov-07 1:04
memberspinoza7-Nov-07 1:04 
General.NET 2 Pin
Menny Even Danan3-Aug-05 0:32
memberMenny Even Danan3-Aug-05 0:32 
GeneralExecuting VBScripts from C# application Pin
For_IT26-Oct-04 1:25
memberFor_IT26-Oct-04 1:25 
AnswerRe: Executing VBScripts from C# application Pin
Shmarya21-Oct-05 4:57
memberShmarya21-Oct-05 4:57 
GeneralCool! And one suggestion! Pin
xxxyyyzzz17-Oct-03 11:25
memberxxxyyyzzz17-Oct-03 11:25 
AnswerRe: Cool! And one suggestion! Pin
pconsulting16-Sep-05 4:34
memberpconsulting16-Sep-05 4:34 
GeneralRe: Cool! And one suggestion! Pin
TeleStar16-Sep-05 13:36
memberTeleStar16-Sep-05 13:36 
Generalproblem Pin
Jesus Oliva17-Dec-02 4:14
memberJesus Oliva17-Dec-02 4:14 
GeneralRe: problem Pin
zhangzq7117-Dec-02 4:45
memberzhangzq7117-Dec-02 4:45 
GeneralC++ Scripting Pin
Alois Kraus22-Aug-02 21:36
sussAlois Kraus22-Aug-02 21:36 
GeneralVery nice example. Pin
Gevik Babakhani22-Aug-02 4:26
memberGevik Babakhani22-Aug-02 4:26 
GeneralRe: Very nice example. Pin
zhangzq7122-Aug-02 4:42
memberzhangzq7122-Aug-02 4:42 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 22 Aug 2002
Article Copyright 2002 by zhangzq71
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid