From e12e655d7007578f387b6440184872c834fbfaf9 Mon Sep 17 00:00:00 2001 From: Roland Winklmeier Date: Thu, 5 May 2016 13:59:35 +0200 Subject: [PATCH] CProcessCtrl Derives from QProcess and adds a static method to start a detached application without a console window. refs #615 --- src/blackmisc/processctrl.cpp | 70 +++++++++++++++++++++++++++++++++++ src/blackmisc/processctrl.h | 37 ++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 src/blackmisc/processctrl.cpp create mode 100644 src/blackmisc/processctrl.h diff --git a/src/blackmisc/processctrl.cpp b/src/blackmisc/processctrl.cpp new file mode 100644 index 000000000..b4eb52eb5 --- /dev/null +++ b/src/blackmisc/processctrl.cpp @@ -0,0 +1,70 @@ +/* Copyright (C) 2016 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "blackmisc/processctrl.h" +#include "blackmisc/buildconfig.h" +#include "blackmisc/logmessage.h" + +#include + +#ifdef Q_OS_WIN +#include +#endif + +namespace BlackMisc +{ + + CProcessCtrl::CProcessCtrl(QObject *parent) : + QProcess(parent) + { } + + #ifdef Q_OS_WIN + bool CProcessCtrl::startDetachedWithoutConsole(const QString &program, const QStringList &arguments) + { + PROCESS_INFORMATION processInfo; + STARTUPINFOW startupInfo; + bool inherit = true; + DWORD flags = 0; + + QString command; + command += program; + command += ' '; + command += arguments.join(' ').replace('/', '\\'); + + memset (&processInfo, 0, sizeof (processInfo)); + memset (&startupInfo, 0, sizeof (startupInfo)); + startupInfo.cb = sizeof (startupInfo); + + flags |= NORMAL_PRIORITY_CLASS; + flags |= DETACHED_PROCESS; + + Q_ASSERT(command.length() <= MAX_PATH); + std::array wszCommandLine {}; + command.toWCharArray(wszCommandLine.data()); + int result = CreateProcess (nullptr, wszCommandLine.data(), 0, 0, inherit, flags, nullptr, nullptr, &startupInfo, &processInfo); + + if (result == 0) + { + CLogMessage(static_cast(nullptr)).warning("Failed to manually launch %1: %2") << program << GetLastError(); + return false; + } + + CloseHandle (processInfo.hProcess); + CloseHandle (processInfo.hThread); + return true; + } + #else + bool CProcessCtrl::startDetachedWithoutConsole(const QString &program, const QStringList &arguments) + { + return QProcess::startDetached(program, arguments); + } + #endif + + +} // ns diff --git a/src/blackmisc/processctrl.h b/src/blackmisc/processctrl.h new file mode 100644 index 000000000..86e49d956 --- /dev/null +++ b/src/blackmisc/processctrl.h @@ -0,0 +1,37 @@ +/* Copyright (C) 2016 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKMISC_PROCESS_H +#define BLACKMISC_PROCESS_H + +#include "blackmiscexport.h" +#include + +namespace BlackMisc +{ + /*! + * Wrapper around QProcess with additional features + */ + class BLACKMISC_EXPORT CProcessCtrl : public QProcess + { + public: + Q_OBJECT + + public: + //! Constructor. + CProcessCtrl(QObject *parent = nullptr); + + //! Start a programm detached and without any console window + static bool startDetachedWithoutConsole(const QString &program, const QStringList &arguments); + }; +} // ns + +#endif // guard