Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7941929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:48:54+00:00 2026-06-03T23:48:54+00:00

When opening Windows Task Manager to view the PID of all open processes, I

  • 0

When opening Windows Task Manager to view the PID of all open processes, I can see that all the open tabs in my Chrome browser have their own unique PID, however I get the same PID no matter what tab is in focus when using:

[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

IntPtr hwnd = GetForegroundWindow();
GetWindowThreadProcessId(hwnd, out pid);

Does anyone know how to uniquely identify browser tabs? And even further, does anyone know how to catch an event when the browser tabs change state or focus? Any browser – Chrome, Firefox, IE

Below is my full code catching the WinEventHook event when the foreground window changes:

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace focusWindow2
{
public partial class Form1 : Form
{
    [DllImport("user32.dll")]
    static extern IntPtr SetWinEventHook(SystemEvents eventMin, SystemEvents eventMax, IntPtr hmodWinEventProc,
    SystemEventHandler lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool UnhookWinEvent(IntPtr hWinEventHook);

    [DllImport("user32.dll")]
    static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

    delegate void SystemEventHandler(IntPtr hWinEventHook, SystemEvents @event, IntPtr hwnd, int idObject, int idChild,
      uint dwEventThread, uint dwmsEventTime);

    enum SystemEvents
    {
        ForegroundWindowChanged = 0x3  // EVENT_SYSTEM_FOREGROUND - The only flag we care about
    }

    IntPtr _WinEventHook;
    SystemEventHandler _WinEventHookHandler;

    public Form1()
    {
        InitializeComponent();

        // Create the handler
        _WinEventHookHandler = new SystemEventHandler(WinEventHook);

        // Set the hook
        _WinEventHook = SetWinEventHook(SystemEvents.ForegroundWindowChanged, SystemEvents.ForegroundWindowChanged,
        IntPtr.Zero, _WinEventHookHandler, 0, 0, 0);

        this.FormClosing += delegate
        {
            UnhookWinEvent(_WinEventHook);
        };

    }

    private void WinEventHook(IntPtr hWinEventHook, SystemEvents @event, IntPtr hwnd, int idObject, int idChild,
  uint dwEventThread, uint dwmsEventTime)
    {
        uint pid = 0;
        GetWindowThreadProcessId(hwnd, out pid);
        Process p = Process.GetProcessById((int)pid);

        string procName = Path.GetFileName(p.ProcessName);

        textBox1.Text += procName + "  " + pid + "\r\n";

    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:48:56+00:00Added an answer on June 3, 2026 at 11:48 pm

    You aren’t going to be able to do what you want to – at least not in a reliable way.

    The browsers are implemented differently. FireFox and older IE versions use a one-process model, IE9 is multiprocess but groups several tabs per process for performance reasons, and Chrome does one process per tab. Other browsers/browser versions do whatever they want (but most likely single-process). You’d have to write specific code for the browsers you want to support, and you’d be dealing with pretty deep-down implementation details.

    For Chrome, iirc the model is one “host window” that handles the rendering. The processes for handling each tab are “worker processes”, with communication back and forth between the host and workers… If Chrome doesn’t have an official API for getting PIDs for the worker processes, then don’t bother 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can i close all the IE browser window in asp.net, I am opening
Is there a way to get multiple organizer windows open so I can view
I'm having some issues understanding how to reference new browser windows after opening them.
I'm not talking about the way and means of opening new windows or tabs,
for a little app, I'm opening a few windows/tabs from my script. Whether the
Does windows have performance counters to measure statistics of a dialup connection? Opening up
print() doesn't work in IE after opening a new window. It works in Chrome.
I have an application that has two different login windows for two different types
I have a batch file that creates a scheduled task using schtasks like this:
I have a Java application that launches multiple windows (JFrames). It works fine when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.