C 程序将控制权交给 TCL/Tk GUI - 如何在关闭应用程序时运行代码?

发布于 2024-11-03 15:01:38 字数 731 浏览 0 评论 0原文

我有一个问题,我似乎找不到答案,但我愿意打赌,对于以前使用过此类应用程序设置的任何人来说,这可能是一个非常简单的问题。

我正在使用一个用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风铃鹿 2024-11-10 15:01:47

事实上有几种方法可以做到这一点:

  1. 不要使用 Tk_Main(),而是编写自己的不调用的嵌入版本
    完成后 Tcl_Exit()。起始代码可以在 generic/tkMain.c 中找到
    TK 来源。
  2. 使用 Tcl_SetExitProc() 注册您自己的退出处理程序,该处理程序将被调用,而不是 exit(),
    允许您将控制权返回给您的程序。
  3. 使用 Tcl_CreateExitHandler() 在执行之前仅注册一些清理回调
    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:

  1. Do not use Tk_Main(), instead write your own version for embedding that does not call
    Tcl_Exit() when done. The code to start from can be found in generic/tkMain.c in the
    Tk source.
  2. Use Tcl_SetExitProc() to register your own exit handler that gets called instead of exit(),
    allowing you to return control back to your program.
  3. Use Tcl_CreateExitHandler() to register only some cleanup callbacks before the
    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

佞臣 2024-11-10 15:01:47

您可以尝试使用 atexit()。您向其传递一个 void (*f)(void) 函数,以便在进程终止时调用该函数。我不确定它在 Windows 下的效果如何。

更接近 Tcl/Tk 的方法是使用 Tcl_CreateExitHandler 注册退出处理程序:

Tcl_CreateExitHandler 安排 Tcl_Exit 在终止应用程序之前调用 proc。这为清理操作提供了一个钩子,例如刷新缓冲区和释放全局内存。

You could try using atexit(). You pass it a void (*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:

Tcl_CreateExitHandler arranges for proc to be invoked by Tcl_Exit before it terminates the application. This provides a hook for cleanup operations such as flushing buffers and freeing global memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文