tcl脚本调用java dll,如何使任何程序使用tcl代码功能的可加载DLL

I have created a GUI using tcl. I want to make some of the core functionalities of the tcl code available to be used by any program which supports dll. For that i have taken a very simple tcl code example, which adds two integer numbers and i have written a c wrapper function to use this functionality. This is working for me. Now how can i create a dll for these two c and tcl files, so that any program can use this addition functionality by simply loading the dll.

Here is my simple tcl code :

/* Filename : simple_addition.tcl */

#!/usr/bin/env tclsh8.5

proc add_two_nos { } {

set a 10

set b 20

set c [expr { $a + $b } ]

puts " c is $c ......."

}

And here is my c wrapper function which uses the above tcl addition functionality :

#include

#include

#include

#include

#include

#include

int main (int argc, char **argv) {

Tcl_Interp *interp;

int code;

char *result;

printf("inside main function \n");

Tcl_FindExecutable(argv[0]);

interp = Tcl_CreateInterp();

code = Tcl_Eval(interp, "source simple_addition.tcl; add_two_nos");

/* Retrieve the result... */

result = Tcl_GetString(Tcl_GetObjResult(interp));

/* Check for error! If an error, message is result. */

if (code == TCL_ERROR) {

fprintf(stderr, "ERROR in script: %s\n", result);

exit(1);

}

/* Print (normal) result if non-empty; we'll skip handling encodings for now */

if (strlen(result)) {

printf("%s\n", result);

}

/* Clean up */

Tcl_DeleteInterp(interp);

exit(0);

}

This c wrapper is working fine for me and gives correct results.

Now I want to create a dll file, so that if i include that dll to any program that supports dll, it should be able to use this addition functionality of the above tcl code. Can anybody please tell me the way i can do it. Please help me. I am new to this dll concept.

解决方案

In order to create the .dll you'll have to use something like Visual Studio and C or C++ to create the .dll (there are lots of other tools out there that can create .dll files but VS is easy to get hold of and to use.) So in VS create a new project, this needs to be a C++ WIN32 project. Select the DLL application type and the Export Symbols additional option.

VS will create a basic .dll that you can then amend to do what you want. I short I'd look at putting the creating/destruction of the intrepter into the dllmain:

BOOL APIENTRY DllMain( HMODULE hModule,

DWORD ul_reason_for_call,

LPVOID lpReserved

)

{

switch (ul_reason_for_call)

{

case DLL_PROCESS_ATTACH:

{

Tcl_FindExecutable(NULL);

interp = Tcl_CreateInterp();

}

case DLL_THREAD_ATTACH:

break ;

case DLL_THREAD_DETACH:

break ;

case DLL_PROCESS_DETACH:

{

Tcl_DeleteInterp(interp);

break;

}

}

return TRUE;

}

and then create functions exported by the .dll that make use of the interpreter. If you aren't familiar with the concept of shared libaries then I'd suggest spending a little time reading up on them, try here and here for some background reading.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值