I would like to focus .exe file (more specifically console application) to one functioning program that the first time launched it works but when executed second time (while the first one exists) it could turn focus to the first launched programm. Is that possible at all in Qt?
Asked
Active
Viewed 4,882 times
1 Answers
10
For single instance, read Run only one instance of a Qt application.
Disregarding portability, if you are running on Windows, another common approach is to create a named pipe (or a named mutex) when the application starts, and destroy it before exiting. If the named pipe already exists, another instance is already running. You could even write to the named pipe asking the other instance to bring its window to front, altough a console application might not benefit from this specifically.
Another way to focus the existing instance is to find the window of the already running instance, then call SetFocus on its HWND
, or the corresponding function on your platform.

ΦXocę 웃 Пepeúpa ツ
- 47,427
- 17
- 69
- 97

jweyrich
- 31,198
- 5
- 66
- 97
-
Thanks a lot, but I didn't get with the _named_pipe_ issue. – elgolondrino Aug 29 '13 at 05:31
-
You call **[CreateNamedPipe](http://msdn.microsoft.com/en-us/library/aa365150)** to create a named pipe, and if it fails (return value is `INVALID_HANDLE_VALUE`), it means another instance already created a pipe with the same name, therefore you know another instance is already running. – jweyrich Aug 29 '13 at 05:34