Set focus on minimized window

Gomo

Member
Joined
Mar 9, 2017
Messages
7
Programming Experience
Beginner
Hello,

I'd like to re-target a running (minimized / in background) CMD.exe so that I can send input to it. I've tried different versions of code so far (which I found online), but none of it seems to be working, or it partially works. Following code is the one I find to be the best, out of the bunch I found.

C#:
        [DllImport("user32.dll")]
        static extern bool SetForegroundWindow(IntPtr hWnd);
        public void MakeWindowActive(string processName)
        {
            Process[] p = Process.GetProcessesByName(processName);
            if (p.Count() > 0)
                SetForegroundWindow(p[0].MainWindowHandle);
        }
        MakeWindowActive("Command Prompt");

Something's wrong and I obviously cannot figure out which part, so here I am, asking for help! If there's a better/different solution for this, I'd gladly take a look into it.
Thanks in advance!
 
I wouldn't expect a Console application Process to have a MainWindowHandle as it's not a GUI application. I would expect that you'd have to use the Windows API to find the handle for the console window.
 
Back
Top Bottom