GetForegroundWindow - MainWindowHandle Number is Error ON IE 11 Why? RRS feed

  • Question

  •        [DllImport("user32.dll")]
            static extern IntPtr GetForegroundWindow();
            private void btnAi_Click(object sender, EventArgs e)
                Process[] processes = Process.GetProcesses();
                foreach (Process p in processes)
                    if (!p.ProcessName.Equals("Idle"))
                        if (p.MainWindowHandle != IntPtr.Zero)
                            listBox1.Items.Add(p.ProcessName + "-" + p.MainWindowHandle);
    MWH = GetForegroundWindow();  // IE 11 --> 132256

    Some Computer is Okay 

    But Some Computer Unable to find the correct code ON IE 11

     Like this ↓

    GetForegroundWindow() get number is 132256, but Processes get Two IE 11 handler number is 199026, 2426540

    Some computers can catch the correct number, some computers can't catch the correct number

    I have confirmed the windows version.(Win 7 32bit)

    IE version (IE 11.0.75)

    Are consistent

    SO I don't know where the problem is.



    • Edited by 天氣 Tuesday, September 18, 2018 7:36 AM
    Tuesday, September 18, 2018 7:02 AM

All replies

  • IE (may) use multiple processes, one for each tab. This is for security reasons. Different factors can come into play including whether protected mode is on or not. But this is how all the browsers work for the most part. In general there is a single parent process that hosts the child processes and (presumably) is using IPC to communicate with them. Unfortunately .NET (and Windows) doesn't directly expose parent-child process information. You have to go digging into the Win32 API to get that information.

    Michael Taylor

    Tuesday, September 18, 2018 2:50 PM