windows 发送/捕获 SIGTERM 的 Win32 API 模拟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1216788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Win32 API analog of sending/catching SIGTERM
提问by Artyom
Under POSIX OS there is signal API that allows to send a signal to process to shut it down with kill and you can catch it with sigaction and do what you need;
在 POSIX OS 下,有一个信号 API,它允许向进程发送信号以使用 kill 将其关闭,您可以使用 sigaction 捕获它并执行您需要的操作;
However, Win32 is not POSIX system, so:
但是,Win32 不是 POSIX 系统,所以:
- How can I handle shutdown events that may come, for example from "End Process" in "Task manager"?
- What is the standard API for sending shutdown signal to Win32 application?
- 我如何处理可能来自的关闭事件,例如来自“任务管理器”中的“结束进程”?
- 向 Win32 应用程序发送关闭信号的标准 API 是什么?
I'm not talking about GUI, I'm talking about TCP/IP server that should be nicely shutdown. that does notrun like windows service.
我不是在谈论 GUI,我在谈论应该很好地关闭的 TCP/IP 服务器。它不像 Windows 服务那样运行。
采纳答案by Christopher
You get a WM_QUIT
message on your first created thread.
您会WM_QUIT
在第一个创建的线程上收到一条消息。
When you don't handle that, your process is forcibly shutdown.
如果您不处理,您的进程将被强行关闭。
So just implement a message queue in your first thread, which looks for the WM_QUIT
message
所以只需在你的第一个线程中实现一个消息队列,它会查找WM_QUIT
消息
回答by Kb.
MSDNs Unix Code Migration Guidehas a chapter about Win32 code conversion and signal handling.
Although Microsoft has decided to archive this brilliant guide, it is very useful.
MSDNs Unix Code Migration Guide有一章关于Win32 代码转换和信号处理。
尽管 Microsoft 已决定将这份出色的指南存档,但它非常有用。
Three methods are described:
Native signals
Event objects
Messages
描述了三种方法: 本
机信号
事件对象
消息
回答by Indy9000
May be Windows Power Management from MSDNwould be helpful. But it deals with system events rather than per process.
可能来自MSDN 的Windows 电源管理会有所帮助。但它处理系统事件而不是每个进程。
For a process, you would be able to detect termination with WM_CLOSE
. You would need to handle windows messages. If it's a console application you would need to install a control handler; take a look at SetConsoleCtrlHandleron MSDN
对于进程,您将能够检测到终止WM_CLOSE
。您将需要处理 Windows 消息。如果是控制台应用程序,则需要安装控制处理程序;看看MSDN上的SetConsoleCtrlHandler