Hi,
so far, I've successfully used the following function to retrieve the IWebBrowser2 pointer to a running Internet Explorer instance, given it's PID.
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->GetCount();
// iterate over all shell windows
for (long i = 0; i < nCount; ++i) {
// get interface to item no i
_variant_t va(i, VT_I4);
IDispatchPtr spDisp = ptr->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->GetHWND();
} catch ( const _com_error &e ) {
// in case ->GetHWND() fails
continue;
}
DWORD browserPID;
GetWindowThreadProcessId( browserWindow, &browserPID );
if ( browserPID == pid ) {
return spBrowser;
}
}
}
return 0;
}
What I do is to launch an explorer.exe
process via CreateProcess
and then use the above function to retrieve the IWebBrowser2Ptr to it (so that I can fiddle with the browser).
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:
PROCESS_INFORMATION pi;
// ...
if ( CreateProcess( ..., &pi ) ) {
// Wait a bit to give the browser a change to show its window
// ...
IWebBrowser2 *pWebBrowser = findBrowserByPID( pi.dwProcessId );
}
The first run of this code works fine, the second one never manages to retrieve the pWebBrowser window.
After a bit of debugging, it was revealed that the findBrowserByPID
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.
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?