Quote Originally Posted by Askarali View Post
I checked GetLastError() immediately after SetForegroundWindow() when it failed though I haven't provided this check here.
As mentioned in post #2, SetForegroundWindow() isn't documented as setting last error so GetLastError() probably isn't valid.

Also, there doesn't appear to be a check that WindowSwitcherUIHandle is valid before being used. It is set from FindWindow() but there's no check that FindWindow() is successful.