Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to find the IWebBrowser2 pointer for an IE8 window given a PID?
    text
    copied!<p>so far, I've successfully used the following function to retrieve the IWebBrowser2 pointer to a running Internet Explorer instance, given it's PID.</p> <pre><code>static SHDocVw::IWebBrowser2Ptr findBrowserByPID( DWORD pid ) { SHDocVw::IShellWindowsPtr ptr; ptr.CreateInstance(__uuidof(SHDocVw::ShellWindows)); if ( ptr == NULL ) { return 0; } // number of shell windows const long nCount = ptr-&gt;GetCount(); // iterate over all shell windows for (long i = 0; i &lt; nCount; ++i) { // get interface to item no i _variant_t va(i, VT_I4); IDispatchPtr spDisp = ptr-&gt;Item(va); SHDocVw::IWebBrowser2Ptr spBrowser(spDisp); if (spBrowser != NULL) { // if there's a document we know this is an IE object // rather than a Windows Explorer instance HWND browserWindow; try { browserWindow = (HWND)spBrowser-&gt;GetHWND(); } catch ( const _com_error &amp;e ) { // in case -&gt;GetHWND() fails continue; } DWORD browserPID; GetWindowThreadProcessId( browserWindow, &amp;browserPID ); if ( browserPID == pid ) { return spBrowser; } } } return 0; } </code></pre> <p>What I do is to launch an <code>explorer.exe</code> process via <code>CreateProcess</code> and then use the above function to retrieve the IWebBrowser2Ptr to it (so that I can fiddle with the browser).</p> <p>Unfortunately, this doesn't seem to work with Internet Explorer 8 anymore, since IE8 seems to reuse processes - at least to some degree. For two code sequences like:</p> <pre><code>PROCESS_INFORMATION pi; // ... if ( CreateProcess( ..., &amp;pi ) ) { // Wait a bit to give the browser a change to show its window // ... IWebBrowser2 *pWebBrowser = findBrowserByPID( pi.dwProcessId ); } </code></pre> <p>The first run of this code works fine, the second one never manages to retrieve the pWebBrowser window.</p> <p>After a bit of debugging, it was revealed that the <code>findBrowserByPID</code> function does find lots of browser windows (and it finds more after starting a second browser instance), but none of them belong to the newly started process. It seems that all windows belong to the first IE process which was started.</p> <p>Does anybody know an alternative way to get the IWebBrowser2 pointer to some IE8 instance? Or is there maybe a way to disable this apparent 'reuse' of processes with IE8?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload