There was at me somehow a problem to operate applications - the services written on Qt, from application - a kernel written also on Qt. And, if the service application is not running, then it should be started, and if it is running, its window should be brought to the foreground. QProcess is used to invoke applications.
It looks like this.
Process creation:
QProcess *app; bool pw = false; ... void createProcess() { app = new QProcess(this); app->setProgram("MyProgram.exe"); app->setArguments(QStringList() << "arg1" << "arg2"); connect(app, SIGNAL(started()), this, SLOT(onProcStarted())); connect(app, SIGNAL(finished()), this, SLOT(onProcFinished())); }
Call:
if (!pw) { //If the application is not running, then run it app->start(QIODevice::ReadOnly); } else { // If it's running, we'll show it from the top HWND hWnd = FindWindow(nullptr, L"Заголовок главного окна"); if (hWnd > 0){ ShowWindow(hWnd, SW_RESTORE); SetForegroundWindow(hWnd); } }
Slots:
void onProcStarted() { pw = true; } void onProcFinished(int) { pw = false; }
As I understood, QProcess does not know how to bring to the forefront the window of the running process. Therefore, I have to resort to WinApi. But showing the window in this form does not work. Moreover, the FindWindow function does not work. It does not return the correct handle. Although there is a window.
The solution was related to the direct indication of the encoding as follows:
QTextCodec *codec = QTextCodec::codecForName("UTF-8"); QString s = codec->toUnicode("Заголовок главного окна"); LPCWSTR lps = (LPCWSTR)s.utf16(); HWND hWnd = FindWindow(nullptr, lps); if (hWnd > 0) { ShowWindow(hWnd, SW_RESTORE); SetForegroundWindow(hWnd); }
А можно по ID процесса выводить на передний план окно?
Если зарыться в API системы, то, думаю, что можно, тут тоже использовался WinAPI.