Here is my solution in pure Windows API:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#pragma comment( lib, "user32" )
#include "types.h"
i32 wmain(ui64 argc, wchar_t **argv)
{
if(argc != 2)
{
return 1;
}
// Find window by Class Name
HWND wnd = NULL;
ui64 tries = 100;
while(--tries != UI64_MAX)
{
wnd = FindWindow(argv[1], NULL);
if(wnd != NULL)
{
break;
}
Sleep(50);
}
if(wnd == NULL)
{
return 2;
}
AllowSetForegroundWindow(ASFW_ANY);
DWORD cur_thread = GetCurrentThreadId();
DWORD fg_pid = 0;
DWORD fg_thread = GetWindowThreadProcessId(wnd, &fg_pid);
AttachThreadInput(cur_thread, fg_thread, TRUE);
SetFocus(wnd);
SetForegroundWindow(wnd);
ShowWindow(wnd, SW_NORMAL);
AttachThreadInput(cur_thread, fg_thread, FALSE);
SetWindowPos(wnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
return 0;
}
Compile it and just call the compiled program with the argument of the window's class name. You can get window's class name with programs like WinSpy++ or similar. If you want it to search by title, I think you can easily modify it yourself. There is tons of info on how to find window handle by title.
Oh yes, it also restores window if it is currently minimised.
If it fails to find the window, it keeps trying up to 100 times with 50 ms intervals (so total 5 seconds), after that it gives up. It is useful if you are starting some program from CMD, and it takes a few seconds to start up, but you want it brought to focus immediately after its ready.