一、
test.tcl
proc add {a b c} {
set d [expr $a + $b + $c]
return $d
}
test.cpp
// gettclvalueofvariable.cpp
#include "stdafx.h"
#include "iostream"
#include "stdio.h"
#include "string"
#include "tcl.h"
#include "assert.h"
#include "tclDecls.h"
#pragma comment(lib,"tcl85.lib")
using namespace std;
#define STRCMD_SIZE 50
int _tmain(int argc, _TCHAR* argv[])
{
Tcl_Main(argc,argv,Tcl_AppInit);
return 0;
}
int Tcl_AppInit(Tcl_Interp *interp)
{
if (Tcl_Init(interp)==(TCL_ERROR))
{
return TCL_ERROR;
}
if(Tcl_EvalFile(interp,"test.tcl")==TCL_ERROR)
{
return TCL_ERROR;
}
char *str = "10";
char *str1 = "11";
char *str12 = "12";
char strcmd[STRCMD_SIZE] = {0};
sprintf(strcmd,"add %s %s %s",str,str1,str12);
Tcl_eval_r(interp,strcmd);
Tcl_GetVar(interp,"d",TCL_GLOBAL_ONLY);
cout<<interp->result<<endl;
char *p = NULL;
p = interp->result;
assert(p != NULL);
cout<<"the value of p is: "<<p<<endl;
return 1;
}
二、
三、
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char* p = "hello";
cout<<"the value of p is: "<<p<<endl;
char m[10] = {0};
string result;
memcpy(m,p,sizeof(p)+1);
result = string(m);
cout<<"the value of m is: "<<m<<endl;
cout<<"the value of result is: "<<result<<endl;
return 0;
}
果后,转换成string,传递给其他c++函数。Tcl脚本同上例。
// 0906.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "tcl.h"
#include "string"
#include "iostream"
#include "stdio.h"
#include "assert.h"
#pragma comment(lib,"tcl85.lib")
#define ARRAYSIZE 30
using namespace std;
string callTcl()
{
Tcl_Interp *interp = Tcl_CreateInterp();
//change string to char*
string a = "123"; //change a to char*
char* str1 = const_cast<char*>(a.c_str());
cout<<"the value of str1 is "<<str1<<endl;
string b = "13"; //change b to char*
char* str2 = const_cast<char*>(b.c_str());
cout<<"the value of str2 is: "<<str2<<endl;
string c = "26"; //change c to char*
char* str3 = const_cast<char*>(c.c_str());
cout<<"the value of str3 is: "<<str3<<endl;
//call tcl and trans the char* to string and return
if (Tcl_EvalFile(interp,"test.tcl") == TCL_ERROR)
{
//return TCL_ERROR;
cout<<"call tcl error"<<endl;
}
char strcmd[ARRAYSIZE] = {0} ;
sprintf(strcmd, "add %s %s %s", str1,str2,str3);
Tcl_eval_r(interp,strcmd);
char *p = NULL;
Tcl_GetVar(interp,"d",TCL_GLOBAL_ONLY);
cout<<"the value of interp->result is: "<<interp->result<<endl;
p = interp->result;
assert(p != NULL);
cout<<"the value of p is: "<<p<<endl;
//trans p to string type
char result[60] = {0};
memcpy(result,p,sizeof(p)+1);
string sresult = string(result);
return(sresult);
}
int _tmain(int argc, _TCHAR* argv[])
{
Tcl_Main(argc,argv,Tcl_AppInit);
return 0;
}
int Tcl_AppInit(Tcl_Interp *interp)
{
if (Tcl_Init(interp)==TCL_ERROR)
{
return TCL_ERROR;
}
string a;
a = callTcl();//call callTcl function
callTcl();
cout<<"the value of a is: "<<a<<endl;
return TCL_OK;
}
运行结果如下图:
四、
test1.tcl
package provide test1 1.0
namespace eval test1 {
namespace export a
variable n 5
proc a {} {
variable n
set n 6
return $n
#puts $n
}
}
test2.tcl
package provide test2 1.0
package require test1 1.0
namespace import test1::a
namespace eval test2 {
set b [a]
puts $b
}
用active tcl 可以运行test2.tcl,返回b的值为6,要想取得名字空间变量的值用set b test1::n即可。
五、
int _tmain(int argc,_TCHAR* argv[] )
{
}
Int Tcl_AppInit(Tcl_Interp *interp)
{
}层次结构
只要在一般的函数里遵循初始化interp→调用tcl脚本→取值即可,下面是实例代码:
test.tcl代码如下:
puts "hello"
proc test {} {
set a 4
return $a
}
C++代码如下:
#include "stdafx.h"
#include "iostream"
#include "stdio.h"
#include "string"
#include "tcl.h"
#include "assert.h"
#include "tclDecls.h"
#include "assert.h"
#pragma comment(lib,"tcl85.lib")
using namespace std;
#pragma comment (lib,"tcl85.lib")
int calltcl();
int _tmain(int argc, _TCHAR* argv[])
{
}
int calltcl()
{
}
运行结果截图:
【注】本部分中的.tcl脚本均和.exe在同一目录下,如果.tcl在某一个文件夹下,比如,test.tcl在testlib文件夹中,则:
if (Tcl_EvalFile(interp,"test.tcl")==TCL_ERROR)改为if (Tcl_EvalFile(interp,"./testlib/test.tcl")==TCL_ERROR)即可
【Tcl同一个名字空间中两个不同过程实现变量共享】
Tcl代码:
namespace eval test {
}
#test::test1
#test::print
C++代码:
#include "stdafx.h"
#include "iostream"
#include "stdio.h"
#include "string"
#include "tcl.h"
#include "assert.h"
#include "tclDecls.h"
#include "assert.h"
using namespace std;
#pragma comment (lib,"tcl85.lib")
int calltcl();
int _tmain(int argc, _TCHAR* argv[])
{
}
int calltcl()
{
}
返回值:
【注】以下代码用同一个解释器一样获取相同结果
#include "stdafx.h"
#include "iostream"
#include "stdio.h"
#include "string"
#include "tcl.h"
#include "assert.h"
#include "tclDecls.h"
#include "assert.h"
using namespace std;
#pragma comment (lib,"tcl85.lib")
int calltcl();
int _tmain(int argc, _TCHAR* argv[])
{
}
int calltcl(
{
}