I have an answer after you have tried more than two hours using Google. This is what I have:
StringBuilder builder = new StringBuilder(500); int foregroundWindowHandle = GetForegroundWindow(); uint remoteThreadId = GetWindowThreadProcessId(foregroundWindowHandle, 0); uint currentThreadId = GetCurrentThreadId();
Since we have a handle to the focus window, we can get its name / class, as well as other necessary information.
In this case, I just recognize the class name:
StringBuilder winClassName = new StringBuilder(); int numChars = CustomViewAPI.Win32.GetClassName((IntPtr)focused, winClassName, winClassName.Capacity);
source share