Jacob Birkett
Jacob Birkett

Reputation: 2125

What signals should I use to terminate/kill applications on Windows?

For POSIX environments it's easy. SIGTERM, SIGINT, SIGQUIT, SIGKILL, SIGHUP, etc.

But I am writing an application in D that needs to manage open applications based on user input, sending signals that the user requests. I need to know the equivalents of these signals for Windows, and their values. What are the most important ones? I found documentation for this here, but it's not as comprehensive as POSIX documentation.

I will be using std.process.kill(Pid pid, int codeOrSignal) from the D standard library documentation, but I don't know what signals I need to send.

Thanks!

Edit: As discussed in the comments, I have tagged both C++ and D because answers in either language are helpful. The two languages are cross-compatible and very easy to port between eachother.

Upvotes: 0

Views: 2255

Answers (1)

Chris Becke
Chris Becke

Reputation: 36071

Windows does not have signals. If you literally want to terminate applications then call TerminateProcess. This can cause it to corrupt any files it is writing to.

If causing corruption is not your goal, then you can close console applications via GenerateConsoleCtrlEvent and GUI applications can be closed by posting their window (or windows) WM_CLOSE messages.

Some apps respond to WM_CLOSE by showing modal dialogs that prompt the user to save files, so you might need to have a timeout on the process handle and just call TerminateProcess anyway if it takes too long.


As noted, unless your application was involved in actually launching console applications you have no way to actually meet the requirements to call GenerateConsoleCtrlEvent, which is unfortunate. Even if attached to a console window that can handle WM_CLOSE events, unless this is a secret way to send ctrl events, the console apps themselves will be unable to respond to WM_CLOSE messages to clean up properly, so you just have to hope for the best and use TerminateProcess.

Documentation: TerminateProcess, GenerateConsoleCtrlEvent, WM_CLOSE

Upvotes: 3

Related Questions