views:

144

answers:

2

I need to get a list of applications that are currently running so that my C++ application and make them take focus.

Has anybody done this?

+1  A: 

First: Applications do not have focus; windows do. Secondly: some windows will not accept focus. So, I don't think it's literally possible.

MSalters
+7  A: 

There is no real cross platform way of doing that. The whole concept of processes, applications, etc. is an operating system specific concept. If you use a certain library to solve the issue, you are not really cross platform, you are limited to the platforms that are supported by this library. E.g. Qt is not universal cross platform, it runs on a lot of platforms, but not on every known one and on platforms where it won't run, a Qt solution won't work. Most UNIX like platforms support the POSIX API (some more, some less) and if you limit yourself to POSIX functions, the solution will work in Linux, BSD, Mac OS X, Solaris, and similar OSes. It will not work on Windows, though. Microsoft decided to drop POSIX support (not that their POSIX support was great to begin with), however Cygwin brings back POSIX support to Windows (Cygwin emulates a complete Linux glibc API on top of Windows). The problem is that not even POSIX is really offering a set of functions to solve your problem here - the way how a POSIX tool like ps gets process information is entirely different on a Linux system compared to a BSD system for example.

The second issue is that you are talking "focus". Focus is something that does not apply to applications. A background application that has no UI and no windows cannot have "focus". What would "focus" mean for such an application? So you are not really interested in a list of running applications, but in a list of running UI applications that have windows and whose windows may get focus. An entirely different thing. The windows systems are even more different between different platforms and POSIX ignores UIs altogether.

Also you have a Visual-C++ tag on your question, so how cross platform must your code really be, as Visual-C++ is a Windows only thing, isn't it? What platforms are you really trying to support (please update your question accordingly), since I doubt there is any better solution than writing a different piece of code for every single supported platform.

Mecki