Getting PID of IE Tabs in Internet Explorer 8+ versions...

Internet Explorer most popular web browser, and also default one in most of the machine. Its always be a confusing when it comes to multiple tabs in IE. Sometimes new tabs created as child thread under parent IE process, or sometimes as child process with new PID.

IE8 and above always be  foxing behavior depending the Reg value of

              HKCU\Software\Microsoft\Internet Explorer\Main – TabProcGrowth.

If that value is set to 10, then IE will create first 10 tabs with different different PID, later on those PID will be preserved for load balancing the tabs and allocated to tabs.

Like if more than 10 tabs is opened in browser than child thread will be created instead of separate process.


Now lets try to get the PID of all opened Tabs. Below is the code snippet :-


IShellWindows  *pIShellWindows = NULL;
IDispatch      *pIDispatch = NULL;
HRESULT         hr;
hr = CoCreateInstance (CLSID_ShellWindows, NULL, CLSCTX_SERVER,
                            IID_IShellWindows, (LPVOID *) & pIShellWindows);
       
pIShellWindows->get_Count(&count);
for ( int counter = 0; counter < count  ; ++counter)
{
     VARIANT vt;
     VariantInit(&vt);
     vt.vt=VT_I4 ;
     vt.lVal = counter;
     hr = pIShellWindows->Item (vt, &pIDispatch);
     if ( SUCCEEDED(hr) && pIDispatch != NULL)
     {
         // "Dispatch success"    
         IWebBrowser2 *tab = NULL;
         hr = pIDispatch->QueryInterface(IID_IWebBrowser2,(void **) &tab);
         LONG_PTR myHwnd;
         tab->get_HWND(&myHwnd);
         //"Get handle succeded [%0x]tab[%0x]",hwnd,myHwnd)
         if ( (HWND)myHwnd == hwnd)
         {
              BSTR location = NULL;
              DWORD lpdwIEProcessID = NULL;
              //"Get IE frame window is success"
              HWND hwndTab = NULL;
              CComQIPtr<IServiceProvider> spServiceProvider = tab;
              if (spServiceProvider != NULL)
              {
                   //"CComQIPTr success"
                   CComQIPtr<IOleWindow> spWindow;
                   if (SUCCEEDED(spServiceProvider->QueryService(
                                              SID_SShellBrowser,IID_IOleWindow,(void**)&spWindow)))
                   {
                        if(SUCCEEDED(spWindow->GetWindow(&hwndTab)))                         
                              GetWindowThreadProcessId(hwndTab,&lpdwIEProcessID);
                        else
                              //"GeWindow failed");
                   }
              }
         }
    }
}


Hope this will help you to find the PIDs of each opened tab in IE.
Thanks..

No comments:

Post a Comment