C 程序将控制权交给 TCL/Tk GUI - 如何在关闭应用程序时运行代码?
我有一个问题,我似乎找不到答案,但我愿意打赌,对于以前使用过此类应用程序设置的任何人来说,这可能是一个非常简单的问题。
我正在使用一个用 C 编写的应用程序,但调用 TCL/TK gui 在屏幕上做很多漂亮的事情。然而,由于 TCL/TK 从 C 运行时的工作方式,一旦您移交了控制权,您就永远无法收回它(即 TCL/TK 解释器处理应用程序退出,而不是返回到原始 main()功能)。
基本上,有一些像这样的代码:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow){
Tk_Main(1,argv,Tcl_AppInit); // Tcl_AppInit is a function that runs at the start
// All code after this point will never, ever run
return 0; // This return never actually occurs
}
这会导致一个小问题。我希望能够在程序退出之前销毁数据结构、关闭文件、检查内存泄漏以及执行其他必要的操作。但是,在 Tk_Main 之后我无法执行此操作。我必须找出 TCL/TK 解释器关闭时运行的钩子,然后告诉它在退出之前运行我的拆卸函数。
有人知道如何或在哪里执行此操作吗?
I have a problem that I can't seem to find the answer to, but I'm willing to bet it's probably a pretty simple one for anybody who has worked with this sort of application setup before.
I'm working with an application that is written in C, but calls a TCL/TK gui to do lots of nice pretty stuff on the screen. However, due to the way that TCL/TK works when run from C, once you've handed over control- you never get it back (i.e. the TCL/TK interpreter handles the application exit, rather than returning to the original main() function).
So basically, there is some code like this:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow){
Tk_Main(1,argv,Tcl_AppInit); // Tcl_AppInit is a function that runs at the start
// All code after this point will never, ever run
return 0; // This return never actually occurs
}
This causes a slight issue. I want to be able to destroy data structures, close files, check for memory leaks, and do other necessary things before the program exits. However, I can't do it after Tk_Main. I have to figure out the hook in the TCL/TK interpreter that runs when it shuts down, then tell that to run my teardown function before quitting.
Anybody know how or where to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实上有几种方法可以做到这一点:
完成后 Tcl_Exit()。起始代码可以在 generic/tkMain.c 中找到
TK 来源。
允许您将控制权返回给您的程序。
Tcl_Finalize() 发生,但无论如何都会退出该进程。
处理您的情况的首选方法可能是 2。Tcl_Exit() 的联机帮助页包含您需要的所有详细信息:http://www.tcl.tk/man/tcl8.5/TclLib/Exit.htm
There are in fact a few ways to do it:
Tcl_Exit() when done. The code to start from can be found in generic/tkMain.c in the
Tk source.
allowing you to return control back to your program.
Tcl_Finalize() happens, but exit the process anyway.
Preferred way to handle your case would probably be 2. The manpage for Tcl_Exit() has all the details you should need: http://www.tcl.tk/man/tcl8.5/TclLib/Exit.htm
您可以尝试使用
atexit()
。您向其传递一个void (*f)(void)
函数,以便在进程终止时调用该函数。我不确定它在 Windows 下的效果如何。更接近 Tcl/Tk 的方法是使用
Tcl_CreateExitHandler
注册退出处理程序:You could try using
atexit()
. You pass it avoid (*f)(void)
function to be called when the process terminates. I'm not sure how well that works under Windows though.A more Tcl/Tk-ish approach would be to use
Tcl_CreateExitHandler
to register an exit handler: