Click here to Skip to main content
Licence 
First Posted 21 Aug 2002
Views 104,035
Bookmarked 51 times

C# Script Engine tutorial

By | 21 Aug 2002 | Article
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

About the Author

zhangzq71

Web Developer

China China

Member

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralSNA Communication PinmemberMario_F5:56 12 Aug '08  
GeneralVS 2005 Pinmemberlshyamal20:27 31 May '07  
GeneralRe: VS 2005 Pinmemberspinoza0:04 7 Nov '07  
General.NET 2 PinmemberMenny Even Danan23:32 2 Aug '05  
GeneralExecuting VBScripts from C# application PinmemberFor_IT0:25 26 Oct '04  
AnswerRe: Executing VBScripts from C# application PinmemberShmarya3:57 21 Oct '05  
GeneralCool! And one suggestion! Pinmemberxxxyyyzzz10:25 17 Oct '03  
AnswerRe: Cool! And one suggestion! Pinmemberpconsulting3:34 16 Sep '05  
GeneralRe: Cool! And one suggestion! PinmemberTeleStar12:36 16 Sep '05  
Generalproblem PinmemberJesus Oliva3:14 17 Dec '02  
GeneralRe: problem Pinmemberzhangzq713:45 17 Dec '02  
GeneralC++ Scripting PinsussAlois Kraus20:36 22 Aug '02  
GeneralVery nice example. PinmemberGevik Babakhani3:26 22 Aug '02  
GeneralRe: Very nice example. Pinmemberzhangzq713:42 22 Aug '02  

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.

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