title: C# How do I get unique PID for each browser tab in external browser ie. Chrome/Firefox/IE
body: <p>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:</p>
  
  <pre><code>[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);
  </code></pre>
  
  <p>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</p>
  
  <p>Below is my full code catching the WinEventHook event when the foreground window changes:</p>
  
  <pre><code>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";
  
      }
  }
  </code></pre>
  
  <p>}</p>