How to hide the console of a process given its PID?

1k views Asked by At

I would like to start a process when I click on a button on my Qt application. I created a process with QProcess::startDetached(..., qint64 * pid) (http://doc.qt.io/qt-5/qprocess.html#startDetached) but I have a console that I would like to hide.

How to hide it? Which function must I to use outside the process?

This code doesn't hide the console of my process (in win32):

if (AttachConsole((DWORD)m_PID))
{
    FreeConsole();
}
1

There are 1 answers

3
Fire Lancer On

If you can recompile the program you are starting, you might make it a Windows rather than console program (in MSVC this is in the projects linker->system, settings, you want /SUBSYSTEM:WINDOWS).

Else the QT start method apparently never creates a console window, while startDetached does, so it might be possible for you to use start depending on your use.


Alternatively the CREATE_NO_WINDOW flag for CreateProcess will prevent the automatic console, quote MSDN:

The process is a console application that is being run without a console window. Therefore, the console handle for the application is not set.

Unfortunately it does not look like QT provides any way to create or use its QProcess with native flags or from a native handle (e.g. no QProcess process(CreateProcess(...))), allthough it was suggest some years back and rejected. QProcess: Make it possible to set native process creating flags

So you would either have to just use the Microsoft API's or find another library for multi-process work.