来自c#包的tcl需要错误
我正在运行以下c#代码: 我得到以下输出: 自由文本 这是一个 收到:8.4 已接收:找不到包http 为什么找不到http包 当我在tclsh上手动尝试同样的事情时,它不会出现任何问题来自c#包的tcl需要错误,c#,package,tcl,require,C#,Package,Tcl,Require,我正在运行以下c#代码: 我得到以下输出: 自由文本 这是一个 收到:8.4 已接收:找不到包http 为什么找不到http包 当我在tclsh上手动尝试同样的事情时,它不会出现任何问题 谢谢 在调用Tcl\u CreateInterp()之前,需要初始化库。通过调用Tcl_FindExecutable初始化库,使用已知的二进制运行对象的名称,但可以相对安全地保留为NULL;(名称不好的)函数所做的其他事情也很重要,例如设置在何处查找其库 C#中的正确声明如下(尽管在本例中,marshallas
谢谢 在调用
Tcl\u CreateInterp()
之前,需要初始化库。通过调用Tcl_FindExecutable
初始化库,使用已知的二进制运行对象的名称,但可以相对安全地保留为NULL
;(名称不好的)函数所做的其他事情也很重要,例如设置在何处查找其库
C#中的正确声明如下(尽管在本例中,marshallas
属性是可选的):
你可以这样调用它,只调用一次:
(在TclAPI
类中作为静态构造函数的一部分这样做可能是最明智的。是的,它属于早期。)
如果这不起作用(我真的无法测试!),你就必须这么做。关键的一点是,它将覆盖Tcl查找其支持脚本(包括
http
包)的默认位置,您可能还需要设置tclibpath
。(请注意,TCL_库
主要用于支持安装前测试,但有被Python在系统级设置的历史,其他系统不应该这样做。小心!)
同样,这必须在调用Tcl\u CreateInterp()
之前完成
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Net;
using System.IO;
namespace TCLRunner
{
class Program
{
private static TclInterpreter interp;
static void Main(string[] args)
{
try
{
interp = new TclInterpreter();
//interp.evalScript(@"cd ..");
interp.evalScript(@"set a ""this is a""");
interp.evalScript(@"puts ""Free text""");
interp.evalScript(@"puts $a");
interp.evalScript(@"package require Tcl");
printResults();
interp.evalScript(@"package require http");
printResults();
//
//
// Shutdown and end connection
}
catch (Exception e)
{
Console.WriteLine("SocketException: {0}", e);
}
}
public static void printResults()
{
string result = interp.Result;
Console.WriteLine("Received: {0}", result);
}
}
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")]
unsafe public static extern char* 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);
}
unsafe public string Result
{
get
{
IntPtr obj = TclAPI.Tcl_GetObjResult(interp);
if (obj == IntPtr.Zero)
{
return "";
}
else
{
return Marshal.PtrToStringAnsi((IntPtr)TclAPI.Tcl_GetStringFromObj(obj, IntPtr.Zero));
}
}
}
}
}
[DllImport("tcl84.DLL")]
public static extern void Tcl_FindExecutable([MarshalAs(UnmanagedType.LPTStr)] string s);
TclAPI.Tcl_FindExecutable(null);