This small C++ program, ActivateHwnd.exe, brings an existing Windows GUI window to the foreground when you provide its window handle (an HWND) on the command line. It accepts either hexadecimal (e.g., 0xABC) or decimal (e.g., 12345) formats and uses several Win32 APIs to restore, raise, and focus that window as reliably as possible.
-
Parses the handle you pass in. The function
ParseHwndtrims whitespace, detects whether the input is hex or decimal (by checking for0x/0Xor A–F digits), and converts it to an integer. It then casts that value to anHWND. Invalid formats are rejected early with a clear error. -
Validates the target window.
IsWindow(target)ensures the handle really belongs to a live window. If not, the program exits with an error. -
Temporarily links input queues (if needed). Foreground activation can fail if threads don’t share input. The code gets:
- the current thread (
GetCurrentThreadId()), - the current foreground window and its GUI thread (
GetForegroundWindow()/GetWindowThreadProcessId()), then callsAttachThreadInput(TRUE)to link the threads when they differ. This increases the chance that focus changes will succeed.
- the current thread (
-
Attempts several activation strategies. The program tries a sequence of gentle-to-strong nudges:
- If minimized, restore:
ShowWindow(target, SW_RESTORE). - Raise and show:
SetWindowPos(target, HWND_TOP, …, SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW|SWP_ASYNCWINDOWPOS). - Set as foreground:
SetForegroundWindow(target). - Bring to top:
BringWindowToTop(target). - Ensure visible:
ShowWindow(target, SW_SHOW). - Give keyboard focus:
SetFocus(target)(not all windows accept focus).
Each step logs a descriptive error with
PrintLastErrorif it fails, but the program keeps going—another step might succeed. - If minimized, restore:
-
Detaches input queues. If it attached earlier, it calls
AttachThreadInput(FALSE)to restore the original state. -
Verifies success. Finally, it checks whether the target actually became the foreground window. If yes, it prints
[OK]; otherwise, it warns that activation couldn’t be guaranteed.
ActivateHwnd.exe <HWND>
e.g. ActivateHwnd.exe 0xABC
ActivateHwnd.exe 12345
The handle can be copied from tools like Spy++, WinDbg, or your own diagnostics.
PrintLastError uses FormatMessageA to display the failing API call, the numeric error code, and the system-provided message. This makes troubleshooting straightforward when activation is blocked by focus rules or window state.
Windows enforces rules to prevent apps from stealing focus. Depending on the current foreground window, thread relationships, and the target’s state (minimized, hidden, not focusable), a single call like SetForegroundWindow may fail. By:
- optionally attaching input to the foreground thread,
- restoring a minimized window,
- raising it in Z-order,
- and showing then focusing it,
the tool maximizes the odds of success without resorting to intrusive techniques.
- You must already know the correct
HWND. If the window has closed or the handle is stale,IsWindowwill fail. - Foreground protection policies may still prevent activation in some scenarios (the program reports this clearly).
- Not all windows are focusable (e.g., tool windows without focus styles).
ActivateHwnd is a pragmatic, minimal utility: feed it a window handle, and it does everything reasonable—and cleanly reversible—to bring that window to the front and give it focus, with helpful diagnostics when Windows says “no.”