Refactor CProcessCtrl to start detached with or without console

QTBUG-53833 - which caused a console application to launch with a console
if it was started with QProcess::startDetached - is fixed in 5.8.0.
There were use cases for both options - with and without console -
but not offered by Qt API. Therefore CProcessCtrl is refactored  as a
wrapper to offer both options. If no console is required, it defaults to
QProcess::startDetached. Otherwise uses its own implementation.

refs #866
This commit is contained in:
Roland Winklmeier
2017-03-07 13:16:30 +01:00
committed by Mathew Sutcliffe
parent 5ab1ae7193
commit b80f204293
2 changed files with 21 additions and 12 deletions

View File

@@ -23,33 +23,36 @@ namespace BlackMisc
{ } { }
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
bool CProcessCtrl::startDetachedWithoutConsole(const QString &program, const QStringList &arguments) bool startDetachedWithConsoleWindow(const QString &program, const QStringList &arguments)
{ {
bool inherit = false;
PROCESS_INFORMATION processInfo; PROCESS_INFORMATION processInfo;
STARTUPINFOW startupInfo; memset (&processInfo, 0, sizeof (processInfo));
bool inherit = true;
DWORD flags = 0; STARTUPINFO startupInfo;
memset (&startupInfo, 0, sizeof (startupInfo));
startupInfo.cb = sizeof (startupInfo);
QString command; QString command;
command += program; command += program;
command += ' '; command += ' ';
command += arguments.join(' ').replace('/', '\\'); command += arguments.join(' ').replace('/', '\\');
memset (&processInfo, 0, sizeof (processInfo)); DWORD flags = 0;
memset (&startupInfo, 0, sizeof (startupInfo));
startupInfo.cb = sizeof (startupInfo);
flags |= NORMAL_PRIORITY_CLASS; flags |= NORMAL_PRIORITY_CLASS;
flags |= DETACHED_PROCESS; flags |= CREATE_UNICODE_ENVIRONMENT;
flags |= CREATE_NEW_CONSOLE;
Q_ASSERT(command.length() <= MAX_PATH); Q_ASSERT(command.length() <= MAX_PATH);
std::array<WCHAR, MAX_PATH> wszCommandLine = {{}}; std::array<WCHAR, MAX_PATH> wszCommandLine = {{}};
command.toWCharArray(wszCommandLine.data()); command.toWCharArray(wszCommandLine.data());
int result = CreateProcess (nullptr, wszCommandLine.data(), 0, 0, inherit, flags, nullptr, nullptr, &startupInfo, &processInfo); int result = CreateProcess (nullptr, wszCommandLine.data(), 0, 0, inherit, flags, nullptr, nullptr, &startupInfo, &processInfo);
if (result == 0) if (result == 0)
{ {
CLogMessage(static_cast<CProcessCtrl*>(nullptr)).warning("Failed to manually launch %1: %2") << program << GetLastError(); CLogMessage(static_cast<CProcessCtrl*>(nullptr)).warning("Failed to start %1: %2") << program << GetLastError();
return false; return false;
} }
@@ -58,11 +61,17 @@ namespace BlackMisc
return true; return true;
} }
#else #else
bool CProcessCtrl::startDetachedWithoutConsole(const QString &program, const QStringList &arguments) bool startDetachedWithConsoleWindow(const QString &program, const QStringList &arguments)
{ {
//! \fixme Handle Linux and OS X here
return QProcess::startDetached(program, arguments); return QProcess::startDetached(program, arguments);
} }
#endif #endif
bool CProcessCtrl::startDetached(const QString &program, const QStringList &arguments, bool withConsoleWindow)
{
if (withConsoleWindow) { return startDetachedWithConsoleWindow(program, arguments); }
else { return QProcess::startDetached(program, arguments); }
}
} // ns } // ns

View File

@@ -30,7 +30,7 @@ namespace BlackMisc
CProcessCtrl(QObject *parent = nullptr); CProcessCtrl(QObject *parent = nullptr);
//! Start a programm detached and without any console window //! Start a programm detached and without any console window
static bool startDetachedWithoutConsole(const QString &program, const QStringList &arguments); static bool startDetached(const QString &program, const QStringList &arguments, bool withConsoleWindow);
}; };
} // ns } // ns