在Windows中发送任意信号? [英] Sending an arbitrary Signal in Windows?

查看:793
本文介绍了在Windows中发送任意信号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Linux支持使用<$ c向任务进程发送任意Posix-Signal,如 SIGINT SIGTERM $ C>杀 -Command。虽然 SIGINT SIGTERM 只是无聊的旧方法,以友好或不友好​​的方式结束流程方式, SIGQUIT 意味着触发核心转储。这可以用来触发正在运行的Java VM打印出一个线程转储,包括所有正在运行的线程的堆栈跟踪 - 整洁!打印完调试信息后,Java VM将继续执行以前的操作;事实上,线程转储只发生在另一个具有最高优先级的衍生线程中。 (您可以使用 kill -3< VM-PID> 自行尝试。)

Linux supports sending an arbitrary Posix-Signal such as SIGINT or SIGTERM to a process using the kill-Command. While SIGINT and SIGTERM are just boring old ways to end a process in a friendly or not-so-friendly kind of way, SIGQUIT is meant to trigger a core dump. This can be used to trigger a running Java VM to print out a thread dump, including the stacktraces of all running threads -- neat! After printing the debugging info, the Java VM will continue doing whatever it was doing before; in fact the thread dump just happens in another spawned thread of maximum priority. (You can try this out yourself by using kill -3 <VM-PID>.)

请注意您还可以使用<$ c $中的<不支持!)信号 SignalHandler 类注册您自己的信号处理程序c> sun.misc -package,所以你可以享受各种乐趣。

Note that you can also register your own signal handlers using the (unsupported!) Signal and SignalHandler classes in the sun.misc-package, so you can have all kinds of fun with it.

但是,我还没有找到一种向Windows进程发送信号的方法。信号由某些用户输入创建: Ctrl-C 触发 SIGINT <例如,在两个平台上都是/ code>。但是,在Windows上手动将信号发送到正在运行但非交互的进程似乎没有任何实用性。显而易见的解决方案是使用Cygwin kill 可执行文件,但是虽然它可以使用适当的Windows API结束Windows进程,但我无法发送 SIGBREAK (Windows相当于 SIGQUIT );事实上,我认为它能够发送到Windows进程的唯一信号是 SIGTERM

However, I have yet to find a way to send a signal to a Windows process. Signals are created by certain user inputs: Ctrl-C triggers a SIGINT on both platforms, for instance. But there does not seem to be any utility to manually send a signal to a running, but non-interactive process on Windows. The obvious solution is to use the Cygwin kill executable, but while it can end Windows processes using the appropriate Windows API, I could not send a SIGBREAK (the Windows equivalent to SIGQUIT) with it; in fact I think the only signal it is able to send to Windows processes is SIGTERM.

所以,要制作简而言之,重复标题:如何向Windows中的进程发送任意信号?

So, to make a long story short and to repeat the headline: How to I send an arbitrary signal to a process in Windows?

推荐答案

如果是什么你想要的是明确地/程序化地杀死任何类型的另一个程序/进程,在SysInternals的pstools中有一个名为pskill的小工具,其行为就像Unixenkill一样。

If what you want is to explicitly/programmaticly kill another program/process of any kind, within the SysInternals' pstools there is a small tool named "pskill" that behaves just like Unixen "kill" would do.

如果你想要别的东西,请继续阅读(虽然我可能在下面的一些细节上错了 - 自从我上次使用WinAPI和Charles Petzold的优秀书籍开发了一个C语言程序以来,这已经很久了。对于Windows作为指导。

If you want something else, keep reading (though I may be wrong on some of the specifics below - it's been eons since I last developed a Windows program in C using only the WinAPI and Charles Petzold's excellent books "Programming for Windows" as a guide).

在Windows上你没有正确的信号,WinMain和WinProc从操作系统接收的功能很简单消息。例如,当您单击窗口的X按钮时,Windows会向该窗口的处理程序发送消息WM_CLOSE。当窗口被删除但程序仍在运行时,它会发送WM_DESTROY。当它即将离开主消息处理循环时,WinMain(不是WinProc)接收WM_QUIT。您的程序应该按预期响应所有这些 - 您实际上可以通过在接收WM_CLOSE时不应该执行的操作来开发不可关闭的应用程序。

On Windows you don't properly have "signals", what functions WinMain and WinProc receive from the Operating System are simple messages. For instance, when you click on the "X" button of a window, Windows sends that windows' handler the message WM_CLOSE. When the window's deleted but program's still running, it sends WM_DESTROY. When it's about to get out of the main message processing loop, WinMain (not WinProc) receives WM_QUIT. Your program should respond to all these as expected - you can actually develop an "unclosable" application by not doing what it should upon receiving a WM_CLOSE.

当用户选择任务时从Windows任务管理器单击结束任务,操作系统将发送WM_CLOSE(和另一个我不记得)。但是,如果使用结束进程,则直接终止该进程,不会发送任何消息(来源:旧的新事物

When user selects the task from Windows Task Manager and clicks "End Task", the OS will send WM_CLOSE (and another one I don't remember). If you use "End Process", though, the process is killed directly, no messages sent ever (source: The Old New Thing

我记得有办法获得另一个进程'的HWND,一次你得到另一个进程可以通过函数PostMessage和DispatchMessage向该窗口发送消息。

I remember there was a way to get the HWND of another process' window, once you get that another process could send that window a message thru functions PostMessage and DispatchMessage.

这篇关于在Windows中发送任意信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆