Bharath
Bharath

Reputation: 85

Sending Ctrl+C event to a process launched using QProcess on Windows

I have a dialog which acts as a configurator for a console application. The dialog’s job is to offer the user a set of widgets (which mirror the options supported by the console application) and when user clicks on the “Start” button, the dialog creates and starts a QProcess with the console application’s name and parameters based on the state of the widgets in the GUI. I am able to start the process successfully and everything works fine. However, when I want to kill the process, the console application needs to shutdown gracefully, meaning it has to close files, flush data, close devices etc., and then terminate.

I used QProcess::close(), this immediately kills the application and the app is unable to shutdown gracefully.

I have used the Win32 GenerateConsoleCtrlEvent(CTRL_C_EVENT, Q_PID::dwProcessId) to send an even to the same. I see that the above API returns a non-zero value (indicating a success, it would return 0 upon failure), but my process continues to run.

Can anyone help me with how I can signal the QProcess to shutdown gracefully? Or is there any other way to do this?

Upvotes: 3

Views: 1912

Answers (1)

GenerateConsoleCtrlEvent takes a process group id, not a process id. You are likely feeding it a process id, since that's what QProcess provides.

QProcess doesn't support creation of a process group at the moment. You need to either start the process manually using winapi, or patch your copy of Qt to amend qtbase/src/corelib/io/qprocess[.h,.cpp,_win.cpp] to pass the CREATE_NEW_PROCESS_GROUP creation flag.

If you don't wish to tweak Qt itself, you can copy the qprocess files to your project, rename the class, and add the changes there.

Upvotes: 1

Related Questions