How to switch to another application on Windows (using C++, Qt)?

c++, qt, windows

Solution

The following code works here without problem on Windows 7:

#include <windows.h>

[...]

// find window handle using the window title
HWND hWnd = ::FindWindow(NULL, L"Window Title");
if (hWnd) {
    // move to foreground
    ::SetForegroundWindow(hWnd);
}

Problem

I would like to enable my GUI users (GI users?) to switch to a known, friendly application directly, e.g. by keyboard shortcut. Ideally, my application would request the OS / Windows to show application by name or main window title string "XYZ". The manual path of action would be ALT+TAB to open the Windows Task Switcher and then locating and navigating to the desired application icon to finally bring it into the foreground of active program windows. Alternatively, navigation via the Task Bar. AutoHotkey has a function WinActivate that does what I want to achieve.

Original source

Related problems