none
Bring IE child process to foreground based on the process ID RRS feed

  • Question

  • Hi there,

    I am trying to bring IE child processes to front using C#. The below code executes, but the action is not completed. Could anyone assist?

    //Please mind the coding, conversions and standards. This is edited for public usage.
    using System;
    using System.Data;
    using System.Diagnostics;
    using System.Runtime;
    using System.Runtime.InteropServices;

    namespace Porcesses
    {
        class Program
        {
            private const int SW_MAXIMIZE = 3;

            [DllImport("user32")]
            private static extern bool ShowWindow(IntPtr hwnd, int SW_MAXIMIZE);

            [DllImport("user32")]
            private static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

            public static void Main(string[] args)
            {
                IntPtr hWnd;
                decimal processId = 21088; //child process ID

                DataTable processIds = new DataTable(); //Test purpose
                processIds.Columns.Add("Process Name", typeof(string));
                processIds.Columns.Add("ID", typeof(decimal));
                processIds.Rows.Add("ID", 21088);

                foreach (DataRow row in processIds.Rows)
                {
                    if (Convert.ToDecimal(row[1]) == processId)
                    {
                            var p = Process.GetProcessById(21088);
                            hWnd = p.MainWindowHandle;
                            ShowWindow(hWnd, SW_MAXIMIZE);
                            SwitchToThisWindow(hWnd, true);

                    }
                }
            }
        }
    }


    The following source was helpful, however the issue persists for IE child process cases.

    https://social.msdn.microsoft.com/Forums/en-US/5c12c02a-5a53-4742-a80b-c93f949fd73e/how-to-bring-an-application-to-front-with-c?forum=csharpgeneral

    Please shout if you need more details.

    Thanks in advance.

    Tuesday, February 11, 2020 8:48 PM

All replies

  • Hi NewbieMahi,

    Thank you for posting here.

    Please try the following code to bring the browser to the foreground.

    I tried IE, Chrome and Edge, and they all worked fine.

        class Program
        {
            [DllImport("user32.dll")]
            static extern bool SetForegroundWindow(IntPtr hWnd);
            [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
            static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
            static void Main()
            {
                Process[] process = Process.GetProcessesByName("iexplore");
                foreach (var item in process)
                {
                    var p = Process.GetProcessById(item.Id);
                    BringProcessToFront(p);
                }
            }
            public static void BringProcessToFront(Process process)
            {
                ShowWindow(process.MainWindowHandle, 1);
                IntPtr handle = process.MainWindowHandle;
                SetForegroundWindow(handle);
            }
        }

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 12, 2020 3:09 AM
  • Hello Timon,

    Thank you very much for your prompt response. The code you have sent works fine, but not in my case. My requirement is to bring up any application window at any point of time based on PID (one at a time). Moreover, nCmdShow=1 is restoring the window to its original position. Unfortunately, only a few of the applications are in full-screen mode. I need to maximize the screen of all applications, which helps me to take screenshots of errors. 

    While testing the script, I have noticed something peculiar. Steps I've followed and queries are as below:

    1. Launch IE and open a website. PID: (Parent:20144|Child frame:14784)

    2. Launch another IE webpage . PID: (Child process: 7836)

    3. Navigated to VS, and ran the script with PID:20144 as input. To my surprise, the script brought the webpage launched as part of the 2nd step on to the screen! After changing the PID to 7836, it still displayed the same 2nd page. Perhaps, it is showing the very recent page. 

    4. There is no browser freezing issue as nCmdShow is 1. I do not understand why the browser gets stuck when the command is to maximize the browser. 

    5. I may need to handle File Explorers as well. But, those do not have a handle?

    Below is the code I've used:

    class Program
    {
        [DllImport("user32.dll")]
        static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
        static void Main()
        {
            /*Process[] process = Process.GetProcessesByName("iexplore");
            foreach (var item in process)
            {
                var p = Process.GetProcessById(item.Id);
                BringProcessToFront(p);
            }*/
            var p = Process.GetProcessById(20144);
            BringProcessToFront(p);
        }
        public static void BringProcessToFront(Process process)
        {
            ShowWindow(process.MainWindowHandle, 1);
            IntPtr handle = process.MainWindowHandle;
            SetForegroundWindow(handle);
        }
    }


    Sorry for dumping too much info. Thanks for your time.

    Cheers - Newbie

    Wednesday, February 12, 2020 4:23 AM