Version 6 of Tcl Interpreter in C# Application

Updated 2003-10-21 20:17:15

Here a short example how to use Tcl Interpreter and Tcl Scripting engine in C# .NET application. Warning here the fist try to do it and it is simpler that one can suppose. In this example you use Tcl as so called unmanaged code form original tcl dll.

    using System.Runtime.InteropServices;
    using System;

    namespace TclWrap 
    {
        public class TclAPI 
        {
                [DllImport("tcl84.DLL")]
                public static extern IntPtr Tcl_CreateInterp();
                [DllImport("tcl84.Dll")]
                public static extern int Tcl_Eval(IntPtr interp,string skript);
                [DllImport("tcl84.Dll")]
                public static extern IntPtr Tcl_GetObjResult(IntPtr interp);
                [DllImport("tcl84.Dll")]
                public static extern string Tcl_GetStringFromObj(IntPtr tclObj,IntPtr length);

        }
        public class TclInterpreter 
        {
                private IntPtr interp;
                public TclInterpreter() 
                {
                        interp = TclAPI.Tcl_CreateInterp();
                        if (interp == IntPtr.Zero) 
                        {
                                throw new SystemException("can not initialize Tcl interpreter");
                        }
                }
                public int evalScript(string script) 
                {
                        return TclAPI.Tcl_Eval(interp,script);        
                }
                public string Result 
                {
                        get { 
                                IntPtr obj = TclAPI.Tcl_GetObjResult(interp);
                                if (obj == IntPtr.Zero) 
                                {
                                        return "";
                                } 
                                else 
                                {
                                        return TclAPI.Tcl_GetStringFromObj(obj,IntPtr.Zero);
                                }
                        }
                }
        }
   }

Using of it in C#

   TclInterpreter interp = new TclInterpreter();
   string result;
   if (interp.evalScript("set a 3; {exp $a + 2}")) {
      result = interp.Result;
   }

You need to copy your tcl84.dll in application dictionary or in system dll dictionary. No futher requirements.

To have really useful tcl interpreter we need to define tcl proc that can invoke C# callback (delegates) and some infrastructure to get parameters from tcl calls. C# have also very good relextion (introspection) capatiblities. So it is possible to invoke all methods in assembly without to program special wrappers for all calls.

sample from Artur Trzewik

bigger sample tcl project (with gui interaction) with .NET executables * Callbacks to .NET (tcl proc ivoke .NET delegate) * Manipulatin of .NET object from Tcl with introspection (reflection) seting and reading of properties and instance variables

http://www.xdobry.de/tclinterop.zip

Warnig: Development state not productivity mature